package com.hzs.system.bonus.controller.manage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.bonus.BdBonusDeduct;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.bonus.params.BonusDeductParams;
import com.hzs.system.bonus.service.IBdBonusDeductService;
import com.hzs.system.bonus.vo.BdBonusDeductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Description:  奖金扣项配置
 * @Author: yuhui
 * @Time: 2022/11/4 11:36
 * @Classname: BdBonusDeductController
 * @PackageName: com.hzs.sale.bonus.controller.manage
 */

@RestController
@RequestMapping("/manage/bonus-deduct")
public class BdBonusDeductController extends BaseController {

    @Autowired
    private IBdBonusDeductService bonusDeductService;
    /**
     * 奖金扣项列表
     * @return
     */
    @GetMapping("list")
    public AjaxResult list(){
        Integer pkCounrey = SecurityUtils.getPkCountry();

        QueryWrapper<BdBonusDeduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY",pkCounrey);
        BdBonusDeductVo bonusDeductVo=new BdBonusDeductVo();
        bonusDeductVo.setPkCountry(pkCounrey);
        List<BdBonusDeductVo> list = bonusDeductService.bonusDeductlist(bonusDeductVo);
        return AjaxResult.success(list);
    }

    /**
     * 修改奖金扣项
     * @return
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody BonusDeductParams bonusDeductParams){
        return AjaxResult.success(bonusDeductService.updateBonusDeduct(bonusDeductParams));
    }

    /**
     * 新增奖项扣项
     * @param bonusDeductParamsList
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody BonusDeductParams bonusDeductParamsList){
        for (BdBonusDeduct bonusDeduct : bonusDeductParamsList.getBonusDeductList()) {
            bonusDeduct.setPkCreator(SecurityUtils.getUserId());
            bonusDeduct.setCreationTime(new Date());
        }
        return AjaxResult.success(bonusDeductService.saveBatch(bonusDeductParamsList.getBonusDeductList()));
    }
}
