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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.achieve.service.impl.CuMemberBonusSettle;
import com.hzs.bonus.detail.service.ICuMemberAssessDetailService;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.enums.EAssessmentType;
import com.hzs.common.domain.member.detail.CuMemberAssessDetail;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.bonus.detail.mapper.CuMemberAssessDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 考核业绩变化明细表 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-10-28
 */
@Service
public class CuMemberAssessDetailServiceImpl extends ServiceImpl<CuMemberAssessDetailMapper, CuMemberAssessDetail> implements ICuMemberAssessDetailService {

    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;


    @Override
    public void deleteCuMemberAssessDetailByPeriod(String tableName, Integer period) {
        baseMapper.deleteCuMemberAssessDetailByPeriod(tableName, period);
    }

    @Override
    public void insertCuMemberAssesDetailByOrder(String tableName, Integer period, Date startDate, Date endDate) {
        baseMapper.insertCuMemberAssesDetailByOrder(tableName, period, startDate, endDate);
    }

    @Override
    public void insertCuMemberAssessDetailByPeriod(String tableName, Integer period, List<CuMemberSettleExt> newCuMemberSettleExtList) {
        List<CuMemberSettleExt> purchaseMemberList = new ArrayList<>();
        List<CuMemberSettleExt> shareMemberList = new ArrayList<>();
        newCuMemberSettleExtList.forEach(cuMemberSettleExt -> {
            if (EAssessmentType.REPURCHASE_ASSESS.getValue() == cuMemberSettleExt.getAssessType()) {
                purchaseMemberList.add(cuMemberSettleExt);
            } else {
                shareMemberList.add(cuMemberSettleExt);
            }
        });
        if (purchaseMemberList.size() > 0) {
            List<List<?>> purchaseHandleCutList = cuMemberBonusSettle.handleCutList(purchaseMemberList);
            purchaseHandleCutList.forEach(cuMemberBonusSettleList ->
                    baseMapper.insertCuMemberAssessDetailByPeriod(tableName, period, cuMemberBonusSettleList, EAssessmentType.REPURCHASE_ASSESS.getValue()) );
        }
        if (shareMemberList.size() > 0) {
            List<List<?>> shareHandleCutList = cuMemberBonusSettle.handleCutList(shareMemberList);
            shareHandleCutList.forEach(cuMemberBonusSettleList ->
                    baseMapper.insertCuMemberAssessDetailByPeriod(tableName, period, cuMemberBonusSettleList, EAssessmentType.SHARE_ASSESS.getValue()));
        }
    }

    @Override
    public void updateCuMemberAssessDetailByPeriod(String tableName, Integer period) {
        baseMapper.updateCuMemberAssessDetailByPeriod(tableName, period);
    }

    @Override
    public void deleteCuMemberAssessDetailByList(List<CuMemberAssessDetail> revokeDetailList) {
        List<List<?>> handleCutList = cuMemberBonusSettle.handleCutList(revokeDetailList,
                MagicNumberConstants.DELETE_DATA_BATCH_UPDATE_NUM);
        handleCutList.forEach(list -> {
            baseMapper.updateCuMemberAssessDetailByRevoke(list);
        });
    }
}
