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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.bonus.achieve.service.ICuMemberTreeService;
import com.hzs.bonus.achieve.service.impl.CuMemberBonusSettle;
import com.hzs.bonus.detail.service.ICuMemberAwardsService;
import com.hzs.common.core.constant.*;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.achieve.ext.CuMemberRetailRangeExt;
import com.hzs.common.domain.member.detail.CuMemberAwards;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.bonus.base.service.ICuMemberService;
import com.hzs.bonus.detail.mapper.CuMemberAwardsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * 会员信息-奖衔升级记录 服务实现类
 */
@Service
public class CuMemberAwardsServiceImpl extends ServiceImpl<CuMemberAwardsMapper, CuMemberAwards> implements ICuMemberAwardsService {

    @Autowired
    private ICuMemberService iCuMemberService;
    @Autowired
    private ICuMemberTreeService iCuMemberTreeService;
    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;

    @Override
    public void calculateCuMemberRetailRangeAwardsInit(String settleDate, Integer period, String rangeTableName) {
        if (settleDate.endsWith("01")) {
            // 每月1号初始化结算表奖衔（月度奖衔归0，V5给S1奖衔）
            iCuMemberTreeService.updateCuMemberRetailRangeInitAward(rangeTableName);
            // 每月1号初始化会员表奖衔（月度奖衔归0，V5给S1奖衔）
            iCuMemberService.updateCuMemberRetailRangeInitAward();
        }
        // 回退结算表奖衔
        iCuMemberTreeService.updateBackCuMemberRetailRangeAward(rangeTableName, period);
        // 回退会员表奖衔
        iCuMemberService.updateBackCuMemberRetailRangeAward(period);
        // 删除会员奖衔升级记录
        baseMapper.deleteCuMemberAwards(period);

        // 更新手动设置奖衔，一种有效期前的，一种有效期到达的
        // 有效期前的奖衔小于手动的，修改奖衔，
        Date date = DateUtils.parseStringToDate(settleDate);
        iCuMemberTreeService.updateCuMemberRetailRangeAwardByDate(rangeTableName, date);
        iCuMemberService.updateCuMemberRetailRangeAwardByDate(date);
    }

    @Override
    public void saveBatchCuMemberAwards(List<CuMemberAwards> cuMemberAwardsList) {
        List<List<?>> cutOrderList = cuMemberBonusSettle.handleCutList(cuMemberAwardsList, MagicNumberConstants.BATCH_UPDATE_NUM);
        cutOrderList.forEach(orderList ->
                baseMapper.saveBatchCuMemberAwards(orderList));
    }

    @Override
    public void deleteCuMemberAwards(List<CuMemberAwards> cuMemberAwards, Integer period) {
        baseMapper.deleteCuMemberAwardsByList(cuMemberAwards, period);
    }

    @Override
    public List<CuMemberAwards> queryCuMemberAwards(List<CuMemberRetailRangeExt> cuMemberRetailRangeExtList, Integer period) {
        return baseMapper.queryCuMemberAwards(cuMemberRetailRangeExtList, period);
    }

    @Override
    public void mergeCuMemberBackAwards(String rangeTableName, Long pkMember, Integer period, Integer isToday) {
        baseMapper.mergeCuMemberBackAwards(rangeTableName, pkMember, period, EAwardsType.RANGE_TYPE.getValue());
        if (EYesNo.YES.getIntValue() == isToday) {
            // 当天，需要处理秒结的分红奖衔
            baseMapper.mergeCuMemberBackAwards(rangeTableName, pkMember, period, EAwardsType.SHARE_TYPE.getValue());
        }
    }

    private void getCuMemberAwards(Map<Long, CuMemberSettleExt> originalCuMemberSettleMap, Integer period, List<CuMemberAwards> cuMemberAwardsList, CuMemberSettleExt enoughAwardsMember) {
        CuMemberAwards cuMemberAwards = CuMemberAwards.builder()
                .pkMember(enoughAwardsMember.getPkMember())
                .period(period)
                .upType(EUpgradeType.AUTO_UPGRADE.getValue())
                // 原等级
                .oldLevel(originalCuMemberSettleMap.get(enoughAwardsMember.getPkMember()).getPkOriAwards())
                .newLevel(enoughAwardsMember.getPkAwards())
                // 默认不免考
                .purchaseStatus(EYesNo.NO.getIntValue())
                .build();
        cuMemberAwards.setPkCreator(MagicNumberConstants.PK_ADMIN);
        cuMemberAwards.setPkCountry(enoughAwardsMember.getPkSettleCountry());
        cuMemberAwardsList.add(cuMemberAwards);
    }

}
