From 089964a497c2528e88ddc610af5f88f631303431 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期一, 18 五月 2026 15:28:16 +0800
Subject: [PATCH] feat: 设备保养新增任务状态,启动/停止与调度器关联

---
 src/main/java/com/ruoyi/device/service/impl/DeviceRepairServiceImpl.java |  263 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 233 insertions(+), 30 deletions(-)

diff --git a/src/main/java/com/ruoyi/device/service/impl/DeviceRepairServiceImpl.java b/src/main/java/com/ruoyi/device/service/impl/DeviceRepairServiceImpl.java
index 1f09d07..23ec164 100644
--- a/src/main/java/com/ruoyi/device/service/impl/DeviceRepairServiceImpl.java
+++ b/src/main/java/com/ruoyi/device/service/impl/DeviceRepairServiceImpl.java
@@ -1,31 +1,44 @@
 package com.ruoyi.device.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 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.ruoyi.common.enums.FileNameType;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.device.dto.DeviceDefectRecordDto;
 import com.ruoyi.device.dto.DeviceRepairDto;
 import com.ruoyi.device.execl.DeviceRepairExeclDto;
-import com.ruoyi.device.mapper.DeviceDefectRecordMapper;
 import com.ruoyi.device.mapper.DeviceRepairMapper;
-import com.ruoyi.device.pojo.DeviceDefectRecord;
 import com.ruoyi.device.pojo.DeviceLedger;
 import com.ruoyi.device.pojo.DeviceRepair;
 import com.ruoyi.device.service.DeviceDefectRecordService;
 import com.ruoyi.device.service.IDeviceLedgerService;
 import com.ruoyi.device.service.IDeviceRepairService;
-import com.ruoyi.framework.web.domain.AjaxResult;
+import com.ruoyi.other.service.TempFileService;
+import com.ruoyi.sales.mapper.CommonFileMapper;
+import com.ruoyi.sales.pojo.CommonFile;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 
 @Service
 @AllArgsConstructor
@@ -38,6 +51,13 @@
     private DeviceRepairMapper deviceRepairMapper;
     @Autowired
     private IDeviceLedgerService deviceLedgerService;
+
+    @Autowired
+    private CommonFileMapper commonFileMapper;
+
+    @Autowired
+    private TempFileService tempFileService;
+
     @Override
     public IPage<DeviceRepairDto> queryPage(Page page, DeviceRepairDto deviceRepairDto) {
 
@@ -45,35 +65,132 @@
     }
 
     @Override
-    public AjaxResult saveDeviceRepair(DeviceRepair deviceRepair) {
+    public Long saveDeviceRepair(DeviceRepair deviceRepair) {
         DeviceLedger byId = deviceLedgerService.getById(deviceRepair.getDeviceLedgerId());
+        if (byId == null) {
+            throw new ServiceException("璁惧鍙拌处涓嶅瓨鍦�");
+        }
         deviceRepair.setDeviceName(byId.getDeviceName());
         deviceRepair.setDeviceModel(byId.getDeviceModel());
-        boolean save = this.save(deviceRepair);
-        if (save){
-            return AjaxResult.success();
+        if (!this.save(deviceRepair)) {
+            throw new ServiceException("娣诲姞澶辫触");
         }
-        return AjaxResult.error();
+        return deviceRepair.getId();
     }
 
     @Override
-    public AjaxResult updateDeviceRepair(DeviceRepair deviceRepair) {
-        if (this.updateById(deviceRepair)) {
-            Long id = deviceRepair.getId();
-            //
-            DeviceDefectRecordDto deviceDefectRecordDto = new DeviceDefectRecordDto();
-            deviceDefectRecordDto.setDeviceLedgerId(id);
-            deviceDefectRecordDto.setStatus("涓ラ噸缂洪櫡");
-            List<DeviceDefectRecordDto> records = deviceDefectRecordService.listPage(new Page<>(1, -1), deviceDefectRecordDto).getRecords();
-            if (!records.isEmpty()){
-                records.forEach(deviceDefectRecord -> {
-                    deviceDefectRecord.setStatus("姝e父");
-                    deviceDefectRecordService.updateByDDR(deviceDefectRecord);
-                });
-            }
-            return AjaxResult.success();
+    public void updateDeviceRepair(DeviceRepair deviceRepair) {
+        DeviceRepair existing = this.getById(deviceRepair.getId());
+        if (existing == null) {
+            throw new ServiceException("鎶ヤ慨鍗曚笉瀛樺湪");
         }
-        return AjaxResult.error();
+        if (!Objects.equals(existing.getStatus(), 0)) {
+            throw new ServiceException("浠呭緟缁翠慨鐘舵�佸彲缂栬緫");
+        }
+        if (!this.updateById(deviceRepair)) {
+            throw new ServiceException("淇敼澶辫触");
+        }
+        clearDefectRecordsIfNeeded(deviceRepair.getId());
+    }
+
+    @Override
+    public void submitDeviceMaintain(DeviceRepair deviceRepair) {
+        if (deviceRepair == null || deviceRepair.getId() == null) {
+            throw new ServiceException("鎶ヤ慨鍗旾D涓嶈兘涓虹┖");
+        }
+        DeviceRepair existing = this.getById(deviceRepair.getId());
+        if (existing == null) {
+            throw new ServiceException("鎶ヤ慨鍗曚笉瀛樺湪");
+        }
+        if (!Objects.equals(existing.getStatus(), 0)) {
+            throw new ServiceException("浠呭緟缁翠慨鐘舵�佸彲鎻愪氦缁翠慨");
+        }
+        if (StringUtils.isEmpty(deviceRepair.getMaintenanceResult())) {
+            throw new ServiceException("璇峰~鍐欑淮淇粨鏋�");
+        }
+        String loginNick = SecurityUtils.getLoginUser().getNickName();
+        if (StringUtils.isEmpty(existing.getMaintenanceName())) {
+            throw new ServiceException("鏈寚瀹氱淮淇汉锛屾棤娉曠淮淇�");
+        }
+        if (!Objects.equals(existing.getMaintenanceName(), loginNick)) {
+            throw new ServiceException("浠呮寚瀹氱殑缁翠慨浜哄彲杩涜缁翠慨");
+        }
+        existing.setMaintenanceResult(deviceRepair.getMaintenanceResult());
+        existing.setMaintenanceTime(deviceRepair.getMaintenanceTime() != null
+                ? deviceRepair.getMaintenanceTime() : LocalDateTime.now());
+        // 缁翠慨澶辫触=2锛屽惁鍒欒繘鍏ュ緟楠屾敹=3
+        if (Objects.equals(deviceRepair.getStatus(), 2)) {
+            existing.setStatus(2);
+        } else {
+            existing.setStatus(3);
+        }
+        if (!this.updateById(existing)) {
+            throw new ServiceException("鎻愪氦缁翠慨澶辫触");
+        }
+        clearDefectRecordsIfNeeded(existing.getId());
+    }
+
+    @Override
+    public void acceptDeviceRepair(DeviceRepair deviceRepair) {
+        if (deviceRepair == null || deviceRepair.getId() == null) {
+            throw new ServiceException("鎶ヤ慨鍗旾D涓嶈兘涓虹┖");
+        }
+        DeviceRepair existing = this.getById(deviceRepair.getId());
+        if (existing == null) {
+            throw new ServiceException("鎶ヤ慨鍗曚笉瀛樺湪");
+        }
+        if (!Objects.equals(existing.getStatus(), 3)) {
+            throw new ServiceException("褰撳墠鐘舵�佷笉鍙獙鏀�");
+        }
+        String loginNick = SecurityUtils.getLoginUser().getNickName();
+        if (StringUtils.isEmpty(existing.getAcceptanceName())) {
+            throw new ServiceException("鏈寚瀹氶獙鏀朵汉锛屾棤娉曢獙鏀�");
+        }
+        if (!Objects.equals(existing.getAcceptanceName(), loginNick)) {
+            throw new ServiceException("浠呮寚瀹氱殑楠屾敹浜哄彲杩涜楠屾敹");
+        }
+        if (StringUtils.isEmpty(deviceRepair.getAcceptanceRemark())) {
+            throw new ServiceException("璇峰~鍐欓獙鏀跺娉�");
+        }
+        existing.setAcceptanceTime(deviceRepair.getAcceptanceTime() != null
+                ? deviceRepair.getAcceptanceTime() : LocalDateTime.now());
+        existing.setAcceptanceRemark(deviceRepair.getAcceptanceRemark());
+        existing.setStatus(1);
+        if (!this.updateById(existing)) {
+            throw new ServiceException("楠屾敹澶辫触");
+        }
+        clearDefectRecordsIfNeeded(existing.getId());
+    }
+
+    private void clearDefectRecordsIfNeeded(Long repairId) {
+        DeviceDefectRecordDto deviceDefectRecordDto = new DeviceDefectRecordDto();
+        deviceDefectRecordDto.setDeviceLedgerId(repairId);
+        deviceDefectRecordDto.setStatus("涓ラ噸缂洪櫡");
+        List<DeviceDefectRecordDto> records = deviceDefectRecordService.listPage(new Page<>(1, -1), deviceDefectRecordDto).getRecords();
+        if (!records.isEmpty()) {
+            records.forEach(deviceDefectRecord -> {
+                deviceDefectRecord.setStatus("姝e父");
+                deviceDefectRecordService.updateByDDR(deviceDefectRecord);
+            });
+        }
+    }
+
+    private static String statusLabel(Integer status) {
+        if (status == null) {
+            return "";
+        }
+        switch (status) {
+            case 0:
+                return "寰呯淮淇�";
+            case 1:
+                return "瀹屾垚";
+            case 2:
+                return "缁翠慨澶辫触";
+            case 3:
+                return "寰呴獙鏀�";
+            default:
+                return "";
+        }
     }
 
     @Override
@@ -83,24 +200,24 @@
             ArrayList<DeviceRepairExeclDto> deviceLedgerExeclDtos = new ArrayList<>();
             supplierManageList.stream().forEach(deviceRepair -> {
                 DeviceRepairExeclDto deviceRepairExeclDto = new DeviceRepairExeclDto();
-                BeanUtils.copyProperties(deviceRepair,deviceRepairExeclDto);
-                deviceRepairExeclDto.setStatusStr(deviceRepair.getStatus() == 0 ? "寰呯淮淇�" : "瀹岀粨");
+                BeanUtils.copyProperties(deviceRepair, deviceRepairExeclDto);
+                deviceRepairExeclDto.setStatusStr(statusLabel(deviceRepair.getStatus()));
 
                 deviceLedgerExeclDtos.add(deviceRepairExeclDto);
             });
             ExcelUtil<DeviceRepairExeclDto> util = new ExcelUtil<DeviceRepairExeclDto>(DeviceRepairExeclDto.class);
             util.exportExcel(response, deviceLedgerExeclDtos, "璁惧鎶ヤ慨瀵煎嚭");
-        }else {
+        } else {
             ArrayList<Long> arrayList = new ArrayList<>();
             Arrays.stream(ids).map(id -> {
-                return arrayList.add( id);
+                return arrayList.add(id);
             });
             List<DeviceRepair> supplierManageList = deviceRepairMapper.selectBatchIds(arrayList);
             ArrayList<DeviceRepairExeclDto> deviceLedgerExeclDtos = new ArrayList<>();
             supplierManageList.stream().forEach(deviceRepair -> {
                 DeviceRepairExeclDto deviceRepairExeclDto = new DeviceRepairExeclDto();
-                BeanUtils.copyProperties(deviceRepair,deviceRepairExeclDto);
-                deviceRepairExeclDto.setStatusStr(deviceRepair.getStatus() == 0 ? "寰呯淮淇�" : "瀹岀粨");
+                BeanUtils.copyProperties(deviceRepair, deviceRepairExeclDto);
+                deviceRepairExeclDto.setStatusStr(statusLabel(deviceRepair.getStatus()));
 
                 deviceLedgerExeclDtos.add(deviceRepairExeclDto);
             });
@@ -116,4 +233,90 @@
         return deviceRepairMapper.detailById(id);
     }
 
+    @Override
+    public void uploadFile(MultipartFile file, Long deviceRepairId, Integer fileType) {
+        if (file == null || file.isEmpty()) {
+            throw new ServiceException("涓婁紶澶辫触,鏂囦欢涓嶈兘涓虹┖");
+        }
+        if (deviceRepairId == null || deviceRepairId <= 0) {
+            throw new ServiceException("涓婁紶澶辫触,璁惧鎶ヤ慨ID涓嶈兘涓虹┖");
+        }
+        int resolvedType = resolveRepairFileType(fileType);
+
+        try {
+            tempFileService.uploadByCommon(file, resolvedType, deviceRepairId);
+        } catch (Exception e) {
+            throw new ServiceException("涓婁紶澶辫触," + e.getMessage());
+        }
+    }
+
+    /** 璁惧鎶ヤ慨闄勪欢绫诲瀷锛�14璁惧闂 15缁翠慨瀹屾垚锛�13涓哄巻鍙茬被鍨嬪綊鍏ヨ澶囬棶棰� */
+    private int resolveRepairFileType(Integer fileType) {
+        if (fileType == null) {
+            return FileNameType.EQUIPMENT_REPAIR_PROBLEM.getValue();
+        }
+        if (Objects.equals(fileType, FileNameType.EQUIPMENT_REPAIR_PROBLEM.getValue())
+                || Objects.equals(fileType, FileNameType.EQUIPMENT_REPAIR_MAINTAIN.getValue())
+                || Objects.equals(fileType, FileNameType.EQUIPMENT_WARRANTY.getValue())) {
+            return fileType;
+        }
+        throw new ServiceException("涓婁紶澶辫触,闄勪欢绫诲瀷鏃犳晥");
+    }
+
+    private List<Integer> repairFileTypes() {
+        return Arrays.asList(
+                FileNameType.EQUIPMENT_REPAIR_PROBLEM.getValue(),
+                FileNameType.EQUIPMENT_REPAIR_MAINTAIN.getValue(),
+                FileNameType.EQUIPMENT_WARRANTY.getValue()
+        );
+    }
+
+    @Override
+    public List<CommonFile> getFiles(Long deviceRepairId) {
+        if (deviceRepairId == null || deviceRepairId <= 0) {
+            return Collections.emptyList();
+        }
+
+        return commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>()
+                .eq(CommonFile::getCommonId, deviceRepairId)
+                .in(CommonFile::getType, repairFileTypes())
+                .orderByAsc(CommonFile::getId));
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void deleteFile(Long fileId) {
+        if (fileId == null || fileId <= 0) {
+            throw new ServiceException("鍒犻櫎澶辫触,浼犲叆鏁版嵁寮傚父");
+        }
+        CommonFile commonFile = commonFileMapper.selectById(fileId);
+        if (commonFile == null) {
+            throw new ServiceException("鍒犻櫎澶辫触,鏂囦欢涓嶅瓨鍦�");
+        }
+        try {
+            if (commonFile.getUrl() != null && !commonFile.getUrl().isEmpty()) {
+                Files.deleteIfExists(Paths.get(commonFile.getUrl()));
+            }
+        } catch (Exception e) {
+            log.error("璁惧淇濅慨鏂囦欢鍒犻櫎澶辫触: {}", e.getMessage());
+            throw new ServiceException("鍒犻櫎澶辫触," + e.getMessage());
+        }
+        commonFileMapper.deleteById(fileId);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public boolean deleteRepairAndFiles(Collection<Long> ids) {
+        if (ids == null || ids.isEmpty()) {
+            throw new ServiceException("鍒犻櫎澶辫触,璇蜂紶鍏ヨ鍒犻櫎鐨勬暟鎹�");
+        }
+
+        List<CommonFile> fileList = commonFileMapper.selectList(new LambdaQueryWrapper<CommonFile>()
+                .in(CommonFile::getCommonId, ids)
+                .in(CommonFile::getType, repairFileTypes()));
+        for (CommonFile commonFile : fileList) {
+            deleteFile(commonFile.getId());
+        }
+        return this.removeBatchByIds(ids);
+    }
 }

--
Gitblit v1.9.3