linzhijie
2021-03-11 93af1c6ffb9ae0e894689ad3a37b548e57d54cff
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
267
package com.ots.project.exam.service.impl;
 
import com.ots.common.enums.UserTypeEnum;
import com.ots.common.utils.DateUtils;
import com.ots.common.utils.StringUtils;
import com.ots.common.utils.file.FileUploadUtils;
import com.ots.common.utils.text.Convert;
import com.ots.framework.config.EssConfig;
import com.ots.project.exam.domain.SysUserExtend;
import com.ots.project.exam.domain.SysUserPaper;
import com.ots.project.exam.domain.TExamPaper;
import com.ots.project.exam.domain.TReportTemplate;
import com.ots.project.exam.mapper.TReportTemplateMapper;
import com.ots.project.exam.service.ISysUserExtendService;
import com.ots.project.exam.service.ISysUserPaperService;
import com.ots.project.exam.service.ITExamPaperService;
import com.ots.project.exam.service.ITReportTemplateService;
import com.ots.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 报告模板配置Service业务层处理
 *
 * @author ots
 * @date 2020-03-24
 */
@Service
public class TReportTemplateServiceImpl implements ITReportTemplateService {
    @Autowired
    private TReportTemplateMapper tReportTemplateMapper;
 
    @Autowired
    private ISysUserExtendService sysUserExtendService;
 
    @Autowired
    private ISysUserPaperService userPaperService;
 
    @Autowired
    private ITExamPaperService paperService;
 
    /**
     * 查询报告模板配置
     *
     * @param id 报告模板配置ID
     * @return 报告模板配置
     */
    @Override
    public TReportTemplate selectTReportTemplateById(Long id) {
        return tReportTemplateMapper.selectTReportTemplateById(id);
    }
 
    @Override
    public String selectTReportTemplateFilePath(TReportTemplate tReportTemplate) {
        TReportTemplate tReportTemplateResult = tReportTemplateMapper.selectTReportTemplate(tReportTemplate);
        if (Objects.nonNull(tReportTemplateResult)) {
            return tReportTemplateResult.getLocation();
        }
        return "";
    }
 
    /**
     * 查询报告模板配置列表
     *
     * @param tReportTemplate 报告模板配置
     * @return 报告模板配置
     */
    @Override
    public List<TReportTemplate> selectTReportTemplateList(TReportTemplate tReportTemplate) {
        return tReportTemplateMapper.selectTReportTemplateList(tReportTemplate);
    }
 
    @Override
    public List<TReportTemplate> getReportTemplates(User user) {
        return getReportTemplates(user, null);
    }
 
    @Override
    public List<TReportTemplate> getReportTemplates(User user, Long prodId) {
 
        if (Objects.nonNull(prodId)) {
            TExamPaper tExamPaper = paperService.selectTExamPaperById(prodId);
            String reportTemplateId = tExamPaper.getReportTemplateId();
            List<TReportTemplate> reportTemplates = selectTReportTemplates(reportTemplateId);
            List<TReportTemplate> ret = reportTemplates.stream().map(item -> {
                String value = item.getReportType() + "-" + item.getTemplateType() + "-" + item.getLangType();
                item.setReportType(value);
                return item;
            }).collect(Collectors.toList());
 
            //取用户授权和产品包配置的交集
            if (!Objects.equals(UserTypeEnum.SYS_USER.getUserType(), user.getUserType())) {
                SysUserExtend sysUserExtend = sysUserExtendService.selectSysUserExtendById(user.getUserId());
                //查询产品包授权分配的报告模板
                SysUserPaper sysUserPaper = new SysUserPaper();
                sysUserPaper.setUserId(user.getUserId());
                sysUserPaper.setProdId(prodId);
 
                List<SysUserPaper> sysUserPapers = userPaperService.selectSysUserPaperList(sysUserPaper);
 
                List<TReportTemplate> templates = new ArrayList<>();
                for (SysUserPaper paper : sysUserPapers) {
                    String rid = paper.getReportTemplateId();
                    if (StringUtils.isBlank(rid)) {
                        continue;
                    }
                    templates.addAll(selectTReportTemplates(rid));
                }
 
                List<Long> collect = ret.stream().map(pp -> pp.getId()).collect(Collectors.toList());
                List<TReportTemplate> resultTemplateList = new ArrayList<>();
                for (TReportTemplate template : templates) {
                    if (collect.contains(template.getId())) {
                        resultTemplateList.add(template);
                    }
                }
                List<TReportTemplate> tReportTemplates = resultTemplateList.stream().collect(Collectors.collectingAndThen
                        (Collectors.toCollection(() ->
                                        new TreeSet<>(Comparator.comparing(t -> t.getLocation()))),
                                ArrayList::new
                        )
                );
                //根据企业用户配置的支持语言类型过滤
                if (Objects.nonNull(sysUserExtend) && Objects.nonNull(sysUserExtend.getLangType())) {
                    List<String> langTypes = Arrays.asList(sysUserExtend.getLangType().split(","));
                    tReportTemplates = tReportTemplates.stream().filter(f -> langTypes.contains(f.getLangType())).collect(Collectors.toList());
                }
                List<TReportTemplate> result = tReportTemplates.stream().map(item -> {
                    String value = item.getReportType() + "-" + item.getTemplateType() + "-" + item.getLangType();
                    item.setReportType(value);
                    return item;
                }).collect(Collectors.toList());
                return result;
            }
            return ret;
        }
 
        if (Objects.equals(UserTypeEnum.SYS_USER.getUserType(), user.getUserType())) {
            TReportTemplate template = new TReportTemplate();
            List<TReportTemplate> tReportTemplates = tReportTemplateMapper.selectTReportTemplateList(template);
            List<TReportTemplate> ret = tReportTemplates.stream().map(item -> {
                String value = item.getReportType() + "-" + item.getTemplateType() + "-" + item.getLangType();
                item.setReportType(value);
                return item;
            }).collect(Collectors.toList());
            return ret;
        }
 
        //查询产品包授权分配的报告模板
        SysUserPaper sysUserPaper = new SysUserPaper();
        if (!Objects.equals(UserTypeEnum.SYS_USER.getUserType(), user.getUserType())) {
            sysUserPaper.setUserId(user.getUserId());
        }
        List<SysUserPaper> sysUserPapers = userPaperService.selectSysUserPaperList(sysUserPaper);
 
        List<TReportTemplate> templates = new ArrayList<>();
        for (SysUserPaper paper : sysUserPapers) {
            String reportTemplateId = paper.getReportTemplateId();
            if (StringUtils.isBlank(reportTemplateId)) {
                continue;
            }
            templates.addAll(selectTReportTemplates(reportTemplateId));
        }
 
        //对象去重
//        List<TReportTemplate> tReportTemplates = templates.stream().distinct().collect(Collectors.toList());
        List<TReportTemplate> tReportTemplates = templates.stream().collect(Collectors.collectingAndThen
                (Collectors.toCollection(() ->
                                new TreeSet<>(Comparator.comparing(t -> t.getLocation()))),
                        ArrayList::new
                )
        );
        //根据企业用户配置的支持语言类型过滤
        if (Objects.equals(UserTypeEnum.ENT_USER.getUserType(), user.getUserType())) {
            SysUserExtend sysUserExtend = sysUserExtendService.selectSysUserExtendById(user.getUserId());
            if (Objects.nonNull(sysUserExtend) && Objects.nonNull(sysUserExtend.getLangType())) {
                List<String> langTypes = Arrays.asList(sysUserExtend.getLangType().split(","));
                tReportTemplates = tReportTemplates.stream().filter(f -> langTypes.contains(f.getLangType())).collect(Collectors.toList());
            }
        }
        List<TReportTemplate> result = tReportTemplates.stream().map(item -> {
            String value = item.getReportType() + "-" + item.getTemplateType() + "-" + item.getLangType();
            item.setReportType(value);
            return item;
        }).collect(Collectors.toList());
 
        return result;
    }
 
    @Override
    public List<TReportTemplate> selectTReportTemplates(String ids) {
        if (Objects.isNull(ids)) {
            return new ArrayList<>();
        }
        return tReportTemplateMapper.selectTReportTemplates(Convert.toStrArray(ids));
    }
 
 
    /**
     * 新增报告模板配置
     *
     * @param tReportTemplate 报告模板配置
     * @return 结果
     */
    @Override
    public int insertTReportTemplate(TReportTemplate tReportTemplate) {
        tReportTemplate.setCreateTime(DateUtils.getNowDate());
        MultipartFile locationFile = tReportTemplate.getLocationFile();
        if (locationFile != null && !locationFile.isEmpty()) {
            try {
                String ufi = FileUploadUtils.upload(EssConfig.getUploadPath(), locationFile);
                tReportTemplate.setLocation(ufi.split("profile/upload")[1]);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return tReportTemplateMapper.insertTReportTemplate(tReportTemplate);
    }
 
    /**
     * 修改报告模板配置
     *
     * @param tReportTemplate 报告模板配置
     * @return 结果
     */
    @Override
    public int updateTReportTemplate(TReportTemplate tReportTemplate) {
        tReportTemplate.setUpdateTime(DateUtils.getNowDate());
        MultipartFile locationFile = tReportTemplate.getLocationFile();
        if (locationFile != null && !locationFile.isEmpty()) {
            try {
                String uploadPath = EssConfig.getUploadPath();
                String ufi = FileUploadUtils.upload(uploadPath, locationFile);
                tReportTemplate.setLocation(ufi.split("profile/upload")[1]);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return tReportTemplateMapper.updateTReportTemplate(tReportTemplate);
    }
 
    /**
     * 删除报告模板配置对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTReportTemplateByIds(String ids) {
        return tReportTemplateMapper.deleteTReportTemplateByIds(Convert.toStrArray(ids));
    }
 
    /**
     * 删除报告模板配置信息
     *
     * @param id 报告模板配置ID
     * @return 结果
     */
    public int deleteTReportTemplateById(Long id) {
        return tReportTemplateMapper.deleteTReportTemplateById(id);
    }
}