IDeviceServiceImpl.java
5.4 KB
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
package vion.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vion.dto.DeviceDTO;
import vion.mapper.DeviceMapper;
import vion.model.Contract;
import vion.model.Device;
import vion.model.DeviceLog;
import vion.service.IDeviceLogService;
import vion.service.IDeviceService;
import vion.vo.DeviceVO;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author vion
* @date 2024/8/12
*/
@Service
@RequiredArgsConstructor
public class IDeviceServiceImpl extends MPJBaseServiceImpl<DeviceMapper, Device> implements IDeviceService {
private final IDeviceLogService deviceLogService;
private final Converter converter;
@Override
public Page<DeviceVO> list(DeviceDTO dto) {
var wrapper = new MPJLambdaWrapper<>(converter.convert(dto, Device.class))
.selectAll(Device.class)
.selectAs(Contract::getName, DeviceVO::getContractName)
.leftJoin(Contract.class, Contract::getContractNo, Device::getContractNo)
.like(StrUtil.isNotBlank(dto.getContractName()), Contract::getName, dto.getContractName())
.orderByDesc(Device::getRecDate);
return this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), DeviceVO.class, wrapper);
}
@Override
public DeviceVO getById(Long id) {
var wrapper = new MPJLambdaWrapper<Device>()
.selectAll(Device.class)
.selectAs(Contract::getName, DeviceVO::getContractName)
.selectCollection(DeviceLog.class, DeviceVO::getLogList)
.leftJoin(DeviceLog.class, DeviceLog::getDeviceId, Device::getId)
.leftJoin(Contract.class, Contract::getContractNo, Device::getContractNo)
.orderByDesc(Device::getRecDate);
return this.selectJoinOne(DeviceVO.class, wrapper);
}
@Override
@Transactional(rollbackFor = Exception.class)
public String update(List<DeviceDTO> dtoList) {
if (this.updateBatchById(converter.convert(dtoList, Device.class))) {
var logList = dtoList.stream()
.map(dto -> {
var deviceLog = new DeviceLog();
deviceLog.setDeviceId(dto.getId());
deviceLog.setOperator(dto.getUserId());
deviceLog.setContent(getStatusStr(dto.getStatus()));
return deviceLog;
}).toList();
deviceLogService.saveBatch(logList);
return "更新成功";
}
return "更新失败";
}
@Override
@Transactional(rollbackFor = Exception.class)
public String recordDevice(List<DeviceDTO> dtoList) {
if (CollUtil.isEmpty(dtoList)) {
return "设备为空";
}
var statusSet = dtoList.stream().map(DeviceDTO::getStatus).collect(Collectors.toSet());
if (statusSet.size() > 1 || !statusSet.contains(1)) {
return "该操作仅支持设备入库";
}
var userId = dtoList.getFirst().getUserId();
var deviceList = converter.convert(dtoList, Device.class);
if (this.saveBatch(deviceList)) {
var logList = deviceList.stream()
.map(d -> {
var deviceLog = new DeviceLog();
deviceLog.setDeviceId(d.getId());
deviceLog.setOperator(userId);
deviceLog.setContent(getStatusStr(d.getStatus()));
return deviceLog;
}).toList();
deviceLogService.saveBatch(logList);
return "保存成功";
}
return "保存失败";
}
@Override
@Transactional(rollbackFor = Exception.class)
public String circulateBatch(List<DeviceDTO> dtoList) {
if (CollUtil.isEmpty(dtoList)) {
return "设备为空";
}
var statusSet = dtoList.stream().map(DeviceDTO::getStatus).collect(Collectors.toSet());
if (statusSet.size() > 1) {
return "该操作不允许对设备进行不同处理";
}
if (statusSet.contains(1)) {
return "该操作不支持入库操作";
}
var userId = dtoList.getFirst().getUserId();
var deviceList = converter.convert(dtoList, Device.class);
deviceList.forEach(d -> {
this.lambdaUpdate().eq(Device::getSerialNum, d.getSerialNum()).update(d);
var deviceLog = new DeviceLog();
deviceLog.setDeviceId(d.getId());
deviceLog.setOperator(userId);
deviceLog.setContent(getStatusStr(d.getStatus()));
deviceLogService.save(deviceLog);
});
return "保存成功";
}
private String getStatusStr(Integer status) {
return switch (status) {
case 1 -> "设备:入库";
case 2 -> "设备:出库";
case 3 -> "设备:维修";
case 4 -> "设备:借测";
case 5 -> "设备:废弃";
default -> "未知操作";
};
}
}