package com.hzs.activity.other.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.activity.other.mapper.AcOtherPriceMapper;
import com.hzs.activity.other.service.IAcOtherPriceConfigService;
import com.hzs.activity.other.service.IAcOtherPriceDataService;
import com.hzs.activity.other.service.IAcOtherPriceService;
import com.hzs.bonus.settle.IMemberSettleBonusApi;
import com.hzs.common.core.enums.EActType;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.domain.activity.other.AcOtherPrice;
import com.hzs.common.domain.activity.other.ext.AcOtherPriceConfigExt;
import com.hzs.common.domain.member.achieve.ext.CuMemberSettlePeriodExt;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 活动其它处理服务
 * @Author: jiang chao
 * @Time: 2024/1/18 11:41
 * @Classname: AcOtherPriceServiceImpl
 * @PackageName: com.hzs.activity.other.service.impl
 */
@Slf4j
@Service
public class AcOtherPriceServiceImpl extends ServiceImpl<AcOtherPriceMapper, AcOtherPrice> implements IAcOtherPriceService {

    @Autowired
    private IAcOtherPriceConfigService iAcOtherPriceConfigService;
    @Autowired
    private IAcOtherPriceDataService iAcOtherPriceDataService;

    @DubboReference
    private IMemberSettleBonusApi iMemberSettleBonusApi;

    @Override
    public AcOtherPrice queryMemberOrderPrice(Long pkBaseConfig, Long pkMember) {
        return baseMapper.queryMemberOrderPrice(pkBaseConfig, pkMember);
    }

    @Override
    public String acOtherPriceHandle() {
        // 定时任务每天凌晨执行昨天的数据，按昨天的数据 - 活动开始前一天的数据，得到截止到现在活动期间内的累计业绩

        // 因为定时任务是每天凌晨跑，所以需要日期 -1
        String endPeriodDate = DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, DateUtils.addDays(new Date(), -1));

        // 查询进行中的活动（所有国家）
        List<AcOtherPriceConfigExt> configList = iAcOtherPriceConfigService.queryList(Arrays.asList(EActType.TOURISM.getValue(), EActType.CAR.getValue()), endPeriodDate, null);

        if (CollectionUtil.isNotEmpty(configList)) {
            log.info("其它奖项定时处理 符合条件的活动配置为 {} 条", configList.size());

            for (AcOtherPriceConfigExt config : configList) {
                try {
                    log.info("其它奖项定时处理 开始处理中，config： {}", config);
                    // 活动时间前一天的期间
                    String startPeriodDate = DateUtils.parseDateToFormat(DateUtils.YYYY_MM_DD, DateUtils.addDays(config.getActStartDate(), -1));
                    // 查询两个时间的期间
                    Map<String, CuMemberSettlePeriodExt> periodMap = iMemberSettleBonusApi.queryPeriodList(Arrays.asList(startPeriodDate, endPeriodDate)).getData();

                    // 开始期间
                    CuMemberSettlePeriodExt startPeriod = periodMap.get(startPeriodDate);
                    // 结束期间
                    CuMemberSettlePeriodExt endPeriod = periodMap.get(endPeriodDate);

                    if (null == endPeriod) {
                        // 没有昨天期间
                        log.warn("其它奖项定时处理 本次活动没有期间数据. yesterdayPeriod: {}, config: {}", endPeriod, config);
                        continue;
                    } else if (null == startPeriod) {
                        // 没有开始期间，直接用昨天期的数据
                        iAcOtherPriceDataService.handleOtherPriceDataOnlyEnd(config, endPeriod.getPkId());
                    } else {
                        // 开始、昨天期间都有
                        iAcOtherPriceDataService.handleOtherPriceDataAll(config, startPeriod.getPkId(), endPeriod.getPkId());
                    }

                    // 处理会员直推业绩
                    baseMapper.handleOtherPrice(endPeriod.getPkId(), config.getPkBaseId(),
                            config.getActStartDate(), config.getActEndDate(),
                            config.getPlaceDeptPv(), config.getPlaceDeptNumber(),
                            config.getParentPv(), config.getPkCountry());

                    log.info("其它奖项定时处理 处理结束");
                } catch (Exception e) {
                    log.error("其它奖项定时处理 异常，config: {}", config, e);
                }
            }

        } else {
            log.info("其它奖项定时处理 本次没有符合条件的活动配置");
        }

        // 清除7天前已经逻辑删除的数据
        iAcOtherPriceDataService.clearOldData();

        return null;
    }

}
