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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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.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.DeviceMaintenancePlanDetailsDto;
import com.yuanchu.mom.dto.DeviceMaintenancePlanDto;
import com.yuanchu.mom.mapper.DeviceMaintenancePlanDetailsMapper;
import com.yuanchu.mom.mapper.DeviceMaintenancePlanMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.DeviceMaintenancePlan;
import com.yuanchu.mom.pojo.DeviceMaintenancePlanDetails;
import com.yuanchu.mom.pojo.User;
import com.yuanchu.mom.service.DeviceMaintenancePlanDetailsService;
import com.yuanchu.mom.service.DeviceMaintenancePlanService;
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.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 设备保养计划表 服务实现类
 * </p>
 *
 * @author 江苏鵷雏网络科技有限公司
 * @since 2024-12-16 06:10:52
 */
@Service
public class DeviceMaintenancePlanServiceImpl extends ServiceImpl<DeviceMaintenancePlanMapper, DeviceMaintenancePlan> implements DeviceMaintenancePlanService {
 
    @Resource
    private DeviceMaintenancePlanDetailsService deviceMaintenancePlanDetailsService;
 
    @Resource
    private DeviceMaintenancePlanDetailsMapper deviceMaintenancePlanDetailsMapper;
 
    @Resource
    private GetLook getLook;
 
    @Resource
    private UserMapper userMapper;
 
    /**
     * 分页查询设备保养计划
     *
     * @param page
     * @return
     */
    @Override
    public Result<IPage<DeviceMaintenancePlan>> selectDeviceMaintenancePlanByPage(IPage page, DeviceMaintenancePlanDto deviceMaintenancePlanDto) {
        IPage<DeviceMaintenancePlan> iPage = baseMapper.selectDeviceParameterPage(page, QueryWrappers.queryWrappers(deviceMaintenancePlanDto));
        return Result.success(iPage);
    }
 
    /**
     * 新增设备保养计划
     *
     * @param deviceMaintenancePlanDto 设备保养计划
     */
    @Override
    public Result addMaintenancePlan(DeviceMaintenancePlanDto deviceMaintenancePlanDto) {
        Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
        User user = userMapper.selectById(userId);
        deviceMaintenancePlanDto.setCompilerId(userId);
        deviceMaintenancePlanDto.setCompiler(user.getName());
        deviceMaintenancePlanDto.setDatePreparation(LocalDateTime.now());
 
        // 查询审核人id
        if (deviceMaintenancePlanDto.getAuditId() != null) {
            User auditUser = userMapper.selectById(deviceMaintenancePlanDto.getAuditId());
            deviceMaintenancePlanDto.setAudit(auditUser.getName());
        }
        this.saveOrUpdate(deviceMaintenancePlanDto);
 
        // 详情赋值并保存
        List<DeviceMaintenancePlanDetailsDto> deviceMaintenancePlanDetails = deviceMaintenancePlanDto.getDeviceMaintenancePlanDetails();
        if (CollectionUtils.isNotEmpty(deviceMaintenancePlanDetails)) { // 详情不为空
            List<DeviceMaintenancePlanDetails> collect = deviceMaintenancePlanDetails.stream().map(deviceMaintenancePlanDetail -> { // 遍历详情
                deviceMaintenancePlanDetail.setMaintenancePlanId(deviceMaintenancePlanDto.getMaintenancePlanId()); // 保养计划ID
                DeviceMaintenancePlanDetails planDetails = new DeviceMaintenancePlanDetails();
                BeanUtils.copyProperties(deviceMaintenancePlanDetail, planDetails);
                return planDetails;
            }).collect(Collectors.toList());
            deviceMaintenancePlanDetailsService.saveBatch(collect);
        }
 
        return Result.success();
    }
 
    /**
     * 修改设备保养计划
     * @param deviceMaintenancePlanDto 设备保养计划
     */
    @Override
    public Result updateMaintenancePlan(DeviceMaintenancePlanDto deviceMaintenancePlanDto) {
        // 查询审核人id
        if (deviceMaintenancePlanDto.getAuditId() != null) {
            User auditUser = userMapper.selectById(deviceMaintenancePlanDto.getAuditId());
            deviceMaintenancePlanDto.setAudit(auditUser.getName());
        }
        this.saveOrUpdate(deviceMaintenancePlanDto);
 
        // 删除原本的详情
        deviceMaintenancePlanDetailsService.remove(Wrappers.<DeviceMaintenancePlanDetails>lambdaQuery().eq(DeviceMaintenancePlanDetails::getMaintenancePlanId, deviceMaintenancePlanDto.getMaintenancePlanId()));
        // 详情赋值并保存
        List<DeviceMaintenancePlanDetailsDto> deviceMaintenancePlanDetails = deviceMaintenancePlanDto.getDeviceMaintenancePlanDetails();
        if (CollectionUtils.isNotEmpty(deviceMaintenancePlanDetails)) { // 详情不为空
            List<DeviceMaintenancePlanDetails> collect = deviceMaintenancePlanDetails.stream().map(deviceMaintenancePlanDetail -> { // 遍历详情
                deviceMaintenancePlanDetail.setDeviceId(deviceMaintenancePlanDto.getDeviceId()); // 设备ID
                deviceMaintenancePlanDetail.setMaintenancePlanId(deviceMaintenancePlanDto.getMaintenancePlanId()); // 保养计划ID
                DeviceMaintenancePlanDetails planDetails = new DeviceMaintenancePlanDetails();
                BeanUtils.copyProperties(deviceMaintenancePlanDetail, planDetails);
                return planDetails;
            }).collect(Collectors.toList());
            deviceMaintenancePlanDetailsService.saveBatch(collect);
        }
        return Result.success();
    }
 
    /**
     * 删除设备保养计划
     *
     * @param deviceMaintenancePlanDto 设备保养计划
     */
    @Override
    public Result deleteMaintenancePlan(DeviceMaintenancePlanDto deviceMaintenancePlanDto) {
        this.removeById(deviceMaintenancePlanDto);
        deviceMaintenancePlanDetailsService.remove(Wrappers.<DeviceMaintenancePlanDetails>lambdaQuery().eq(DeviceMaintenancePlanDetails::getMaintenancePlanId, deviceMaintenancePlanDto.getMaintenancePlanId()));
        return Result.success();
    }
 
    /**
     * 导出设备保养计划
     *
     * @param maintenancePlanId 设备保养计划id
     * @param response          响应
     */
    @Override
    public Result exportDeviceMaintenancePlanDto(Integer maintenancePlanId, HttpServletResponse response) {
        // 查询设备保养计划
        DeviceMaintenancePlanDto deviceMaintenancePlan = baseMapper.selectMaintenancePlanById(maintenancePlanId);
 
        // 查询设备保养计划详情
        List<DeviceMaintenancePlanDetailsDto> deviceMaintenancePlanDetailsDtoList = deviceMaintenancePlanDetailsMapper.deviceInspectionRecordDetailsList(maintenancePlanId);
        // 设置序号
        deviceMaintenancePlanDetailsDtoList.forEach(deviceInspectionRecordDetails -> {
            deviceInspectionRecordDetails.setIndex(deviceMaintenancePlanDetailsDtoList.indexOf(deviceInspectionRecordDetails) + 1);
        });
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/word/maintenance-plan.docx");
        Configure configure = Configure.builder()
                .bind("deviceMaintenancePlanDetailsDtoList", new HackLoopTableRenderPolicy())
                .build();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("deviceMaintenancePlan", deviceMaintenancePlan);
                    put("deviceMaintenancePlanDetailsDtoList", deviceMaintenancePlanDetailsDtoList);
                    // 编制人签名地址
                    put("compilerUrl", UserUtils.getFinalUserSignatureUrl(deviceMaintenancePlan.getCompilerId()));
                    // 审核人签名地址
                    put("auditUrl", UserUtils.getFinalUserSignatureUrl(deviceMaintenancePlan.getAuditId()));
                }});
 
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    "设备保养计划表", "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();
    }
 
    /**
     * 查询设备保养计划详情
     *
     * @param maintenancePlanId 设备保养计划id
     */
    @Override
    public Result<DeviceMaintenancePlanDto> getMaintenancePlanDetail(Integer maintenancePlanId) {
        // 查询设备保养计划
        DeviceMaintenancePlan deviceMaintenancePlan = baseMapper.selectById(maintenancePlanId);
        // 查询详情
        DeviceMaintenancePlanDto deviceMaintenancePlanDto = new DeviceMaintenancePlanDto();
        BeanUtils.copyProperties(deviceMaintenancePlan, deviceMaintenancePlanDto);
        deviceMaintenancePlanDto.setDeviceMaintenancePlanDetails(deviceMaintenancePlanDetailsMapper.deviceInspectionRecordDetailsList(maintenancePlanId));
        return Result.success(deviceMaintenancePlanDto);
    }
 
    /**
     * 审核设备保养计划
     *
     * @param deviceMaintenancePlanDto 设备保养计划
     */
    @Override
    public Result reviewMaintenancePlanStatus(DeviceMaintenancePlanDto deviceMaintenancePlanDto) {
        LambdaUpdateWrapper<DeviceMaintenancePlan> wrapper = Wrappers.<DeviceMaintenancePlan>lambdaUpdate()
                .eq(DeviceMaintenancePlan::getMaintenancePlanId, deviceMaintenancePlanDto.getMaintenancePlanId())
                .set(DeviceMaintenancePlan::getStatus, deviceMaintenancePlanDto.getStatus())        // 审核状态
                .set(DeviceMaintenancePlan::getAuditRemark, deviceMaintenancePlanDto.getAuditRemark());// 审核备注
 
        // 为0清除审核人
        if (deviceMaintenancePlanDto.getStatus().equals(0)) {
            wrapper.set(DeviceMaintenancePlan::getAuditId, null)
                    .set(DeviceMaintenancePlan::getAudit, null);
        }
        this.update(wrapper); // 更新
        return Result.success();
    }
 
 
}