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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.achieve.service.impl.CuMemberBonusSettle;
import com.hzs.bonus.base.mapper.CuAwardsControlMapper;
import com.hzs.bonus.member.mapper.CuMemberActivityMapper;
import com.hzs.bonus.member.param.MemberActivityParam;
import com.hzs.bonus.member.service.ICuMemberActivity;
import com.hzs.bonus.member.vo.MemberActivityDetailVO;
import com.hzs.bonus.member.vo.MemberActivityVO;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.reflect.ReflectUtils;
import com.hzs.common.domain.member.base.CuAwardsControl;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @description: 会员活动数据查询
 * @author: sui q
 * @time: 2024/7/9 10:06
 * @classname: CuMemberActivityImpl
 * @package_name: com.hzs.bonus.member.service.impl
 * version 1.0.0
 */
@Service
public class CuMemberActivityImpl extends ServiceImpl<CuMemberActivityMapper, T> implements ICuMemberActivity {

    @Autowired
    private CuMemberBonusSettle cuMemberBonusSettle;

    @Override
    public List<MemberActivityVO> queryCuMemberActivityVO(String settleTable, MemberActivityParam memberActivityParam) {
        return baseMapper.queryCuMemberActivityVO(settleTable, memberActivityParam);
    }

    @Override
    public List<MemberActivityVO> queryRecCuMemberActivityVO(String settleTable, MemberActivityParam memberActivityParam) {
        return baseMapper.queryRecCuMemberActivityVO(settleTable, memberActivityParam);
    }

    @Override
    public Map<Long, List<MemberActivityDetailVO>> queryCuMemberActivityDetailVO(String beforeSettleTable, String settleTable, List<MemberActivityVO> memberActivityVoList, MemberActivityParam memberActivityParam) {
        // 处理memberActivityParam  先排序
        List<BigDecimal> compareList = new ArrayList<>();
        int placeDeptNum = getPlaceDeptNum(memberActivityParam, compareList);
        // 升序
        List<List<?>> lists = cuMemberBonusSettle.handleCutList(memberActivityVoList);
        Map<Long, List<MemberActivityDetailVO>> activityDetailMap = new HashMap<>();
        for (List<?> list : lists) {
            if (placeDeptNum > 0) {
                memberActivityParam.setPlaceDeptNum(placeDeptNum);
                memberActivityParam.setMaxPlaceDeptAchieve(compareList.get(4));
            }
            List<MemberActivityDetailVO> activityDetailVOList = baseMapper.queryCuMemberActivityDetailVO(beforeSettleTable, settleTable, list, memberActivityParam);
            // 如果要查询安置部门的值，需要验证安置业绩是否达标
            packageMemberActivityByPlaceAchieve(compareList, placeDeptNum, activityDetailMap, activityDetailVOList);
        }
        return activityDetailMap;
    }

    private void packageMemberActivityByPlaceAchieve(List<BigDecimal> compareList, int placeDeptNum, Map<Long, List<MemberActivityDetailVO>> activityDetailMap, List<MemberActivityDetailVO> activityDetailVOList) {
        if (placeDeptNum > 0) {
            Map<Long, List<MemberActivityDetailVO>> validateMemberMap = new HashMap<>();
            packageDetailMap(validateMemberMap, activityDetailVOList);
            for (Long pkMember : validateMemberMap.keySet()) {
                List<MemberActivityDetailVO> memberActivityDetailVoList = validateMemberMap.get(pkMember);
                int j = 0;
                int flag = 0;
                for (BigDecimal placeDeptAchieve : compareList) {
                    if (ComputeUtil.compareValue(placeDeptAchieve)) {
                        while (j < memberActivityDetailVoList.size()) {
                            MemberActivityDetailVO memberActivityDetailVO = memberActivityDetailVoList.get(j);
                            j++;
                            if (ComputeUtil.compareValue(memberActivityDetailVO.getSumPv(), placeDeptAchieve)) {
                                flag += 1;
                                break;
                            }
                        }
                    }
                }
                if (flag >= placeDeptNum) {
                    memberActivityDetailVoList.sort((Comparator.comparing(MemberActivityDetailVO::getPlaceDept)));
                    activityDetailMap.put(pkMember, memberActivityDetailVoList);
                }
            }
        } else {
            packageDetailMap(activityDetailMap, activityDetailVOList);
        }
    }

    private int getPlaceDeptNum(MemberActivityParam memberActivityParam, List<BigDecimal> compareList) {
        int placeDeptNum = 0;
        for (int i = 1; i < 6; i++) {
            BigDecimal placeDeptAchieve = ReflectUtils.invokeGetter(memberActivityParam, "placeDeptAchieve" + i);
            compareList.add(placeDeptAchieve);
            if (ComputeUtil.compareValue(placeDeptAchieve)) {
                placeDeptNum += 1;
            }
        }
        compareList.sort(Comparator.naturalOrder());
        return placeDeptNum;
    }

    @Override
    public Map<Long, List<MemberActivityDetailVO>> queryCouCuMemberActivityDetailVO(String beforeSettleTable, String settleTable, List<MemberActivityVO> memberActivityVoList, MemberActivityParam memberActivityParam) {
        List<BigDecimal> compareList = new ArrayList<>();
        int placeDeptNum = getPlaceDeptNum(memberActivityParam, compareList);
        // 升序
        List<List<?>> lists = cuMemberBonusSettle.handleCutList(memberActivityVoList);
        Map<Long, List<MemberActivityDetailVO>> activityDetailMap = new HashMap<>();
        for (List<?> list : lists) {
            if (placeDeptNum > 0) {
                memberActivityParam.setPlaceDeptNum(placeDeptNum);
                memberActivityParam.setMaxPlaceDeptAchieve(compareList.get(4));
            }
            List<MemberActivityDetailVO> activityDetailVOList = baseMapper.queryCouCuMemberActivityDetailVO(beforeSettleTable, settleTable, list, memberActivityParam);
            // 如果要查询安置部门的值，需要验证安置业绩是否达标
            packageMemberActivityByPlaceAchieve(compareList, placeDeptNum, activityDetailMap, activityDetailVOList);
        }
        return activityDetailMap;
    }

    private void packageDetailMap(Map<Long, List<MemberActivityDetailVO>> activityDetailMap, List<MemberActivityDetailVO> activityDetailVOList) {
        for (MemberActivityDetailVO memberActivityDetailVO : activityDetailVOList) {
            if (!ComputeUtil.compareValue(memberActivityDetailVO.getSumPv(), BigDecimal.ZERO)) {
                memberActivityDetailVO.setSumPv(BigDecimal.ZERO);
            }
            if (activityDetailMap.containsKey(memberActivityDetailVO.getPkMember())) {
                activityDetailMap.get(memberActivityDetailVO.getPkMember()).add(memberActivityDetailVO);
            } else {
                List<MemberActivityDetailVO> memberActivityDetailVOList = new ArrayList<>();
                memberActivityDetailVOList.add(memberActivityDetailVO);
                activityDetailMap.put(memberActivityDetailVO.getPkMember(), memberActivityDetailVOList);
            }
        }
    }
}
