Pārlūkot izejas kodu

add white user table

huodongdong 6 gadi atpakaļ
vecāks
revīzija
2798495bf0

+ 43 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/controller/WhiteUserController.java

@@ -3,22 +3,32 @@ package cn.rankin.userservice.controller;
 import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
 import cn.rankin.common.utils.enums.BaseOrderEnum;
+import cn.rankin.common.utils.util.ListUtil;
 import cn.rankin.data.api.user.dto.WhiteUserDTO;
 import cn.rankin.data.api.user.dto.WhiteUserSearchDTO;
+import cn.rankin.data.api.user.entity.TerminalUser;
 import cn.rankin.data.api.user.entity.WhiteUser;
 import cn.rankin.userservice.code.UserServiceAPICode;
+import cn.rankin.userservice.service.TerminalUserService;
 import cn.rankin.userservice.service.WhiteUserService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
 
 @RestController
 @RequestMapping(value = "/white/user")
 public class WhiteUserController {
 
     @Autowired
+    private TerminalUserService terminalUserService;
+
+    @Autowired
     private WhiteUserService whiteUserService;
 
     @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
@@ -27,6 +37,7 @@ public class WhiteUserController {
         if (whiteUser == null) {
             return APIResult.error(UserServiceAPICode.NOT_EXISTS);
         }
+        setUserInfo(whiteUser);
         return APIResult.ok(whiteUser);
     }
 
@@ -36,6 +47,7 @@ public class WhiteUserController {
         if (whiteUser != null) {
             return APIResult.ok(whiteUser);
         }
+        setUserInfo(whiteUser);
         return APIResult.error(UserServiceAPICode.error("创建失败"));
     }
 
@@ -45,6 +57,7 @@ public class WhiteUserController {
         if (whiteUser != null) {
             return APIResult.ok(whiteUser);
         }
+        setUserInfo(whiteUser);
         return APIResult.error(UserServiceAPICode.error("更新失败"));
     }
 
@@ -70,6 +83,36 @@ public class WhiteUserController {
         LinkedHashMap<String, BaseOrderEnum> sort = new LinkedHashMap<>();
         sort.put("gmtModified", BaseOrderEnum.DESC);
         Page<WhiteUser> page = whiteUserService.search(sample, pageNo, pageSize, sort);
+        List<WhiteUser> whiteUserList = page.getList();
+        setUserInfo(whiteUserList);
         return APIResult.ok(page);
     }
+
+    public void setUserInfo(WhiteUser whiteUser) {
+        if (whiteUser == null) {
+            return;
+        }
+        String userId = whiteUser.getUserId();
+        TerminalUser terminalUser = terminalUserService.find(userId);
+        if (terminalUser != null) {
+            whiteUser.setCode(terminalUser.getCode());
+        }
+    }
+
+    public void setUserInfo(List<WhiteUser> whiteUserList) {
+        if (CollectionUtils.isEmpty(whiteUserList)) {
+            return;
+        }
+        List<String> userIdList = new ArrayList<>();
+        whiteUserList.forEach(whiteUser -> userIdList.add(whiteUser.getUserId()));
+        List<TerminalUser> terminalUserList = terminalUserService.findByIds(userIdList);
+        Map<String, TerminalUser> terminalUserMap = ListUtil.convert(terminalUserList, "id", TerminalUser.class);
+        for (WhiteUser whiteUser : whiteUserList) {
+            String userId = whiteUser.getUserId();
+            TerminalUser terminalUser = terminalUserMap.get(userId);
+            if (terminalUser != null) {
+                whiteUser.setCode(terminalUser.getCode());
+            }
+        }
+    }
 }