gongchunyi
5 天以前 089964a497c2528e88ddc610af5f88f631303431
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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.DeviceRepairMapper;
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.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
@Slf4j
public class DeviceRepairServiceImpl extends ServiceImpl<DeviceRepairMapper, DeviceRepair> implements IDeviceRepairService {
 
    @Autowired
    private DeviceDefectRecordService deviceDefectRecordService;
    @Autowired
    private DeviceRepairMapper deviceRepairMapper;
    @Autowired
    private IDeviceLedgerService deviceLedgerService;
 
    @Autowired
    private CommonFileMapper commonFileMapper;
 
    @Autowired
    private TempFileService tempFileService;
 
    @Override
    public IPage<DeviceRepairDto> queryPage(Page page, DeviceRepairDto deviceRepairDto) {
 
        return deviceRepairMapper.queryPage(page, deviceRepairDto);
    }
 
    @Override
    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());
        if (!this.save(deviceRepair)) {
            throw new ServiceException("添加失败");
        }
        return deviceRepair.getId();
    }
 
    @Override
    public void updateDeviceRepair(DeviceRepair deviceRepair) {
        DeviceRepair existing = this.getById(deviceRepair.getId());
        if (existing == null) {
            throw new ServiceException("报修单不存在");
        }
        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("报修单ID不能为空");
        }
        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("报修单ID不能为空");
        }
        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("正常");
                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
    public void export(HttpServletResponse response, Long[] ids) {
        if (ids == null || ids.length == 0) {
            List<DeviceRepair> supplierManageList = this.list();
            ArrayList<DeviceRepairExeclDto> deviceLedgerExeclDtos = new ArrayList<>();
            supplierManageList.stream().forEach(deviceRepair -> {
                DeviceRepairExeclDto deviceRepairExeclDto = new DeviceRepairExeclDto();
                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 {
            ArrayList<Long> arrayList = new ArrayList<>();
            Arrays.stream(ids).map(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(statusLabel(deviceRepair.getStatus()));
 
                deviceLedgerExeclDtos.add(deviceRepairExeclDto);
            });
            ExcelUtil<DeviceRepairExeclDto> util = new ExcelUtil<DeviceRepairExeclDto>(DeviceRepairExeclDto.class);
            util.exportExcel(response, deviceLedgerExeclDtos, "设备报修导出");
        }
 
    }
 
    @Override
    public DeviceRepairDto detailById(Long id) {
 
        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);
    }
}