Parcourir la source

refactor: realize userInform service to get user

linwenhua il y a 2 ans
Parent
commit
dd129680c0

+ 0 - 3
smsb-customer-manager-app/src/main/java/com/inspur/customer/service/tenant/TenantExceptionInformStrategyServiceImpl.java

@@ -12,9 +12,6 @@ import org.apache.dubbo.config.annotation.DubboService;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 
-import java.util.HashMap;
-import java.util.Map;
-
 /**
  * tenant exception inform strategy service implement
  * @author linwenhua

+ 77 - 0
smsb-customer-manager-app/src/main/java/com/inspur/customer/service/tenant/UserInformServiceImpl.java

@@ -0,0 +1,77 @@
+package com.inspur.customer.service.tenant;
+
+import com.alibaba.cola.exception.BizException;
+import com.inspur.customer.client.keycloak.KeycloakService;
+import com.inspur.customer.client.tenant.TenantExceptionInformStrategyService;
+import com.inspur.customer.client.tenant.UserInformService;
+import com.inspur.customer.context.inform.InformLevelEnum;
+import com.inspur.customer.context.inform.InformTypeEnum;
+import com.inspur.customer.object.inform.InformAddresseeCO;
+import com.inspur.customer.object.keycloak.KeycloakUserCO;
+import com.inspur.customer.object.tenant.TenantExceptionInformStrategyCO;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.dubbo.config.annotation.DubboReference;
+import org.apache.dubbo.config.annotation.DubboService;
+
+import javax.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author linwenhua
+ * @date 2022-12-02 16:19
+ **/
+@Slf4j
+@DubboService(interfaceClass = UserInformService.class)
+public class UserInformServiceImpl implements UserInformService {
+
+    @DubboReference
+    private KeycloakService keycloakService;
+    @DubboReference
+    private TenantExceptionInformStrategyService tenantExceptionInformStrategyService;
+
+    @Override
+    public InformAddresseeCO getInformAddresseesByInformLevel(@Nullable String userId, String tenant, List<String> org, InformLevelEnum informLevel) {
+        // get base user
+        List<KeycloakUserCO> users = new ArrayList<>();
+        InformAddresseeCO informAddressee = new InformAddresseeCO();
+        if (userId != null) {
+            users.addAll(keycloakService.getUsersByIds(Collections.singletonList(userId)));
+        }
+        // get and set inform strategy
+        log.info("tenant: {}", tenant);
+        TenantExceptionInformStrategyCO informStrategy = tenantExceptionInformStrategyService.getTenantInformStrategyCache(tenant);
+        informAddressee.setInformStrategy(informStrategy.getInformStrategy(String.valueOf(informLevel.getId())));
+        // get additional user
+        switch (informLevel) {
+            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 BizException("500", "类型错误");
+            }
+        }
+        informAddressee.setUsers(users);
+        return informAddressee;
+    }
+
+    @Override
+    public InformAddresseeCO getInformAddresseesByInformType(@Nullable String userId, String tenant, List<String> orgs, InformTypeEnum informType) {
+        return getInformAddresseesByInformLevel(userId, tenant, orgs, informType.getLevel());
+    }
+}

+ 20 - 0
smsb-customer-manager-client/src/main/java/com/inspur/customer/client/tenant/UserInformService.java

@@ -0,0 +1,20 @@
+package com.inspur.customer.client.tenant;
+
+import com.inspur.customer.context.inform.InformLevelEnum;
+import com.inspur.customer.context.inform.InformTypeEnum;
+import com.inspur.customer.object.inform.InformAddresseeCO;
+import com.inspur.customer.object.keycloak.KeycloakUserCO;
+import org.springframework.lang.Nullable;
+
+import java.util.List;
+
+/**
+ * @author linwenhua
+ * @date 2022-12-02 16:17
+ **/
+public interface UserInformService {
+
+    InformAddresseeCO getInformAddresseesByInformLevel(@Nullable String userId, String tenant, List<String> departments, InformLevelEnum informLevel);
+
+    InformAddresseeCO getInformAddresseesByInformType(String userId, String tenant, List<String> departments, InformTypeEnum informType);
+}

+ 4 - 0
smsb-customer-manager-client/src/main/java/com/inspur/customer/context/inform/InformLevelEnum.java

@@ -52,6 +52,10 @@ public enum InformLevelEnum {
     @Getter
     private final String name;
 
+    public static InformLevelEnum getLevelById(int id) {
+        return Arrays.stream(InformLevelEnum.values()).filter(informLevel -> id == informLevel.getId()).findFirst().orElse(null);
+    }
+
     public static String getLevelName(int id) {
         return Arrays.stream(InformLevelEnum.values()).filter(informLevel -> id == informLevel.getId()).findFirst().map(InformLevelEnum::getName).orElse(null);
     }

+ 64 - 0
smsb-customer-manager-client/src/main/java/com/inspur/customer/object/inform/InformAddresseeCO.java

@@ -0,0 +1,64 @@
+package com.inspur.customer.object.inform;
+
+import com.inspur.customer.object.keycloak.KeycloakUserCO;
+import com.inspur.customer.object.tenant.ExceptionInformStrategyCmd;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.Serializable;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @author linwenhua
+ * @date 2022-12-06 09:41
+ **/
+public class InformAddresseeCO implements Serializable {
+
+    private static final long serialVersionUID = -2056897808671772295L;
+
+    @Setter
+    @Getter
+    private ExceptionInformStrategyCmd informStrategy;
+
+    @Setter
+    private List<KeycloakUserCO> users;
+
+    public List<String> getEmailAddressees() {
+        if (Boolean.TRUE.equals(informStrategy.getEmail())) {
+            return users.stream().map(KeycloakUserCO::getEmail).filter(StringUtils::isNotBlank).collect(Collectors.toList());
+        }
+        return Collections.emptyList();
+    }
+
+    public List<String> getWeChatAddressees() {
+        if (Boolean.TRUE.equals(informStrategy.getWechat())) {
+            return users.stream().map(KeycloakUserCO::getWechat).filter(StringUtils::isNotBlank).collect(Collectors.toList());
+        }
+        return Collections.emptyList();
+    }
+
+    public List<String> getUserIds() {
+        if (Boolean.TRUE.equals(informStrategy.getWechatApplet())) {
+            return users.stream().map(KeycloakUserCO::getId).filter(StringUtils::isNotBlank).collect(Collectors.toList());
+        }
+        return Collections.emptyList();
+    }
+
+    public List<String> getWeChatAppletAddressees() {
+        if (Boolean.TRUE.equals(informStrategy.getWechatApplet())) {
+            return users.stream().map(KeycloakUserCO::getWechatAppletOpenId).filter(StringUtils::isNotBlank).collect(Collectors.toList());
+        }
+        return Collections.emptyList();
+    }
+
+    public List<String> getPhoneAddressees() {
+        if (Boolean.TRUE.equals(informStrategy.getNote())) {
+            return users.stream().map(KeycloakUserCO::getPhone).filter(StringUtils::isNotBlank).collect(Collectors.toList());
+        }
+        return Collections.emptyList();
+    }
+
+}

+ 1 - 1
smsb-customer-manager-client/src/main/java/com/inspur/customer/object/tenant/ExceptionInformStrategyCmd.java

@@ -28,7 +28,7 @@ public class ExceptionInformStrategyCmd extends Command {
     private Boolean email;
 
     /**
-     * inform user by wechat
+     * inform user by weChat
      */
     private Boolean wechat;
 

+ 12 - 0
smsb-customer-manager-client/src/main/java/com/inspur/customer/object/tenant/TenantExceptionInformStrategyCO.java

@@ -1,6 +1,7 @@
 package com.inspur.customer.object.tenant;
 
 import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
 
 import java.io.Serializable;
 import java.util.HashMap;
@@ -12,6 +13,7 @@ import java.util.Map;
  * @date 2022-06-07 18:37
  **/
 @Data
+@Slf4j
 public class TenantExceptionInformStrategyCO implements Serializable {
 
     private static final long serialVersionUID = -2981913767151035603L;
@@ -54,6 +56,16 @@ public class TenantExceptionInformStrategyCO implements Serializable {
         return strategy.get(level).getStrategyFlag();
     }
 
+    public ExceptionInformStrategyCmd getInformStrategy(String level) {
+        ExceptionInformStrategyCmd informStrategyCmd = strategy.get(level);
+        if (informStrategyCmd == null) {
+            log.error("strategy is null: {}", level);
+            // email and weChat
+            informStrategyCmd =  new ExceptionInformStrategyCmd(6);
+        }
+        return informStrategyCmd;
+    }
+
     public void updateStrategy(String strategyKey, ExceptionInformStrategyCmd informStrategyCmd) {
         this.strategy.put(strategyKey, informStrategyCmd);
     }