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

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.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.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.*;

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

    @Autowired
    private ICuMemberSettlePeriodService iCuMemberSettlePeriodService;
    @Autowired
    private ICuMemberTreeService iCuMemberTreeService;
    @Autowired
    private ICuMemberAchieveService iCuMemberAchieveService;

    @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 month = DateUtils.currentMonth(settleDate);
        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 = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(currentMonthDate);
        if (day <= 6) {
            // 计算上月
            String beforeFirstMonthDate = DateUtils.beforeMonthFirstDateStr(settleDate);
            String beforeTable = TableNameConstants.CU_MEMBER_ACHIEVE + DateUtils.getMonth(beforeFirstMonthDate);
            CuMemberSettlePeriod beforePeriod = iCuMemberSettlePeriodService.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 = iCuMemberTreeService.queryExistOracleTable(monthTableName);
        if (number == null || number <= 0) {
            // 创建表
            iCuMemberAchieveService.createCuMemberMonthTable(monthTableName);
            iCuMemberTreeService.createCuMemberTreeDayOnlyIndex(monthTableName);
            iCuMemberTreeService.createCuMemberTreeParentIndex(monthTableName);
            iCuMemberTreeService.createCuMemberTreePlaceIndex(monthTableName);
        }
        if (day >= 1 && day <= 6) {
            iCuMemberAchieveService.mergeCuMemberMonth(day, DateUtils.beforeMonthLastDateStr(settleDate));
        }
        iCuMemberAchieveService.mergeCuMemberMonth(0, DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, settleDate));
    }

}
