package com.hfkj.schedule;

import com.hfkj.common.utils.DateUtil;
import com.hfkj.entity.*;
import com.hfkj.service.rebate.BsRebateActivityService;
import com.hfkj.service.rebate.BsRebatePartMerService;
import com.hfkj.service.rebate.BsRebateRewardRosterService;
import com.hfkj.service.rebate.BsRebateRewardService;
import com.hfkj.sysenum.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @className: RebateActivitySchedule
 * @author: HuRui
 * @date: 2023/10/27
 **/
@Configuration
public class RebateActivitySchedule {

    private static final Logger log = LoggerFactory.getLogger(RebateActivitySchedule.class);

    @Resource
    private BsRebateActivityService rebateActivityService;

    @Resource
    private BsRebateRewardService rebateRewardService;
    @Resource
    private BsRebatePartMerService rebatePartMerService;

    /**
     * 生成返利奖池
     * 每个月1号 凌晨0点10分生成
     */
    @Scheduled(cron="0 10 0 1 * ?")
    public void generateReward() {
        try {
            Calendar startTime = Calendar.getInstance();
            startTime.setTime(new Date());
            startTime.add(Calendar.MONTH, - 1);
            startTime.set(Calendar.DATE, startTime.getActualMinimum(Calendar.DAY_OF_MONTH));
            startTime.set(Calendar.HOUR_OF_DAY, 00);
            startTime.set(Calendar.MINUTE, 00);
            startTime.set(Calendar.SECOND, 00);

            Calendar endTime = Calendar.getInstance();
            endTime.setTime(new Date());
            endTime.add(Calendar.MONTH, -1);
            endTime.set(Calendar.DATE, endTime.getActualMaximum(Calendar.DAY_OF_MONTH));
            endTime.set(Calendar.HOUR_OF_DAY, 23);
            endTime.set(Calendar.MINUTE, 59);
            endTime.set(Calendar.SECOND, 59);

            Map<String,Object> param = new HashMap<>();
            param.put("status", RebatePartMerStatusEnum.status1.getNumber());
            List<BsRebatePartMer> merList = rebatePartMerService.getMerList(param);

            List<Long> merIdList = new ArrayList<>();
            for (BsRebatePartMer partMer : merList) {
                merIdList.add(partMer.getMerId());
            }

            // 生成奖池
            rebateRewardService.createReward(startTime.getTime(), endTime.getTime(), merIdList.stream().distinct().collect(Collectors.toList()));

        } catch (Exception e) {
            log.error("生成奖池异常：" + e);
        }

    }

    /**
     * 取消订单
     * 1分钟扫描一次
     */
    @Scheduled(cron="0 0/1 * * * ?")
    public void endActivity() {
        Map<String,Object> param = new HashMap<>();
        param.put("endTimeE", new Date().getTime());
        param.put("status", RebateActivityStatusEnum.status2.getNumber());

        List<BsRebateActivity> activityList = rebateActivityService.getActivityList(param);
        for (BsRebateActivity activity : activityList) {
            try {

                // 关闭活动
                rebateActivityService.closeActivity(activity.getActivityNo(), RebateActivityEndTypeEnum.type1);

            } catch (Exception e) {

            }
        }
    }
}
