package com.cool.store.service.tp.impl;

import com.cool.store.dao.tp.TpRuleDAO;
import com.cool.store.entity.tp.TpRuleDO;
import com.cool.store.request.tp.TpPenaltyRuleUpdateRequest;
import com.cool.store.request.tp.TpRewardRuleUpdateRequest;
import com.cool.store.request.tp.TpRuleQueryRequest;
import com.cool.store.service.dict.impl.DictService;
import com.cool.store.service.tp.TpRuleService;
import com.cool.store.utils.BeanUtil;
import com.cool.store.utils.TpHelper;
import com.cool.store.vo.tp.TpPenaltyRuleDetailVO;
import com.cool.store.vo.tp.TpPenaltyRuleListVO;
import com.cool.store.vo.tp.TpRewardRuleDetailVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 十二分制-奖惩规则 服务实现类
 * </p>
 *
 * @author wangff
 * @since 2025/11/4
 */
@Service
@RequiredArgsConstructor
public class TpRuleServiceImpl implements TpRuleService {
    private final TpRuleDAO tpRuleDAO;
    private final DictService dictService;

    @Override
    public Boolean insertPenaltyRule(TpPenaltyRuleUpdateRequest request) {
        TpRuleDO tpRuleDO = BeanUtil.toBean(request, TpRuleDO.class);
        tpRuleDO.setRuleNo(TpHelper.generateRuleNo(false));
        tpRuleDO.setType(0);
        return tpRuleDAO.insertSelective(tpRuleDO);
    }

    @Override
    public Boolean updatePenaltyRule(TpPenaltyRuleUpdateRequest request) {
        TpRuleDO tpRuleDO = BeanUtil.toBean(request, TpRuleDO.class);
        return tpRuleDAO.updateSelective(tpRuleDO);
    }

    @Override
    public TpPenaltyRuleDetailVO penaltyRuleDetail(Long ruleId) {
        TpRuleDO tpRuleDO = tpRuleDAO.getById(ruleId);
        if (Objects.nonNull(tpRuleDO)) {
            TpPenaltyRuleDetailVO vo = BeanUtil.toBean(tpRuleDO, TpPenaltyRuleDetailVO.class);
            dictService.fillDictField(vo);
            return vo;
        }
        return null;
    }

    @Override
    public PageInfo<TpPenaltyRuleListVO> penaltyRulePage(TpRuleQueryRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<TpRuleDO> list = tpRuleDAO.getEffectiveList(request);
        PageInfo<TpRuleDO> page = new PageInfo<>(list);
        PageInfo<TpPenaltyRuleListVO> newPage = BeanUtil.toPage(page, TpPenaltyRuleListVO.class);
        dictService.fillDictField(newPage.getList());
        return newPage;
    }

    @Override
    public Boolean insertRewardRule(TpRewardRuleUpdateRequest request) {
        TpRuleDO tpRuleDO = BeanUtil.toBean(request, TpRuleDO.class);
        tpRuleDO.setRuleNo(TpHelper.generateRuleNo(true));
        tpRuleDO.setType(1);
        return tpRuleDAO.insertSelective(tpRuleDO);
    }

    @Override
    public Boolean updateRewardRule(TpRewardRuleUpdateRequest request) {
        TpRuleDO tpRuleDO = BeanUtil.toBean(request, TpRuleDO.class);
        return tpRuleDAO.updateSelective(tpRuleDO);
    }

    @Override
    public TpRewardRuleDetailVO rewardRuleDetail(Long ruleId) {
        TpRuleDO tpRuleDO = tpRuleDAO.getById(ruleId);
        if (Objects.nonNull(tpRuleDO)) {
            TpRewardRuleDetailVO vo = BeanUtil.toBean(tpRuleDO, TpRewardRuleDetailVO.class);
            dictService.fillDictField(vo);
            return vo;
        }
        return null;
    }

    @Override
    public PageInfo<TpRewardRuleDetailVO> rewardRulePage(TpRuleQueryRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<TpRuleDO> list = tpRuleDAO.getEffectiveList(request);
        PageInfo<TpRuleDO> page = new PageInfo<>(list);
        PageInfo<TpRewardRuleDetailVO> newPage = BeanUtil.toPage(page, TpRewardRuleDetailVO.class);
        dictService.fillDictField(newPage.getList());
        return newPage;
    }

    @Override
    public Boolean deleteRuleByIds(List<Long> ruleIds) {
        return tpRuleDAO.deleteByIds(ruleIds);
    }

    @Override
    public Boolean enableRuleByIds(List<Long> ruleIds, Integer enable) {
        return tpRuleDAO.updateStatus(ruleIds, enable);
    }
}
