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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.achieve.service.ICuMemberAchieveService;
import com.hzs.bonus.achieve.service.ICuMemberMonthAchieveService;
import com.hzs.bonus.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.achieve.CuMemberMonthAchieve;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.member.ext.CuMemberMonthAchieveExt;
import com.hzs.common.domain.member.honor.CuHonorWall;
import com.hzs.common.domain.member.honor.CuMemberHonorWall;
import com.hzs.bonus.achieve.mapper.CuMemberMonthAchieveMapper;
import com.hzs.bonus.achieve.service.ICuMemberTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 会员月数据 服务实现类
 *
 * @author hzs
 * @since 2023-06-06
 */
@Service
public class CuMemberMonthAchieveServiceImpl extends ServiceImpl<CuMemberMonthAchieveMapper, CuMemberMonthAchieve> implements ICuMemberMonthAchieveService {

    @Autowired
    private ICuMemberSettlePeriodService cuMemberSettlePeriodService;
    @Autowired
    private ICuMemberTreeService cuMemberTreeService;
    @Autowired
    private ICuMemberAchieveService cuMemberAchieveService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertCuMemberAchieveByEveryDay(Date settleDate) {
        // 推荐业绩，复购业绩，
        insertCuMemberMonthAchieve(settleDate);
        // 月表统计数据
        insertCuMemberCuMemberMonthYearAchieve(settleDate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertCuMemberMonthAchieve(Date settleDate) {
        // 判断日期 1月7日 发放去年奖章  1 4 7 10 计算季度
        // 1-6号计算上个月
        int day = DateUtils.currentDay(settleDate);
        String settleDateStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, settleDate);
        String table = TableNameConstants.CU_MEMBER_ACHIEVE + DateUtils.getMonth(settleDateStr);
        // 获得当前月第一天
        String currentMonthDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.currentMonthFirstDate(settleDate));
        CuMemberSettlePeriod cuMemberSettlePeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(currentMonthDate);
        if (day <= 6) {
            // 计算上月
            String beforeFirstMonthDate = DateUtils.beforeMonthFirstDateStr(settleDate);
            String beforeTable = TableNameConstants.CU_MEMBER_ACHIEVE + DateUtils.getMonth(beforeFirstMonthDate);
            CuMemberSettlePeriod beforePeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(beforeFirstMonthDate);
            baseMapper.insertCuMemberMonthAchieve(beforeTable, beforePeriod.getPkId(), DateUtils.getMonth(beforeFirstMonthDate));
        }
        // 计算当前月
        String settleMonth = DateUtils.getMonth(currentMonthDate);
        baseMapper.insertCuMemberMonthAchieve(table, cuMemberSettlePeriod.getPkId(), settleMonth);
    }

    @Override
    public void insertCuMemberCuMemberMonthYearAchieve(Date settleDate) {
        // 1-6号修改上月数据+当月数据 7号及以后只修改当月数据
        // 创建业绩每月统计报表-年表
        // 月表
        int day = DateUtils.currentDay(settleDate);
        String monthTableName = TableNameConstants.CU_MEMBER_MONTH + DateUtils.getMonth(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, settleDate));
        Integer number = cuMemberTreeService.queryExistOracleTable(monthTableName);
        if (number == null || number <= 0) {
            // 创建表
            cuMemberAchieveService.createCuMemberMonthTable(monthTableName);
            cuMemberTreeService.createCuMemberTreeDayOnlyIndex(monthTableName);
            cuMemberTreeService.createCuMemberTreeParentIndex(monthTableName);
            cuMemberTreeService.createCuMemberTreePlaceIndex(monthTableName);
        }
        if (day >= 1 && day <= 6) {
            cuMemberAchieveService.mergeCuMemberMonth(day, DateUtils.beforeMonthLastDateStr(settleDate));
        }
        cuMemberAchieveService.mergeCuMemberMonth(0, DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, settleDate));
    }

    private void grantHonorWall(Integer startPeriod, Integer endPeriod, String settleMonth, Integer recommend, Integer repurchase,
                                Map<String, CuHonorWall> honorWallMap, List<CuMemberHonorWall> cuMemberHonorWallList) {
        // 先发月徽章 查询值，查询前3名
        List<CuMemberMonthAchieveExt> cuMemberMonthAchieveList = baseMapper.queryCuMemberMonthAchieveByDate(startPeriod, endPeriod, recommend, repurchase);
        if (CollectionUtil.isNotEmpty(cuMemberMonthAchieveList)) {
            Map<Integer, List<CuMemberMonthAchieveExt>> monthAchieveMap = new HashMap<>();
            cuMemberMonthAchieveList.forEach(cuMemberMonthAchieve -> {
                if (monthAchieveMap.containsKey(cuMemberMonthAchieve.getWallType())) {
                    monthAchieveMap.get(cuMemberMonthAchieve.getWallType()).add(cuMemberMonthAchieve);
                } else {
                    List<CuMemberMonthAchieveExt> cuMemberMonthAchieveExtList = new ArrayList<>();
                    cuMemberMonthAchieveExtList.add(cuMemberMonthAchieve);
                    monthAchieveMap.put(cuMemberMonthAchieve.getWallType(), cuMemberMonthAchieveExtList);
                }
            });
            monthAchieveMap.forEach((wallType, cuMemberMonthAchieveExtList) -> {
                for (int i = 1; i <= cuMemberMonthAchieveExtList.size(); i++) {
                    CuMemberMonthAchieveExt cuMemberMonthAchieveExt = cuMemberMonthAchieveExtList.get(i - 1);
                    String key = cuMemberMonthAchieveExt.getPkCountry().toString() + cuMemberMonthAchieveExt.getWallType() + i;
                    if (honorWallMap.containsKey(key)) {
                        CuMemberHonorWall cuMemberHonorWall = CuMemberHonorWall.builder()
                                .pkMember(cuMemberMonthAchieveExt.getPkMember())
                                .pkHonorWall(honorWallMap.get(key).getPkId())
                                .settleMonth(settleMonth)
                                .build();
                        cuMemberHonorWall.setPkCountry(cuMemberMonthAchieveExt.getPkCountry());
                        cuMemberHonorWall.setPkCreator(MagicNumberConstants.PK_ADMIN);
                        cuMemberHonorWallList.add(cuMemberHonorWall);
                    }
                }
            });
        }
    }
}
