package com.hzs.system.bonus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.bonus.BdBonusDeduct;
import com.hzs.common.security.utils.SecurityUtils;

import com.hzs.system.bonus.mapper.BdBonusDeductMapper;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 奖金扣项配置 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-11-04
 */
@Service
public class BdBonusDeductServiceImpl extends ServiceImpl<BdBonusDeductMapper, BdBonusDeduct> implements IBdBonusDeductService {

    @Autowired
    private RedisService redisService;

    /**
     * @description: 查询奖金扣项配置
     * @author: zhang jing
     * @date: 2022/11/16 11:26
     * @param: []
     * @return: java.util.List<com.hzs.sale.bonus.vo.BdBonusDeductVo>
     **/
    @Override
    public List<BdBonusDeductVo> bonusDeductlist(BdBonusDeductVo bonusDeductVo) {
        return baseMapper.bonusDeductlist(bonusDeductVo);
    }

    /**
     * @description: 新增或修改奖金扣项配置
     * @author: zhang jing
     * @date: 2022/11/16 12:03
     * @param: [bonusDeductParams]
     * @return: boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBonusDeduct(BonusDeductParams bonusDeductParams) {
        Integer pkCounrey = SecurityUtils.getPkCountry();
        for (BdBonusDeduct bonusDeduct : bonusDeductParams.getBonusDeductList()) {
            bonusDeduct.setPkCountry(pkCounrey);
            if (StringUtils.isNotNull(bonusDeduct.getPkId())) {
                bonusDeduct.setPkModified(SecurityUtils.getUserId());
                bonusDeduct.setModifiedTime(new Date());
                baseMapper.updateById(bonusDeduct);
                String redisKey = CacheConstants.BD_BONUS_DEDUCT + bonusDeduct.getPkId();
                redisService.deleteObject(redisKey);
            } else {
                bonusDeduct.setPkCreator(SecurityUtils.getUserId());
                bonusDeduct.setCreationTime(new Date());
                baseMapper.insert(bonusDeduct);
                String redisKey = CacheConstants.BD_BONUS_DEDUCT + bonusDeduct.getPkId();
                redisService.deleteObject(redisKey);
            }
        }
        return true;
    }

    @Override
    public List<BdBonusDeduct> queryBonusDeduct() {
        QueryWrapper<BdBonusDeduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("PK_ID,PK_BONUS_ITEMS,BACK_RATIO,PLATFORM_RATIO");
        return baseMapper.selectList(queryWrapper);
    }
}
