linzhijie
2021-03-11 e1801037f0d65bcd61f9a7dc0970bea09bb048a0
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
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() {
        
        TExamReport tExamReportTemp = new TExamReport();
        tExamReportTemp.setFinish(1);
        tExamReportTemp.setSendTesterStatus(0);
        List<TExamReport> tExamReportList = tExamReportMapper.selectTExamReportList(tExamReportTemp);
        if (ExamUtil.isListEmpty(tExamReportList)) {
            return;
        }
        
        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();
        
        
        List<String> allReportList = new ArrayList<>();
        
        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());
            }
            
            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) {
        
        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();
            String templateType = tReportTemplateMap.get(Long.valueOf(reportId)).getTemplateType();
            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;
    }
}