linzhijie
2021-03-11 c33914ba0a98c823c4b4d7da21cdd476906c9924
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
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
public class TReportTemplateServiceImpl implements ITReportTemplateService {
    @Autowired
    private TReportTemplateMapper tReportTemplateMapper;
    @Autowired
    private ISysUserExtendService sysUserExtendService;
    @Autowired
    private ISysUserPaperService userPaperService;
    @Autowired
    private ITExamPaperService paperService;
    
    @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 "";
    }
    
    @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().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));
    }
    
    @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);
    }
    
    @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);
    }
    
    @Override
    public int deleteTReportTemplateByIds(String ids) {
        return tReportTemplateMapper.deleteTReportTemplateByIds(Convert.toStrArray(ids));
    }
    
    public int deleteTReportTemplateById(Long id) {
        return tReportTemplateMapper.deleteTReportTemplateById(id);
    }
}