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
package com.ots.project.monitor.job.task;
 
import com.ots.common.enums.LangTypeEnum;
import com.ots.common.enums.ReportTypeEnum;
import com.ots.common.enums.SenderCode;
import com.ots.common.utils.StringUtils;
import com.ots.common.utils.poi.WordUtil;
import com.ots.framework.config.EssConfig;
import com.ots.project.exam.domain.TExamReport;
import com.ots.project.exam.domain.TReportTemplate;
import com.ots.project.exam.mapper.TExamReportMapper;
import com.ots.project.exam.mapper.TReportTemplateMapper;
import com.ots.project.exam.service.IEntTestSendtaskMailService;
import com.ots.project.exam.service.ITExamReportService;
import com.ots.project.exam.service.ITReportTemplateService;
import com.ots.project.exam.service.impl.EntTestMemberServiceImpl;
import com.ots.project.exam.service.impl.EntTestSendtaskServiceImpl;
import com.ots.project.tool.email.EmailMessage;
import com.ots.project.tool.exam.ExamUtil;
import com.ots.project.tool.report.MAQ.MAQReport;
import com.ots.project.tool.report.ReportResultData;
import com.ots.project.tool.sms.ShortMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 邮件短信发送任务
 */
@Component("SenderEmailReportTask")
@Slf4j
public class SenderEmailReportTask {
 
    private static final String SMS_ZJCM = "SMS_ZJCM";
    private static final String SMS_ZJCM_INTERNATIONAL = "SMS_ZJCM_INTERNATIONAL";
    private static final String EMAIL_126 = "EMAIL_126";
    @Autowired
    EntTestSendtaskServiceImpl entTestSendtaskService;
    @Autowired
    EntTestMemberServiceImpl entTestMemberService;
    @Autowired
    ShortMessage shortMessage;
    @Autowired
    EmailMessage emailMessage;
    @Autowired
    IEntTestSendtaskMailService entTestSendtaskMailService;
    @Autowired
    TExamReportMapper tExamReportMapper;
    @Autowired
    private ITExamReportService tExamReportService;
    @Autowired
    private TReportTemplateMapper tReportTemplateMapper;
    @Autowired
    private ITReportTemplateService itReportTemplateService;
 
    public void senderTask() {
        // 查找report表得出要发送报告的用户
        TExamReport tExamReportTemp = new TExamReport();
        tExamReportTemp.setFinish(1);
        tExamReportTemp.setSendTesterStatus(0);
        List<TExamReport> tExamReportList = tExamReportMapper.selectTExamReportList(tExamReportTemp);
        if (ExamUtil.isListEmpty(tExamReportList)) {
            return;
        }
 
        // textMapList的
        List<Map<String, Object>> textMapList = new ArrayList<>();
        List<String> deleteFileStrList = new ArrayList<>();
        // 语言类型
        Map<String, LangTypeEnum> langTypeEnumMap = getLangTypeEnumMap();
        // 报告类型
        Map<String, ReportTypeEnum> reportTypeEnumMap = getStringReportTypeEnumMap();
        // 查出全部的报告,减少消耗的性能
        Map<Long, TReportTemplate> tReportTemplateMap = getAllTReportTemplateMap();
 
        // 遍历每个report表
        // 给测试者发送邮件
        List<String> allReportList = new ArrayList<>();
 
        // hr 邮箱
        Map<String, List<String>> hrEmail = new HashMap<>();
 
        // 循环每个个报告记录
        for (TExamReport tExamReport : tExamReportList) {
            String[] reportIds = tExamReport.getReportTemplateId().split(",");
 
            // 遍历出模版生成文件
            List<String> reprotList =  saveReport(textMapList, deleteFileStrList, langTypeEnumMap, reportTypeEnumMap, tReportTemplateMap, allReportList, tExamReport, reportIds);
 
            // 调用发送程序发给用户
            if (emailMessage.sendEmailwithAttachment(tExamReport.getMemberEmail(), "测试报告", "您已完成测试", reprotList.stream().toArray(String[]::new)).isResult()) {
                tExamReport.setSendTesterStatus(SenderCode.success.getCode());
            } else { // 发送成功,发送失败处理
                tExamReport.setSendTesterStatus(SenderCode.fail.getCode());
            }
 
            // 发送给Hr
            if (emailMessage.sendEmailwithAttachment(tExamReport.getTestEmail(), "测试报告", "您已完成测试", reprotList.stream().toArray(String[]::new)).isResult()) {
                tExamReport.setSendHrStatus(SenderCode.success.getCode());
            } else { // 发送成功,发送失败处理
                tExamReport.setSendHrStatus(SenderCode.fail.getCode());
            }
 
            tExamReportService.updateTExamReportStatus(tExamReport);
        }
    }
 
    private void setHrFiles(Map<String, List<String>> hrEmail, TExamReport tExamReport, List<String> reprotList) {
        // 保留发给Hr的邮箱
        List<String> hrEmailList =  hrEmail.get(tExamReport.getTestEmail());
        if (ExamUtil.isListEmpty(hrEmailList)) {
            List<String> hrFileList = new ArrayList<>();
            hrFileList.addAll(reprotList);
            hrEmail.put(tExamReport.getTestEmail(), hrFileList);
        } else {
            hrEmailList.addAll(reprotList);
        }
    }
 
    private List<String> saveReport(List<Map<String, Object>> textMapList, List<String> deleteFileStrList, Map<String, LangTypeEnum> langTypeEnumMap, Map<String, ReportTypeEnum> reportTypeEnumMap, Map<Long, TReportTemplate> tReportTemplateMap, List<String> allReportList, TExamReport tExamReport, String[] reportIds) {
        List<String> sendReportList = new ArrayList<>();
        for (String reportId : reportIds) {
            // 模拟调用接口
            Map<String, String> thaiCalculationMap = new HashMap<>();
            MAQReport report = new MAQReport();
            WordUtil util = new WordUtil();
            // 导出基础版本还是详细版
            String reportType = tReportTemplateMap.get(Long.valueOf(reportId)).getReportType();// "Brief";
            String templateType = tReportTemplateMap.get(Long.valueOf(reportId)).getTemplateType();// "Brief";
            String path = "";
            Map<String, Object> textMap = new HashMap<>();
            TReportTemplate tReportTemplate = tReportTemplateMap.get(Long.valueOf(reportId));
            try {
                ReportResultData reportResultData = report.getTemplateParameters(thaiCalculationMap, reportTypeEnumMap.get(templateType), langTypeEnumMap.get(tExamReport.getLangType()));
                tExamReportService.setTextPicture(tExamReport, reportResultData, deleteFileStrList, langTypeEnumMap.get(tExamReport.getLangType()), textMap);
                path = itReportTemplateService.selectTReportTemplateFilePath(tReportTemplate);
                if (StringUtils.isEmpty(path)) {
                    continue;
                }
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
 
            // 生成报告文件
            String zipFileName = util.makeReportFile(path, tReportTemplate, textMap, new HashMap<>(), deleteFileStrList);
            allReportList.add(zipFileName);
            for (String fileName : allReportList) {
                sendReportList.add(EssConfig.getProfile() + "/" + fileName);
            }
            // 清空,给下一个用户用
            textMapList.clear();
            textMap.clear();
            // 删除临时文件
            util.deleteFileByStr(deleteFileStrList);
        }
 
        return sendReportList;
    }
 
    private Map<Long, TReportTemplate> getAllTReportTemplateMap() {
        List<TReportTemplate> tReportTemplateList = tReportTemplateMapper.selectTReportTemplateList(new TReportTemplate());
        Map<Long, TReportTemplate> tReportTemplateMap = new HashMap<>();
        for (TReportTemplate tReportTemplate : tReportTemplateList) {
            tReportTemplateMap.put(tReportTemplate.getId(), tReportTemplate);
        }
        return tReportTemplateMap;
    }
 
    private Map<String, ReportTypeEnum> getStringReportTypeEnumMap() {
        Map<String, ReportTypeEnum> reportTypeEnumMap = new HashMap<>();
        for (ReportTypeEnum reportTypeEnumTemp : ReportTypeEnum.values()) {
            reportTypeEnumMap.put(reportTypeEnumTemp.getCode(), reportTypeEnumTemp);
        }
        return reportTypeEnumMap;
    }
 
    private Map<String, LangTypeEnum> getLangTypeEnumMap() {
        Map<String, LangTypeEnum> langTypeEnumMap = new HashMap<>();
        for (LangTypeEnum langTypeEnumTemp : LangTypeEnum.values()) {
            langTypeEnumMap.put(langTypeEnumTemp.getCode(), langTypeEnumTemp);
        }
        return langTypeEnumMap;
    }
}