| | |
| | | import com.ruoyi.device.excel.upload.DeviceCalibrationPlanDetailUpload; |
| | | import com.ruoyi.device.mapper.DeviceCalibrationPlanDetailMapper; |
| | | import com.ruoyi.device.mapper.DeviceCalibrationPlanMapper; |
| | | import com.ruoyi.device.mapper.DeviceMapper; |
| | | import com.ruoyi.device.pojo.Device; |
| | | import com.ruoyi.device.pojo.DeviceCalibrationPlan; |
| | | import com.ruoyi.device.pojo.DeviceCalibrationPlanDetail; |
| | | import com.ruoyi.device.service.DeviceCalibrationPlanDetailService; |
| | |
| | | private InformationNotificationService informationNotificationService; |
| | | @Resource |
| | | private UserMapper userMapper; |
| | | @Resource |
| | | private DeviceMapper deviceMapper; |
| | | |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | public IPage<DeviceCalibrationPlanDetail> pageDeviceCalibrationPlanDetail(Page page, DeviceCalibrationPlanDetail deviceCalibrationPlanDetails) { |
| | | public IPage<DeviceCalibrationPlanDetailDto> pageDeviceCalibrationPlanDetail(Page page, DeviceCalibrationPlanDetail deviceCalibrationPlanDetails) { |
| | | if (deviceCalibrationPlanDetails.getPlanId() == null) { |
| | | return new Page(); |
| | | return new Page<>(); |
| | | } |
| | | return deviceCalibrationPlanDetailMapper.pageDeviceCalibrationPlanDetail(page, QueryWrappers.queryWrappers(deviceCalibrationPlanDetails)); |
| | | } |
| | | |
| | | /** |
| | | * 按年份查询设备校准计划(不存在则自动创建),并自动补全已到点的设备明细 |
| | | * @param planYear 年份 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public DeviceCalibrationPlanDto getOrCreatePlanByYear(String planYear) { |
| | | if (StringUtils.isBlank(planYear)) { |
| | | planYear = String.valueOf(LocalDate.now().getYear()); |
| | | } |
| | | DeviceCalibrationPlan plan = getOrCreatePlanEntity(planYear); |
| | | ensureCalibrationDetails(plan); |
| | | DeviceCalibrationPlanDto planDto = new DeviceCalibrationPlanDto(); |
| | | BeanUtils.copyProperties(plan, planDto); |
| | | DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | planDto.setWriteTimeStr(plan.getWriteTime() == null ? null : plan.getWriteTime().format(dateFormatter)); |
| | | planDto.setRatifyTimeStr(plan.getRatifyTime() == null ? null : plan.getRatifyTime().format(dateFormatter)); |
| | | if (plan.getWriteUserId() != null) { |
| | | User writeUser = userMapper.selectById(plan.getWriteUserId()); |
| | | if (writeUser != null) { |
| | | planDto.setWriteName(writeUser.getName()); |
| | | } |
| | | } |
| | | if (plan.getRatifyUserId() != null) { |
| | | User ratifyUser = userMapper.selectById(plan.getRatifyUserId()); |
| | | if (ratifyUser != null) { |
| | | planDto.setRatifyName(ratifyUser.getName()); |
| | | } |
| | | } |
| | | return planDto; |
| | | } |
| | | |
| | | /** |
| | | * 设备档案变更后,把设备同步到对应年份的设备校准计划明细(仅当已进入提醒期/已过期时生成) |
| | | * @param device 设备 |
| | | */ |
| | | @Override |
| | | public void syncDeviceCalibrationDetail(Device device) { |
| | | LocalDate remind = calcCalibrationRemindDate(device); |
| | | if (remind == null || remind.isAfter(LocalDate.now())) { |
| | | return; |
| | | } |
| | | DeviceCalibrationPlan plan = getOrCreatePlanEntity(String.valueOf(remind.getYear())); |
| | | fillCalibrationDetailIfAbsent(plan, device); |
| | | } |
| | | |
| | | /** |
| | | * 查询年份对应的计划,不存在则创建 |
| | | */ |
| | | private DeviceCalibrationPlan getOrCreatePlanEntity(String planYear) { |
| | | DeviceCalibrationPlan plan = baseMapper.selectOne(Wrappers.<DeviceCalibrationPlan>lambdaQuery() |
| | | .eq(DeviceCalibrationPlan::getPlanYear, planYear) |
| | | .orderByDesc(DeviceCalibrationPlan::getPlanId) |
| | | .last("limit 1")); |
| | | if (plan == null) { |
| | | plan = new DeviceCalibrationPlan(); |
| | | plan.setPlanName(planYear + "年度设备校准计划"); |
| | | plan.setPlanYear(planYear); |
| | | plan.setWriteUserId(SecurityUtils.getUserId().intValue()); |
| | | plan.setWriteTime(LocalDateTime.now()); |
| | | baseMapper.insert(plan); |
| | | } |
| | | return plan; |
| | | } |
| | | |
| | | /** |
| | | * 扫描所有设备,把已到点(下次校准日期前1个月起,含已过期)的设备补进本年度计划 |
| | | */ |
| | | private void ensureCalibrationDetails(DeviceCalibrationPlan plan) { |
| | | List<Device> devices = deviceMapper.selectList(null); |
| | | for (Device device : devices) { |
| | | fillCalibrationDetailIfAbsent(plan, device); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设备的提醒起点日期:下次校准日期前1个月(取不到下次日期时用校准有效期前1个月兜底) |
| | | */ |
| | | private LocalDate calcCalibrationRemindDate(Device device) { |
| | | LocalDateTime base = device.getNextCalibrationDate() != null ? device.getNextCalibrationDate() : device.getActivationDate(); |
| | | return base == null ? null : base.toLocalDate().minusMonths(1); |
| | | } |
| | | |
| | | /** |
| | | * 已到点且计划中尚无该设备本轮明细时,自动补一条 |
| | | */ |
| | | private void fillCalibrationDetailIfAbsent(DeviceCalibrationPlan plan, Device device) { |
| | | LocalDate remind = calcCalibrationRemindDate(device); |
| | | // 未到点不生成;未来年份的提醒不补进当前计划 |
| | | if (remind == null || remind.isAfter(LocalDate.now())) { |
| | | return; |
| | | } |
| | | if (plan.getPlanYear() != null && String.valueOf(remind.getYear()).compareTo(plan.getPlanYear()) > 0) { |
| | | return; |
| | | } |
| | | Long exist = deviceCalibrationPlanDetailMapper.selectCount(Wrappers.<DeviceCalibrationPlanDetail>lambdaQuery() |
| | | .eq(DeviceCalibrationPlanDetail::getPlanId, plan.getPlanId()) |
| | | .eq(DeviceCalibrationPlanDetail::getDeviceId, device.getId()) |
| | | .eq(DeviceCalibrationPlanDetail::getPlanDate, remind)); |
| | | if (exist != null && exist > 0) { |
| | | return; |
| | | } |
| | | DeviceCalibrationPlanDetail detail = new DeviceCalibrationPlanDetail(); |
| | | detail.setPlanId(plan.getPlanId()); |
| | | detail.setDeviceId(device.getId()); |
| | | String deviceName = device.getDeviceName() == null ? "" : device.getDeviceName(); |
| | | if (StringUtils.isNotBlank(device.getSpecificationModel())) { |
| | | deviceName = deviceName + " " + device.getSpecificationModel(); |
| | | } |
| | | detail.setDeviceName(deviceName); |
| | | detail.setDeviceAmount("1"); |
| | | detail.setDeviceNumber(device.getManagementNumber()); |
| | | detail.setVerificationUnit(device.getCalibrationServices()); |
| | | detail.setVerificationCycles(device.getCalibrationDate()); |
| | | detail.setLastDate(device.getLastCalibrationDate() == null ? null : device.getLastCalibrationDate().toLocalDate()); |
| | | detail.setPlanDate(remind); |
| | | deviceCalibrationPlanDetailMapper.insert(detail); |
| | | } |
| | | |
| | | /** |
| | | * 导出设备校准计划 |
| | | * @param deviceCalibrationPlanId |
| | | * @param response |