TRX 1 year ago
parent
commit
babd480f07

+ 16 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/org/DelUserCountParam.java

@@ -0,0 +1,16 @@
+package com.zhongshu.card.client.model.org;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * @author TRX
+ * @date 2024/6/6
+ */
+@Data
+public class DelUserCountParam {
+    @Schema(description = "用户UserId")
+    private String userId;
+    @Schema(description = "机构ID")
+    private String oid;
+}

+ 22 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/org/UpdateUserStateParam.java

@@ -0,0 +1,22 @@
+package com.zhongshu.card.client.model.org;
+
+import com.zhongshu.card.client.utils.type.UserState;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * @author TRX
+ * @date 2024/6/6
+ */
+@Data
+public class UpdateUserStateParam {
+
+    @Schema(description = "用户UserId")
+    private String userId;
+
+    @Schema(description = "机构ID")
+    private String oid;
+
+    @Schema(description = "用户状态")
+    UserState userState;
+}

+ 15 - 9
FullCardClient/src/main/java/com/zhongshu/card/client/model/org/UserCountAddParam.java

@@ -2,9 +2,12 @@ package com.zhongshu.card.client.model.org;
 
 import com.zhongshu.card.client.model.base.SuperParam;
 import com.zhongshu.card.client.utils.type.Sex;
+import com.zhongshu.card.client.utils.type.UserState;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
 
+import java.util.List;
+
 /**
  * @author TRX
  * @date 2024/5/31
@@ -12,21 +15,12 @@ import lombok.Data;
 @Data
 public class UserCountAddParam extends SuperParam {
 
-    @Schema(description = "用户ID")
-    private String userId;
-
-    @Schema(description = "登录名")
-    private String loginName;
-
     @Schema(description = "用户手机号")
     private String phone;
 
     @Schema(description = "姓名")
     private String name;
 
-    @Schema(description = "用户名  用户真实姓名的拼音")
-    private String spellCode;
-
     @Schema(description = "头像")
     private String profilePic;
 
@@ -35,4 +29,16 @@ public class UserCountAddParam extends SuperParam {
 
     @Schema(description = "地址")
     private String address;
+
+    @Schema(description = "用户状态")
+    UserState state = UserState.Normal;
+
+    @Schema(description = "部门信息")
+    List<String> departmentIds;
+
+    @Schema(description = "角色信息")
+    List<String> roleIds;
+
+    @Schema(description = "密码")
+    String passWord;
 }

+ 6 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/service/org/UserAccountService.java

@@ -20,6 +20,12 @@ public interface UserAccountService {
     @RequestMapping(value = "update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
     ResultContent update(@RequestBody UserCountAddParam param);
 
+    // 删除机构用户
+    ResultContent deleteOrgUser(DelUserCountParam param);
+
+    ResultContent updateOrgUserState(UpdateUserStateParam param);
+
+    // 当前用户修改密码
     ResultContent updatePassWord(PassWordModel param);
 
     // 重置用户密码(默认密码)

+ 2 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/utils/type/UserType.java

@@ -8,6 +8,8 @@ import lombok.Getter;
 public enum UserType {
     Consumer("普通用户"),
     Platform("平台用户"),
+    Student("学生"),
+    Teacher("老师"),
     SuperAdmin("超级管理员用户"),
     ;
 

+ 16 - 4
FullCardServer/src/main/java/com/zhongshu/card/server/core/controller/org/UserController.java

@@ -5,9 +5,7 @@ import com.github.microservice.auth.security.helper.AuthHelper;
 import com.github.microservice.auth.security.type.AuthType;
 import com.zhongshu.card.client.model.base.AuthTypeParam;
 import com.zhongshu.card.client.model.base.UserIdModel;
-import com.zhongshu.card.client.model.org.OrganizationModel;
-import com.zhongshu.card.client.model.org.OrganizationUserSearch;
-import com.zhongshu.card.client.model.org.UserCountDetailsModel;
+import com.zhongshu.card.client.model.org.*;
 import com.zhongshu.card.client.ret.ResultContent;
 import com.zhongshu.card.client.service.org.OrganizationService;
 import com.zhongshu.card.client.service.org.UserAccountService;
@@ -45,7 +43,21 @@ public class UserController {
     @Autowired
     UserAccountService userAccountService;
 
-    @ResourceAuth(value = "user",type = AuthType.User)
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "添加-编辑用户", description = "添加-编辑用户")
+    @RequestMapping(value = {"update"}, method = {RequestMethod.POST})
+    public ResultContent update(@RequestBody UserCountAddParam param) {
+        return userAccountService.update(param);
+    }
+
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "删除机构用户", description = "删除机构用户")
+    @RequestMapping(value = {"deleteOrgUser"}, method = {RequestMethod.POST})
+    public ResultContent deleteOrgUser(@RequestBody DelUserCountParam param) {
+        return userAccountService.deleteOrgUser(param);
+    }
+
+    @ResourceAuth(value = "user", type = AuthType.User)
     @Operation(summary = "重置用户密码", description = "重置用户密码(重置为默认密码)")
     @RequestMapping(value = {"resetPassWord"}, method = {RequestMethod.POST})
     public ResultContent resetPassWord(@RequestBody UserIdModel param) {

+ 2 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/dao/org/DepartmentDao.java

@@ -20,4 +20,6 @@ public interface DepartmentDao extends MongoDao<Department> {
     Department findByName(String name);
 
     List<Department> findByParentIdAndOid(String parentId, String oid);
+
+    List<Department> findByOidAndIdIn(String oid, List<String> ids);
 }

+ 4 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/dao/org/RoleDao.java

@@ -6,6 +6,8 @@ import com.zhongshu.card.server.core.dao.org.extend.UserCountDaoExtend;
 import com.zhongshu.card.server.core.domain.org.Organization;
 import com.zhongshu.card.server.core.domain.org.Role;
 
+import java.util.List;
+
 /**
  * @author TRX
  * @date 2024/3/21
@@ -17,4 +19,6 @@ public interface RoleDao extends MongoDao<Role>, RoleDaoExtend {
     Role findTopByNameAndOid(String name, String oid);
 
     Role findTopByCodeAndOid(String code, String oid);
+
+    List<Role> findByOidAndIdIn(String oid, List<String> ids);
 }

+ 0 - 3
FullCardServer/src/main/java/com/zhongshu/card/server/core/domain/org/OrganizationUser.java

@@ -46,9 +46,6 @@ public class OrganizationUser extends SuperMain {
     @Indexed
     private String code;
 
-    @Schema(description = "登录名")
-    private String loginName;
-
     @Schema(description = "用户手机号")
     @Indexed
     private String phone;

+ 1 - 1
FullCardServer/src/main/java/com/zhongshu/card/server/core/domain/org/UserAccount.java

@@ -65,5 +65,5 @@ public class UserAccount extends SuperMain {
     private Boolean isUpdatedPsw;
 
     @Schema(description = "是否登录过")
-    private Boolean isLogined;
+    private Boolean isLogined = Boolean.FALSE;
 }

+ 11 - 1
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/TestService.java

@@ -2,6 +2,7 @@ package com.zhongshu.card.server.core.service;
 
 import com.zhongshu.card.client.ret.ResultContent;
 import com.zhongshu.card.client.service.org.OrganizationService;
+import com.zhongshu.card.server.core.service.base.RedisService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -16,7 +17,16 @@ public class TestService {
     @Autowired
     OrganizationService organizationService;
 
-    public ResultContent text(){
+    @Autowired
+    RedisService redisService;
+
+    public ResultContent text() {
+        try {
+            redisService.setValueSecond("a", "123", 3);
+            log.info("a: {}", redisService.getValue("a"));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
         return organizationService.initSuperOrganization();
     }
 }

+ 197 - 9
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/org/UserAccountServiceImpl.java

@@ -1,7 +1,10 @@
 package com.zhongshu.card.server.core.service.org;
 
 import com.github.microservice.auth.client.content.ResultState;
+import com.github.microservice.auth.client.model.UserAuthModel;
+import com.github.microservice.auth.client.service.RoleService;
 import com.github.microservice.auth.client.service.UserService;
+import com.github.microservice.auth.client.type.LoginType;
 import com.github.microservice.auth.security.type.AuthType;
 import com.github.microservice.components.data.base.util.PageEntityUtil;
 import com.zhongshu.card.client.model.base.AuthTypeParam;
@@ -12,14 +15,21 @@ import com.zhongshu.card.client.ret.ResultMessage;
 import com.zhongshu.card.client.service.org.OrganizationService;
 import com.zhongshu.card.client.service.org.UserAccountService;
 import com.zhongshu.card.client.utils.type.Sex;
+import com.zhongshu.card.client.utils.type.UserLoginType;
 import com.zhongshu.card.client.utils.type.UserState;
-import com.zhongshu.card.server.core.dao.org.UserCountDao;
+import com.zhongshu.card.client.utils.type.UserType;
+import com.zhongshu.card.server.core.dao.org.*;
 import com.zhongshu.card.server.core.dataConfig.CardDefault;
-import com.zhongshu.card.server.core.domain.org.Organization;
-import com.zhongshu.card.server.core.domain.org.UserAccount;
+import com.zhongshu.card.server.core.domain.org.*;
 import com.zhongshu.card.server.core.service.base.SuperService;
 import com.zhongshu.card.server.core.util.BeanUtils;
 import lombok.extern.slf4j.Slf4j;
+import net.sourceforge.pinyin4j.PinyinHelper;
+import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
+import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
+import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
+import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
+import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -30,7 +40,9 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.Assert;
 
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * @author TRX
@@ -50,16 +62,159 @@ public class UserAccountServiceImpl extends SuperService implements UserAccountS
     @Autowired
     UserService userService;
 
+    @Autowired
+    RoleDao roleDao;
+
+    @Autowired
+    DepartmentDao departmentDao;
+
+    @Autowired
+    OrganizationDao organizationDao;
+
+    @Autowired
+    OrganizationUserDao organizationUserDao;
+
+    @Autowired
+    RoleService roleService;
+
+    /**
+     * 添加编辑用户
+     *
+     * @param param
+     * @return
+     */
     @Override
     public ResultContent update(UserCountAddParam param) {
-        UserAccount userCount = new UserAccount();
-        if (StringUtils.isNotEmpty(param.getUserId())) {
-            UserAccount userCount1 = userCountDao.findTopByUserId(param.getUserId());
-            if (ObjectUtils.isEmpty(userCount1)) {
-                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getUserId()));
+        String oid = param.getOid();
+        if (StringUtils.isEmpty(oid)) {
+            oid = getCurrentOid();
+        }
+        Assert.hasText(oid, "oid不能为空");
+        Assert.hasText(param.getPhone(), "phone不能为空");
+        Assert.hasText(param.getName(), "name不能为空");
+        if (param.getState() == null) {
+            param.setState(UserState.Normal);
+        }
+        // 机构信息
+        Organization organization = organizationDao.findTopByOid(oid);
+        if (ObjectUtils.isEmpty(organization)) {
+            return ResultContent.buildFail(String.format("oid不存在:%s", oid));
+        }
+        List<Role> roles = new ArrayList<>();
+        List<Department> departments = new ArrayList<>();
+        if (ObjectUtils.isNotEmpty(param.getRoleIds())) {
+            roles = roleDao.findByOidAndIdIn(oid, param.getRoleIds());
+        }
+        if (ObjectUtils.isNotEmpty(param.getDepartmentIds())) {
+            departments = departmentDao.findByOidAndIdIn(oid, param.getDepartmentIds());
+        }
+        UserAccount temp = null;
+        String loginName = param.getPhone();
+        String userId = "";
+        if (StringUtils.isEmpty(param.getId())) {
+            // 添加
+            // 判断用户是否存在
+            temp = userCountDao.findTopByLoginName(loginName);
+            if (ObjectUtils.isEmpty(temp)) {
+                String password = param.getPassWord();
+                if (StringUtils.isEmpty(password)) {
+                    password = CardDefault.DEFAULT_PASSWORD;
+                }
+
+                //权限中心:添加用户
+                com.github.microservice.auth.client.content.ResultContent<String> addModelResult = userService.add(UserAuthModel.builder().loginType(LoginType.Phone).loginValue(loginName).passWord(password).build());
+                if (addModelResult.getState() != ResultState.Success) {
+                    return ResultContent.buildFail(String.format("权限中心添加用户出错:%s", addModelResult.getMsg()));
+                }
+                temp = new UserAccount();
+                BeanUtils.copyProperties(param, temp);
+                userId = addModelResult.getContent();
+                temp.setUserId(userId);
+                temp.setUserType(UserType.Consumer);
+                temp.setLoginType(UserLoginType.All);
+                temp.setOid(null);
+                temp.setLoginName(loginName);
+                temp.setIsLogined(Boolean.FALSE);
+                temp.setSpellCode(getPinyin(param.getName()));
+                userCountDao.save(temp);
+            }
+        } else {
+            temp = userCountDao.findTopById(param.getId());
+            if (ObjectUtils.isEmpty(temp)) {
+                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
             }
+            BeanUtils.copyProperties(param, temp);
+            temp.setSpellCode(getPinyin(param.getName()));
+            userCountDao.save(temp);
         }
-        // 在授权中心添加用户
+
+        //权限中心: 在角色组添加用户
+        String[] roleGroupIds = roles.stream().map(it -> it.getRoleGroupId()).collect(Collectors.toList()).toArray(String[]::new);
+        roleService.addUsersToRoleGroup(userId, roleGroupIds);
+
+        OrganizationUser organizationUser = organizationUserDao.findTopByOrganizationAndUser(organization, temp);
+        if (ObjectUtils.isEmpty(organizationUser)) {
+            // 不在机构中
+            organizationUser = new OrganizationUser();
+            organizationUser.setOrganization(organization);
+            organizationUser.setUser(temp);
+            organizationUser.setUserId(temp.getUserId());
+            organizationUser.setIsAdmin(Boolean.FALSE);
+            organizationUser.setAuthType(organizationUser.getAuthType());
+            organizationUser.setCreateUserId(getCurrentUserId());
+        } else {
+            //权限中心: 删除以前的,这不存在的角色组
+            List<Role> oldRoles = organizationUser.getRoles();
+            if (ObjectUtils.isNotEmpty(oldRoles)) {
+                List<String> ids = roles.stream().map(it -> it.getRoleGroupId()).collect(Collectors.toList());
+                String[] userIds = new String[]{userId};
+                for (Role oldRole : oldRoles) {
+                    if (!ids.contains(oldRole.getRoleGroupId())) {
+                        roleService.removeUserFromRoleGroup(oldRole.getRoleGroupId(), userIds);
+                    }
+                }
+            }
+        }
+        organizationUser.setPhone(temp.getPhone());
+        organizationUser.setName(temp.getName());
+        organizationUser.setState(temp.getState());
+        organizationUser.setRoles(roles);
+        organizationUser.setDepartments(departments);
+        organizationUserDao.save(organizationUser);
+
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 删除机构用户
+     *
+     * @param param
+     * @return
+     */
+    @Override
+    public ResultContent deleteOrgUser(DelUserCountParam param) {
+
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 修改用户状态
+     *
+     * @param param
+     * @return
+     */
+    @Override
+    public ResultContent updateOrgUserState(UpdateUserStateParam param) {
+        String userId = param.getUserId();
+        String oid = param.getOid();
+        if (StringUtils.isEmpty(oid)) {
+            oid = getCurrentOid();
+        }
+        Organization organization = organizationDao.findTopByOid(oid);
+        UserAccount temp = userCountDao.findTopByUserId(userId);
+        OrganizationUser organizationUser = organizationUserDao.findTopByOrganizationAndUser(organization, temp);
+        organizationUser.setState(param.getUserState());
+        organizationUserDao.save(organizationUser);
         return ResultContent.buildSuccess();
     }
 
@@ -91,6 +246,7 @@ public class UserAccountServiceImpl extends SuperService implements UserAccountS
 
     /**
      * 重置密码
+     *
      * @param param
      * @return
      */
@@ -193,4 +349,36 @@ public class UserAccountServiceImpl extends SuperService implements UserAccountS
         }
         return model;
     }
+
+    private String getPinyin(String chinaStr) {
+        if (StringUtils.isEmpty(chinaStr)) {
+            return "";
+        }
+        HanyuPinyinOutputFormat forMat = new HanyuPinyinOutputFormat();
+        forMat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
+        forMat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
+        forMat.setVCharType(HanyuPinyinVCharType.WITH_V);
+        char[] arrays = chinaStr.trim().toCharArray();
+        String result = "";
+        try {
+            for (int i = 0; i < arrays.length; i++) {
+                char ti = arrays[i];
+                if (Character.toString(ti).matches("[\\u4e00-\\u9fa5]")) { //匹配是否是中文
+                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(ti, forMat);
+                    result += temp[0];
+                } else {
+                    result += ti;
+                }
+            }
+        } catch (BadHanyuPinyinOutputFormatCombination e) {
+            e.printStackTrace();
+        }
+        //判断如果包含重庆,则替换拼音中的zhongqing为chongqing
+        if (chinaStr.indexOf("重庆") == -1) {
+            //do nothing
+        } else {
+            result = result.replace("zhongqing", "chongqing");
+        }
+        return result;
+    }
 }