hsy
2026-08-28 a8e4132c3e2e9c3b3fcb0360901b35571b6464ea
src/main/java/com/ruoyi/device/service/impl/DeviceRepairServiceImpl.java
@@ -6,6 +6,7 @@
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.ruoyi.basic.enums.RecordTypeEnum;
import com.ruoyi.basic.utils.FileUtil;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.device.dto.DeviceDefectRecordDto;
@@ -21,6 +22,11 @@
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.measuringinstrumentledger.mapper.SparePartsMapper;
import com.ruoyi.measuringinstrumentledger.pojo.SpareParts;
import com.ruoyi.approve.pojo.KnowledgeBase;
import com.ruoyi.approve.service.KnowledgeBaseService;
import com.ruoyi.device.pojo.DeviceRepairSparePart;
import com.ruoyi.device.service.IDeviceRepairSparePartService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.measuringinstrumentledger.pojo.SparePartsRequisitionRecord;
import com.ruoyi.measuringinstrumentledger.service.SparePartsRequisitionRecordService;
import jakarta.servlet.http.HttpServletResponse;
@@ -45,6 +51,8 @@
    private final IDeviceLedgerService deviceLedgerService;
    private final SparePartsMapper sparePartsMapper;
    private final SparePartsRequisitionRecordService sparePartsRequisitionRecordService;
    private final IDeviceRepairSparePartService deviceRepairSparePartService;
    private final KnowledgeBaseService knowledgeBaseService;
    private final FileUtil fileUtil;
    private static final int STATUS_PENDING_REPAIR = 0;
@@ -92,6 +100,11 @@
                || oldDeviceRepair.getStatus() != STATUS_COMPLETED)) {
            return AjaxResult.error("请先提交验收审批,验收通过后才可完结");
        }
        // 清理旧的备件使用记录,防止重复累计
        deviceRepairSparePartService.remove(new QueryWrapper<DeviceRepairSparePart>()
                .eq("source_id", deviceRepairDto.getId())
                .eq("source_type", 1));
        // 处理备件使用情况
        if (CollectionUtils.isNotEmpty(deviceRepairDto.getSparePartsUseList())) {
            List<Long> sparePartIds = new ArrayList<>();
@@ -114,6 +127,14 @@
                        record.setSparePartsId(sparePartUse.getId());
                        record.setQuantity(sparePartUse.getQuantity());
                        sparePartsRequisitionRecordService.save(record);
                        // 插入记录到 device_repair_spare_part
                        DeviceRepairSparePart partRecord = new DeviceRepairSparePart();
                        partRecord.setSourceId(deviceRepairDto.getId());
                        partRecord.setSourceType(1); // 1 报修
                        partRecord.setSparePartId(sparePartUse.getId());
                        partRecord.setQuantity(sparePartUse.getQuantity());
                        deviceRepairSparePartService.save(partRecord);
                    } else {
                        return AjaxResult.error("备件 " + spareParts.getName() + " 数量不足");
                    }
@@ -194,6 +215,47 @@
        update.setAcceptanceRemark(deviceRepairDto.getAcceptanceRemark());
        update.setStatus(STATUS_COMPLETED);
        if (this.updateById(update)) {
            try {
                DeviceLedger deviceLedger = deviceLedgerService.getById(oldDeviceRepair.getDeviceLedgerId());
                List<DeviceRepairSparePart> parts = deviceRepairSparePartService.getListWithPartName(deviceRepairDto.getId(), 1);
                String sparePartsInfo = "";
                if (CollectionUtils.isNotEmpty(parts)) {
                    StringBuilder sb = new StringBuilder();
                    for (DeviceRepairSparePart p : parts) {
                        sb.append(p.getSparePartName()).append("(").append(p.getQuantity()).append("个) ");
                    }
                    sparePartsInfo = " 配件使用情况: " + sb.toString();
                }
                KnowledgeBase kb = new KnowledgeBase();
                String typeName = deviceLedger.getType();
                String deviceModel = deviceLedger.getDeviceModel();
                String repairTypeStr = oldDeviceRepair.getRepairType();
                String repairTypeLabel = DictUtils.getDictLabel("device_repair_type", repairTypeStr);
                if (StringUtils.isBlank(repairTypeLabel)) {
                    repairTypeLabel = "维修";
                }
                kb.setTitle((typeName != null ? typeName : "设备") + "_" + (deviceModel != null ? deviceModel : "") + " 的 " + repairTypeLabel + "记录");
                kb.setScenario("设备维修");
                kb.setType("解决方案");
                String remarkStr = oldDeviceRepair.getRemark() == null ? "" : oldDeviceRepair.getRemark();
                kb.setProblem(String.format("设备号: %s, 设备名称: %s, 故障描述: %s",
                        deviceLedger.getUniqueCode(), deviceLedger.getDeviceName(),
                        remarkStr));
                String reasonStr = oldDeviceRepair.getReasonAnalysis() == null ? "" : oldDeviceRepair.getReasonAnalysis();
                String handlingStr = oldDeviceRepair.getHandlingMeasures() == null ? "" : oldDeviceRepair.getHandlingMeasures();
                kb.setSolution(String.format("原因分析: %s. 处理措施: %s.%s",
                        reasonStr, handlingStr, sparePartsInfo));
                kb.setCreator(oldDeviceRepair.getMaintenanceName());
                knowledgeBaseService.save(kb);
            } catch(Exception e) {
                log.error("保存知识库失败", e);
            }
            return AjaxResult.success();
        }
        return AjaxResult.error("验收审批失败");
@@ -251,6 +313,8 @@
    public DeviceRepairVo detailById(Long id) {
        DeviceRepairVo vo = deviceRepairMapper.detailById(id);
        vo.setStorageBlobVOs(fileUtil.getStorageBlobVOsByRecordTypeAndRecordId(RecordTypeEnum.DEVICE_REPAIR, id));
        List<DeviceRepairSparePart> parts = deviceRepairSparePartService.getListWithPartName(id, 1);
        vo.setSparePartsUseListDetails(parts);
        return vo;
    }