package com.foreverwin.mesnac.equip.service.impl;

import com.foreverwin.mesnac.common.constant.Constants;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.helper.NextNumberHelper;
import com.foreverwin.mesnac.common.model.InspectionItem;
import com.foreverwin.mesnac.common.model.InspectionItemDetail;
import com.foreverwin.mesnac.common.service.InspectionItemDetailService;
import com.foreverwin.mesnac.common.service.InspectionItemService;
import com.foreverwin.mesnac.equip.mapper.ResourceInspectTaskMapper;
import com.foreverwin.mesnac.equip.model.*;
import com.foreverwin.mesnac.equip.service.*;
import com.foreverwin.mesnac.meapi.mapper.ResrceMapper;
import com.foreverwin.mesnac.meapi.model.UserGroup;
import com.foreverwin.mesnac.meapi.service.UserGroupService;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.equip.mapper.ResourceInspectPlanMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
 * <p>
 * 设备检验计划表 服务实现类
 * </p>
 *
 * @author pavel.Liu
 * @since 2021-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceInspectPlanServiceImpl extends ServiceImpl<ResourceInspectPlanMapper, ResourceInspectPlan> implements ResourceInspectPlanService {

    @Autowired
    private NextNumberHelper nextNumberHelper;
    @Autowired
    public  ResourceInspectPlanService resourceInspectPlanService;
    @Autowired
    public InspectionItemService inspectionItemService;
    @Autowired
    public InspectionItemDetailService inspectionItemDetailService;
    @Autowired
    public ResourceInspectResourceService resourceInspectResourceService;
    @Autowired
    public InspectPlanUserGroupService inspectPlanUserGroupService;
    @Autowired
    public UserGroupService userGroupService;
    @Autowired
    public ResourceInspectTaskService resourceInspectTaskService;
    @Autowired
    public ResourceInspectTaskParamService resourceInspectTaskParamService;
    @Autowired
    public ResourceInspectTaskMapper resourceInspectTaskMapper;
    @Autowired
    public ResrceMapper resrceMapper;

    @Override
    public IPage<ResourceInspectPlan> selectPage(FrontPage<ResourceInspectPlan> frontPage, ResourceInspectPlan resourceInspectPlan) {
        QueryWrapper<ResourceInspectPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceInspectPlan);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<ResourceInspectPlan> selectList(ResourceInspectPlan resourceInspectPlan) {
        QueryWrapper<ResourceInspectPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceInspectPlan);
        return super.list(queryWrapper);
    }

    /**
     * 保存设备检验计划
     * @param resourceInspectPlan
     */
    @Override
    public Boolean saveResourcePlanData(ResourceInspectPlan resourceInspectPlan) {
        // 判断检验项目清单HANDLE是否存在
        String inspectionItemBo = resourceInspectPlan.getInspectionItemBo();
        InspectionItem inspectionItem = inspectionItemService.getById(inspectionItemBo);
        if (inspectionItem == null){
            throw BusinessException.build("检验项目清单不存在！！！");
        }
        String category = resourceInspectPlan.getCategory();
        //查询设备计划是否存在
        String site = CommonMethods.getSite();
        resourceInspectPlan.setSite(site);
        // 计划编号
        String planNo = resourceInspectPlan.getPlanNo();
        // 计划版本
        String revision =  resourceInspectPlan.getRevision();
        String handle = HandleEnum.RESOURCE_INSPECT_PLAN.getHandle(site, planNo, revision);
        ResourceInspectPlan planHandle = resourceInspectPlanService.getById(handle);
        // 创建/修改人员
        String user = CommonMethods.getUser();
        Boolean returnStatus = false;
        //计划是否已存在
        if (planHandle == null){
            // 创建人员和创建时间
            resourceInspectPlan.setCreatedDateTime(LocalDateTime.now());
            resourceInspectPlan.setCreateUser(user);
            returnStatus = true;
            resourceInspectPlan.setStatus("401");
        }else{
            ResourceInspectTask resourceInspectTask = new ResourceInspectTask();
            resourceInspectTask.setResourceInspectPlanBo(planHandle.getHandle());
            List<ResourceInspectTask> resourceInspectTasks = resourceInspectTaskService.selectList(resourceInspectTask);
            if (!planHandle.getScheduleType().equals(resourceInspectPlan.getScheduleType()) && resourceInspectTasks.size() > 0){
                throw BusinessException.build("已创建任务的点检计划，不能修改排程方式！");
            }
            // 保留创建人员和创建时间
            String createUser = planHandle.getCreateUser();
            LocalDateTime createdDateTime = planHandle.getCreatedDateTime();
            resourceInspectPlan.setCreateUser(createUser);
            resourceInspectPlan.setCreatedDateTime(createdDateTime);
            // 更新修改人员和修改时间
            resourceInspectPlan.setModifyUser(user);
            resourceInspectPlan.setModifiedDateTime(LocalDateTime.now());
            // 删除设备清单
            deleteEquipList(handle);
            // 删除用户组清单
            InspectPlanUserGroup inspectPlanUserGroup = new InspectPlanUserGroup();
            inspectPlanUserGroup.setResourceInspectPlanBo(handle);
            inspectPlanUserGroupService.deleteAll(inspectPlanUserGroup);
        }
        resourceInspectPlan.setHandle(handle);
        //保存计划下设备清单(全删全增)
        List<ResourceInspectResource> equips = resourceInspectPlan.getMaintainPlanEquipList();
        if (equips == null || equips.size() < 1){
            throw BusinessException.build("未添加设备！！！");
        }
        Collection<ResourceInspectResource> resourceColl = new ArrayList<>();
        StringBuffer resInfo = new StringBuffer("[");
        for (ResourceInspectResource equip: equips) {
            equip.setResourceInspectPlanBo(handle);
            equip.setCreateUser(user);
            equip.setCreatedDateTime(LocalDateTime.now());
            String resourceNo = equip.getResourceNo();
            Integer cnt = resourceInspectTaskMapper.findResourceTaskByResrceAndCategory(site, category, resourceNo,handle);
            if (cnt > 0){
                resInfo.append(resourceNo+",");
            }
            String resourceHandle = HandleEnum.RESOURCE.getHandle(site, resourceNo);
            equip.setHandle(HandleEnum.RESOURCE_INSPECT_RESOURCE.getHandle(site,handle,resourceHandle));
            resourceColl.add(equip);
        }
        if (!resInfo.toString().equals("[")){
            throw BusinessException.build("设备编号" + resInfo.append("]") + "已经维护了相同点检类型的点检计划！");
        }
        resourceInspectResourceService.saveBatch(resourceColl);
        // 保存用户组清单
        List<InspectPlanUserGroup> userGroups = resourceInspectPlan.getInspectPlanUserGroupList();
        if (userGroups != null && userGroups.size() > 0){
            Collection<InspectPlanUserGroup> userGroupColl = new ArrayList<>();
            for (InspectPlanUserGroup userGroup: userGroups) {
                userGroup.setResourceInspectPlanBo(handle);
                userGroup.setCreateUser(user);
                userGroup.setCreatedDateTime(LocalDateTime.now());
                userGroup.setHandle(handle+";"+userGroup.getUserGroupBo());
                userGroupColl.add(userGroup);
            }
            inspectPlanUserGroupService.saveBatch(userGroupColl);
        }
        // 保存或更新点检计划
        resourceInspectPlanService.saveOrUpdate(resourceInspectPlan);
        // 当更新/保存计划时，自动创建任务
        // 计划是否启用，非启用状态不创建
        if (resourceInspectPlan.getStatus().equals("301")){
            resourceInspectPlanService.autoCreateTaskByResourceInspectPlan(resourceInspectPlan);
        }
        return returnStatus;
    }

    /**
     *  按HANDLE检索数据
     *  虽然参数是ResourceInspectPlan类型，但按编号、站点、版本检索，返回值唯一
     * @param resourceInspectPlan
     * @return
     */
    @Override
    public ResourceInspectPlan getInspectPlanByHandle(ResourceInspectPlan resourceInspectPlan) {
        String planNo = resourceInspectPlan.getPlanNo();
        String revision = resourceInspectPlan.getRevision();
        String site = resourceInspectPlan.getSite();
        String handle = HandleEnum.RESOURCE_INSPECT_PLAN.getHandle(site, planNo, revision);
        ResourceInspectPlan inspectPlanById = resourceInspectPlanService.getById(handle);
        if (inspectPlanById == null){
            throw BusinessException.build("检验计划编号不存在！！！");
        }
        // 根据点检计划HANDLE获取设备清单
        ResourceInspectResource resourceInspectResource = new ResourceInspectResource();
        resourceInspectResource.setResourceInspectPlanBo(handle);
        List<ResourceInspectResource> resourceList = resourceInspectResourceService.selectList(resourceInspectResource);
        inspectPlanById.setMaintainPlanEquipList(resourceList);
        // 项目明细清单——根据项目handle获取
        String inspectionItemBo = inspectPlanById.getInspectionItemBo();
        InspectionItemDetail inspectionItemDetail = new InspectionItemDetail();
        inspectionItemDetail.setInspectionItemBo(inspectionItemBo);
        List<InspectionItemDetail> itemDetailList = inspectionItemDetailService.selectList(inspectionItemDetail);
        inspectPlanById.setInspectionItemDetailList(itemDetailList);
        // 用户组清单——根据项目计划handle获取
        InspectPlanUserGroup inspectPlanUserGroup = new InspectPlanUserGroup();
        inspectPlanUserGroup.setResourceInspectPlanBo(handle);
        List<InspectPlanUserGroup> userGroupList = inspectPlanUserGroupService.selectList(inspectPlanUserGroup);
        for (InspectPlanUserGroup userGroup:userGroupList) {
            String userGroupBo = userGroup.getUserGroupBo();
            UserGroup userGroupById = userGroupService.getById(userGroupBo);
            userGroup.setDescription(userGroupById.getDescription());
            userGroup.setUserGroupNo(userGroupById.getUserGroup());
        }
        inspectPlanById.setInspectPlanUserGroupList(userGroupList);
        return inspectPlanById;
    }

    @Override
    public void deleteInspectionPlanByHandle(ResourceInspectPlan resourceInspectPlan) {
        String handle = resourceInspectPlan.getHandle();
        ResourceInspectPlan inspectPlanById = resourceInspectPlanService.getById(handle);
        if (inspectPlanById == null){
            throw BusinessException.build("点检计划删除失败！");
        }
        ResourceInspectTask resourceInspectTask = new ResourceInspectTask();
        resourceInspectTask.setResourceInspectPlanBo(inspectPlanById.getHandle());
        List<ResourceInspectTask> resourceInspectTasks = resourceInspectTaskService.selectList(resourceInspectTask);
        if (resourceInspectTasks != null && resourceInspectTasks.size() > 0){
            throw BusinessException.build("当前计划已创建任务，不允许删除！");
        }
        // 设备清单表数据删除
        ResourceInspectResource resourceInspectResource = new ResourceInspectResource();
        resourceInspectResource.setResourceInspectPlanBo(handle);
        QueryWrapper<ResourceInspectResource> resourceQueryWrapper = new QueryWrapper<>();
        resourceQueryWrapper.setEntity(resourceInspectResource);
        resourceInspectResourceService.remove(resourceQueryWrapper);
        // 用户组清单数据删除
        deleteUserGroupList(handle);
        // 主表数据删除
        resourceInspectPlanService.removeById(handle);
    }

    /**
     * 自动创建任务
     * @param resourceInspectPlan
     */
    @Override
    public void autoCreateTaskByResourceInspectPlan(ResourceInspectPlan resourceInspectPlan) {
        // 标记字段：0为启用；1为更新
        Integer sign = 0;
        // 基本数据
            //  排程周期比较、有效期、任务数量
        String site = resourceInspectPlan.getSite();
        String frequencyUnit = resourceInspectPlan.getFrequencyUnit();
        Double frequency = resourceInspectPlan.getFrequency();
        LocalDateTime indateStart = resourceInspectPlan.getIndateStart();
        LocalDateTime indateEnd = resourceInspectPlan.getIndateEnd();
        Long planTaskCount = resourceInspectPlan.getPlanTaskCount();
        Integer taskCount = Integer.valueOf(planTaskCount.toString());
        // 点检计划handle
        String planHandle = resourceInspectPlan.getHandle();
        // 设备清单
        List<ResourceInspectResource> maintainPlanEquipList = resourceInspectPlan.getMaintainPlanEquipList();
        // 自动生产点检任务编号——点检任务handle
        List<String> taskNoList = nextNumberHelper.getNextNumber(site, Constants.RESOURCE_INSPECT_TASK, maintainPlanEquipList.size()*taskCount);
        // 遍历设备
        int taskNoNum = 0;
        for (ResourceInspectResource equip: maintainPlanEquipList) {
            // 判断是否创建任务：状态，仅启用状态创建任务
            String equipStatus = equip.getStatus();
            if (!equipStatus.equals("301")){
                continue;
            }
            String resourceNo = equip.getResourceNo();
            // 设备是否点检
            String inspectByResrce = resrceMapper.isInspectByResrce(site, resourceNo);
            if (inspectByResrce != null && inspectByResrce.equals("N")){
                continue;
            }
            /**
             * 按计划handle和设备编号，查询检验任务
             * bug：每次都会获取设备的所有检验任务，不做改动的情况下：数据会累加造成数据遍历量较大
             */
            ResourceInspectTask resourceInspectTask = new ResourceInspectTask();
            resourceInspectTask.setResourceInspectPlanBo(planHandle);
            resourceInspectTask.setResrce(resourceNo);
            resourceInspectTask.setSite(site);
            List<ResourceInspectTask> taskList = resourceInspectTaskMapper.getInspectTaskList(resourceInspectTask);
                // 遍历，并选取状态为新建及处理中401新建 301启用
            int taskNum = 0;
            if (taskList.size() > 0){
                sign = 1;
            }
            for (ResourceInspectTask task: taskList) {
                // 检验任务状态：1、处理中DOING：不变更任务；2、新建NEW：将任务状态变为取消；3、完成COMPLETE
                String taskStatus = task.getStatus();
                if (taskStatus.equals(Constants.INSPECTION_TASK_STATUS_DOING)){
                        taskNum++;
                }else {
                    if (taskStatus.equals(Constants.INSPECTION_TASK_STATUS_NEW)){
                        task.setStatus(Constants.INSPECTION_TASK_STATUS_COMPLETE);
                        resourceInspectTaskService.updateById(task);
                    }
                }
            }
            if (taskNum < taskCount){
                taskNum = taskCount - taskNum;
            }else{
                continue;
            }
            // 判断是否创建任务：任务生成时间在有效期内
            LocalDateTime taskCreateDate = equip.getTaskCreateDate();
            if (taskCreateDate.isAfter(indateStart) && taskCreateDate.isBefore(indateEnd)) {
                Collection<ResourceInspectTask> resourceInspectTaskList = new ArrayList<>();
                if (taskNum == taskCount){
                    while (taskNum>0){
                        resourceInspectTask.setTaskCreateDate(taskCreateDate);
                        ResourceInspectTask equipInspectTask = setResourceInspectTask(resourceInspectPlan, resourceInspectTask,sign);
                        resourceInspectTaskList.add(equipInspectTask);
                        taskCreateDate = addTime(frequencyUnit,frequency,taskCreateDate);
                        if (taskCreateDate.isAfter(indateEnd)){
                            break;
                        }
                        taskNum--;
                    }
                }else{
                    // 获取处理中的点检任务最新的创建时间
                    resourceInspectTask.setStatus(Constants.INSPECTION_TASK_STATUS_DOING);
                    LocalDateTime createdDateTime = resourceInspectTaskMapper.getMaxCreateTimeByStatus(resourceInspectTask);
                    if (taskCreateDate.isAfter(createdDateTime)){
                        // 循环创建检验任务
                        for (int i = 0;i < taskNum;i++){
                            resourceInspectTask.setTaskCreateDate(taskCreateDate);
                            ResourceInspectTask equipInspectTask = setResourceInspectTask(resourceInspectPlan, resourceInspectTask,sign);
                            resourceInspectTaskList.add(equipInspectTask);
                            taskCreateDate = addTime(frequencyUnit,frequency,taskCreateDate);
                            if (taskCreateDate.isAfter(indateEnd)){
                                break;
                            }
                        }
                    }else {
                        while (taskCreateDate.isBefore(createdDateTime)){
                            taskCreateDate = addTime(frequencyUnit,frequency,taskCreateDate);
                        }
                        for (int i = 0;i < taskNum;i++){
                            if (taskCreateDate.isAfter(indateEnd)){
                                break;
                            }
                            resourceInspectTask.setTaskCreateDate(taskCreateDate);
                            ResourceInspectTask equipInspectTask = setResourceInspectTask(resourceInspectPlan, resourceInspectTask,sign);
                            resourceInspectTaskList.add(equipInspectTask);
                            taskCreateDate = addTime(frequencyUnit,frequency,taskCreateDate);
                        }
                    }
                }
                for (ResourceInspectTask task:resourceInspectTaskList) {
                    task.setTaskNo(taskNoList.get(taskNoNum));
                    task.setHandle(HandleEnum.RESOURCE_INSPECT_TASK.getHandle(site,taskNoList.get(taskNoNum)));
                    taskNoNum++;
                }
                resourceInspectTaskService.saveBatch(resourceInspectTaskList);
            }
        }
    }

    @Override
    public void autoCreateTaskByResourceInspectTask(ResourceInspectTask resourceInspectTask) {
        // 检验计划HANDLE、检验计划：周期频率、单位；排程方式；前后容差时间
        String site = resourceInspectTask.getSite();
        String planHandle = resourceInspectTask.getResourceInspectPlanBo();
        ResourceInspectPlan inspectPlan = resourceInspectPlanService.getById(planHandle);
        Double frequency = inspectPlan.getFrequency();
        String frequencyUnit = inspectPlan.getFrequencyUnit();
        String scheduleType = inspectPlan.getScheduleType();
        String resrce = resourceInspectTask.getResrce();
        String resourceBo = HandleEnum.RESOURCE.getHandle(site, resrce);
        ResourceInspectTask newInspectTask = new ResourceInspectTask();
        newInspectTask.setResrce(resourceInspectTask.getResrce());
        newInspectTask.setResourceBo(resourceInspectTask.getResourceBo());
        if (scheduleType.equals("CYC")){
            LocalDateTime maxCreateTime = resourceInspectTaskMapper.selectMaxCreateTimeByPlanBoAnd(resourceInspectTask);
            LocalDateTime nextTaskDate = addTime(frequencyUnit, frequency, maxCreateTime);
            newInspectTask.setTaskCreateDate(nextTaskDate);
        }else{
            LocalDateTime modifiedDateTime = resourceInspectTask.getModifiedDateTime();
            LocalDateTime nextTaskDate = addTime(frequencyUnit, frequency, modifiedDateTime);
            newInspectTask.setTaskCreateDate(nextTaskDate);
        }
        String planStatus = inspectPlan.getStatus();
        // 点检计划非启用状态
        if (!planStatus.equals("301")){
            return;
        }
        ResourceInspectResource resource = resourceInspectResourceService.getById(HandleEnum.RESOURCE_INSPECT_RESOURCE.getHandle(site,planHandle,resourceBo));
        if (resource == null){
            return;
        }
        String resourceStatus = resource.getStatus();
        // 设备非启用状态
        if (!resourceStatus.equals("301")){
            return;
        }
        LocalDateTime indateEnd = inspectPlan.getIndateEnd();
        if (newInspectTask.getTaskCreateDate().isAfter(indateEnd)){
            return;
        }
        // 设备s是否点检
        String inspectByResrce = resrceMapper.isInspectByResrce(site, resrce);
        if (inspectByResrce != null && inspectByResrce.equals("N")){
            return;
        }
        ResourceInspectTask inspectTask = setResourceInspectTask(inspectPlan, newInspectTask, -1);
        List<String> nextNumber = nextNumberHelper.getNextNumber(site, Constants.RESOURCE_INSPECT_TASK, 1);
        inspectTask.setTaskNo(nextNumber.get(0));
        inspectTask.setHandle(HandleEnum.RESOURCE_INSPECT_TASK.getHandle(site,nextNumber.get(0)));
        resourceInspectTaskService.save(inspectTask);
    }

    /**
     * 删除设备清单
     */
    private void deleteEquipList(String ResourceInspectPlanBo){
        ResourceInspectResource resourceInspectResource = new ResourceInspectResource();
        resourceInspectResource.setResourceInspectPlanBo(ResourceInspectPlanBo);
        QueryWrapper<ResourceInspectResource> resourceQueryWrapper = new QueryWrapper<>();
        resourceQueryWrapper.setEntity(resourceInspectResource);
        resourceInspectResourceService.remove(resourceQueryWrapper);
    }

    /**
     * 删除用户组清单
     */
    private void deleteUserGroupList(String ResourceInspectPlanBo){
        InspectPlanUserGroup userGroup = new InspectPlanUserGroup();
        userGroup.setResourceInspectPlanBo(ResourceInspectPlanBo);
        QueryWrapper<InspectPlanUserGroup> userGroupWrapper = new QueryWrapper<>();
        userGroupWrapper.setEntity(userGroup);
        inspectPlanUserGroupService.remove(userGroupWrapper);
    }

    /**
     * 时间+频率时间
     * 周期：单位、频率；时间
     */
    private LocalDateTime addTime(String frequencyUnit,double frequency,LocalDateTime taskTime){
        long minutes = 0;
        switch (frequencyUnit){
            case "D":
                minutes = Math.round(frequency*60*24);
                break;
            case "H":
                minutes = Math.round(frequency*60);
                break;
            case "W":
                minutes = Math.round(frequency*60*24*7);
                break;
            case "M":
                minutes = Math.round(frequency*60*24*30);
                break;
            default:
                throw BusinessException.build("周期时间不存在！");
        }
        return taskTime.plusMinutes(minutes);
    }

    /**
     * 创建检验任务
     */
    private ResourceInspectTask setResourceInspectTask(ResourceInspectPlan resourceInspectPlan,ResourceInspectTask resourceInspectTask,Integer sign){
        ResourceInspectTask inspectTask = new ResourceInspectTask();
        // 基本字段录入：站点、状态、计划HANDLE、检验项清单HANDLE、检验类型
        inspectTask.setSite(resourceInspectPlan.getSite());
        inspectTask.setStatus(Constants.INSPECTION_TASK_STATUS_NEW);
        inspectTask.setResourceInspectPlanBo(resourceInspectPlan.getHandle());
        inspectTask.setInspectionItemBo(resourceInspectPlan.getInspectionItemBo());
        inspectTask.setCategory(resourceInspectPlan.getCategory());
        // 设备HANDLE
        inspectTask.setResourceBo(resourceInspectTask.getResourceBo());
        inspectTask.setResrce(resourceInspectTask.getResrce());
        // 任务创建时间
        inspectTask.setTaskCreateDate(resourceInspectTask.getTaskCreateDate());
        // 排程周期、频率、单位、点检类型
        inspectTask.setScheduleType(resourceInspectPlan.getScheduleType());
        inspectTask.setFrequency(resourceInspectPlan.getFrequency());
        inspectTask.setFrequencyUnit(resourceInspectPlan.getFrequencyUnit());
        inspectTask.setCategory(resourceInspectPlan.getCategory());
        // 计划开始、计划完成时间
        LocalDateTime taskCreateDate = inspectTask.getTaskCreateDate();
        Long beforeTolerance = resourceInspectPlan.getBeforeTolerance();
        Long afterTolerance = resourceInspectPlan.getAfterTolerance();
            // 若不存在前后容差，则不提供计划完成时间；计划开始时间为任务创建时间
        if (beforeTolerance == null || beforeTolerance < 0 ){
            inspectTask.setPlanStartDate(taskCreateDate);
        }else {
            inspectTask.setPlanStartDate(taskCreateDate.minusDays(beforeTolerance));
        }
        if (!(afterTolerance == null || afterTolerance < 0 )){
            inspectTask.setPlanCompleteDate(taskCreateDate.plusDays(afterTolerance));
        }
        // 创建人员、创建时间；修改人员、修改时间
        if (sign == 1){ // sign==1，更新，添加修改人员和修改时间
            inspectTask.setModifyUser(resourceInspectPlan.getModifyUser());
            inspectTask.setModifiedDateTime(resourceInspectPlan.getModifiedDateTime());
        }
        inspectTask.setCreateUser(resourceInspectPlan.getCreateUser());
        inspectTask.setCreatedDateTime(LocalDateTime.now());
        return inspectTask;
    }


}