|
|
@@ -0,0 +1,258 @@
|
|
|
+package com.inspur.customer.service.inform;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.inspur.customer.client.inform.CommonInformService;
|
|
|
+import com.inspur.customer.client.inform.InformMessageRecordService;
|
|
|
+import com.inspur.customer.client.keycloak.KeycloakService;
|
|
|
+import com.inspur.customer.client.tenant.TenantExceptionInformStrategyService;
|
|
|
+import com.inspur.customer.context.inform.MessageTemplateEnum;
|
|
|
+import com.inspur.customer.object.inform.*;
|
|
|
+import com.inspur.customer.object.keycloak.KeycloakUserCO;
|
|
|
+import com.inspur.customer.object.tenant.ExceptionInformStrategyCmd;
|
|
|
+import com.inspur.customer.object.tenant.TenantExceptionInformStrategyCO;
|
|
|
+import com.inspur.customer.service.inform.handler.EmailMessageHandler;
|
|
|
+import com.inspur.customer.service.inform.handler.NoteMessageHandler;
|
|
|
+import com.inspur.customer.service.inform.handler.WeChatAppletMessageHandler;
|
|
|
+import com.inspur.customer.service.inform.handler.WeChatMessageHandler;
|
|
|
+import com.inspur.inform.client.email.SmsbEmailService;
|
|
|
+import com.inspur.inform.client.sms.SmsbSmsService;
|
|
|
+import com.inspur.inform.client.wechat.IWeChatService;
|
|
|
+import com.inspur.inform.client.wechat.applet.IWxAppletUserMessageService;
|
|
|
+import com.inspur.inform.object.applet.message.WxAppletUserMessageDto;
|
|
|
+import com.inspur.inform.object.message.EmailMessage;
|
|
|
+import com.inspur.inform.object.message.SmsMessage;
|
|
|
+import com.inspur.inform.object.message.WechatMessage;
|
|
|
+import com.inspur.logging.object.EventBaseException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+import org.apache.dubbo.config.annotation.DubboService;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+
|
|
|
+/**
|
|
|
+ * common inform service impl
|
|
|
+ * @author linwenhua
|
|
|
+ * @date 2022-06-29 15:30
|
|
|
+ **/
|
|
|
+@Slf4j
|
|
|
+@DubboService(interfaceClass = CommonInformService.class)
|
|
|
+public class CommonInformServiceImpl implements CommonInformService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private EmailMessageHandler emailMessageHandler;
|
|
|
+ @Resource
|
|
|
+ private NoteMessageHandler noteMessageHandler;
|
|
|
+ @Resource
|
|
|
+ private WeChatMessageHandler weChatMessageHandler;
|
|
|
+ @Resource
|
|
|
+ private WeChatAppletMessageHandler weChatAppletMessageHandler;
|
|
|
+
|
|
|
+ @DubboReference
|
|
|
+ private KeycloakService keycloakService;
|
|
|
+ @DubboReference
|
|
|
+ private TenantExceptionInformStrategyService tenantExceptionInformStrategyService;
|
|
|
+ @DubboReference
|
|
|
+ private InformMessageRecordService informMessageRecordService;
|
|
|
+
|
|
|
+ @DubboReference(async = true)
|
|
|
+ private SmsbEmailService smsbEmailService;
|
|
|
+ @DubboReference(async = true)
|
|
|
+ private SmsbSmsService smsbSmsService;
|
|
|
+ @DubboReference(async = true)
|
|
|
+ private IWeChatService weChatService;
|
|
|
+ @DubboReference(async = true)
|
|
|
+ private IWxAppletUserMessageService wxAppletUserMessageService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void commonInform(CommonInformCO informCo) {
|
|
|
+ // get base user
|
|
|
+ List<KeycloakUserCO> users = keycloakService.getUsersByIds(Collections.singletonList(informCo.getUserId()));
|
|
|
+ // get strategy
|
|
|
+ TenantExceptionInformStrategyCO informStrategyCo = tenantExceptionInformStrategyService.getTenantInformStrategyCache(informCo.getTenant());
|
|
|
+ ExceptionInformStrategyCmd informStrategyCmd = informStrategyCo.getStrategy().get(String.valueOf(informCo.getTypeEnum().getLevel().getId()));
|
|
|
+ switch (informCo.getTypeEnum().getLevel()) {
|
|
|
+ case HINT : {
|
|
|
+ log.info("hint");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case INTERMEDIATE: {
|
|
|
+ log.info("intermediate");
|
|
|
+ //todo replace get user method
|
|
|
+ users.addAll(keycloakService.getUsersInRole("ROLE_SUPER_ADMIN"));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case URGENT : {
|
|
|
+ log.info("urgent");
|
|
|
+ //todo replace get user method
|
|
|
+ users.addAll(keycloakService.getUsersInRole("ROLE_SUPER_ADMIN"));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default : {
|
|
|
+ log.info("not support");
|
|
|
+ throw new EventBaseException("类型错误", 500);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // execute inform
|
|
|
+ executeInformUserCmd(informCo, informStrategyCmd, users);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void tenantLevelInform(CommonInformCO informCo) {
|
|
|
+ log.info("tenant level: {}", informCo.getTenant());
|
|
|
+ // get user
|
|
|
+ List<KeycloakUserCO> users = keycloakService.getGroupSupervisor(informCo.getTenant());
|
|
|
+ // get strategy
|
|
|
+ TenantExceptionInformStrategyCO informStrategyCo = tenantExceptionInformStrategyService.getTenantInformStrategyCache(informCo.getTenant());
|
|
|
+ ExceptionInformStrategyCmd informStrategyCmd = informStrategyCo.getStrategy().get(String.valueOf(informCo.getTypeEnum().getLevel().getId()));
|
|
|
+ // execute inform
|
|
|
+ executeInformUserCmd(informCo, informStrategyCmd, users);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void departmentLevelInform(CommonInformCO informCo) {
|
|
|
+ log.info("department level: {}", informCo.getOrg());
|
|
|
+ // get user
|
|
|
+ List<KeycloakUserCO> users = keycloakService.getGroupAdmin(informCo.getOrg());
|
|
|
+ // get strategy
|
|
|
+ TenantExceptionInformStrategyCO informStrategyCo = tenantExceptionInformStrategyService.getTenantInformStrategyCache(informCo.getTenant());
|
|
|
+ ExceptionInformStrategyCmd informStrategyCmd = informStrategyCo.getStrategy().get(String.valueOf(informCo.getTypeEnum().getLevel().getId()));
|
|
|
+ // execute inform
|
|
|
+ executeInformUserCmd(informCo, informStrategyCmd, users);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void singleInformMethod(CommonInformCO informCo) {
|
|
|
+ log.info("single inform level: {}", informCo.getUserId());
|
|
|
+ List<KeycloakUserCO> users = keycloakService.getUsersByIds(Collections.singletonList(informCo.getUserId()));
|
|
|
+ // get strategy
|
|
|
+ TenantExceptionInformStrategyCO informStrategyCo = tenantExceptionInformStrategyService.getTenantInformStrategyCache(informCo.getTenant());
|
|
|
+ ExceptionInformStrategyCmd informStrategyCmd = informStrategyCo.getStrategy().get(String.valueOf(informCo.getTypeEnum().getLevel().getId()));
|
|
|
+ // execute inform
|
|
|
+ executeInformUserCmd(informCo, informStrategyCmd, users);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void executeInformUserCmd(CommonInformCO informCo, ExceptionInformStrategyCmd informStrategyCmd, List<KeycloakUserCO> users) {
|
|
|
+ InformAddressees addressees = new InformAddressees(users.size());
|
|
|
+ users.forEach(user -> {
|
|
|
+ log.info("user: {}", user);
|
|
|
+ addressees.addPhone(user.getPhone());
|
|
|
+ addressees.addEmailAddressee(user.getEmail());
|
|
|
+ addressees.addOpenId(user.getWechat());
|
|
|
+ addressees.addUserId(user.getId());
|
|
|
+ });
|
|
|
+ try {
|
|
|
+ // save record
|
|
|
+ Long contentId = this.saveSendRecord(informCo, informStrategyCmd, users);
|
|
|
+ // send message
|
|
|
+ InformResult informResult = sendMessage(informCo, informStrategyCmd, addressees);
|
|
|
+ // update record
|
|
|
+ updateSendRecord(informResult, contentId, informStrategyCmd, addressees.getUserIds());
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ log.error("序列化出错: {}", e.getMessage(), e);
|
|
|
+ throw new CustomerBaseException("参数序列化出错", 500);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("模板转换出错: {}", e.getMessage(), e);
|
|
|
+ throw new CustomerBaseException("模板转换出错", 500);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private InformResult sendMessage(CommonInformCO informCo, ExceptionInformStrategyCmd informStrategyCmd, InformAddressees addressees) throws Exception {
|
|
|
+ MessageTemplateEnum templateEnum = informCo.getTypeEnum().getTemplateEnum();
|
|
|
+ // inform result
|
|
|
+ InformResult informResult = new InformResult();
|
|
|
+ if (Boolean.TRUE.equals(informStrategyCmd.getEmail())) {
|
|
|
+ EmailMessage emailMessage = emailMessageHandler.makeEmailMessage(informCo.getEmailMessage(), templateEnum.getEmail(), addressees.getEmailAddressees());
|
|
|
+ log.info("email");
|
|
|
+ informResult.setEmailResult(smsbEmailService.asyncSendEmail(emailMessage));
|
|
|
+ }
|
|
|
+ if (Boolean.TRUE.equals(informStrategyCmd.getNote())) {
|
|
|
+ SmsMessage smsMessage = noteMessageHandler.makeSmsMessage(informCo.getNoteMessage(), addressees.getPhones(), templateEnum.getNote());
|
|
|
+ log.info("note");
|
|
|
+ informResult.setNoteResult(smsbSmsService.asyncSend(smsMessage));
|
|
|
+ }
|
|
|
+ if (Boolean.TRUE.equals(informStrategyCmd.getWechat())) {
|
|
|
+ WechatMessage wechatMessage = weChatMessageHandler.makeWeChatMessage(informCo.getWeChatMessage(), addressees.getOpenIds(), templateEnum.getWechat());
|
|
|
+ log.info("wechat");
|
|
|
+ informResult.setWeChatResult(weChatService.asyncSend(wechatMessage));
|
|
|
+ }
|
|
|
+ if (Boolean.TRUE.equals(informStrategyCmd.getWechatApplet())) {
|
|
|
+ WxAppletUserMessageDto wxAppletUserMessageDto = weChatAppletMessageHandler.makeWeChatAppletMessage(informCo.getWeChatAppletMessage(), templateEnum.getApplet());
|
|
|
+ log.info("applet");
|
|
|
+ informResult.setWeChatAppletResult(wxAppletUserMessageService.asyncInsertBatch(wxAppletUserMessageDto, addressees.getUserIds()));
|
|
|
+ }
|
|
|
+ return informResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long saveSendRecord(CommonInformCO informCo, ExceptionInformStrategyCmd informStrategyCmd, List<KeycloakUserCO> users) throws JsonProcessingException {
|
|
|
+ InformMessageRecordDto messageRecordDto = new InformMessageRecordDto();
|
|
|
+ messageRecordDto.setTenant(informCo.getTenant());
|
|
|
+ messageRecordDto.setOrg(informCo.getOrg());
|
|
|
+ Map<String, String> messageMap = new HashMap<>(4);
|
|
|
+ if (Boolean.TRUE.equals(informStrategyCmd.getEmail())) {
|
|
|
+ messageMap.put(InformMessageRecordDto.EMAIL_KEY, objectMapper.writeValueAsString(informCo.getEmailMessage()));
|
|
|
+ }
|
|
|
+ if (Boolean.TRUE.equals(informStrategyCmd.getNote())) {
|
|
|
+ messageMap.put(InformMessageRecordDto.NOTE_KEY, objectMapper.writeValueAsString(informCo.getNoteMessage()));
|
|
|
+ }
|
|
|
+ if (Boolean.TRUE.equals(informStrategyCmd.getWechat())) {
|
|
|
+ messageMap.put(InformMessageRecordDto.WECHAT_KEY, objectMapper.writeValueAsString(informCo.getWeChatMessage()));
|
|
|
+ }
|
|
|
+ if (Boolean.TRUE.equals(informStrategyCmd.getWechatApplet())) {
|
|
|
+ messageMap.put(InformMessageRecordDto.WECHAT_APPLET_KEY, objectMapper.writeValueAsString(informCo.getWeChatAppletMessage()));
|
|
|
+ }
|
|
|
+ messageRecordDto.setMessageMap(messageMap);
|
|
|
+ return informMessageRecordService.saveRecord(users, informStrategyCmd, messageRecordDto);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateSendRecord(InformResult informResult, Long contentId, ExceptionInformStrategyCmd informStrategyCmd, List<String> userIds) {
|
|
|
+ CompletableFuture[] completableFutures = setFutureCombine(informResult, informStrategyCmd.getCount());
|
|
|
+ for (CompletableFuture completableFuture : completableFutures) {
|
|
|
+ log.info("com: {}", completableFuture);
|
|
|
+ }
|
|
|
+ CompletableFuture<Void> completableFuture = CompletableFuture.allOf(completableFutures);
|
|
|
+ completableFuture.whenComplete((result, error) -> {
|
|
|
+ log.info("error: {}", error);
|
|
|
+ log.info("update result flag");
|
|
|
+ informResult.updateResultFlag();
|
|
|
+ });
|
|
|
+ // update record
|
|
|
+ informMessageRecordService.updateRecord(contentId, informResult.getResultFlag());
|
|
|
+ }
|
|
|
+
|
|
|
+ public CompletableFuture[] setFutureCombine(InformResult informResult, Integer size) {
|
|
|
+ int index = 0;
|
|
|
+ CompletableFuture[] completableFutures = new CompletableFuture[size];
|
|
|
+ if (informResult.getNoteResult() != null) {
|
|
|
+ log.info("index: {}", index);
|
|
|
+ completableFutures[index] = informResult.getNoteResult();
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ if (informResult.getEmailResult() != null) {
|
|
|
+ log.info("index: {}", index);
|
|
|
+ completableFutures[index] = informResult.getEmailResult();
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ if (informResult.getWeChatResult() != null) {
|
|
|
+ log.info("index: {}", index);
|
|
|
+ completableFutures[index] = informResult.getWeChatResult();
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ if (informResult.getWeChatAppletResult() != null) {
|
|
|
+ log.info("index: {}", index);
|
|
|
+ completableFutures[index] = informResult.getWeChatAppletResult();
|
|
|
+ }
|
|
|
+ return completableFutures;
|
|
|
+ }
|
|
|
+}
|