package cn.cslg.pas.service; import cn.cslg.pas.common.model.cronModel.Personnel; import cn.cslg.pas.common.utils.DateUtils2; import cn.cslg.pas.common.utils.RabbitMQUtils; import cn.cslg.pas.domain.business.ImportTask; import cn.cslg.pas.domain.business.ImportTaskCondition; import cn.cslg.pas.domain.business.Project; import cn.cslg.pas.domain.business.SystemDict; import cn.cslg.pas.service.business.ImportTaskConditionService; import cn.cslg.pas.service.business.ProjectService; import cn.cslg.pas.service.business.SystemDictService; import cn.cslg.pas.service.permissions.PermissionService; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @Service public class MailSendService { @Autowired private PermissionService permissionService; @Value("${PASUrl}") private String url; @Autowired private RabbitMQUtils rabbitMQUtils; @Autowired private ImportTaskConditionService importTaskConditionService; @Autowired private ProjectService projectService; @Autowired private SystemDictService systemDictService; //发送协同邮件 public String sendSynery(String personId, Boolean ifInner, Integer projectId, Integer taskId, Integer reportType, String code) { Map map = new LinkedHashMap<>(); map.put("title", "任务通知"); String name = ""; //判断是否为内部人员 if (ifInner) { String res = ""; try { res = permissionService.getPersonnelByIdsFromPCS(Arrays.asList(personId)); } catch (Exception e) { } if (!res.equals("")) { map.put("template", "mail/SyneryInside.html"); JSONObject jsonObject = JSONObject.parseObject(res); List personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class); map.put("value1", personnels.get(0).getPersonnelName()); map.put("email", personnels.get(0).getPersonnelEmail()); map.put("img", "/logo.png"); map.put("value2", url + "/Incomplete?taskId=" + taskId + "&reportId=" + projectId + "&type=1" + "&reportType=" + reportType); } } else { map.put("template", "mail/Synery.html"); String orgin = personId; String parStr = "\\<([^}]*)\\>"; Pattern pattern = Pattern.compile(parStr); Matcher matcher = pattern.matcher(orgin); if (matcher.find()) { String personName = orgin.substring(0, matcher.start()); String email = matcher.group(0); email = email.replace("<", ""); email = email.replace(">", ""); map.put("value1", personName); map.put("email", email); map.put("img", "/logo.png"); map.put("value2", code); map.put("value3", url + "/identificationCode"); } } rabbitMQUtils.sendEmailMessage(map); return name; } //发送邮件给抄送人 public void sentToCCMail(String personId, Boolean ifInner, String createPersonName) { Map map = new LinkedHashMap<>(); map.put("title", "任务通知"); //判断是否为内部人员 if (ifInner) { String res = ""; try { res = permissionService.getPersonnelByIdsFromPCS(Arrays.asList(personId)); } catch (Exception e) { } if (!res.equals("")) { map.put("template", "mail/SyneryTo.html"); JSONObject jsonObject = JSONObject.parseObject(res); List personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class); map.put("value1", personnels.get(0).getPersonnelName()); map.put("email", personnels.get(0).getPersonnelEmail()); map.put("img", "/logo.png"); map.put("value2", createPersonName); rabbitMQUtils.sendEmailMessage(map); } } else { map.put("template", "mail/Synery.html"); String orgin = personId; String parStr = "\\<([^}]*)\\>"; Pattern pattern = Pattern.compile(parStr); Matcher matcher = pattern.matcher(orgin); if (matcher.find()) { String personName = orgin.substring(0, matcher.start()); String email = matcher.group(0); email = email.replace("<", ""); email = email.replace(">", ""); map.put("value1", createPersonName); map.put("email", email); map.put("img", "/logo.png"); map.put("value2", personName); } } rabbitMQUtils.sendEmailMessage(map); } //任务开始邮件 public void sendAddTaskEmail(String taskName, String addPerson, String endTime, String handlePerson, String email) { Map map = new LinkedHashMap<>(); map.put("title", "任务通知"); map.put("template", "mail/eTask.html"); map.put("value1", handlePerson); map.put("img", "/logo.png"); map.put("email", email); map.put("value2", addPerson); map.put("value3", taskName); map.put("value4", endTime); map.put("value5", url); rabbitMQUtils.sendEmailMessage(map); } public void sendImportTaskDoneEmail(String taskTypeName, String taskName, String personId) { String res = ""; try { res = permissionService.getPersonnelByIdsFromPCS(Arrays.asList(personId)); } catch (Exception e) { } if (!res.equals("")) { JSONObject jsonObject = JSONObject.parseObject(res); List personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class); Personnel personnel = personnels.get(0); Map map = new LinkedHashMap<>(); map.put("title", "任务通知"); map.put("template", "mail/eTaskDone.html"); map.put("value1", personnel.getPersonnelName()); map.put("img", "/logo.png"); map.put("email", personnel.getPersonnelEmail()); map.put("value2", taskTypeName); map.put("value3", taskName); rabbitMQUtils.sendEmailMessage(map); } } public void sendCycleImportTaskDoneEmail(ImportTask importTask) { ImportTaskCondition importTaskCondition = importTaskConditionService.getById(importTask.getImportTaskConditionId()); Integer projectId = importTaskCondition.getProjectId(); Project project = projectService.getById(projectId); String personId = importTask.getCreateId(); String res = ""; try { res = permissionService.getPersonnelByIdsFromPCS(Arrays.asList(personId)); } catch (Exception e) { } if (!res.equals("")) { JSONObject jsonObject = JSONObject.parseObject(res); List personnels = JSON.parseArray(jsonObject.getString("data"), Personnel.class); Personnel personnel = personnels.get(0); Map map = new LinkedHashMap<>(); map.put("title", "监控通知"); map.put("template", "mail/cycleTaskDone.html"); map.put("value1", personnel.getPersonnelName()); map.put("img", "/logo.png"); map.put("email", personnel.getPersonnelEmail()); map.put("value2", project.getName()); String dateRange = ""; if (importTask != null) { if (importTask.getLastUpdateToDate() != null) { if (importTask.getUpdateToDate() != null) { String toDateStr = DateUtils2.dateTimeToPatentStr(importTask.getUpdateToDate()); String fromDateStr = DateUtils2.dateTimeToPatentStr(importTask.getLastUpdateToDate()); dateRange = toDateStr + "~" + fromDateStr; } } else if (importTask.getUpdateToDate() != null) { String toDateStr = DateUtils2.dateTimeToPatentStr(importTask.getUpdateToDate()); dateRange = "<" + toDateStr; } } map.put("value3", dateRange); map.put("value4", importTask.getAllNum()); rabbitMQUtils.sendEmailMessage(map); } } public void sendSysCycleStartEmail() { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(SystemDict::getType, "ADMIN"); List systemDictList = systemDictService.list(queryWrapper); if(systemDictList.size()==0){ return; } Map map = new LinkedHashMap<>(); map.put("title", "监控通知"); map.put("template", "mail/sysCycleStart.html"); map.put("img", "/logo.png"); if (systemDictList != null && systemDictList.size() > 0) { SystemDict systemDict = systemDictList.remove(0); map.put("email", systemDict.getValue()); if (systemDictList.size() > 0) { String[] ccs = new String[systemDictList.size()]; for (int i = 0; i < systemDictList.size(); i++) { ccs[i] = systemDictList.get(i).getValue(); } map.put("cc", ccs); } } rabbitMQUtils.sendEmailMessage(map); } }