zss
2025-02-17 087991c76f078defe5eb55d84223021b4199fb3d
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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.dto.DeviceInspectionRecordDto;
import com.yuanchu.mom.mapper.DeviceInspectionRecordMapper;
import com.yuanchu.mom.mapper.DeviceMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.Device;
import com.yuanchu.mom.pojo.DeviceInspectionRecord;
import com.yuanchu.mom.pojo.DeviceInspectionRecordDetails;
import com.yuanchu.mom.pojo.User;
import com.yuanchu.mom.service.DeviceInspectionRecordDetailsService;
import com.yuanchu.mom.service.DeviceInspectionRecordService;
import com.yuanchu.mom.utils.HackLoopTableRenderPolicy;
import com.yuanchu.mom.utils.UserUtils;
import com.yuanchu.mom.utils.QueryWrappers;
import com.yuanchu.mom.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
 
/**
 * <p>
 * 设备点检记录表 服务实现类
 * </p>
 *
 * @author 江苏鵷雏网络科技有限公司
 * @since 2024-12-16 04:25:14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DeviceInspectionRecordServiceImpl extends ServiceImpl<DeviceInspectionRecordMapper, DeviceInspectionRecord> implements DeviceInspectionRecordService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private GetLook getLook;
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private DeviceInspectionRecordDetailsService deviceInspectionRecordDetailsService;
 
    /**
     * 分页查询设备点检记录
     *
     * @param page 当前页码
     */
    @Override
    public Result<IPage<DeviceInspectionRecord>> getDeviceInspectionRecordByPage(IPage page, DeviceInspectionRecordDto deviceInspectionRecordDto) {
        IPage<DeviceInspectionRecord> iPage = baseMapper.selectDeviceParameterPage(page, QueryWrappers.queryWrappers(deviceInspectionRecordDto));
        return Result.success(iPage);
    }
 
 
    /**
     * 查询点检详情
     * @param inspectionRecordId
     * @return
     */
    @Override
    public Result getDeviceInspectionRecord(Integer inspectionRecordId) {
        DeviceInspectionRecord deviceInspectionRecord = baseMapper.selectById(inspectionRecordId);
        DeviceInspectionRecordDto dto = new DeviceInspectionRecordDto();
        BeanUtils.copyProperties(deviceInspectionRecord, dto);
        List<DeviceInspectionRecordDetails> list = deviceInspectionRecordDetailsService.list(Wrappers.<DeviceInspectionRecordDetails>lambdaQuery().eq(DeviceInspectionRecordDetails::getInspectionRecordId, inspectionRecordId));
        dto.setDetails(list);
        return Result.success(dto);
    }
 
    /**
     * 新增设备点检记录
     *
     * @param deviceInspectionRecord 设备点检记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addDeviceInspectionRecord(DeviceInspectionRecordDto deviceInspectionRecord) {
 
        Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
        User user = userMapper.selectById(userId);
        deviceInspectionRecord.setRecorderId(userId);
        deviceInspectionRecord.setRecorder(user.getName());
 
        // 查询复核人id
        if (deviceInspectionRecord.getReviewerId() != null) {
            User reviewUser = userMapper.selectById(deviceInspectionRecord.getReviewerId());
            deviceInspectionRecord.setReviewer(reviewUser.getName());
        }
        this.saveOrUpdate(deviceInspectionRecord);
        if (CollectionUtils.isNotEmpty(deviceInspectionRecord.getDetails())) {
            for (DeviceInspectionRecordDetails detail : deviceInspectionRecord.getDetails()) {
                detail.setInspectionRecordId(deviceInspectionRecord.getInspectionRecordId());
            }
            deviceInspectionRecordDetailsService.saveBatch( deviceInspectionRecord.getDetails());
        }
 
        return Result.success();
    }
 
 
    /**
     * 修改设备点检记录
     *
     * @param deviceInspectionRecord 设备点检记录
     */
    @Override
    public Result updateInspectionRecordAndDetails(DeviceInspectionRecordDto deviceInspectionRecord) {
        // 查询复核人id
        if (deviceInspectionRecord.getReviewerId() != null) {
            User reviewUser = userMapper.selectById(deviceInspectionRecord.getReviewerId());
            deviceInspectionRecord.setReviewer(reviewUser.getName());
        }
        this.saveOrUpdate(deviceInspectionRecord);
        deviceInspectionRecordDetailsService.remove(Wrappers.<DeviceInspectionRecordDetails>lambdaQuery().eq(DeviceInspectionRecordDetails::getInspectionRecordId, deviceInspectionRecord.getInspectionRecordId()));
        if (CollectionUtils.isNotEmpty(deviceInspectionRecord.getDetails())) {
            for (DeviceInspectionRecordDetails detail : deviceInspectionRecord.getDetails()) {
                detail.setInspectionRecordId(deviceInspectionRecord.getInspectionRecordId());
            }
            deviceInspectionRecordDetailsService.saveBatch( deviceInspectionRecord.getDetails());
        }
        return Result.success();
    }
 
    /**
     * 删除设备点检记录
     *
     * @param deviceInspectionRecord 设备点检记录
     */
    @Override
    public Result deleteDeviceInspectionRecordOrDetails(DeviceInspectionRecordDto deviceInspectionRecord) {
        this.removeById(deviceInspectionRecord);
        deviceInspectionRecordDetailsService.remove(Wrappers.<DeviceInspectionRecordDetails>lambdaQuery().eq(DeviceInspectionRecordDetails::getInspectionRecordId, deviceInspectionRecord.getInspectionRecordId()));
        return Result.success();
    }
 
    /**
     * 复核点检记录
     * @param dto
     * @return
     */
    @Override
    public Result reviewDeviceInspectionRecord(DeviceInspectionRecordDto dto) {
        LambdaUpdateWrapper<DeviceInspectionRecord> wrapper = Wrappers.<DeviceInspectionRecord>lambdaUpdate()
                .eq(DeviceInspectionRecord::getInspectionRecordId, dto.getInspectionRecordId())
                .set(DeviceInspectionRecord::getStatus, dto.getStatus())
                .set(DeviceInspectionRecord::getReviewerRemark, dto.getReviewerRemark());
 
        // 为0清除审核人
        if (dto.getStatus().equals(0)) {
            wrapper.set(DeviceInspectionRecord::getReviewerId, null)
                    .set(DeviceInspectionRecord::getReviewer, null);
        }
        this.update(wrapper);
 
        return Result.success();
    }
 
    /**
     * 导出设备点检记录
     *
     * @param deviceInspectionRecordId 设备点检记录id
     * @param response                 响应
     */
    @Override
    public Result exportDeviceInspectionRecord(Integer deviceInspectionRecordId, HttpServletResponse response) {
        DeviceInspectionRecord deviceInspectionRecord = baseMapper.selectById(deviceInspectionRecordId);
        DeviceInspectionRecordDto deviceInspectionRecordDto = new DeviceInspectionRecordDto();
        BeanUtils.copyProperties(deviceInspectionRecord, deviceInspectionRecordDto);
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        deviceInspectionRecordDto.setTestDateString(deviceInspectionRecord.getTestDate() == null ? null : deviceInspectionRecord.getTestDate().format(dateFormatter));
 
        List<DeviceInspectionRecordDetails> deviceInspectionRecordDetailsList = deviceInspectionRecordDetailsService.list(Wrappers.<DeviceInspectionRecordDetails>lambdaQuery().eq(DeviceInspectionRecordDetails::getInspectionRecordId, deviceInspectionRecordId));
 
        Integer deviceId = deviceInspectionRecord.getDeviceId();
 
        Device device = deviceMapper.selectById(deviceId);
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/word/device-inspection-record.docx");
        Configure configure = Configure.builder()
                .bind("deviceInspectionRecordDetailsList", new HackLoopTableRenderPolicy())
                .build();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("deviceInspectionRecord", deviceInspectionRecordDto);
                    put("deviceInspectionRecordDetailsList", deviceInspectionRecordDetailsList);
                    put("device", device);
                    put("recorderUrl", UserUtils.getFinalUserSignatureUrl(deviceInspectionRecordDto.getRecorderId()));
                    put("reviewerUrl", UserUtils.getFinalUserSignatureUrl(deviceInspectionRecordDto.getReviewerId()));
                }});
 
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    device.getDeviceName() + "点检记录", "UTF-8");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName + ".docx");
            OutputStream os = response.getOutputStream();
            template.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
        return Result.success();
    }
}