package com.hzs.report.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.report.member.CuMemberPerk;
import com.hzs.common.domain.report.member.CuMemberPerkDetail;
import com.hzs.common.domain.report.member.ext.CuMemberPerkExt;
import com.hzs.report.member.mapper.CuMemberPerkMapper;
import com.hzs.report.member.param.MemberPerkQueryParam;
import com.hzs.report.member.service.ICuMemberPerkDetailService;
import com.hzs.report.member.service.ICuMemberPerkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 会员补贴配置 服务实现类
 *
 * @author zhangjing
 * @since 2024-04-22
 */
@Service
public class CuMemberPerkServiceImpl extends ServiceImpl<CuMemberPerkMapper, CuMemberPerk> implements ICuMemberPerkService {

    @Autowired
    private ICuMemberPerkDetailService iCuMemberPerkDetailService;

    @Override
    public List<CuMemberPerkExt> queryList(MemberPerkQueryParam param, Integer pkCountry) {
        return baseMapper.queryList(param, pkCountry);
    }

    @Override
    public CuMemberPerkExt queryDetail(Long pkId) {
        return baseMapper.queryDetail(pkId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveBatchData(List<CuMemberPerk> cuMemberPerkList, Long userId, Integer pkCountry) {
        // 当前时间
        Date nowDate = new Date();

        for (CuMemberPerk cuMemberPerk : cuMemberPerkList) {
            cuMemberPerk.setStartDate(DateUtils.getStartTime(cuMemberPerk.getStartDate()));
            cuMemberPerk.setEndDate(DateUtils.getEndTime(cuMemberPerk.getEndDate()));
            cuMemberPerk.setCalculateStartDate(cuMemberPerk.getStartDate());
            // 计算结束时间 是在统计时间 + 7天，因为需要重算处理
            cuMemberPerk.setCalculateEndDate(DateUtils.addDays(cuMemberPerk.getEndDate(), 7));
            cuMemberPerk.setCreationTime(nowDate);
            cuMemberPerk.setPkCreator(userId);
            cuMemberPerk.setPkCountry(pkCountry);

            cuMemberPerk.setPkModified(null);
            cuMemberPerk.setModifiedTime(null);
            // 清除复制数据
            this.clearCopyData(cuMemberPerk);
        }
        return this.saveBatch(cuMemberPerkList);
    }

    @Override
    public boolean updateData(CuMemberPerk cuMemberPerk, Long userId) {
        // 当前时间
        Date nowDate = new Date();

        cuMemberPerk.setStartDate(DateUtils.getStartTime(cuMemberPerk.getStartDate()));
        cuMemberPerk.setEndDate(DateUtils.getEndTime(cuMemberPerk.getEndDate()));
        cuMemberPerk.setCalculateStartDate(cuMemberPerk.getStartDate());
        // 计算结束时间 是在统计时间 + 7天，因为需要重算处理
        cuMemberPerk.setCalculateEndDate(DateUtils.addDays(cuMemberPerk.getEndDate(), 7));
        cuMemberPerk.setModifiedTime(nowDate);
        cuMemberPerk.setPkModified(userId);

        // 清除复制数据
        this.clearCopyData(cuMemberPerk);

        return this.updateById(cuMemberPerk);
    }

    /**
     * 清除复制数据
     *
     * @param cuMemberPerk
     */
    private void clearCopyData(CuMemberPerk cuMemberPerk) {
        cuMemberPerk.setFirstAmount(BigDecimal.ZERO);
        cuMemberPerk.setFirstAchieve(BigDecimal.ZERO);
        cuMemberPerk.setRepurchaseAmount(BigDecimal.ZERO);
        cuMemberPerk.setRepurchaseAchieve(BigDecimal.ZERO);
        cuMemberPerk.setUsedPolicy(BigDecimal.ZERO);
        cuMemberPerk.setUsedRepurchase(BigDecimal.ZERO);
        cuMemberPerk.setDistinctFirstAmount(BigDecimal.ZERO);
        cuMemberPerk.setDistinctFirstAchieve(BigDecimal.ZERO);
        cuMemberPerk.setDistinctRepurchaseAmount(BigDecimal.ZERO);
        cuMemberPerk.setDistinctRepurchaseAchieve(BigDecimal.ZERO);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deletePerkData(List<Long> pkIdList, Long userId) {
        // 当前时间
        Date nowDate = new Date();

        // 删除补贴配置明细
        UpdateWrapper<CuMemberPerkDetail> detailUpdateWrapper = new UpdateWrapper<>();
        detailUpdateWrapper.eq(SystemFieldConstants.DEL_FLAG, EDelFlag.UN_DELETE.getValue());
        detailUpdateWrapper.in("PK_MEMBER_PERK", pkIdList);
        detailUpdateWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        detailUpdateWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        detailUpdateWrapper.set(SystemFieldConstants.MODIFIED_TIME, nowDate);
        iCuMemberPerkDetailService.update(detailUpdateWrapper);

        // 删除补贴配置
        UpdateWrapper<CuMemberPerk> perkUpdateWrapper = new UpdateWrapper<>();
        perkUpdateWrapper.eq(SystemFieldConstants.DEL_FLAG, EDelFlag.UN_DELETE.getValue());
        perkUpdateWrapper.in(SystemFieldConstants.PK_ID, pkIdList);
        perkUpdateWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        perkUpdateWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        perkUpdateWrapper.set(SystemFieldConstants.MODIFIED_TIME, nowDate);
        this.update(perkUpdateWrapper);
    }

}
