package com.microservices.pms.task;

import com.microservices.common.core.utils.DateUtils;
import com.microservices.pms.product.domain.PmsProductPlan;
import com.microservices.pms.product.mapper.PmsProductPlanMapper;
import com.microservices.pms.product.service.IPmsProductStatusService;
import com.microservices.pms.utils.PmsConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author otto
 */
@Component
public class PmsJob {
    private static final Logger log = LoggerFactory.getLogger(PmsJob.class);

    @Autowired
    private PmsProductPlanMapper pmsProductPlanMapper;
    @Autowired
    private IPmsProductStatusService pmsProductStatusService;

    /**
     * 每日凌晨检查产品计划：
     * 1、未启动的产品计划是否有已达到开始时间的，若存在则设置为已过期
     * 2、进行中的产品计划是否有已过期，若存在则设置为已过期
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void checkProductPlanStatus() {
        Date nowZeroTime= DateUtils.getZeroDate(DateUtils.getNowDate());
        PmsProductPlan pmsProductPlanSearch=new PmsProductPlan();
        // 未启动状态id
        Long notStartedStatusId=pmsProductStatusService.selectPmsProductStatusByNameAndType(PmsConstants.PRODUCT_PLAN_NOT_STARTED_STATUS_NAME,PmsConstants.PRODUCT_STATUS_OR_PRIORITY_PLAN_TYPE);
        // 进行中状态id
        Long inProgressStatusId=pmsProductStatusService.selectPmsProductStatusByNameAndType(PmsConstants.PRODUCT_PLAN_IN_PROGRESS_STATUS_NAME,PmsConstants.PRODUCT_STATUS_OR_PRIORITY_PLAN_TYPE);
        // 已过期状态id
        Long expiredStatusId=pmsProductStatusService.selectPmsProductStatusByNameAndType(PmsConstants.PRODUCT_PLAN_EXPIRED_STATUS_NAME,PmsConstants.PRODUCT_STATUS_OR_PRIORITY_PLAN_TYPE);
        // 获取未启动计划列表，更新状态
        pmsProductPlanSearch.setStatusId(notStartedStatusId);
        List<PmsProductPlan> notStartedPmsProductPlanList=pmsProductPlanMapper.selectPmsProductPlanList(pmsProductPlanSearch);
        notStartedPmsProductPlanList.forEach(pmsProductPlan -> {
                    Date startZeroTime=DateUtils.getZeroDate(pmsProductPlan.getStartTime());
                    if(!nowZeroTime.before(startZeroTime)){
                        pmsProductPlan.setStatusId(inProgressStatusId);
                        pmsProductPlanMapper.updatePmsProductPlan(pmsProductPlan);
                        log.info("产品计划【{}】状态发生变更：由[未启动]变为[进行中]", pmsProductPlan.getPlanTitle());
                    }
                });
        // 获取进行中计划列表，更新状态
        pmsProductPlanSearch.setStatusId(inProgressStatusId);
        List<PmsProductPlan> inProgressPmsProductPlanList=pmsProductPlanMapper.selectPmsProductPlanList(pmsProductPlanSearch);
        inProgressPmsProductPlanList.forEach(pmsProductPlan -> {
            Date endZeroTime=DateUtils.getZeroDate(pmsProductPlan.getEndTime());
            if(nowZeroTime.after(endZeroTime)){
                pmsProductPlan.setStatusId(expiredStatusId);
                pmsProductPlanMapper.updatePmsProductPlan(pmsProductPlan);
                log.info("产品计划【{}】状态发生变更：由[进行中]变为[已过期]", pmsProductPlan.getPlanTitle());
            }
        });
    }
}
