package com.ozs.web.controller.tool;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ozs.common.core.domain.entity.SysDictData;
import com.ozs.common.enums.PmProjectStatus;
import com.ozs.common.enums.WarnStatus;
import com.ozs.common.utils.DateUtils;
import com.ozs.pm.doman.PmDemand;
import com.ozs.pm.service.IPmDemandService;
import com.ozs.system.service.ISysDictTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目管理任务调度
 */
@Component
@Slf4j
public class PmTaskServer {
    @Autowired
    private IPmDemandService pmDemandService;
    @Autowired
    private ISysDictTypeService iSysDictTypeService;

    /**
     * 更新预警状态
     */
    public void updateDemandWarnStatus(){
        log.info("******** 项目更新预警状态任务  开始");
        LambdaQueryWrapper<PmDemand> lw = new LambdaQueryWrapper<>();
        lw.ne(PmDemand::getProjectStatus, PmProjectStatus.COMPLETION_CONSTRUCTION.getCode());
        List<PmDemand> list = this.pmDemandService.list(lw);
        if(ObjectUtils.isEmpty(list)){
            return;
        }
        Date now = DateUtils.parseDate(DateUtils.getDate());
        List<PmDemand> updateList = new ArrayList<>();
        List<SysDictData> data = iSysDictTypeService.selectDictDataByType("alert_time_setting");
        int setting1 = Integer.parseInt(data.stream().filter(item -> item.getDictLabel().equals("计划提报时间")).collect(Collectors.toList()).get(0).getDictValue());
        log.info("******** setting1 == " + setting1);
        int setting2 = Integer.parseInt(data.stream().filter(item -> item.getDictLabel().equals("计划完成时间")).collect(Collectors.toList()).get(0).getDictValue());
        log.info("******** setting2 == " + setting2);
        int setting3 = Integer.parseInt(data.stream().filter(item -> item.getDictLabel().equals("计划交付(实施)时间")).collect(Collectors.toList()).get(0).getDictValue());
        log.info("******** setting3 == " + setting3);
        list.forEach(item-> {
            //需求提报时间：根据数据阈值中设置的提报时间进行判断，进行预警
            int diff1 = DateUtils.differentDaysByMillisecond(item.getPlanDemandSubTime(), now);
            if (ObjectUtils.isEmpty(item.getRealDemandCommitTime()) && diff1 >= setting1) {
                log.info("******** 需求提报预警: " + item.getProjectName());
                PmDemand pmDemand = new PmDemand();
                pmDemand.setDemandId(item.getDemandId());
                pmDemand.setWarnStatus(WarnStatus.DEMAND_COMMT_WARNING.getCode());
                updateList.add(pmDemand);
            } else {
                //采购完成时间：根据数据阈值中设置的采购完成时间进行判断，进行预警
                int diff2 = DateUtils.differentDaysByMillisecond(item.getPlanPurchaseFinishTime(), now);
                if ( ObjectUtils.isEmpty(item.getRealDemandCommitTime()) && diff2 >= setting2) {
                    log.info("******** 采购完成预警: " + item.getProjectName());
                    PmDemand pmDemand = new PmDemand();
                    pmDemand.setDemandId(item.getDemandId());
                    pmDemand.setWarnStatus(WarnStatus.PURCHASE_FINISH_WARNING.getCode());
                    updateList.add(pmDemand);
                } else {
                    //计划交付时间：根据数据阈值中设置的交付时间进行判断，进行预警
                    int diff3 = DateUtils.differentDaysByMillisecond(item.getPlanDeliverTime(), now);
                    if (ObjectUtils.isEmpty(item.getRealDemandCommitTime()) && diff3 >= setting3) {
                        log.info("******** 计划交付预警: " + item.getProjectName());
                        PmDemand pmDemand = new PmDemand();
                        pmDemand.setDemandId(item.getDemandId());
                        pmDemand.setWarnStatus(WarnStatus.PLAN_DELIVER_WARNING.getCode());
                        updateList.add(pmDemand);
                    }
                }
            }
        });

        if(!ObjectUtils.isEmpty(updateList)){
            this.pmDemandService.updateBatchById(updateList);
        }
        log.info("******** 项目更新预警状态任务  结束");
    }

}
