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.*;
import com.hzs.common.core.enums.EOrderType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.member.achieve.CuMemberAchieve;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.bonus.achieve.mapper.CuMemberAchieveMapper;
import com.hzs.bonus.achieve.param.MemberAchieveParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 会员业绩记录 服务实现类
 */
@Slf4j
@Service
public class CuMemberAchieveServiceImpl extends ServiceImpl<CuMemberAchieveMapper, CuMemberAchieve> implements ICuMemberAchieveService {

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


    @Override
    public String createCuMemberAchieve(String date) {
        // 创建业绩月表记录表
        String tableName = TableNameConstants.CU_MEMBER_ACHIEVE + DateUtils.getMonth(date);
        Integer num = cuMemberTreeService.queryExistOracleTable(tableName);
        if (num == null || num <= 0) {
            baseMapper.createCuMemberAchieveTable(tableName);
            cuMemberTreeService.createCuMemberTableUniqueIndex(tableName);
            cuMemberTreeService.createCuMemberTableSequence(tableName, TableNameConstants.CU_MEMBER_ACHIEVE_SEQ);
        }
        return tableName;
    }

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

    @Override
    public void mergeCuMemberMonth(Integer day, String date) {
        String tableName = TableNameConstants.CU_MEMBER_MONTH + DateUtils.getMonth(date);
        CuMemberSettlePeriod cuMemberSettlePeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(date);
        // 每月6号删除上月数据,重新生成
//        if (day == 6) {
        Integer number = cuMemberTreeService.queryExistOracleTable(tableName);
        if (number != null && number > 0) {
            cuMemberTreeService.truncateCuMemberTreeTable(tableName);
        }
//        }
        baseMapper.mergeCuMemberMonth(DateUtils.getMonth(date), cuMemberSettlePeriod.getPkId());
    }

    @Override
    public Boolean chargeBackSaOrder(SaOrder saOrder) {
        // 根据支付时间查询期间
        // 查询期间的cu_member_tree 查询网体，根据网体更新业绩，更新7天前到昨天网体业绩
        MemberAchieveParam cuMemberAchieve = MemberAchieveParam.builder()
                .pkRate(saOrder.getPkRate())
                .pkMember(saOrder.getPkMember())
                .newAmount(BigDecimal.ZERO)
                .newPv(BigDecimal.ZERO)
                .repNewAmount(BigDecimal.ZERO)
                .repNewPv(BigDecimal.ZERO)
                .newRealAmount(BigDecimal.ZERO)
                .newRealPv(BigDecimal.ZERO)
                .build();
        if (Objects.equals(EOrderType.REGISTER_ORDER.getValue(), saOrder.getOrderType()) ||
                Objects.equals(EOrderType.FICTITIOUS_ORDER.getValue(), saOrder.getOrderType())) {
            cuMemberAchieve.setNewAmount(saOrder.getOrderAmount());
            cuMemberAchieve.setNewPv(saOrder.getOrderAchieve());
        } else {
            cuMemberAchieve.setRepNewAmount(saOrder.getOrderAmount());
            cuMemberAchieve.setRepNewPv(saOrder.getOrderAchieve());
        }
//        String payTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, saOrder.getPayTime());
//        int period = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(payTime).getPkId();
//        String sourceTable = TableNameConstants.CU_MEMBER_TREE + DateUtils.getMonth(payTime);
//        for (int i = 7; i > 0; i--) {
//            String settleDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.beforeDate(i, ChronoUnit.DAYS));
//            int settlePeriod = cuMemberSettlePeriodService.getCuMemberSettlePeriodByDate(settleDate).getPkId();
//            String targetTable = TableNameConstants.CU_MEMBER_SETTLE + settlePeriod;
//            baseMapper.chargeBackSaOrder(targetTable, sourceTable, period, cuMemberAchieve);
//        }
        return Boolean.TRUE;
    }


}
