吴羡
4 小时以前 2f07589b54d56085af77b50d580890cd4130b444
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
package com.ruoyi.device.service.impl;
 
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.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.device.dto.DeviceBreakdownMaintenance;
import com.ruoyi.device.mapper.DeviceBreakdownMaintenanceMapper;
import com.ruoyi.device.mapper.DeviceMapper;
import com.ruoyi.device.pojo.Device;
import com.ruoyi.device.service.DeviceBreakdownMaintenanceService;
import com.ruoyi.device.util.DeviceFlowPermissionUtils;
import com.ruoyi.inspect.util.UserUtils;
import com.ruoyi.system.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.HashMap;
 
/**
 * <p>
 * 设备故障维修表 服务实现类
 * </p>
 *
 * @author 江苏鵷雏网络科技有限公司
 * @since 2024-12-17 04:50:57
 */
@Service
public class DeviceBreakdownMaintenanceServiceImpl extends ServiceImpl<DeviceBreakdownMaintenanceMapper, DeviceBreakdownMaintenance> implements DeviceBreakdownMaintenanceService {
 
    @Resource
    private UserMapper userMapper;
    @Resource
    private DeviceMapper deivceMapper;
 
    /**
     * 设备故障维修列表
     * @param page
     * @param deviceBreakdownMaintenance
     * @return
     */
    @Override
    public IPage<DeviceBreakdownMaintenance> pageDeviceBreakdownMaintenance(Page page, DeviceBreakdownMaintenance deviceBreakdownMaintenance) {
        return baseMapper.pageDeviceBreakdownMaintenance(page, QueryWrappers.queryWrappers(deviceBreakdownMaintenance));
    }
 
    /**
     * 新增设备故障维修
     * @param deviceBreakdownMaintenance
     * @return
     */
    @Override
    public boolean addDeviceBreakdownMaintenance(DeviceBreakdownMaintenance deviceBreakdownMaintenance) {
        DeviceBreakdownMaintenance breakdownMaintenance = new DeviceBreakdownMaintenance();
        //当前登录用户
        User user = userMapper.selectById(SecurityUtils.getUserId().intValue());
        checkFlowPermission(deviceBreakdownMaintenance, user);
 
        switch (deviceBreakdownMaintenance.getFlowType()) {
            case 0:
                BeanUtils.copyProperties(deviceBreakdownMaintenance, breakdownMaintenance);
                // 申请
                breakdownMaintenance.setDamageOrMalfunction(deviceBreakdownMaintenance.getDamageOrMalfunction());
                breakdownMaintenance.setApplicantUserId(user.getId());
                breakdownMaintenance.setApplicantUser(user.getName());
                breakdownMaintenance.setRepairDate(deviceBreakdownMaintenance.getRepairDate());
 
                baseMapper.insert(breakdownMaintenance);
                break;
            case 1:
                breakdownMaintenance.setMaintenanceId(deviceBreakdownMaintenance.getMaintenanceId());
                // 申请部门负责人意见
                breakdownMaintenance.setDepartmentHeadOpinion(deviceBreakdownMaintenance.getDepartmentHeadOpinion());
                breakdownMaintenance.setDepartmentHeadDate(LocalDate.now());
 
                baseMapper.updateById(breakdownMaintenance);
                break;
            case 2:
                breakdownMaintenance.setMaintenanceId(deviceBreakdownMaintenance.getMaintenanceId());
                // 计量室意见
                breakdownMaintenance.setMaintenanceRecord(deviceBreakdownMaintenance.getMaintenanceRecord());
                breakdownMaintenance.setMaintenanceUser(deviceBreakdownMaintenance.getMaintenanceUser());
                breakdownMaintenance.setMaintenanceDate(deviceBreakdownMaintenance.getMaintenanceDate());
 
                breakdownMaintenance.setIsFinish(1);
 
                baseMapper.updateById(breakdownMaintenance);
                break;
        }
        return true;
    }
 
    private void checkFlowPermission(DeviceBreakdownMaintenance deviceBreakdownMaintenance, User user) {
        Integer flowType = deviceBreakdownMaintenance.getFlowType();
        Integer maintenanceId = deviceBreakdownMaintenance.getMaintenanceId();
        if (flowType != null && flowType > 0) {
            DeviceBreakdownMaintenance existing = baseMapper.selectById(maintenanceId);
            if (existing == null) {
                throw new ServiceException("单据不存在");
            }
            DeviceFlowPermissionUtils.checkFinished(existing.getIsFinish());
            Integer expectedUserId;
            switch (flowType) {
                case 1:
                    expectedUserId = existing.getDepartmentHeadUserId();
                    break;
                case 2:
                    // 维修记录环节未单独指定责任人,由申请人填写维修结果
                    expectedUserId = existing.getApplicantUserId();
                    break;
                default:
                    expectedUserId = null;
                    break;
            }
            DeviceFlowPermissionUtils.checkResponsible(expectedUserId, user.getId());
        } else if (maintenanceId != null) {
            DeviceBreakdownMaintenance existing = baseMapper.selectById(maintenanceId);
            if (existing != null) {
                DeviceFlowPermissionUtils.checkFinished(existing.getIsFinish());
                DeviceFlowPermissionUtils.checkResponsible(existing.getApplicantUserId(), user.getId());
            }
        }
    }
 
    /**
     * 导出设备故障维修
     * @param maintenanceId 设备故障维修id
     * @param response 响应
     */
    @Override
    public void exportDeviceBreakdownMaintenance(Integer maintenanceId, HttpServletResponse response) {
        // 查询外部设备申请
        DeviceBreakdownMaintenance deviceBreakdownMaintenance = baseMapper.selectById(maintenanceId);
 
        Device device = null;
        if (deviceBreakdownMaintenance.getDeviceId() != null) {
            device = deivceMapper.selectById(deviceBreakdownMaintenance.getDeviceId());
            device = device == null ? new Device() : device;
        }
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/word/device-breakdown-maintenance.docx");
        Configure configure = Configure.builder()
                .build();
        Device finalDevice = device;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("deviceBreakdownMaintenance", deviceBreakdownMaintenance);
                    put("device", finalDevice);
                    // 申请人签名
                    put("applicantUrl", UserUtils.getFinalUserSignatureOrTransparent(deviceBreakdownMaintenance.getApplicantUserId()));
                    // 部门负责人签名
                    put("headUrl", UserUtils.getFinalUserSignatureOrTransparent(deviceBreakdownMaintenance.getDepartmentHeadUserId()));
                }});
 
        try {
            response.setContentType("application/msword");
            String deviceName = device.getDeviceName() == null ? "" : device.getDeviceName();
            String fileName = URLEncoder.encode(
                    deviceName + "设备故障维修申请表", "UTF-8");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName + ".docx");
            OutputStream os = response.getOutputStream();
            template.write(os);
            os.flush();
            os.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
    }
}