|
|
@@ -1,300 +0,0 @@
|
|
|
-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.customer.service.inform.object.InformResult;
|
|
|
-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(timeout = 3000, async = true)
|
|
|
- private SmsbEmailService smsbEmailService;
|
|
|
- @DubboReference(timeout = 3000, async = true)
|
|
|
- private SmsbSmsService smsbSmsService;
|
|
|
- @DubboReference(timeout = 3000, async = true)
|
|
|
- private IWeChatService weChatService;
|
|
|
- @DubboReference(timeout = 3000, 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());
|
|
|
- log.info("strategy: {}", informStrategyCo);
|
|
|
- ExceptionInformStrategyCmd informStrategyCmd = informStrategyCo.getStrategy().get(String.valueOf(informCo.getTypeEnum().getLevel().getId()));
|
|
|
- // execute inform
|
|
|
- executeInformUserCmd(informCo, informStrategyCmd, users);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void superAdminLevelInform(CommonInformCO informCo) {
|
|
|
- List<KeycloakUserCO> users = keycloakService.getSuperAdmin();
|
|
|
- ExceptionInformStrategyCmd informStrategyCmd = new ExceptionInformStrategyCmd();
|
|
|
- informStrategyCmd.setEmail(true);
|
|
|
- informStrategyCmd.setNote(true);
|
|
|
- informStrategyCmd.setWechat(true);
|
|
|
- informStrategyCmd.setWechatApplet(true);
|
|
|
- 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);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * execute inform action
|
|
|
- * @param informCo inform object
|
|
|
- * @param informStrategyCmd strategy
|
|
|
- * @param users users
|
|
|
- */
|
|
|
- private void executeInformUserCmd(CommonInformCO informCo, ExceptionInformStrategyCmd informStrategyCmd, List<KeycloakUserCO> users) {
|
|
|
- // set addressees
|
|
|
- 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);
|
|
|
- } catch (IOException e) {
|
|
|
- if (e instanceof JsonProcessingException) {
|
|
|
- log.error("序列化出错: {}", e.getMessage(), e);
|
|
|
- throw new CustomerBaseException("参数序列化出错", 500);
|
|
|
- }
|
|
|
- log.error("模板转换出错: {}", e.getMessage(), e);
|
|
|
- throw new CustomerBaseException("模板转换出错", 500);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * send message
|
|
|
- * @param informCo inform co
|
|
|
- * @param informStrategyCmd strategy
|
|
|
- * @param addressees address
|
|
|
- * @return inform result(CompletableFuture)
|
|
|
- */
|
|
|
- private InformResult sendMessage(CommonInformCO informCo, ExceptionInformStrategyCmd informStrategyCmd, InformAddressees addressees) throws IOException {
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * save record
|
|
|
- * @param informCo inform co
|
|
|
- * @param informStrategyCmd strategy
|
|
|
- * @param users users
|
|
|
- * @return content id
|
|
|
- * @throws JsonProcessingException json
|
|
|
- */
|
|
|
- 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);
|
|
|
- log.info("strategy: {}", informStrategyCmd);
|
|
|
- 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);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * update record after future done
|
|
|
- * @param informResult result
|
|
|
- * @param contentId contentId
|
|
|
- * @param informStrategyCmd strategy
|
|
|
- */
|
|
|
- private void updateSendRecord(InformResult informResult, Long contentId, ExceptionInformStrategyCmd informStrategyCmd) {
|
|
|
- CompletableFuture<Void> totalFuture = setFutureCombine(informResult, informStrategyCmd.getCount());
|
|
|
- totalFuture.whenComplete((result, error) -> {
|
|
|
- log.error("future error: {}", error.getMessage(), error);
|
|
|
- log.info("update result flag");
|
|
|
- });
|
|
|
- totalFuture.join();
|
|
|
- informResult.updateResultFlag();
|
|
|
- // update record
|
|
|
- informMessageRecordService.updateRecord(contentId, informResult.getResultFlag());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * set future combine
|
|
|
- * @param informResult result
|
|
|
- * @param size future count
|
|
|
- * @return combined future
|
|
|
- */
|
|
|
- public CompletableFuture<Void> setFutureCombine(InformResult informResult, Integer size) {
|
|
|
- int index = 0;
|
|
|
- CompletableFuture[] completableFutures = new CompletableFuture[size];
|
|
|
- if (informResult.getNoteResult() != null) {
|
|
|
- completableFutures[index] = informResult.getNoteResult();
|
|
|
- index++;
|
|
|
- }
|
|
|
- if (informResult.getEmailResult() != null) {
|
|
|
- completableFutures[index] = informResult.getEmailResult();
|
|
|
- index++;
|
|
|
- }
|
|
|
- if (informResult.getWeChatResult() != null) {
|
|
|
- completableFutures[index] = informResult.getWeChatResult();
|
|
|
- index++;
|
|
|
- }
|
|
|
- if (informResult.getWeChatAppletResult() != null) {
|
|
|
- completableFutures[index] = informResult.getWeChatAppletResult();
|
|
|
- }
|
|
|
- log.info("combine future");
|
|
|
- return CompletableFuture.allOf(completableFutures);
|
|
|
- }
|
|
|
-}
|