package org.jeecg.modules.stm.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.stm.my.entity.MyTask;
import org.jeecg.modules.stm.my.service.IMyTaskService;
import org.jeecg.modules.stm.plan.main.entity.StPlanMan;
import org.jeecg.modules.stm.plan.main.service.IStPlanManService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


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

@Slf4j
@Component
public class StPlanManJob implements Job {
    @Autowired
    private IStPlanManService stPlanManService;
    @Autowired
    private IMyTaskService myTaskService;
    @Autowired
    private ISysUserService sysUserService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        String sysday= DateUtils.timestamptoStr(null);


        log.info(String.format(" 科技治理平台 风险计划任务 StPlanManJob !  时间:" + sysday));
        QueryWrapper<StPlanMan> stPlanManQueryWrapper = new QueryWrapper<>();
        stPlanManQueryWrapper.eq("plan_start_date",sysday);
        List<StPlanMan> list = stPlanManService.list(stPlanManQueryWrapper);
        for (int i=0;i<list.size();i++){

            StPlanMan stb=list.get(i);
            stb.getProjectName(); //项目名称


            SysUser sysUser = sysUserService.getUserByName(stb.getHeadCode());
            if(sysUser==null){
                sysUser = sysUserService.getUserByName(stb.getApproveUser());
            }
            if(sysUser==null){
                sysUser = sysUserService.getUserByName(stb.getCreatedUser());
            }

            MyTask myTask = new MyTask();
            myTask.setUid(sysUser.getId());
            myTask.setTp(6);
            myTask.setTarget(stb.getProjectName());
            myTask.setTargetId("0");
            myTask.setStTime(stb.getPlanStartDate());
            myTask.setTaskName("待处理");
            myTask.setName(stb.getProjectName());
            myTask.setSta(0);
            myTask.setPriority("M");
            myTask.setDes("");
            myTask.setLinkAddr("/project/plan/StPlanManList");
            myTaskService.save(myTask);

        }

        String sysyear=sysday.substring(0,4);
        //Date planStartDate
        QueryWrapper<StPlanMan> stPlanManQueryWrapper2 = new QueryWrapper<>();
        stPlanManQueryWrapper2.eq("exe_rule",2);
        List<StPlanMan> list2 = stPlanManService.list(stPlanManQueryWrapper2);
        for(int j=0;j<list2.size();j++){
            StPlanMan stb=list2.get(j);
            Date planStartDate=stb.getPlanStartDate();
            if(planStartDate!=null){
             String rq=   DateUtils.formatDate(planStartDate,"yyyy-MM-dd");
             String rqary[]=rq.split("-");
             String year=rqary[0];
             String m=rqary[1];
             String d=rqary[2];
             int gz= stb.getExeRuleCycle().intValue();
             String ksrq=sysyear+"-"+m+"-"+d;
             boolean isok=false;
             //判断是 年 月
                if(gz==1||gz==2){
                    if(ksrq.equals(sysday)){
                        //开始 入库
                        isok=true;
                    }
                }else if(gz==3){
                    //季度
                    isok=isEqQ(m,d,sysday);
                }

                if(isok){

                    SysUser sysUser = sysUserService.getUserByName(stb.getHeadCode());
                    if(sysUser==null){
                        sysUser = sysUserService.getUserByName(stb.getApproveUser());
                    }
                    if(sysUser==null){
                        sysUser = sysUserService.getUserByName(stb.getCreatedUser());
                    }

                    MyTask myTask = new MyTask();
                    myTask.setUid(sysUser.getId());
                    myTask.setTp(6);
                    myTask.setTarget(stb.getProjectName());
                    myTask.setTargetId("0");
                    myTask.setStTime(stb.getPlanStartDate());
                    myTask.setTaskName("待处理");
                    myTask.setName(stb.getProjectName());
                    myTask.setSta(0);
                    myTask.setPriority("M");
                    myTask.setDes("");
                    myTask.setLinkAddr("/project/plan/StPlanManList");
                    myTaskService.save(myTask);

                }




               // stb.getProjectName(); //项目名称

            }

        }



    }

    //判断 是否执行
    public boolean isEqQ(String m,String d,String sysday){
        boolean isOk=false;
        String sysyear=sysday.substring(0,4);
        String pm=m;
        if(m.startsWith("0")){
            pm=m.substring(1);
        }
        //147 10  4月 30
        if(pm.equals("1")||pm.equals("4")||pm.equals("7")||pm.equals("10")){
            String runrq1=sysyear+"-01"+"-"+d;
            String runrq4=sysyear+"-04"+"-"+d;
            String runrq7=sysyear+"-07"+"-"+d;
            String runrq10=sysyear+"-10"+"-"+d;
            if(d.equals("31")){
                runrq4=sysyear+"-04"+"-"+"30";
            }
            if(runrq1.equals(sysday)||runrq4.equals(sysday)||runrq7.equals(sysday)||runrq10.equals(sysday)){
                isOk=true;
            }
        }
        //258 11  如果 29 30  11月 30天
        if(pm.equals("2")||pm.equals("5")||pm.equals("8")||pm.equals("11")){
            String runrq2=sysyear+"-02"+"-"+d;
            String runrq5=sysyear+"-05"+"-"+d;
            String runrq8=sysyear+"-08"+"-"+d;
            String runrq11=sysyear+"-11"+"-"+d;
            if(d.equals("31")){
                runrq2=sysyear+"-02"+"-"+"28";
                runrq11=sysyear+"-11"+"-"+"30";
            }
            if(d.equals("30")||d.equals("29")){
                runrq2=sysyear+"-02"+"-"+"28";

            }
            if(runrq2.equals(sysday)||runrq5.equals(sysday)||runrq8.equals(sysday)||runrq11.equals(sysday)){
                isOk=true;
            }
        }
        //369 12  6月30天
        if(pm.equals("3")||pm.equals("6")||pm.equals("9")||pm.equals("12")){
            String runrq3=sysyear+"-03"+"-"+d;
            String runrq6=sysyear+"-06"+"-"+d;
            String runrq9=sysyear+"-09"+"-"+d;
            String runrq12=sysyear+"-12"+"-"+d;
            if(d.equals("31")){
                runrq6=sysyear+"-06"+"-"+"30";
            }
            if(runrq3.equals(sysday)||runrq6.equals(sysday)||runrq9.equals(sysday)||runrq12.equals(sysday)){
                isOk=true;
            }
        }

     return  isOk;
    }

    }


