zss
2025-01-13 8d85246f061e3da623c7b9eb4e323ee724b4de0b
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
package com.yuanchu.mom.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.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.mapper.DeviceExternalApplyMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.DeviceExternalApply;
import com.yuanchu.mom.pojo.User;
import com.yuanchu.mom.service.DeviceExternalApplyService;
import com.yuanchu.mom.utils.UserUtils;
import com.yuanchu.mom.utils.QueryWrappers;
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.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;
    @Resource
    private GetLook getLook;
 
    /**
     * 利用外部设备申请列表
     * @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(getLook.selectPowerByMethodAndUserId(null).get("userId"));
        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());
 
                // 处理人信息
                User departmentHeadUser = userMapper.selectById(deviceExternalApply.getDepartmentHeadUserId());
                apply.setApplicantUserId(departmentHeadUser.getId());
                apply.setApplicantUser(departmentHeadUser.getName());
 
                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;
    }
 
    /**
     * 导出利用外部设备申请
     *
     * @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.getFinalUserSignatureUrl(deviceAccidentReport.getApplicantUserId()));
                    // 部门负责人签名
                    put("departmentHeadUserUrl", UserUtils.getFinalUserSignatureUrl(deviceAccidentReport.getDepartmentHeadUserId()));
                    // 计量室人签名
                    put("meteringRoomUserUrl", UserUtils.getFinalUserSignatureUrl(deviceAccidentReport.getMeteringRoomUserId()));
                    // 批准人签名
                    put("approverUserUrl", UserUtils.getFinalUserSignatureUrl(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();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
    }
}