吴羡
9 小时以前 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
184
185
186
187
188
189
190
191
192
193
194
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.mapper.DeviceExternalApplyMapper;
import com.ruoyi.device.pojo.DeviceExternalApply;
import com.ruoyi.device.service.DeviceExternalApplyService;
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 10:28:43
 */
@Service
public class DeviceExternalApplyServiceImpl extends ServiceImpl<DeviceExternalApplyMapper, DeviceExternalApply> implements DeviceExternalApplyService {
 
    @Resource
    private UserMapper userMapper;
 
    /**
     * 利用外部设备申请列表
     * @param page
     * @param deviceExternalApply
     * @return
     */
    @Override
    public IPage<DeviceExternalApply> pageDeviceExternalApply(Page page, DeviceExternalApply deviceExternalApply) {
        return baseMapper.pageDeviceExternalApply(page, QueryWrappers.queryWrappers(deviceExternalApply));
    }
 
    /**
     * 新增利用外部设备申请
     * @param deviceExternalApply
     * @return
     */
    @Override
    public boolean addDeviceExternalApply(DeviceExternalApply deviceExternalApply) {
        DeviceExternalApply apply = new DeviceExternalApply();
        // 当前登录用户信息和部门
        User user = userMapper.selectById(SecurityUtils.getUserId().intValue());
        checkFlowPermission(deviceExternalApply, user);
        switch (deviceExternalApply.getFlowType()) {
            case 0:
                BeanUtils.copyProperties(deviceExternalApply, apply);
                // 申请
                apply.setUseReason(deviceExternalApply.getUseReason());
                apply.setApplicantUserId(user.getId());
                apply.setApplicantUser(user.getName());
                apply.setApplicantDate(LocalDate.now());
 
                baseMapper.insert(apply);
                break;
            case 1:
                apply.setExternalApplyId(deviceExternalApply.getExternalApplyId());
                // 申请部门负责人意见
                apply.setDepartmentHeadOpinion(deviceExternalApply.getDepartmentHeadOpinion());
                apply.setDepartmentHeadDate(LocalDate.now());
 
                // 计量室信息
                User meteringRoomUser = userMapper.selectById(deviceExternalApply.getMeteringRoomUserId());
                apply.setMeteringRoomUserId(meteringRoomUser.getId());
                apply.setMeteringRoomUser(meteringRoomUser.getName());
 
                baseMapper.updateById(apply);
                break;
            case 2:
                apply.setExternalApplyId(deviceExternalApply.getExternalApplyId());
                // 计量室意见
                apply.setMeteringRoomOpinion(deviceExternalApply.getMeteringRoomOpinion());
                apply.setMeteringRoomDate(LocalDate.now());
 
                // 批准人信息
                User approverUser = userMapper.selectById(deviceExternalApply.getApproverUserId());
                apply.setApproverUserId(approverUser.getId());
                apply.setApproverUser(approverUser.getName());
 
                baseMapper.updateById(apply);
                break;
            case 3:
                apply.setExternalApplyId(deviceExternalApply.getExternalApplyId());
                //批准人
                apply.setApproverOpinion(deviceExternalApply.getApproverOpinion());
                apply.setApproverDate(LocalDate.now());
 
                apply.setIsFinish(1);
                baseMapper.updateById(apply);
                break;
        }
 
        return true;
    }
 
    private void checkFlowPermission(DeviceExternalApply deviceExternalApply, User user) {
        Integer flowType = deviceExternalApply.getFlowType();
        Integer externalApplyId = deviceExternalApply.getExternalApplyId();
        if (flowType != null && flowType > 0) {
            DeviceExternalApply existing = baseMapper.selectById(externalApplyId);
            if (existing == null) {
                throw new ServiceException("单据不存在");
            }
            DeviceFlowPermissionUtils.checkFinished(existing.getIsFinish());
            Integer expectedUserId;
            switch (flowType) {
                case 1:
                    expectedUserId = existing.getDepartmentHeadUserId();
                    break;
                case 2:
                    expectedUserId = existing.getMeteringRoomUserId();
                    break;
                case 3:
                    expectedUserId = existing.getApproverUserId();
                    break;
                default:
                    expectedUserId = null;
                    break;
            }
            DeviceFlowPermissionUtils.checkResponsible(expectedUserId, user.getId());
        } else if (externalApplyId != null) {
            DeviceExternalApply existing = baseMapper.selectById(externalApplyId);
            if (existing != null) {
                DeviceFlowPermissionUtils.checkFinished(existing.getIsFinish());
                DeviceFlowPermissionUtils.checkResponsible(existing.getApplicantUserId(), user.getId());
            }
        }
    }
 
    /**
     * 导出利用外部设备申请
     *
     * @param externalApplyId 外部设备申请id
     * @param response
     */
    @Override
    public void exportDeviceExternalApply(Integer externalApplyId, HttpServletResponse response) {
        // 查询外部设备申请
        DeviceExternalApply deviceAccidentReport = baseMapper.selectDeviceExternalById(externalApplyId);
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/word/device-external-apply.docx");
        Configure configure = Configure.builder()
                .build();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("deviceAccidentReport", deviceAccidentReport);
                    // 申请人签名
                    put("applicantUserUrl", UserUtils.getFinalUserSignatureOrTransparent(deviceAccidentReport.getApplicantUserId()));
                    // 部门负责人签名
                    put("departmentHeadUserUrl", UserUtils.getFinalUserSignatureOrTransparent(deviceAccidentReport.getDepartmentHeadUserId()));
                    // 计量室人签名
                    put("meteringRoomUserUrl", UserUtils.getFinalUserSignatureOrTransparent(deviceAccidentReport.getMeteringRoomUserId()));
                    // 批准人签名
                    put("approverUserUrl", UserUtils.getFinalUserSignatureOrTransparent(deviceAccidentReport.getApproverUserId()));
                }});
 
        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();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
    }
}