Explorar el Código

增加企业绑定和查看绑定列表

MOKASZ\wzk12044 hace 5 años
padre
commit
b586d6bfc5

+ 39 - 6
src/main/java/com/mokamrp/privates/controller/UserCorpController.java

@@ -3,6 +3,7 @@ package com.mokamrp.privates.controller;
 
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.mokamrp.privates.constant.Constants;
+import com.mokamrp.privates.entity.SetUserCorpsHandle;
 import com.mokamrp.privates.help.AjaxResult;
 import com.mokamrp.privates.interceptor.AuthUser;
 import com.mokamrp.privates.mapper.pojo.Corp;
@@ -10,10 +11,9 @@ import com.mokamrp.privates.mapper.pojo.User;
 import com.mokamrp.privates.mapper.pojo.UserCorp;
 import com.mokamrp.privates.service.UserCorpService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
 import java.util.List;
 import java.util.Map;
 
@@ -38,14 +38,47 @@ public class UserCorpController {
      */
     @GetMapping("/getAuthUserCorps")
     public Object getAuthUserCorps(@AuthUser User authUser){
-        List<Corp> res = userCorpService.getAuthUserCorps(authUser.getId());
+        List<Corp> res = userCorpService.getCorpsByUserId(authUser.getId());
         return AjaxResult.success(Constants.SUCCESS,res);
     }
 
+    /**
+     * 通过用户ID查询管辖的企业列表
+     * @param authUser
+     * @param userId
+     * @return
+     */
+    @GetMapping("/getCorpsByUserId")
+    public Object getCorpsByUserId(@AuthUser User authUser,@RequestParam(name = "userId") Integer userId){
+        if(authUser.getIsSuperAdmin() == 1){
+            List<Corp> res = userCorpService.getCorpsByUserId(userId);
+            return AjaxResult.success(res);
+        }else{
+            return AjaxResult.error("Permission denied");
+        }
+    }
+
+    /**
+     * 绑定用户企业信息
+     * @param setUserCorpsHandle
+     * @param authUser
+     * @return
+     */
     @PostMapping("/setUserCorpsByUserId")
-    public Object setUserCorpsByUserId(@AuthUser User authUser){
+    public Object setUserCorpsByUserId(@Valid @RequestBody SetUserCorpsHandle setUserCorpsHandle, @AuthUser User authUser){
         if (authUser.getIsSuperAdmin() == 1){
+            userCorpService.setUserCorpsByUserId(setUserCorpsHandle.getUserIds(), setUserCorpsHandle.getCorpIds());
+        }else{
+            return AjaxResult.error("Permission denied");
+        }
+        return AjaxResult.success(Constants.SUCCESS);
+    }
+
 
+    @GetMapping("/getUserCorpsByUserId")
+    public Object getUserCorpsByUserId(@RequestParam(name = "userId") String UserId){
+        if (UserId == null || UserId.isEmpty()){
+            return AjaxResult.error("userId null");
         }
         return null;
     }

+ 13 - 0
src/main/java/com/mokamrp/privates/entity/SetUserCorpsHandle.java

@@ -0,0 +1,13 @@
+package com.mokamrp.privates.entity;
+
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+import java.util.List;
+
+@Data
+public class SetUserCorpsHandle {
+    private List<Integer> userIds;
+
+    private List<Integer> corpIds;
+}

+ 4 - 1
src/main/java/com/mokamrp/privates/service/UserCorpService.java

@@ -22,7 +22,10 @@ public interface UserCorpService extends IService<UserCorp> {
      * @param userId
      * @return
      */
-    public List<Corp> getAuthUserCorps (Integer userId);
+    public List<Corp> getCorpsByUserId(Integer userId);
 
     public List<Integer> getAuthUserCorpIds(Integer userId);
+
+    public boolean setUserCorpsByUserId(List<Integer> userIds,List<Integer> corpIds);
+
 }

+ 36 - 1
src/main/java/com/mokamrp/privates/service/impl/UserCorpServiceImpl.java

@@ -2,6 +2,7 @@ package com.mokamrp.privates.service.impl;
 
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.mokamrp.privates.mapper.pojo.Corp;
+import com.mokamrp.privates.mapper.pojo.User;
 import com.mokamrp.privates.mapper.pojo.UserCorp;
 import com.mokamrp.privates.mapper.UserCorpMapper;
 import com.mokamrp.privates.service.CorpService;
@@ -33,7 +34,7 @@ public class UserCorpServiceImpl extends ServiceImpl<UserCorpMapper, UserCorp> i
      * @param userId
      * @return
      */
-    public List<Corp> getAuthUserCorps(Integer userId) {
+    public List<Corp> getCorpsByUserId(Integer userId) {
         List<Integer> corpIds = new ArrayList<Integer>();
         List<UserCorp> corps = this.selectList(new EntityWrapper<UserCorp>().eq("user_id", userId));
         for (UserCorp userCorp : corps) {
@@ -58,4 +59,38 @@ public class UserCorpServiceImpl extends ServiceImpl<UserCorpMapper, UserCorp> i
         return corpIds;
     }
 
+    /**
+     * 绑定账户和企业ID
+     * @param userIds
+     * @param corpIds
+     * @return
+     */
+    public boolean setUserCorpsByUserId(List<Integer> userIds,List<Integer> corpIds){
+        for (int i = 0; i < userIds.size(); i++){
+            //.当前库里当前用户的所有企业
+            List<UserCorp> corps = this.selectList(new EntityWrapper<UserCorp>().eq("user_id",userIds.get(i)));
+            List<UserCorp> add = new ArrayList<UserCorp>();
+            //.遍历本次需要绑定的企业ID
+            for (int c = 0; c < corpIds.size(); c++){
+                //.遍历企业库里企业 和本次绑定企业
+                Boolean hasCorp = false;
+                for (UserCorp userCorp : corps){
+                    if (userCorp.getCorpId().equals(corpIds.get(c))){
+                        hasCorp = true;
+                    }
+                }
+                if (hasCorp == false){
+                    UserCorp userCorpObj = new UserCorp();
+                    userCorpObj.setCorpId(corpIds.get(c));
+                    userCorpObj.setUserId(userIds.get(i));
+                    add.add(userCorpObj);
+                }
+            }
+            if (add.size() > 0){
+                this.insertBatch(add);
+            }
+        }
+        return true;
+    }
+
 }