package co.yixiang.yshop.module.member.service.integralrule;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.dict.core.util.DictFrameworkUtils;
import co.yixiang.yshop.module.member.controller.admin.integralrule.vo.*;
import co.yixiang.yshop.module.member.convert.integralrule.IntegralRuleConvert;
import co.yixiang.yshop.module.member.dal.dataobject.integralrule.IntegralRuleDO;
import co.yixiang.yshop.module.member.dal.mysql.integralrule.IntegralRuleMapper;
import co.yixiang.yshop.module.system.service.dict.DictDataService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.member.enums.ErrorCodeConstants.INTEGRAL_RULE_NOT_EXISTS;
import static co.yixiang.yshop.module.system.enums.DictTypeConstants.*;

/**
 * 会员积分规则 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class IntegralRuleServiceImpl implements IntegralRuleService {

    @Resource
    private IntegralRuleMapper integralRuleMapper;
    @Resource
    private DictDataService dictDataService;

    @Override
    public Long createIntegralRule(IntegralRuleCreateReqVO createReqVO) {
        // 插入
        IntegralRuleDO integralRule = IntegralRuleConvert.INSTANCE.convert(createReqVO);
        integralRuleMapper.insert(integralRule);
        // 返回
        return integralRule.getId();
    }

    @Override
    public void updateIntegralRule(IntegralRuleUpdateReqVO updateReqVO) {
        // 校验存在
        validateIntegralRuleExists(updateReqVO.getId());
        // 更新
        IntegralRuleDO updateObj = IntegralRuleConvert.INSTANCE.convert(updateReqVO);
        integralRuleMapper.updateById(updateObj);
    }

    @Override
    public void deleteIntegralRule(Long id) {
        // 校验存在
        validateIntegralRuleExists(id);
        // 删除
        integralRuleMapper.deleteById(id);
    }

    private void validateIntegralRuleExists(Long id) {
        if (integralRuleMapper.selectById(id) == null) {
            throw exception(INTEGRAL_RULE_NOT_EXISTS);
        }
    }

    @Override
    public IntegralRuleDO getIntegralRule(Long id) {
        return integralRuleMapper.selectById(id);
    }

    @Override
    public List<IntegralRuleDO> getIntegralRuleList(Collection<Long> ids) {
        return integralRuleMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<IntegralRuleDO> getIntegralRulePage(IntegralRulePageReqVO pageReqVO) {
        return integralRuleMapper.selectPage(pageReqVO);
    }

    @Override
    public List<IntegralRuleDO> getIntegralRuleList(IntegralRuleExportReqVO exportReqVO) {
        return integralRuleMapper.selectList(exportReqVO);
    }

    @Override
    public List<IntegralRuleDO> getIntegralRuleByTypes(List<String> typeList) {
        return integralRuleMapper.selectList(Wrappers.<IntegralRuleDO>lambdaQuery().in(IntegralRuleDO::getType, typeList).eq(IntegralRuleDO::getEnable,Boolean.TRUE));
    }

    @Override
    public IntegralRuleVO getRule() {
        List<IntegralRuleDO> list = this.getIntegralRuleList(new IntegralRuleExportReqVO());

        IntegralRuleVO vo = new IntegralRuleVO();
        vo.setRuleList(list);

        vo.setIntegralDeductionRule(DictFrameworkUtils.parseDictDataValue(INTEGRAL_CONFIG, INTEGRAL_DEDUCTION_RULE));
        vo.setIntegralName(DictFrameworkUtils.parseDictDataValue(INTEGRAL_CONFIG, INTEGRAL_NAME));
        vo.setIntegralEnable(DictFrameworkUtils.parseDictDataValue(INTEGRAL_CONFIG, INTEGRAL_ENABLE));
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRule(IntegralRuleVO vo) {
        List<IntegralRuleDO> ruleList = vo.getRuleList();

        integralRuleMapper.delete();
        integralRuleMapper.insertBatch(ruleList, ruleList.size());

        dictDataService.updateDictByLabel(INTEGRAL_DEDUCTION_RULE, vo.getIntegralDeductionRule());
        dictDataService.updateDictByLabel(INTEGRAL_NAME, vo.getIntegralName());
        dictDataService.updateDictByLabel(INTEGRAL_ENABLE, vo.getIntegralEnable());
    }

}
