|
|
|
|
@ -1,11 +1,29 @@ |
|
|
|
|
|
|
|
|
|
package org.springblade.modules.business.service.impl; |
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
|
import org.springblade.core.mp.base.BaseServiceImpl; |
|
|
|
|
import org.springblade.core.tool.utils.CollectionUtil; |
|
|
|
|
import org.springblade.modules.business.pojo.entity.maintenance.MaintenancePlanDetail; |
|
|
|
|
import org.springblade.modules.business.pojo.entity.maintenance.MaintenanceTask; |
|
|
|
|
import org.springblade.modules.business.pojo.entity.maintenance.MaintenanceTaskDetail; |
|
|
|
|
import org.springblade.modules.business.service.IMaintenancePlanDetailService; |
|
|
|
|
import org.springblade.modules.business.service.IMaintenancePlanService; |
|
|
|
|
import org.springblade.modules.business.mapper.MaintenancePlanMapper; |
|
|
|
|
import org.springblade.modules.business.pojo.entity.maintenance.MaintenancePlan; |
|
|
|
|
import org.springblade.modules.business.service.IMaintenanceTaskDetailService; |
|
|
|
|
import org.springblade.modules.business.service.IMaintenanceTaskService; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
|
|
import java.time.LocalDate; |
|
|
|
|
import java.time.ZoneId; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 巡检计划 服务实现类 |
|
|
|
|
@ -14,7 +32,165 @@ import org.springframework.stereotype.Service; |
|
|
|
|
* @since 2024-10-14 |
|
|
|
|
*/ |
|
|
|
|
@Service |
|
|
|
|
@AllArgsConstructor |
|
|
|
|
public class MaintenancePlanServiceImpl extends BaseServiceImpl<MaintenancePlanMapper, MaintenancePlan> implements IMaintenancePlanService { |
|
|
|
|
|
|
|
|
|
private final IMaintenancePlanDetailService planDetailService; |
|
|
|
|
private final IMaintenanceTaskService taskService; |
|
|
|
|
private final IMaintenanceTaskDetailService taskDetailService; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
public boolean create(MaintenancePlan plan) { |
|
|
|
|
long count = this.count(Wrappers.lambdaQuery(MaintenancePlan.class).eq(MaintenancePlan::getDeptId, plan.getDeptId())); |
|
|
|
|
// 一个实验室只能有一条巡检计划
|
|
|
|
|
if (count > 0) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
plan.setIsOpen(0); |
|
|
|
|
plan.setPlanCode("编码格式待定"); |
|
|
|
|
// plan.setPlanName("");
|
|
|
|
|
// plan.setAddress("");
|
|
|
|
|
// plan.setServicemanId(null);
|
|
|
|
|
boolean save = this.save(plan); |
|
|
|
|
|
|
|
|
|
// 保存巡检计划
|
|
|
|
|
List<MaintenancePlanDetail> details = plan.getDetails(); |
|
|
|
|
if (CollectionUtil.isNotEmpty(details)) { |
|
|
|
|
details.forEach(detail -> detail.setPlanId(plan.getId())); |
|
|
|
|
planDetailService.saveBatch(details); |
|
|
|
|
} |
|
|
|
|
return save; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
public boolean planUpdate(MaintenancePlan plan) { |
|
|
|
|
// 保存巡检计划
|
|
|
|
|
List<MaintenancePlanDetail> details = plan.getDetails(); |
|
|
|
|
if (CollectionUtil.isNotEmpty(details)) { |
|
|
|
|
// 需要新增的详情
|
|
|
|
|
List<MaintenancePlanDetail> saveList = new ArrayList<>(); |
|
|
|
|
// 需要修改的详情
|
|
|
|
|
List<MaintenancePlanDetail> updateList = new ArrayList<>(); |
|
|
|
|
for (MaintenancePlanDetail detail : details) { |
|
|
|
|
if (detail.getId() != null) { |
|
|
|
|
updateList.add(detail); |
|
|
|
|
} else { |
|
|
|
|
detail.setPlanId(plan.getId()); |
|
|
|
|
saveList.add(detail); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
planDetailService.updateBatchById(updateList); |
|
|
|
|
planDetailService.saveBatch(saveList); |
|
|
|
|
} |
|
|
|
|
return this.updateById(plan); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
|
public boolean planRemove(List<Long> ids) { |
|
|
|
|
planDetailService.remove(Wrappers.lambdaQuery(MaintenancePlanDetail.class).in(MaintenancePlanDetail::getPlanId, ids)); |
|
|
|
|
return this.deleteLogic(ids); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public MaintenancePlan planDetail(MaintenancePlan plan) { |
|
|
|
|
MaintenancePlan planDetail = this.getById(plan.getId()); |
|
|
|
|
planDetail.setDetails(planDetailService.list(Wrappers.lambdaQuery(MaintenancePlanDetail.class).eq(MaintenancePlanDetail::getPlanId, planDetail.getId()))); |
|
|
|
|
return planDetail; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean planOpen(MaintenancePlan plan) { |
|
|
|
|
return this.updateById(plan); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean createTask(LocalDate date) { |
|
|
|
|
LocalDate.now(); |
|
|
|
|
// 获取所有已开启的巡检计划
|
|
|
|
|
List<MaintenancePlan> plans = this.list(Wrappers.lambdaQuery(MaintenancePlan.class).eq(MaintenancePlan::getIsOpen, 1)); |
|
|
|
|
List<Long> ids = plans.stream().map(MaintenancePlan::getId).collect(Collectors.toList()); |
|
|
|
|
// 获取所有巡检详情
|
|
|
|
|
List<MaintenancePlanDetail> planDetails = planDetailService.list(Wrappers.lambdaQuery(MaintenancePlanDetail.class).in(MaintenancePlanDetail::getPlanId, ids)); |
|
|
|
|
// 按计划分组
|
|
|
|
|
Map<Long, List<MaintenancePlanDetail>> listMap = planDetails.stream().collect(Collectors.groupingBy(MaintenancePlanDetail::getPlanId)); |
|
|
|
|
|
|
|
|
|
for (Map.Entry<Long, List<MaintenancePlanDetail>> longListEntry : listMap.entrySet()) { |
|
|
|
|
Long planId = longListEntry.getKey(); |
|
|
|
|
List<MaintenancePlanDetail> details = longListEntry.getValue(); |
|
|
|
|
List<MaintenancePlanDetail> newDetails = new ArrayList<>(); |
|
|
|
|
if (CollectionUtil.isNotEmpty(details)) { |
|
|
|
|
for (MaintenancePlanDetail detail : details) { |
|
|
|
|
// 下次开始时间
|
|
|
|
|
LocalDate localDate = detail.getNextStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); |
|
|
|
|
// 如果下次开始时间在当天或当天之前,生成巡检任务
|
|
|
|
|
if (localDate.isBefore(date) || localDate.equals(date)) { |
|
|
|
|
localDate = date; |
|
|
|
|
// 周期:1月 2季度 3半年
|
|
|
|
|
Integer period = detail.getPeriod(); |
|
|
|
|
// 计算下次开始时间
|
|
|
|
|
if (period == 1) { |
|
|
|
|
localDate.minusMonths(-1); |
|
|
|
|
} else if (period == 2) { |
|
|
|
|
localDate.minusMonths(-3); |
|
|
|
|
} else if (period == 3) { |
|
|
|
|
localDate.minusMonths(-6); |
|
|
|
|
} |
|
|
|
|
Date nextDate = Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); |
|
|
|
|
detail.setNextStartTime(nextDate); |
|
|
|
|
newDetails.add(detail); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (!newDetails.isEmpty()) { |
|
|
|
|
try { |
|
|
|
|
// 保存巡查任务
|
|
|
|
|
saveTask(plans, planId, newDetails); |
|
|
|
|
// 修改计划详情
|
|
|
|
|
planDetailService.updateBatchById(newDetails); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error(e.toString()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void saveTask(List<MaintenancePlan> plans, Long planId, List<MaintenancePlanDetail> newDetails) { |
|
|
|
|
// 获取计划详情
|
|
|
|
|
List<MaintenancePlan> planList = plans.stream().filter(plan -> plan.getId().equals(planId)).toList(); |
|
|
|
|
MaintenancePlan maintenancePlan = planList.get(0); |
|
|
|
|
|
|
|
|
|
if (maintenancePlan != null) { |
|
|
|
|
// 巡检任务赋值保存
|
|
|
|
|
MaintenanceTask maintenanceTask = new MaintenanceTask(); |
|
|
|
|
maintenanceTask.setPlanId(maintenancePlan.getId()); |
|
|
|
|
maintenanceTask.setTaskCode(maintenancePlan.getPlanCode()); |
|
|
|
|
maintenanceTask.setDeptId(maintenancePlan.getDeptId()); |
|
|
|
|
maintenanceTask.setTaskName(maintenancePlan.getPlanName()); |
|
|
|
|
maintenanceTask.setAddress(maintenancePlan.getAddress()); |
|
|
|
|
maintenanceTask.setServicemanId(maintenancePlan.getServicemanId()); |
|
|
|
|
maintenanceTask.setTaskContent(maintenancePlan.getPlanContent()); |
|
|
|
|
taskService.save(maintenanceTask); |
|
|
|
|
|
|
|
|
|
// 保存任务详情
|
|
|
|
|
List<MaintenanceTaskDetail> taskDetails = new ArrayList<>(); |
|
|
|
|
for (MaintenancePlanDetail newPlanDetail : newDetails) { |
|
|
|
|
MaintenanceTaskDetail taskDetail = new MaintenanceTaskDetail(); |
|
|
|
|
taskDetail.setTaskId(maintenanceTask.getId()); |
|
|
|
|
taskDetail.setFloorName(newPlanDetail.getFloorName()); |
|
|
|
|
taskDetail.setDeptId(newPlanDetail.getDeptId()); |
|
|
|
|
taskDetail.setMajorName(newPlanDetail.getMajorName()); |
|
|
|
|
taskDetail.setCheckContent(newPlanDetail.getCheckContent()); |
|
|
|
|
taskDetail.setCraft(newPlanDetail.getCraft()); |
|
|
|
|
taskDetail.setPeriod(newPlanDetail.getPeriod()); |
|
|
|
|
taskDetails.add(taskDetail); |
|
|
|
|
} |
|
|
|
|
taskDetailService.saveBatch(taskDetails); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|