|  | @@ -1,6 +1,7 @@
 | 
	
		
			
				|  |  |  package com.xunmei.core.resumption.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import cn.hutool.core.collection.CollectionUtil;
 | 
	
		
			
				|  |  | +import cn.hutool.core.date.DateField;
 | 
	
		
			
				|  |  |  import cn.hutool.core.date.DateUtil;
 | 
	
		
			
				|  |  |  import cn.hutool.core.util.ObjectUtil;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
	
	
		
			
				|  | @@ -12,6 +13,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.constant.ErrorMsgConstants;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.constant.SecurityConstants;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.domain.DateRange;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.domain.R;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.enums.OrgTypeEnum;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.enums.PlanStatus;
 | 
	
	
		
			
				|  | @@ -181,6 +183,11 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |          AppPlan oldPlan = baseMapper.selectById(dto.getId());
 | 
	
		
			
				|  |  |          Long parentId = IDHelper.id();
 | 
	
		
			
				|  |  |          List<Object> hsPlans = new ArrayList<>();
 | 
	
		
			
				|  |  | +        Date now = new Date();
 | 
	
		
			
				|  |  | +        Date effectiveTime = new Date();
 | 
	
		
			
				|  |  | +        if (!immediateEffect) {
 | 
	
		
			
				|  |  | +            effectiveTime = DateUtil.offset(DateUtil.beginOfDay(DateUtils.getStartAndEnd(now, oldPlan.getPlanCycle()).getEndTime()), DateField.DAY_OF_YEAR, 1);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          //如果计划所属机构类型为省联社,生成下发子计划
 | 
	
		
			
				|  |  |          if (ObjectUtil.equal(oldPlan.getPlanOfOrgType(), 1L)) {
 | 
	
		
			
				|  |  |              for (DistributeStatusDto d :
 | 
	
	
		
			
				|  | @@ -192,7 +199,16 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |                  //如果子计划已存在
 | 
	
		
			
				|  |  |                  if (tempplan != null) {
 | 
	
		
			
				|  |  |                      //从停用状态下发,才会有已存在的子计划;
 | 
	
		
			
				|  |  | -                    tempplan.setPlanStatus(d.getStatus() > 0 ? 1 : 2);
 | 
	
		
			
				|  |  | +                    if (d.getStatus() > 0) {
 | 
	
		
			
				|  |  | +                        tempplan.setPlanStatus(1);
 | 
	
		
			
				|  |  | +                        tempplan.setPublishTime(now);
 | 
	
		
			
				|  |  | +                        tempplan.setEffectiveTime(effectiveTime);
 | 
	
		
			
				|  |  | +                    } else {
 | 
	
		
			
				|  |  | +                        tempplan.setPlanStatus(2);
 | 
	
		
			
				|  |  | +                        tempplan.setPublishTime(null);
 | 
	
		
			
				|  |  | +                        tempplan.setEffectiveTime(null);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |                      tempplan.setDistributePlanStatus(d.getStatus().toString());
 | 
	
		
			
				|  |  |                      tempplan.setDistributeStatus("1");
 | 
	
		
			
				|  |  |                      if (!immediateEffect) {
 | 
	
	
		
			
				|  | @@ -223,6 +239,9 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |                      plan.setPlanOfOrgType(Long.valueOf(orgService.selectSysOrgById(d.getOrgId(), SecurityConstants.INNER).getType()));
 | 
	
		
			
				|  |  |                      plan.setPlanStatus(d.getStatus());
 | 
	
		
			
				|  |  |                      plan.setDistributePlanStatus(d.getStatus().toString());
 | 
	
		
			
				|  |  | +                    if (ObjectUtil.equal(d.getStatus(), 1)) {
 | 
	
		
			
				|  |  | +                        plan.setPublishTime(now);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                      plan.setCreateTime(new Date());
 | 
	
		
			
				|  |  |                      plan.setId(null);
 | 
	
		
			
				|  |  |  //            plan.setPlanName(plan.getPlanName()+"下发"+orgService.selectOrgById(d.getOrgId(),SecurityConstants.INNER).getName());
 | 
	
	
		
			
				|  | @@ -263,6 +282,8 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |          plan.setDistribute("1");
 | 
	
		
			
				|  |  |          //设置计划状态为使用中
 | 
	
		
			
				|  |  |          plan.setPlanStatus(1);
 | 
	
		
			
				|  |  | +        plan.setPublishTime(now);
 | 
	
		
			
				|  |  | +        plan.setEffectiveTime(effectiveTime);
 | 
	
		
			
				|  |  |  //        plan.setBuildTaskNow(null);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          baseMapper.updateById(plan);
 | 
	
	
		
			
				|  | @@ -287,6 +308,13 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |          Integer planStatus = plan.getPlanStatus();
 | 
	
		
			
				|  |  |          //设置计划状态
 | 
	
		
			
				|  |  |          plan.setPlanStatus(1);
 | 
	
		
			
				|  |  | +        Date now = new Date();
 | 
	
		
			
				|  |  | +        Date effectiveTime = new Date();
 | 
	
		
			
				|  |  | +        if (!immediateEffect) {
 | 
	
		
			
				|  |  | +            effectiveTime = DateUtil.offset(DateUtil.beginOfDay(DateUtils.getStartAndEnd(now, plan.getPlanCycle()).getEndTime()), DateField.DAY_OF_YEAR, 1);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        plan.setPublishTime(now);
 | 
	
		
			
				|  |  | +        plan.setEffectiveTime(effectiveTime);
 | 
	
		
			
				|  |  |          //如果是行社撤回需要修改下发状态
 | 
	
		
			
				|  |  |          //行社下发
 | 
	
		
			
				|  |  |          if (plan.getPlanOfOrgType() == 3 && "0".equals(plan.getDistribute())) {
 | 
	
	
		
			
				|  | @@ -389,6 +417,10 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |  //        deleteTaskByPlanId(deleteByPlanIds);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 删除本轮次及之前的所有任务(无论状态)
 | 
	
		
			
				|  |  | +     * @param solidPlanIds
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  |      @Async
 | 
	
		
			
				|  |  |      protected void deleteTaskByPlanId(List<Long> solidPlanIds) {
 | 
	
		
			
				|  |  |          if (CollectionUtils.isEmpty(solidPlanIds)) {
 | 
	
	
		
			
				|  | @@ -397,7 +429,6 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |          Date date = DateUtil.beginOfDay(new Date());
 | 
	
		
			
				|  |  |          resumptionMapper.delete(new LambdaQueryWrapper<Resumption>()
 | 
	
		
			
				|  |  |                  .in(Resumption::getSolidId, solidPlanIds)
 | 
	
		
			
				|  |  | -                .ne(Resumption::getStatus, ResumptionTaskStatus.Completed.getCode())
 | 
	
		
			
				|  |  |                  .and(w ->
 | 
	
		
			
				|  |  |                          w.ge(Resumption::getYmdDate, date)
 | 
	
		
			
				|  |  |                                  .or(w1 -> w1.ge(Resumption::getPlanEndTime, date))
 | 
	
	
		
			
				|  | @@ -532,10 +563,9 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |              LambdaQueryWrapper<AppPlanToPoint> pointWrapper = new LambdaQueryWrapper<>();
 | 
	
		
			
				|  |  |              pointWrapper.eq(AppPlanToPoint::getPlanId, app.getId());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            //任务为使用中且有已完成或停用,则下个周期生效,需要副本;任务为使用中且无已完成,则删除所有任务,并重新生成任务
 | 
	
		
			
				|  |  | -            Boolean needCopy = (ObjectUtil.equal(app.getTaskHasCompleted(), 1)
 | 
	
		
			
				|  |  | -                    && ObjectUtil.equal(existPlan.getPlanStatus(), PlanStatus.USING.getCode()))
 | 
	
		
			
				|  |  | -                    || ObjectUtil.equal(existPlan.getPlanStatus(), PlanStatus.DELETED.getCode());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            //任务为使用中且有已完成或当前周期内停用,则下个周期生效,需要副本;任务为使用中且无已完成,则删除所有任务,并重新生成任务
 | 
	
		
			
				|  |  | +            Boolean needCopy = needCopyOnEdit(existPlan);
 | 
	
		
			
				|  |  |              List<Object> needRebuildPlans = new ArrayList<>();
 | 
	
		
			
				|  |  |              //修改所有子计划
 | 
	
		
			
				|  |  |              List<AppPlan> appPlans = baseMapper.selectList(new LambdaQueryWrapper<AppPlan>().eq(AppPlan::getParentId, plan.getId()));
 | 
	
	
		
			
				|  | @@ -549,15 +579,17 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |                      baseMapper.copyChildren(ap.getId(), newChildPlanId, newPlanID);
 | 
	
		
			
				|  |  |                      baseMapper.copyPlanOrgs(ap.getId(), newChildPlanId);
 | 
	
		
			
				|  |  |                      baseMapper.copyPlanRoles(ap.getId(), newChildPlanId);
 | 
	
		
			
				|  |  | -                    Integer c= baseMapper.copyPlanPoints(ap.getId(), newChildPlanId);
 | 
	
		
			
				|  |  | +                    Integer c = baseMapper.copyPlanPoints(ap.getId(), newChildPlanId);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |                      baseMapper.updateById(ap);
 | 
	
		
			
				|  |  |                      baseMapper.deleteById(ap.getId());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                    child=baseMapper.selectById(newChildPlanId);
 | 
	
		
			
				|  |  | -                } else {
 | 
	
		
			
				|  |  | +                    child = baseMapper.selectById(newChildPlanId);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                if (needRebuildOnEdit(child)) {
 | 
	
		
			
				|  |  |                      needRebuildPlans.add(child);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |                  Map<Long, AppPlanToPoint> planPoints = appPlanToItemMapper.selectList(pointWrapper).stream()
 | 
	
		
			
				|  |  |                          .collect(Collectors.toMap(AppPlanToPoint::getPointId, p -> p));
 | 
	
		
			
				|  |  |  //                Date defaultEffectiveDate = calendar.getTime();
 | 
	
	
		
			
				|  | @@ -707,7 +739,7 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |              plan.setPlanCreateOrgName(null);
 | 
	
		
			
				|  |  |              baseMapper.updateById(plan);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -            if (!needCopy && ObjectUtil.equal(plan.getPlanOfOrgType(), OrgTypeEnum.HANG_SHE.getCode().longValue())) {
 | 
	
		
			
				|  |  | +            if (needRebuildOnEdit(plan)) {
 | 
	
		
			
				|  |  |                  needRebuildPlans.add(baseMapper.selectById(plan.getId()));
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              resumptionTaskBusiness.rebuildCurrentCycleTask(needRebuildPlans);
 | 
	
	
		
			
				|  | @@ -718,6 +750,59 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 编辑时,该计划是否需要副本。
 | 
	
		
			
				|  |  | +     * 1.使用中且还没有到生效时间,不需要;
 | 
	
		
			
				|  |  | +     * 2.使用中且有已完成时,需要
 | 
	
		
			
				|  |  | +     * 3.使用中且无完成,不需要副本
 | 
	
		
			
				|  |  | +     * <p>
 | 
	
		
			
				|  |  | +     * 4.停用,本周期之前停用,不需要
 | 
	
		
			
				|  |  | +     * 5.停用,本周期停用,需要
 | 
	
		
			
				|  |  | +     * 6.草稿,不需要
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param plan
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private Boolean needCopyOnEdit(AppPlan plan) {
 | 
	
		
			
				|  |  | +        DateRange dateRange = DateUtils.getStartAndEnd(new Date(), plan.getPlanCycle());
 | 
	
		
			
				|  |  | +        if (ObjectUtil.equal(plan.getPlanStatus(), PlanStatus.USING.getCode()) && new Date().before(plan.getEffectiveTime())) {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        } else if (ObjectUtil.equal(plan.getPlanStatus(), PlanStatus.USING.getCode())
 | 
	
		
			
				|  |  | +                && ObjectUtil.isNotNull(plan.getTaskHasCompleted())
 | 
	
		
			
				|  |  | +                && ObjectUtil.notEqual(plan.getTaskHasCompleted(), 0)) {
 | 
	
		
			
				|  |  | +            return true;
 | 
	
		
			
				|  |  | +        } else if (ObjectUtil.equal(plan.getPlanStatus(), PlanStatus.USING.getCode()) && (ObjectUtil.isNull(plan.getTaskHasCompleted()) ||
 | 
	
		
			
				|  |  | +                ObjectUtil.equal(plan.getTaskHasCompleted(), 0))) {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        } else if (ObjectUtil.equal(plan.getPlanStatus(), PlanStatus.DELETED.getCode()) && dateRange.getStartTime().after(plan.getWithdrawTime())) {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        } else if (ObjectUtil.equal(plan.getPlanStatus(), PlanStatus.DELETED.getCode())
 | 
	
		
			
				|  |  | +                && dateRange.getStartTime().before(plan.getWithdrawTime())
 | 
	
		
			
				|  |  | +                && dateRange.getEndTime().after(plan.getWithdrawTime())) {
 | 
	
		
			
				|  |  | +            return true;
 | 
	
		
			
				|  |  | +        } else if (ObjectUtil.equal(plan.getPlanStatus(), PlanStatus.DRAFT.getCode())) {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 编辑时,是否需要重新生成任务
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param plan
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private Boolean needRebuildOnEdit(AppPlan plan) {
 | 
	
		
			
				|  |  | +        if (ObjectUtil.equal(plan.getPlanStatus(), PlanStatus.USING.getCode())
 | 
	
		
			
				|  |  | +                && (ObjectUtil.isNull(plan.getTaskHasCompleted()) || ObjectUtil.equal(plan.getPlanStatus(), 0))
 | 
	
		
			
				|  |  | +                && !new Date().before(plan.getEffectiveTime())) {
 | 
	
		
			
				|  |  | +            return true;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return false;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public AppPlanVo findById(Long id) {
 | 
	
		
			
				|  |  |          AppPlan appPlan = baseMapper.selectById(id);
 | 
	
	
		
			
				|  | @@ -797,7 +882,7 @@ public class AppPlanServiceImpl extends ServiceImpl<AppPlanMapper, AppPlan> impl
 | 
	
		
			
				|  |  |                  .select(AppPlan::getId, AppPlan::getSolidId));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          if (ObjectUtil.equal(plan.getTaskHasCompleted(), 1)) {
 | 
	
		
			
				|  |  | -            //有已完成时,计划逻辑删除,删除当前轮次及之后未完成的任务
 | 
	
		
			
				|  |  | +            //有已完成时,计划逻辑删除,删除当前轮次及之后任务
 | 
	
		
			
				|  |  |              LambdaQueryWrapper<AppPlan> wrapper = new LambdaQueryWrapper<>();
 | 
	
		
			
				|  |  |              wrapper.in(AppPlan::getId, plans.stream().map(AppPlan::getId).collect(Collectors.toList()));
 | 
	
		
			
				|  |  |  
 |