package com.willalp.web.controller.canteen;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.canteen.domain.HsGift;
import com.willalp.canteen.service.IHsGiftService;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.enums.NumberRuleEnum;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.number.domain.req.NumberRuleReq;
import com.willalp.number.service.IHsSysNumberRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 礼品登记Controller
 *
 * @author songyu
 * @date 2022-03-03
 */
@RestController
@RequestMapping("/canteen/gift")
public class HsGiftController extends BaseController
{
    @Autowired
    private IHsGiftService hsGiftService;

    @Autowired
    private IHsSysNumberRuleService hsSysNumberRuleService;
    /**
     * 查询礼品登记列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:gift:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsGift hsGift)
    {
        startPage();
        List<HsGift> list = hsGiftService.selectHsGiftList(hsGift);
        return getDataTable(list);
    }

    /**
     * 导出礼品登记列表
     */
    @PreAuthorize("@ss.hasPermi('canteen:gift:export')")
    @Log(title = "礼品登记", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsGift hsGift)
    {
        List<HsGift> list = hsGiftService.selectHsGiftList(hsGift);
        ExcelUtil<HsGift> util = new ExcelUtil<HsGift>(HsGift.class);
        return util.exportExcel(list, "礼品登记数据");
    }

    /**
     * 获取礼品登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('canteen:gift:query')")
    @GetMapping(value = "/{giftId}")
    public AjaxResult getInfo(@PathVariable("giftId") String giftId)
    {
        return AjaxResult.success(hsGiftService.getById(giftId));
    }

    /**
     * 新增礼品登记
     */
    @PreAuthorize("@ss.hasPermi('canteen:gift:add')")
    @Log(title = "礼品登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsGift hsGift)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsGift byId = hsGiftService.getById(Long.valueOf(idStr));
            if (null == byId) {
                hsGift.setGiftId(idStr);
                break;
            }
        }
        hsGift.setCreateBy(SecurityUtils.getUsername());
        hsGift.setCreateTime(DateUtils.getNowDate());
        //编号
        NumberRuleReq numberRuleReq =new NumberRuleReq();
        numberRuleReq.setRuleId(NumberRuleEnum.GIFT.getValue());
        String number = hsSysNumberRuleService.getNumber(numberRuleReq);
        hsGift.setGiftNumber(number);
        return toAjax(hsGiftService.save(hsGift));
    }

    /**
     * 修改礼品登记
     */
    @PreAuthorize("@ss.hasPermi('canteen:gift:edit')")
    @Log(title = "礼品登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsGift hsGift)
    {
        hsGift.setUpdateBy(SecurityUtils.getUsername());
        hsGift.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsGiftService.updateById(hsGift));
    }

    /**
     * 删除礼品登记
     */
    @PreAuthorize("@ss.hasPermi('canteen:gift:remove')")
    @Log(title = "礼品登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{giftIds}")
    public AjaxResult remove(@PathVariable String[] giftIds)
    {
        return toAjax(hsGiftService.removeByIds(Arrays.asList(giftIds)));
    }
}
