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.ICuMemberSettlePeriodService;
import com.hzs.bonus.achieve.service.ICuMemberTreeService;
import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.enums.EAwardsType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.achieve.CuMemberTree;
import com.hzs.bonus.achieve.mapper.CuMemberTreeMapper;
import lombok.extern.slf4j.Slf4j;
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.*;

/**
 * 会员网体历史记录 服务实现类
 */
@Slf4j
@Service
public class CuMemberTreeServiceImpl extends ServiceImpl<CuMemberTreeMapper, CuMemberTree> implements ICuMemberTreeService {

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCuMemberTree(String date) {
        // 创建日表记录表
        int period = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(date).getPkId();
        String settleTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
        Integer number = baseMapper.queryExistOracleTable(settleTableName);
        if (number == null || number <= 0) {
            baseMapper.createCuMemberSettleTable(settleTableName);
            baseMapper.createCuMemberTreeDayOnlyIndex(settleTableName);
            baseMapper.createCuMemberTreeParentIndex(settleTableName);
            baseMapper.createCuMemberTreePlaceIndex(settleTableName);
        }
        // 创建新零售级差结算记录表
        String retailRangeTableName = TableNameConstants.CU_MEMBER_RETAIL_RANGE + period;
        number = baseMapper.queryExistOracleTable(retailRangeTableName);
        if (number == null || number <= 0) {
            baseMapper.createCuMemberRetailRangeTable(retailRangeTableName);
            baseMapper.createCuMemberTreeDayOnlyIndex(retailRangeTableName);
            baseMapper.createCuMemberTreeParentIndex(retailRangeTableName);
        }
        number = baseMapper.queryExistOrderTableCount(retailRangeTableName);
        if (number == null || number <= 0) {
            baseMapper.insertCuMemberRetailRangeByMember(retailRangeTableName);
        }

        // 创建月表记录表
        createMonthTreeTableName(date);
        // 创建月表业绩记录表
        iCuMemberAchieveService.createCuMemberAchieve(date);

        // 创建秒接表，秒结表只需要表结构，跟日表一模一样，秒结表期间是date+1期间
        Date tomorrowDate = DateUtils.afterDate(1, ChronoUnit.DAYS, DateUtils.parseStringToDate(date));
        String tomorrow = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, tomorrowDate);
        int nextPeriod = iCuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(tomorrow).getPkId();
        String secondRetailRangeTableName = TableNameConstants.CU_MEMBER_RETAIL_S + nextPeriod;
        number = baseMapper.queryExistOracleTable(secondRetailRangeTableName);
        if (number == null || number <= 0) {
            baseMapper.createCuMemberRetailRangeSecondTable(secondRetailRangeTableName);
            baseMapper.createCuMemberTreeDayOnlyIndex(secondRetailRangeTableName);
            baseMapper.createCuMemberTreeParentIndex(secondRetailRangeTableName);
        }
    }

    private String createMonthTreeTableName(String date) {
        Integer number;
        String monthTableName = TableNameConstants.CU_MEMBER_TREE + DateUtils.getMonth(date);
        number = baseMapper.queryExistOracleTable(monthTableName);
        if (number == null || number <= 0) {
            baseMapper.createCuMemberTreeTable(monthTableName);
            baseMapper.createCuMemberTableUniqueIndex(monthTableName);
            baseMapper.createCuMemberTreeParentIndex(monthTableName);
            baseMapper.createCuMemberTreePlaceIndex(monthTableName);
            baseMapper.createCuMemberTableSequence(monthTableName, TableNameConstants.CU_MEMBER_TREE_SEQ);
        }
        return monthTableName;
    }

    @Override
    public Integer queryExistOracleTable(String tableName) {
        return baseMapper.queryExistOracleTable(tableName);
    }

    @Override
    public void createCuMemberTableUniqueIndex(String tableName) {
        baseMapper.createCuMemberTableUniqueIndex(tableName);
    }

    @Override
    public void createCuMemberTableSequence(String tableName, String tableNameSeq) {
        baseMapper.createCuMemberTableSequence(tableName, tableNameSeq);
    }

    @Override
    public void updateBackCuMemberRetailRangeAward(String rangeTableName, Integer period) {
        baseMapper.updateBackCuMemberRetailRangeAward(rangeTableName, period, EAwardsType.RANGE_TYPE.getValue());
        baseMapper.updateBackCuMemberRetailRangeAward(rangeTableName, period, EAwardsType.SHARE_TYPE.getValue());
    }

    @Override
    public void updateCuMemberRetailRangeAwardByDate(String rangeTableName, Date settleDate) {
        baseMapper.updateCuMemberRetailRangeAwardByDate(rangeTableName, settleDate, EAwardsType.RANGE_TYPE.getValue());
        baseMapper.updateCuMemberRetailRangeAwardByDate(rangeTableName, settleDate, EAwardsType.SHARE_TYPE.getValue());
    }

    @Override
    public void updateCuMemberRetailRangeInitAward(String rangeTableName) {
        baseMapper.updateCuMemberRetailRangeInitAward(rangeTableName);
        baseMapper.updateCuMemberRetailRangeInitV5Award(rangeTableName);
    }

    @Override
    public void truncateCuMemberTreeTable(String tableName) {
        baseMapper.truncateCuMemberTreeTable(tableName);
    }

    @Override
    public void createCuMemberTreeDayOnlyIndex(String tableName) {
        baseMapper.createCuMemberTreeDayOnlyIndex(tableName);
    }

    @Override
    public void createCuMemberTreeParentIndex(String tableName) {
        baseMapper.createCuMemberTreeParentIndex(tableName);
    }

    @Override
    public void createCuMemberTreePlaceIndex(String tableName) {
        baseMapper.createCuMemberTreePlaceIndex(tableName);
    }

}
