|  | @@ -1,18 +1,34 @@
 | 
	
		
			
				|  |  |  package com.xunmei.core.resumption.controller;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import cn.hutool.core.date.DateField;
 | 
	
		
			
				|  |  |  import cn.hutool.core.date.DateTime;
 | 
	
		
			
				|  |  | +import cn.hutool.core.date.DateUtil;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.constant.SecurityConstants;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.domain.R;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.utils.DateHelper;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.utils.KeyValue;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.utils.ResumptionType;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.utils.Ymd;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.web.domain.AjaxResult;
 | 
	
		
			
				|  |  | +import com.xunmei.core.resumption.domain.AppPlan;
 | 
	
		
			
				|  |  | +import com.xunmei.core.resumption.dto.doTaskDto;
 | 
	
		
			
				|  |  | +import com.xunmei.core.resumption.service.AppPlanService;
 | 
	
		
			
				|  |  | +import com.xunmei.core.resumption.service.ISysResumptionService;
 | 
	
		
			
				|  |  |  import com.xunmei.core.resumption.task.ResumptionTaskBusiness;
 | 
	
		
			
				|  |  | +import com.xunmei.system.api.RemoteOrgService;
 | 
	
		
			
				|  |  | +import com.xunmei.system.api.RemoteWorkTimeService;
 | 
	
		
			
				|  |  | +import com.xunmei.system.api.domain.SysOrg;
 | 
	
		
			
				|  |  | +import com.xunmei.system.api.domain.SysWorkTime;
 | 
	
		
			
				|  |  | +import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | -import org.springframework.web.bind.annotation.GetMapping;
 | 
	
		
			
				|  |  | -import org.springframework.web.bind.annotation.RequestMapping;
 | 
	
		
			
				|  |  | -import org.springframework.web.bind.annotation.RestController;
 | 
	
		
			
				|  |  | +import org.springframework.web.bind.annotation.*;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import java.util.ArrayList;
 | 
	
		
			
				|  |  |  import java.util.List;
 | 
	
		
			
				|  |  |  import java.util.TimeZone;
 | 
	
		
			
				|  |  | +import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * @author LuoJun
 | 
	
	
		
			
				|  | @@ -22,7 +38,14 @@ import java.util.TimeZone;
 | 
	
		
			
				|  |  |  public class TaskController {
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      ResumptionTaskBusiness taskBusiness;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private AppPlanService appPlanService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private RemoteWorkTimeService workTimeService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private RemoteOrgService orgService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private ISysResumptionService resumptionService;
 | 
	
		
			
				|  |  |      @GetMapping("/dayTask")
 | 
	
		
			
				|  |  |      public AjaxResult dayTask() {
 | 
	
		
			
				|  |  |  //        //执行周期为每周的履职计划Plan_cycle=0,6
 | 
	
	
		
			
				|  | @@ -42,7 +65,7 @@ public class TaskController {
 | 
	
		
			
				|  |  |          datetime.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  |  //        SCHEDULEDTASKSLOG.info("================开始执行每日每小时履职任务生成任务================");
 | 
	
		
			
				|  |  |  //        taskBusiness.syncHour(datetime,6);
 | 
	
		
			
				|  |  | -        taskBusiness.syncDay(datetime,2);
 | 
	
		
			
				|  |  | +        taskBusiness.syncDay(datetime, 2);
 | 
	
		
			
				|  |  |  //        SCHEDULEDTASKSLOG.info("================每日每小时履职任务生成任务执行结束================");
 | 
	
		
			
				|  |  |          return AjaxResult.success();
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -52,7 +75,7 @@ public class TaskController {
 | 
	
		
			
				|  |  |          DateTime datetime = new DateTime();
 | 
	
		
			
				|  |  |          datetime.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  |  //        SCHEDULEDTASKSLOG.info("================开始执行每周履职任务生成任务================");
 | 
	
		
			
				|  |  | -        taskBusiness.syncWeek(datetime,3);
 | 
	
		
			
				|  |  | +        taskBusiness.syncWeek(datetime, 3);
 | 
	
		
			
				|  |  |  //        SCHEDULEDTASKSLOG.info("================每周履职任务生成任务执行结束================");
 | 
	
		
			
				|  |  |          return AjaxResult.success();
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -62,30 +85,340 @@ public class TaskController {
 | 
	
		
			
				|  |  |          DateTime datetime = new DateTime();
 | 
	
		
			
				|  |  |          datetime.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  |          DateHelper dateHelper = new DateHelper(datetime);
 | 
	
		
			
				|  |  | -        int month =dateHelper.getMonth();
 | 
	
		
			
				|  |  | +        int month = dateHelper.getMonth();
 | 
	
		
			
				|  |  |          int day = dateHelper.getDay();
 | 
	
		
			
				|  |  |  //        SCHEDULEDTASKSLOG.info("================开始执行每月,每季,每半年,每年履职任务生成任务================");
 | 
	
		
			
				|  |  | -        if(month==1&&day==1){
 | 
	
		
			
				|  |  | +        if (month == 1 && day == 1) {
 | 
	
		
			
				|  |  |              //1.1日获取执行周期为每月,每季,每半年,每年的履职计划Plan_cycle=2,3,4,5
 | 
	
		
			
				|  |  | -            taskBusiness.syncMonth(datetime,4);
 | 
	
		
			
				|  |  | -            taskBusiness.syncQuarter(datetime,5);
 | 
	
		
			
				|  |  | -            taskBusiness.syncHalfYear(datetime,6);
 | 
	
		
			
				|  |  | -            taskBusiness.syncYear(datetime,7);
 | 
	
		
			
				|  |  | -        }else if((month==4&&day==1)||(month==10&&day==1)){
 | 
	
		
			
				|  |  | +            taskBusiness.syncMonth(datetime, 4);
 | 
	
		
			
				|  |  | +            taskBusiness.syncQuarter(datetime, 5);
 | 
	
		
			
				|  |  | +            taskBusiness.syncHalfYear(datetime, 6);
 | 
	
		
			
				|  |  | +            taskBusiness.syncYear(datetime, 7);
 | 
	
		
			
				|  |  | +        } else if ((month == 4 && day == 1) || (month == 10 && day == 1)) {
 | 
	
		
			
				|  |  |              //4.1||10.1日获取执行周期为每月,每季的履职计划Plan_cycle=2,3
 | 
	
		
			
				|  |  | -            taskBusiness.syncMonth(datetime,4);
 | 
	
		
			
				|  |  | -            taskBusiness.syncQuarter(datetime,5);
 | 
	
		
			
				|  |  | -        }else if(month==7&&day==1){
 | 
	
		
			
				|  |  | +            taskBusiness.syncMonth(datetime, 4);
 | 
	
		
			
				|  |  | +            taskBusiness.syncQuarter(datetime, 5);
 | 
	
		
			
				|  |  | +        } else if (month == 7 && day == 1) {
 | 
	
		
			
				|  |  |              //7.1日获取执行周期为每月,每季,每半年的履职计划Plan_cycle=2,3,4
 | 
	
		
			
				|  |  | -            taskBusiness.syncMonth(datetime,4);
 | 
	
		
			
				|  |  | -            taskBusiness.syncQuarter(datetime,5);
 | 
	
		
			
				|  |  | -            taskBusiness.syncHalfYear(datetime,6);
 | 
	
		
			
				|  |  | +            taskBusiness.syncMonth(datetime, 4);
 | 
	
		
			
				|  |  | +            taskBusiness.syncQuarter(datetime, 5);
 | 
	
		
			
				|  |  | +            taskBusiness.syncHalfYear(datetime, 6);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        }else {
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  |              //其他获取每月履职计划Plan_cycle=2
 | 
	
		
			
				|  |  | -            taskBusiness.syncMonth(datetime,4);
 | 
	
		
			
				|  |  | +            taskBusiness.syncMonth(datetime, 4);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  //        SCHEDULEDTASKSLOG.info("================每月,每季,每半年,每年履职任务生成任务执行结束================");
 | 
	
		
			
				|  |  |          return AjaxResult.success();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @PostMapping("/createResumption")
 | 
	
		
			
				|  |  | +    public void createResumption(@RequestBody doTaskDto request) {
 | 
	
		
			
				|  |  | +        String time = request.getTime();
 | 
	
		
			
				|  |  | +        DateTime datetime = new DateTime(DateUtil.parse(time, "yyyy-MM-dd"));
 | 
	
		
			
				|  |  | +        datetime.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        //当有周期存在时保持原有接口不变(doTask),否则按照新的接口创建
 | 
	
		
			
				|  |  | +        Integer plancycle = request.getPlancycle();
 | 
	
		
			
				|  |  | +        if (plancycle != null) {
 | 
	
		
			
				|  |  | +//            if (plancycle == 6) {
 | 
	
		
			
				|  |  | +//                taskBusiness.syncHour(datetime, plancycle);
 | 
	
		
			
				|  |  | +//            }
 | 
	
		
			
				|  |  | +            if (plancycle == 2) {
 | 
	
		
			
				|  |  | +                taskBusiness.syncDay(datetime, plancycle);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (plancycle == 3) {
 | 
	
		
			
				|  |  | +                taskBusiness.syncWeek(datetime, plancycle);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (plancycle == 4) {
 | 
	
		
			
				|  |  | +                taskBusiness.syncMonth(datetime, plancycle);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (plancycle == 5) {
 | 
	
		
			
				|  |  | +                taskBusiness.syncQuarter(datetime, plancycle);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (plancycle == 6) {
 | 
	
		
			
				|  |  | +                taskBusiness.syncHalfYear(datetime, plancycle);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (plancycle == 7) {
 | 
	
		
			
				|  |  | +                taskBusiness.syncYear(datetime, plancycle);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            //按照新的接口创建
 | 
	
		
			
				|  |  | +            List<Long> orgIds = request.getOrgIds();
 | 
	
		
			
				|  |  | +            List<Long> planIds = request.getPlanIds();
 | 
	
		
			
				|  |  | +            this.createNewResumptionByOrgAndPlan(orgIds, planIds, datetime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void createNewResumptionByOrgAndPlan(List<Long> orgIds, List<Long> planIds, DateTime datetime) {
 | 
	
		
			
				|  |  | +        //更加计划id获取启用状态的计划
 | 
	
		
			
				|  |  | +        List<AppPlan> appPlans = getAppPlanByPlanIds(planIds);
 | 
	
		
			
				|  |  | +        //获取有作息的机构id
 | 
	
		
			
				|  |  | +        List<Long> workOrgIds = workTimeService.findOrgIdsByYmd(datetime.toString().substring(0, 10), SecurityConstants.INNER).getData();
 | 
	
		
			
				|  |  | +        for (AppPlan plan : appPlans) {
 | 
	
		
			
				|  |  | +            //获取每个计划要生成任务的机构
 | 
	
		
			
				|  |  | +            List<Long> orgIdsByPlanListNew = getOrgIdsByPlanIds(plan, orgIds, workOrgIds);
 | 
	
		
			
				|  |  | +            //获取履职角色
 | 
	
		
			
				|  |  | +            List<Long> roleIds = appPlanService.findRoleByPlan(plan.getId());
 | 
	
		
			
				|  |  | +            //开始生成任务
 | 
	
		
			
				|  |  | +            saveResumptionByOrgAndPlan(orgIdsByPlanListNew, plan, roleIds, datetime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    private void saveResumptionByOrgAndPlan(List<Long> orgIdsByPlanListNew, AppPlan plan, List<Long> roleIds, DateTime datetime) {
 | 
	
		
			
				|  |  | +        if (plan.getPlanCycle() == 2) {
 | 
	
		
			
				|  |  | +            //按天生成任务
 | 
	
		
			
				|  |  | +            saveResumptionByDay(orgIdsByPlanListNew, plan, roleIds, datetime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (plan.getPlanCycle() == 3) {
 | 
	
		
			
				|  |  | +            //按周生成任务
 | 
	
		
			
				|  |  | +            saveResumptionByWeek(orgIdsByPlanListNew, plan, roleIds, datetime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (plan.getPlanCycle() == 4) {
 | 
	
		
			
				|  |  | +            //按月生成任务
 | 
	
		
			
				|  |  | +            saveResumptionByMonth(orgIdsByPlanListNew, plan, roleIds, datetime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (plan.getPlanCycle() == 5) {
 | 
	
		
			
				|  |  | +            //按季度生成任务
 | 
	
		
			
				|  |  | +            saveResumptionByQuarter(orgIdsByPlanListNew, plan, roleIds, datetime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (plan.getPlanCycle() == 6) {
 | 
	
		
			
				|  |  | +            //按半年生成任务
 | 
	
		
			
				|  |  | +            saveResumptionByHalfYear(orgIdsByPlanListNew, plan, roleIds, datetime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (plan.getPlanCycle() == 7) {
 | 
	
		
			
				|  |  | +            //按年生成任务
 | 
	
		
			
				|  |  | +            saveResumptionByYear(orgIdsByPlanListNew, plan, roleIds, datetime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (plan.getPlanCycle() == 1) {
 | 
	
		
			
				|  |  | +            //按小时生成任务
 | 
	
		
			
				|  |  | +            saveResumptionByHour(orgIdsByPlanListNew, plan, roleIds, datetime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    private void saveResumptionByHour(List<Long> orgIdsByPlanListNew, AppPlan appPlan, List<Long> roleIds, DateTime dateTime) {
 | 
	
		
			
				|  |  | +        for (final Long orgId : orgIdsByPlanListNew) {
 | 
	
		
			
				|  |  | +            Ymd y = ResumptionType.DAY.toYmd(dateTime);
 | 
	
		
			
				|  |  | +            y.setOrgId(orgId);
 | 
	
		
			
				|  |  | +            y.setEnable(true);
 | 
	
		
			
				|  |  | +            final SysWorkTime workTime = this.workTimeService.findWorkTimeByYmd(y).getData();
 | 
	
		
			
				|  |  | +            if (workTime != null) {
 | 
	
		
			
				|  |  | +                List<String[]> workTimes = new ArrayList<>();
 | 
	
		
			
				|  |  | +                if (appPlan.getNotResumptionOnRest() != null && appPlan.getNotResumptionOnRest() == 1 &&
 | 
	
		
			
				|  |  | +                        StringUtils.isNotEmpty(workTime.getNoonbreakStart()) && StringUtils.isNotEmpty(workTime.getNoonbreakEnd())) {
 | 
	
		
			
				|  |  | +                    workTimes.add(new String[]{workTime.getOpenTime(), workTime.getNoonbreakStart()});
 | 
	
		
			
				|  |  | +                    workTimes.add(new String[]{workTime.getNoonbreakEnd(), workTime.getCloseTime()});
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    workTimes.add(new String[]{workTime.getOpenTime(), workTime.getCloseTime()});
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                for (int ti = 0; ti < workTimes.size(); ti++) {
 | 
	
		
			
				|  |  | +                    String[] time = workTimes.get(ti);
 | 
	
		
			
				|  |  | +                    String openStr = time[0];
 | 
	
		
			
				|  |  | +                    String closeStr = time[1];
 | 
	
		
			
				|  |  | +                    String[] openArr = openStr.split(":");
 | 
	
		
			
				|  |  | +                    String[] closeArr = closeStr.split(":");
 | 
	
		
			
				|  |  | +                        /*如果营业前是00:00-09:00,营业后是17:00-24:00,
 | 
	
		
			
				|  |  | +                        当前每小时履职任务是10:00-16:00,少了09:00-10:00和16:00-17:00的任务,
 | 
	
		
			
				|  |  | +                        建议这2个时间段也生成履职任务*/
 | 
	
		
			
				|  |  | +                    int openHour = 0;
 | 
	
		
			
				|  |  | +                    if (ti == 0) {
 | 
	
		
			
				|  |  | +                        openHour = Integer.parseInt(openArr[0]) - 1;
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        openHour = Integer.parseInt(openArr[0]);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    int openMinute = Integer.parseInt(openArr[1]);
 | 
	
		
			
				|  |  | +                    int closeHour = Integer.parseInt(closeArr[0]);
 | 
	
		
			
				|  |  | +                    int closeMinute = Integer.parseInt(closeArr[1]);
 | 
	
		
			
				|  |  | +                    if (openMinute < closeMinute) {
 | 
	
		
			
				|  |  | +                        closeHour += 1;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    int len = closeHour - openHour;
 | 
	
		
			
				|  |  | +                    if (len > 1) {
 | 
	
		
			
				|  |  | +                        int i = ti == 0 ? 1 : 0;
 | 
	
		
			
				|  |  | +                        for (; i < len; i++) {//为每个作息点生成履职任务
 | 
	
		
			
				|  |  | +                            dateTime.setField(DateField.HOUR_OF_DAY, openHour + i);
 | 
	
		
			
				|  |  | +                            dateTime.setField(DateField.MINUTE, openMinute);
 | 
	
		
			
				|  |  | +                            //每个作息点的开始时间和结束时间,不能直接操作dateTime,后面的操作会覆盖前面的操作
 | 
	
		
			
				|  |  | +                            DateTime time1 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +                            time1.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +                            DateTime time2 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +                            time2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +                            DateTime planstartTime = time1.setField(DateField.HOUR_OF_DAY, openHour + i).setField(DateField.MINUTE, openMinute).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +                            DateTime planendTime = time2.setField(DateField.HOUR_OF_DAY, openHour + i + 1).setField(DateField.MINUTE, openMinute).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +                            resumptionService.newbuild(orgId, ResumptionType.HOUR, dateTime, true, appPlan, null, null, roleIds, planstartTime, planendTime);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +//                RESUMPTIONLOG.error("{} 没有作息", ResumptionType.DAY.toYmd(dateTime));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    private void saveResumptionByYear(List<Long> orgIdsByPlanListNew, AppPlan appPlan, List<Long> roleIds, DateTime dateTime) {
 | 
	
		
			
				|  |  | +        DateTime time1 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +        time1.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        DateTime time2 = new DateTime(DateHelper.getLastDayOfYear(DateUtil.year(dateTime)));
 | 
	
		
			
				|  |  | +        time2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        DateTime planstartTime = time1.setField(DateField.HOUR_OF_DAY, 0).setField(DateField.MINUTE, 0).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +        DateTime planendTime = DateUtil.endOfDay(time2).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +        for (Long orgId : orgIdsByPlanListNew) {
 | 
	
		
			
				|  |  | +            this.resumptionService.newbuild(orgId, ResumptionType.YEAR, dateTime, true, appPlan, null, null, roleIds, planstartTime, planendTime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    private void saveResumptionByHalfYear(List<Long> orgIdsByPlanListNew, AppPlan appPlan, List<Long> roleIds, DateTime dateTime) {
 | 
	
		
			
				|  |  | +        DateHelper dateHelper = new DateHelper(dateTime);
 | 
	
		
			
				|  |  | +        KeyValue<Integer, Integer> yearHalfYear = new KeyValue<>(DateUtil.year(dateTime), dateHelper.getHalfyear());
 | 
	
		
			
				|  |  | +        DateTime time1 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +        time1.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        DateTime time2 = new DateTime(DateHelper.getLastDayOfHalfyear(yearHalfYear.getKey(), yearHalfYear.getValue()));
 | 
	
		
			
				|  |  | +        time2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        DateTime planstartTime = time1.setField(DateField.HOUR_OF_DAY, 0).setField(DateField.MINUTE, 0).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +        DateTime planendTime = DateUtil.endOfDay(time2).setField(DateField.MILLISECOND, 0);//.offset(DateField.MILLISECOND, -999);
 | 
	
		
			
				|  |  | +        for (Long orgId : orgIdsByPlanListNew) {
 | 
	
		
			
				|  |  | +            this.resumptionService.newbuild(orgId, ResumptionType.HALFYEAR, dateTime, true, appPlan, null, null, roleIds, planstartTime, planendTime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    private void saveResumptionByQuarter(List<Long> orgIdsByPlanListNew, AppPlan appPlan, List<Long> roleIds, DateTime dateTime) {
 | 
	
		
			
				|  |  | +        KeyValue<Integer, Integer> yearQuarter = new KeyValue<>(DateUtil.year(dateTime), DateUtil.quarter(dateTime));
 | 
	
		
			
				|  |  | +        DateTime time1 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +        time1.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        DateTime time2 = new DateTime(DateHelper.getLastDayOfQuarter(yearQuarter.getKey(), yearQuarter.getValue()));
 | 
	
		
			
				|  |  | +        time2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        DateTime planstartTime = time1.setField(DateField.HOUR_OF_DAY, 0).setField(DateField.MINUTE, 0).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +        DateTime planendTime = DateUtil.endOfDay(time2).setField(DateField.MILLISECOND, 0);//.offset(DateField.MILLISECOND, -999);
 | 
	
		
			
				|  |  | +        for (Long orgId : orgIdsByPlanListNew) {
 | 
	
		
			
				|  |  | +            this.resumptionService.newbuild(orgId, ResumptionType.QUARTER, dateTime, true, appPlan, null, null, roleIds, planstartTime, planendTime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    private void saveResumptionByMonth(List<Long> orgIdsByPlanListNew, AppPlan appPlan, List<Long> roleIds, DateTime dateTime) {
 | 
	
		
			
				|  |  | +        KeyValue<Integer, Integer> yearMonth = new KeyValue<>(DateUtil.year(dateTime), DateUtil.month(dateTime) + 1);
 | 
	
		
			
				|  |  | +        DateTime time1 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +        time1.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        DateTime time2 = new DateTime(DateHelper.getLastDayOfMonth(yearMonth.getKey(), yearMonth.getValue()));
 | 
	
		
			
				|  |  | +        time2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        DateTime planstartTime = time1.setField(DateField.HOUR_OF_DAY, 0).setField(DateField.MINUTE, 0).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +        DateTime planendTime = DateUtil.endOfDay(time2).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +        for (Long orgId : orgIdsByPlanListNew) {
 | 
	
		
			
				|  |  | +            this.resumptionService.newbuild(orgId, ResumptionType.MONTH, dateTime, true, appPlan, null, null, roleIds, planstartTime, planendTime);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    private void saveResumptionByWeek(List<Long> orgIdsByPlanListNew, AppPlan appPlan, List<Long> roleIds, DateTime dateTime) {
 | 
	
		
			
				|  |  | +        KeyValue<Integer, Integer> yearWeek = new KeyValue<>(DateUtil.year(dateTime), DateUtil.weekOfYear(dateTime));
 | 
	
		
			
				|  |  | +        DateTime time1 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +        time1.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        DateTime time2 = new DateTime(DateHelper.getLastDayOfWeek(yearWeek.getKey(), yearWeek.getValue()));
 | 
	
		
			
				|  |  | +        if (time2.isBefore(time1)) {//说明跨年了
 | 
	
		
			
				|  |  | +            time2 = new DateTime(DateHelper.getLastDayOfWeek(yearWeek.getKey() + 1, yearWeek.getValue()));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        time2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +        DateTime planstartTime = time1.setField(DateField.HOUR_OF_DAY, 0).setField(DateField.MINUTE, 0).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +        ;
 | 
	
		
			
				|  |  | +        DateTime planendTime = DateUtil.endOfDay(time2).setField(DateField.MILLISECOND, 0);//.offset(DateField.MILLISECOND, -999);
 | 
	
		
			
				|  |  | +        for (Long orgId : orgIdsByPlanListNew) {
 | 
	
		
			
				|  |  | +            this.resumptionService.newbuild(orgId, ResumptionType.WEEK, dateTime, true, appPlan, null, null, roleIds, planstartTime, planendTime);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    private void saveResumptionByDay(List<Long> orgIdsByPlanListNew, AppPlan appPlan, List<Long> roleIds, DateTime dateTime) {
 | 
	
		
			
				|  |  | +        for (Long orgId : orgIdsByPlanListNew) {
 | 
	
		
			
				|  |  | +            Ymd y = ResumptionType.DAY.toYmd(dateTime);
 | 
	
		
			
				|  |  | +            y.setOrgId(orgId);
 | 
	
		
			
				|  |  | +            y.setEnable(true);
 | 
	
		
			
				|  |  | +            final SysWorkTime workTime = this.workTimeService.findWorkTimeByYmd(y).getData();
 | 
	
		
			
				|  |  | +            DateTime planstartTime = null;
 | 
	
		
			
				|  |  | +            DateTime planendTime = null;
 | 
	
		
			
				|  |  | +            if (workTime != null) {
 | 
	
		
			
				|  |  | +                String worktime = workTime.getWorkTime();//上班时间
 | 
	
		
			
				|  |  | +                String workofftime = workTime.getWorkOffTime();//下班时间
 | 
	
		
			
				|  |  | +                String opentime = workTime.getOpenTime();//营业时间
 | 
	
		
			
				|  |  | +                String closetime = workTime.getCloseTime();//营业终了
 | 
	
		
			
				|  |  | +                if (appPlan.getPlanExec() == 1) {//全天 上班时间-下班时间 00:00:00-23:59:59
 | 
	
		
			
				|  |  | +                    DateTime time1 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +                    time1.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +                    DateTime time2 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +                    time2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +                    planstartTime = time1.setField(DateField.HOUR_OF_DAY, 0).setField(DateField.MINUTE, 0).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +                    planendTime = DateUtil.endOfDay(time2).setField(DateField.MILLISECOND, 0);//.offset(DateField.MILLISECOND, -999);
 | 
	
		
			
				|  |  | +                    //planstartTime = time1.setField(DateField.HOUR_OF_DAY,Integer.parseInt(worktime.split(":")[0])).setField(DateField.MINUTE,Integer.parseInt(worktime.split(":")[1])).setField(DateField.SECOND,0);
 | 
	
		
			
				|  |  | +                    //planendTime = time2.setField(DateField.HOUR_OF_DAY,Integer.parseInt(workofftime.split(":")[0])).setField(DateField.MINUTE,Integer.parseInt(workofftime.split(":")[1])).setField(DateField.SECOND,0);
 | 
	
		
			
				|  |  | +                } else if (appPlan.getPlanExec() == 2) {//营业前 上班时间-营业时间
 | 
	
		
			
				|  |  | +                    DateTime time1 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +                    time1.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +                    DateTime time2 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +                    time2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +                    planstartTime = time1.setField(DateField.HOUR_OF_DAY, 0).setField(DateField.MINUTE, 0).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +                    planendTime = time2.setField(DateField.HOUR_OF_DAY, Integer.parseInt(opentime.split(":")[0])).setField(DateField.MINUTE, Integer.parseInt(opentime.split(":")[1])).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +                } else if (appPlan.getPlanExec() == 3) {//营业期间 营业时间-营业终了
 | 
	
		
			
				|  |  | +                    DateTime time1 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +                    time1.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +                    DateTime time2 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +                    time2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +                    planstartTime = time1.setField(DateField.HOUR_OF_DAY, Integer.parseInt(opentime.split(":")[0])).setField(DateField.MINUTE, Integer.parseInt(opentime.split(":")[1])).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +                    planendTime = time2.setField(DateField.HOUR_OF_DAY, Integer.parseInt(closetime.split(":")[0])).setField(DateField.MINUTE, Integer.parseInt(closetime.split(":")[1])).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +                } else if (appPlan.getPlanExec() == 4) {//营业终 营业终了-23.59.59
 | 
	
		
			
				|  |  | +                    DateTime time1 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +                    time1.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +                    DateTime time2 = new DateTime(dateTime);
 | 
	
		
			
				|  |  | +                    time2.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 | 
	
		
			
				|  |  | +                    planstartTime = time1.setField(DateField.HOUR_OF_DAY, Integer.parseInt(closetime.split(":")[0])).setField(DateField.MINUTE, Integer.parseInt(closetime.split(":")[1])).setField(DateField.SECOND, 0).setField(DateField.MILLISECOND, 0);
 | 
	
		
			
				|  |  | +                    planendTime = DateUtil.endOfDay(time2).setField(DateField.MILLISECOND, 0);//.offset(DateField.MILLISECOND, -999);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                resumptionService.newbuild(orgId, ResumptionType.DAY, dateTime, true, appPlan, null, null, roleIds, planstartTime, planendTime);
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +//                RESUMPTIONLOG.error("{} 没有作息", ResumptionType.DAY.toYmd(dateTime));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    private List<Long> getPlanOrgIds(AppPlan appPlan) {
 | 
	
		
			
				|  |  | +        List<Long> orgIds = null;
 | 
	
		
			
				|  |  | +        if (appPlan.getExecType()!=null&&appPlan.getExecType() == 0) {//0机构类型需要安类型去机构里查询所以的机构
 | 
	
		
			
				|  |  | +            orgIds = orgService.findListByOrgType(appPlan.getExecOrgType(),SecurityConstants.INNER).getData();
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            List<SysOrg> sysOrgs = orgService.selectByOrgType(appPlan.getExecOrgType(), SecurityConstants.INNER);
 | 
	
		
			
				|  |  | +            orgIds = sysOrgs.stream().map(SysOrg::getId).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +//            orgIds = appPlanService.findExecOrgByPlan(appPlan.getId());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return orgIds;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private List<Long> getOrgIdsByPlanIds(AppPlan plan, List<Long> orgIds, List<Long> workOrgIds) {
 | 
	
		
			
				|  |  | +        //获取计划关联的机构
 | 
	
		
			
				|  |  | +        List<Long> orgIdsByPlans = getPlanOrgIds(plan);
 | 
	
		
			
				|  |  | +        if (orgIdsByPlans.size() == 0) {
 | 
	
		
			
				|  |  | +            throw new RuntimeException("计划未关联机构");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //去掉传过来的机构id中不在计划关联机构中的数据,,得到要生成任务的机构id
 | 
	
		
			
				|  |  | +        List<Long> orgIdsByPlanListNew = new ArrayList<>();
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isNotEmpty(orgIds)) {
 | 
	
		
			
				|  |  | +            for (Long orgId : orgIds) {
 | 
	
		
			
				|  |  | +                if (orgIdsByPlans.contains(orgId)) {
 | 
	
		
			
				|  |  | +                    orgIdsByPlanListNew.add(orgId);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            orgIdsByPlanListNew = orgIdsByPlans;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (plan.getPlanCycle() == 0 || plan.getPlanCycle() == 6) {
 | 
	
		
			
				|  |  | +            //如果是日计划或者小时计划,只生成有作息的机构的任务
 | 
	
		
			
				|  |  | +            orgIdsByPlanListNew.retainAll(workOrgIds);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return orgIdsByPlanListNew;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private List<AppPlan> getAppPlanByPlanIds(List<Long> planIds) {
 | 
	
		
			
				|  |  | +        List<AppPlan> appPlans = new ArrayList<>();
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isNotEmpty(planIds)) {
 | 
	
		
			
				|  |  | +            //获取计划
 | 
	
		
			
				|  |  | +            appPlans = appPlanService.list((new QueryWrapper<AppPlan>()).lambda().in(AppPlan::getId, planIds).eq(AppPlan::getPlanStatus, "1"));
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            appPlans = appPlanService.list((new QueryWrapper<AppPlan>()).lambda().eq(AppPlan::getPlanStatus, "1"));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (appPlans.size() == 0) {
 | 
	
		
			
				|  |  | +            throw new RuntimeException("没有可用的计划");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return appPlans;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  }
 |