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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.ruoyi.manage.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.Pictures;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.manage.dto.ClientSatisfactionDto;
import com.ruoyi.manage.mapper.ClientSatisfactionAnalyseFileMapper;
import com.ruoyi.manage.mapper.ClientSatisfactionAttachmentMapper;
import com.ruoyi.manage.mapper.ClientSatisfactionMapper;
import com.ruoyi.manage.pojo.ClientSatisfaction;
import com.ruoyi.manage.pojo.ClientSatisfactionAnalyseFile;
import com.ruoyi.manage.pojo.ClientSatisfactionAttachment;
import com.ruoyi.manage.service.ClientSatisfactionService;
import com.ruoyi.system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
 
 
/**
 * 客户满意度
 *
 * @author zhuo
 * @since 2024-11-09
 */
@Service
public class ClientSatisfactionServiceImpl extends ServiceImpl<ClientSatisfactionMapper, ClientSatisfaction> implements ClientSatisfactionService {
 
    @Resource
    private ClientSatisfactionAnalyseFileMapper clientSatisfactionAnalyseFileMapper;
 
    @Resource
    private ClientSatisfactionAttachmentMapper clientSatisfactionAttachmentMapper;
 
    @Resource
    private UserMapper userMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Value("${wordUrl}")
    private String wordUrl;
 
    /**
     * 客户满意度调查列表
     *
     * @param page
     * @param clientSatisfaction
     * @return
     */
    @Override
    public IPage<ClientSatisfaction> pageClientSatisfaction(Page page, ClientSatisfaction clientSatisfaction) {
        return baseMapper.pageClientSatisfaction(page, QueryWrappers.queryWrappers(clientSatisfaction));
    }
 
    /**
     * 新增客户分析附件
     *
     * @param file
     * @return
     */
    @Override
    public boolean uploadAnalyseFile(MultipartFile file) {
 
        String urlString;
        String pathName;
        String path;
        String filename = file.getOriginalFilename();
        String contentType = file.getContentType();
        ClientSatisfactionAnalyseFile analyseFile = new ClientSatisfactionAnalyseFile();
        analyseFile.setFileName(filename);
        if (contentType != null && contentType.startsWith("image/")) {
            // 是图片
            path = imgUrl;
            analyseFile.setType(1);
        } else {
            // 是文件
            path = wordUrl;
            analyseFile.setType(2);
        }
        try {
            File realpath = new File(path);
            if (!realpath.exists()) {
                realpath.mkdirs();
            }
            pathName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmss")) + "_" + file.getOriginalFilename();
            urlString = realpath + "/" + pathName;
            file.transferTo(new File(urlString));
            analyseFile.setFileUrl(pathName);
            clientSatisfactionAnalyseFileMapper.insert(analyseFile);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ErrorException(e.getMessage());
        }
    }
 
    /**
     * 查询客户分析附件
     *
     * @param page
     * @param analyseFile
     * @return
     */
    @Override
    public IPage<ClientSatisfactionAnalyseFile> pageAnalyseFile(Page page, ClientSatisfactionAnalyseFile analyseFile) {
        return clientSatisfactionAnalyseFileMapper.pageAnalyseFile(page, QueryWrappers.queryWrappers(analyseFile));
    }
 
    /**
     * 客户满意度导出
     *
     * @param clientSatisfactionId
     * @param response
     */
    @Override
    public void exportWordClientSatisfaction(Integer clientSatisfactionId, HttpServletResponse response) {
        // 查询客户满意度调查
        ClientSatisfactionDto clientSatisfaction = baseMapper.exportWordClientSatisfaction(clientSatisfactionId);
        if (clientSatisfaction == null) {
            throw new RuntimeException("客户满意度调查不存在");
        }
        // 查询确认人的签名
        String confirmPersonUrl = null;
        // 状态为确认且确认人不为空
        if (clientSatisfaction.getConfirmStatus() != null && clientSatisfaction.getConfirmStatus() != 0 && clientSatisfaction.getConfirmPersonId() != null) {
            confirmPersonUrl = userMapper.selectById(clientSatisfaction.getConfirmPersonId()).getSignatureUrl();
            if (StringUtils.isBlank(confirmPersonUrl)) {
                throw new ErrorException("找不到验证人的签名");
            }
        }
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/customer-satisfaction-questionnaire.docx");
        ConfigureBuilder builder = Configure.builder();
        String finalConfirmPersonUrl = confirmPersonUrl;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render(
                new HashMap<String, Object>() {{
                    put("clientSatisfaction", clientSatisfaction);
                    put("finalConfirmPersonUrl", StringUtils.isNotBlank(finalConfirmPersonUrl) ? Pictures.ofLocal(imgUrl + "/" + finalConfirmPersonUrl).create() : null);
                }});
 
        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("导出失败");
        }
    }
 
    /**
     * 确认客户满意度
     *
     * @param clientSatisfaction 要修改客户满意度的状态对象
     * @param userId             修改人id
     */
    @Override
    public void confirmClientSatisfaction(ClientSatisfaction clientSatisfaction, Integer userId) {
        User user = userMapper.selectById(userId);
        if (user != null) {
            clientSatisfaction.setConfirmPerson(user.getName());
            clientSatisfaction.setConfirmPersonId(user.getId());
        }
        clientSatisfaction.setConfirmStatus(1);
        baseMapper.updateById(clientSatisfaction);
    }
 
    /**
     * 查询满意度附件
     *
     * @param clientSatisfactionId 满意度记录id
     * @return
     */
    @Override
    public List<ClientSatisfactionAttachment> selectAttachments(Integer clientSatisfactionId) {
        return clientSatisfactionAttachmentMapper.selectList(new QueryWrapper<ClientSatisfactionAttachment>().lambda()
                .eq(ClientSatisfactionAttachment::getClientSatisfactionId, clientSatisfactionId)
                .orderByAsc(ClientSatisfactionAttachment::getSort, ClientSatisfactionAttachment::getId));
    }
 
    /**
     * 上传满意度附件
     *
     * @param clientSatisfactionId 满意度记录id
     * @param file
     * @return
     */
    @Override
    public ClientSatisfactionAttachment uploadAttachment(Integer clientSatisfactionId, MultipartFile file) {
        if (baseMapper.selectById(clientSatisfactionId) == null) {
            throw new BaseException("客户满意度调查不存在");
        }
        if (file == null || file.isEmpty()) {
            throw new BaseException("附件不能为空");
        }
        // 系统全局上传上限为200MB,这里提前给出友好提示
        if (file.getSize() > 200L * 1024 * 1024) {
            throw new BaseException("附件不能超过200MB");
        }
        String originalName = file.getOriginalFilename();
        if (originalName == null || originalName.trim().isEmpty()) {
            originalName = "未命名文件";
        }
        // 个别浏览器可能带上路径前缀,只保留文件名本身
        originalName = originalName.replaceAll(".*[/\\\\]", "");
        try {
            // 图片放图片目录(/img 静态路径),其余任意格式放 word 目录(/word 静态路径)
            boolean image = originalName.matches("(?i).+\\.(jpg|jpeg|png|gif)$");
            File directory = new File(image ? imgUrl : wordUrl);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            String fileUrl = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmssSSS")) + "-" + originalName;
            file.transferTo(new File(directory, fileUrl));
            ClientSatisfactionAttachment attachment = new ClientSatisfactionAttachment();
            attachment.setClientSatisfactionId(clientSatisfactionId);
            attachment.setFileName(originalName);
            attachment.setFileUrl(fileUrl);
            attachment.setSort(selectAttachments(clientSatisfactionId).size());
            clientSatisfactionAttachmentMapper.insert(attachment);
            return attachment;
        } catch (IOException e) {
            throw new BaseException("附件上传失败");
        }
    }
 
    /**
     * 删除满意度附件
     *
     * @param id 附件id
     * @return
     */
    @Override
    public boolean deleteAttachment(Long id) {
        return clientSatisfactionAttachmentMapper.deleteById(id) > 0;
    }
}