package com.by4cloud.platformx.device.service.impl; import cn.hutool.core.bean.BeanUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.by4cloud.platformx.common.core.util.R; import com.by4cloud.platformx.device.constant.CommonStatusContant; import com.by4cloud.platformx.device.dto.InspectionPlanQueryDTO; import com.by4cloud.platformx.device.entity.*; import com.by4cloud.platformx.device.mapper.*; import com.by4cloud.platformx.device.service.InspectionPlanService; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; /** * 巡检计划 * * @author syt * @date 2025-04-22 09:06:43 */ @Service @AllArgsConstructor public class InspectionPlanServiceImpl extends ServiceImpl implements InspectionPlanService { private final InspectionPlanDeviceMapper planDeviceMapper; private final InspectionPlanDeviceSerialMapper planDeviceSerialMapper; private final InspectionPlanDeviceStandardMapper planDeviceStandardMapper; private final InspectionTaskMapper taskMapper; private final InspectionTaskItemMapper taskItemMapper; @Override public boolean saveDeep(InspectionPlan inspectionPlan) { baseMapper.insert(inspectionPlan); //计划与设备关联 if (inspectionPlan.getDeviceList()!=null&&inspectionPlan.getDeviceList().size()>0){ for (InspectionPlanDevice planDevice:inspectionPlan.getDeviceList() ) { planDevice.setPlanId(inspectionPlan.getId()); planDeviceMapper.insert(planDevice); //巡检计划设备具体序列号 if (planDevice.getSerialList()!=null&&planDevice.getSerialList().size()>0){ planDevice.getSerialList().stream().forEach(item->{ InspectionPlanDeviceSerial serial = new InspectionPlanDeviceSerial(); serial.setSerialNo(item); serial.setPlanDeviceId(planDevice.getId()); planDeviceSerialMapper.insert(serial); }); } //巡检计划巡检标准 if (planDevice.getStandardsList()!=null&&planDevice.getStandardsList().size()>0){ planDevice.getStandardsList().stream().forEach(item->{ InspectionPlanDeviceStandards standards = new InspectionPlanDeviceStandards(); standards.setStandardId(item); standards.setPlanDeviceId(planDevice.getId()); planDeviceStandardMapper.insert(standards); }); } } } //根据是否生成任务标识生成任务 if (inspectionPlan.getTaskFlag()==CommonStatusContant.INSPECTION_PLAN_FLAG_GEN_Y){ InspectionTask task = BeanUtil.copyProperties(inspectionPlan, InspectionTask.class); task.setId(null); task.setTaskStatus(CommonStatusContant.INSPECTION_TASK_NO_STARTED); task.setTaskName(inspectionPlan.getPlanName()+"的任务"); task.setPlanId(inspectionPlan.getId()); taskMapper.insert(task); //任务明细 if (inspectionPlan.getDeviceList()!=null&&inspectionPlan.getDeviceList().size()>0){ for (InspectionPlanDevice planDevice:inspectionPlan.getDeviceList() ) { //巡检计划设备具体序列号 if (planDevice.getSerialList()!=null&&planDevice.getSerialList().size()>0){ for (String serial:planDevice.getSerialList() ) { //巡检计划巡检标准 if (planDevice.getStandardsList()!=null&&planDevice.getStandardsList().size()>0){ for (Long standardId:planDevice.getStandardsList() ) { InspectionTaskItem item = new InspectionTaskItem(); item.setDeviceId(planDevice.getDeviceId()); item.setTaskId(task.getId()); item.setStandardId(standardId); item.setSerialNo(serial); taskItemMapper.insert(item); } } } } } } } return true; } @Override public IPage pageNew(Page page, InspectionPlanQueryDTO queryDTO) { return baseMapper.page(page,queryDTO); } @Override public InspectionPlan getByIdNew(Long id) { InspectionPlan plan = baseMapper.selectById(id); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("plan_id",id); List planDeviceList = planDeviceMapper.selectList(queryWrapper); if (planDeviceList.size()>0){ planDeviceList.stream().forEach(device->{ List serials = planDeviceSerialMapper.selectSerialList(device.getId()); if (serials.size()>0) { device.setSerialList(serials); } List standardIdList = planDeviceStandardMapper.selectStandardIdList(device.getId()); if (standardIdList.size()>0) { device.setStandardsList(standardIdList); } }); } plan.setDeviceList(planDeviceList); return plan; } @Override public boolean updateByIdNew(InspectionPlan inspectionPlan) { baseMapper.updateById(inspectionPlan); //计划与设备关联 if (inspectionPlan.getDeviceList()!=null&&inspectionPlan.getDeviceList().size()>0){ for (InspectionPlanDevice planDevice:inspectionPlan.getDeviceList() ) { planDevice.setPlanId(inspectionPlan.getId()); if (planDevice.getId()!=null){ planDeviceMapper.updateById(planDevice); }else { planDeviceMapper.insert(planDevice); } //巡检计划设备具体序列号 if (planDevice.getSerialList()!=null&&planDevice.getSerialList().size()>0){ QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("plan_device_id",planDevice.getId()); planDeviceSerialMapper.delete(queryWrapper); planDevice.getSerialList().stream().forEach(item->{ InspectionPlanDeviceSerial serial = new InspectionPlanDeviceSerial(); serial.setSerialNo(item); serial.setPlanDeviceId(planDevice.getId()); planDeviceSerialMapper.insert(serial); }); } //巡检计划巡检标准 if (planDevice.getStandardsList()!=null&&planDevice.getStandardsList().size()>0){ QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("plan_device_id",planDevice.getId()); planDeviceStandardMapper.delete(queryWrapper); planDevice.getStandardsList().stream().forEach(item->{ InspectionPlanDeviceStandards standards = new InspectionPlanDeviceStandards(); standards.setStandardId(item); standards.setPlanDeviceId(planDevice.getId()); planDeviceStandardMapper.insert(standards); }); } } } //根据是否生成任务标识生成任务 if (inspectionPlan.getTaskFlag()==CommonStatusContant.INSPECTION_PLAN_FLAG_GEN_Y){ InspectionTask task = BeanUtil.copyProperties(inspectionPlan, InspectionTask.class); task.setId(null); task.setTaskStatus(CommonStatusContant.INSPECTION_TASK_NO_STARTED); task.setTaskName(inspectionPlan.getPlanName()+"的任务"); task.setPlanId(inspectionPlan.getId()); taskMapper.insert(task); //任务明细 if (inspectionPlan.getDeviceList()!=null&&inspectionPlan.getDeviceList().size()>0){ for (InspectionPlanDevice planDevice:inspectionPlan.getDeviceList() ) { //巡检计划设备具体序列号 if (planDevice.getSerialList()!=null&&planDevice.getSerialList().size()>0){ for (String serial:planDevice.getSerialList() ) { //巡检计划巡检标准 if (planDevice.getStandardsList()!=null&&planDevice.getStandardsList().size()>0){ for (Long standardId:planDevice.getStandardsList() ) { InspectionTaskItem item = new InspectionTaskItem(); item.setDeviceId(planDevice.getDeviceId()); item.setTaskId(task.getId()); item.setStandardId(standardId); item.setSerialNo(serial); taskItemMapper.insert(item); } } } } } } } return true; } @Override public R gentTask(Long id) { InspectionPlan plan = getByIdNew(id); InspectionTask task = BeanUtil.copyProperties(plan, InspectionTask.class); task.setId(null); task.setTaskStatus(CommonStatusContant.INSPECTION_TASK_NO_STARTED); task.setTaskName(plan.getPlanName()+"的任务"); task.setPlanId(plan.getId()); taskMapper.insert(task); //任务明细 if (plan.getDeviceList()!=null&&plan.getDeviceList().size()>0){ for (InspectionPlanDevice planDevice:plan.getDeviceList() ) { //巡检计划设备具体序列号 if (planDevice.getSerialList()!=null&&planDevice.getSerialList().size()>0){ for (String serial:planDevice.getSerialList() ) { //巡检计划巡检标准 if (planDevice.getStandardsList()!=null&&planDevice.getStandardsList().size()>0){ for (Long standardId:planDevice.getStandardsList() ) { InspectionTaskItem item = new InspectionTaskItem(); item.setDeviceId(planDevice.getDeviceId()); item.setTaskId(task.getId()); item.setStandardId(standardId); item.setSerialNo(serial); taskItemMapper.insert(item); } } } } } } //跟新计划标识 plan.setTaskFlag(CommonStatusContant.INSPECTION_PLAN_FLAG_GEN_Y); baseMapper.updateById(plan); return R.ok(); } }