Bladeren bron

新增用户查询/新增/修改/禁用功能

wangliang 1 jaar geleden
bovenliggende
commit
988e3f8376

+ 63 - 0
sop-business/src/main/java/com/qmth/sop/business/bean/dto/RoleDto.java

@@ -0,0 +1,63 @@
+package com.qmth.sop.business.bean.dto;
+
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import com.qmth.sop.common.enums.RoleTypeEnum;
+import io.swagger.annotations.ApiModelProperty;
+
+import java.io.Serializable;
+
+/**
+ * @Description: 用户dto
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2023/8/7
+ */
+public class RoleDto implements Serializable {
+
+    @JsonSerialize(using = ToStringSerializer.class)
+    @ApiModelProperty(value = "主键")
+    private Long id;
+
+    @ApiModelProperty(value = "角色名称")
+    private String name;
+
+    @ApiModelProperty(value = "是否启用,0:停用,1:启用")
+    private Boolean enable;
+
+    @ApiModelProperty(value = "角色类别,ADMIN:超级管理员,PMO:总负责人,BUSSINESS:业务线负责人,REGION_MANAGER:大区经理,REGION_COORDINATOR:区域协调人,EFFECT_ENGINEER:实施工程师,ASSISTANT_ENGINEER:助理工程师,QA:QA,CUSTOM:技术客服")
+    private RoleTypeEnum type;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Boolean getEnable() {
+        return enable;
+    }
+
+    public void setEnable(Boolean enable) {
+        this.enable = enable;
+    }
+
+    public RoleTypeEnum getType() {
+        return type;
+    }
+
+    public void setType(RoleTypeEnum type) {
+        this.type = type;
+    }
+}

+ 17 - 13
sop-business/src/main/java/com/qmth/sop/business/bean/dto/UserDto.java

@@ -2,11 +2,11 @@ package com.qmth.sop.business.bean.dto;
 
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
-import com.qmth.sop.business.entity.SysRole;
 import com.qmth.sop.common.enums.GenderEnum;
 
 import java.io.Serializable;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * @Description: 用户dto
@@ -24,14 +24,26 @@ public class UserDto implements Serializable {
     private String realName;
     private String mobileNumber;
     private Boolean enable;
-    private String remark;
 
     @JsonSerialize(using = ToStringSerializer.class)
     private Long orgId;
 
     private String orgName;
     private GenderEnum gender;
-    private List<SysRole> roles;
+    private String genderStr;
+    private List<RoleDto> roles;
+
+    public String getGenderStr() {
+        if (Objects.nonNull(this.gender)) {
+            return this.gender.getTitle();
+        } else {
+            return genderStr;
+        }
+    }
+
+    public void setGenderStr(String genderStr) {
+        this.genderStr = genderStr;
+    }
 
     public GenderEnum getGender() {
         return gender;
@@ -41,11 +53,11 @@ public class UserDto implements Serializable {
         this.gender = gender;
     }
 
-    public List<SysRole> getRoles() {
+    public List<RoleDto> getRoles() {
         return roles;
     }
 
-    public void setRoles(List<SysRole> roles) {
+    public void setRoles(List<RoleDto> roles) {
         this.roles = roles;
     }
 
@@ -89,14 +101,6 @@ public class UserDto implements Serializable {
         this.enable = enable;
     }
 
-    public String getRemark() {
-        return remark;
-    }
-
-    public void setRemark(String remark) {
-        this.remark = remark;
-    }
-
     public Long getOrgId() {
         return orgId;
     }

+ 9 - 1
sop-business/src/main/java/com/qmth/sop/business/entity/SysUser.java

@@ -98,7 +98,15 @@ public class SysUser extends BaseEntity implements Serializable {
      */
     public void setPasswordInfo(String password) {
         this.password = Base64Util.encode(password.substring(password.length() - 6, password.length()).getBytes());
-        this.enable = true;
+    }
+
+    /**
+     * 更新密码
+     *
+     * @param password
+     */
+    public void updatePasswordInfo(String password) {
+        this.password = Base64Util.encode(password.substring(password.length() - 6, password.length()).getBytes());
     }
 
     public UserSourceEnum getSource() {

+ 4 - 0
sop-business/src/main/java/com/qmth/sop/business/service/impl/SysOrgServiceImpl.java

@@ -151,6 +151,10 @@ public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> impleme
     @Override
     @Transactional
     public Boolean enable(SysOrg org) {
+        Optional.ofNullable(org).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("参数不能为空"));
+        Optional.ofNullable(org.getId()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("id不能为空"));
+        Optional.ofNullable(org.getEnable()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("启用/禁用不能为空"));
+
         // 禁用时,校验机构
         if (!org.getEnable()) {
             // 机构下是否有用户

+ 21 - 16
sop-business/src/main/java/com/qmth/sop/business/service/impl/SysUserServiceImpl.java

@@ -4,10 +4,12 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.google.gson.reflect.TypeToken;
 import com.qmth.boot.api.exception.ApiException;
 import com.qmth.boot.core.enums.Platform;
 import com.qmth.sop.business.bean.auth.AuthBean;
 import com.qmth.sop.business.bean.auth.ExpireTimeBean;
+import com.qmth.sop.business.bean.dto.RoleDto;
 import com.qmth.sop.business.bean.dto.UserDto;
 import com.qmth.sop.business.bean.dto.VerifyCodeCheckDto;
 import com.qmth.sop.business.bean.result.LoginResult;
@@ -25,10 +27,7 @@ import com.qmth.sop.common.contant.SystemConstant;
 import com.qmth.sop.common.enums.AppSourceEnum;
 import com.qmth.sop.common.enums.ExceptionResultEnum;
 import com.qmth.sop.common.enums.FieldUniqueEnum;
-import com.qmth.sop.common.util.IpUtil;
-import com.qmth.sop.common.util.ResultUtil;
-import com.qmth.sop.common.util.ServletUtil;
-import com.qmth.sop.common.util.SessionUtil;
+import com.qmth.sop.common.util.*;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -213,10 +212,13 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
             userDtoIPage.getRecords().forEach(m -> {
                 //角色
                 List<SysRole> roles = sysRoleService.listRolesByUserId(Long.valueOf(m.getId()));
-                m.setRoles(roles);
+                if (!CollectionUtils.isEmpty(roles)) {
+                    m.setRoles(GsonUtil.fromJson(GsonUtil.toJson(roles), new TypeToken<List<RoleDto>>() {
+                    }.getType()));
+                }
             });
         }
-        return this.baseMapper.query(iPage, userInfo, orgId, roleId, enable);
+        return userDtoIPage;
     }
 
     /**
@@ -231,33 +233,29 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
         SysUserService sysUserService = SpringContextHolder.getBean(SysUserService.class);
         try {
             SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
-
-            // 手机号检验
-            if (Objects.nonNull(sysUser.getMobileNumber())) {
-                SysUser checkMobileNumber = this.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getOrgId, sysUser.getOrgId()).eq(SysUser::getEnable, true).eq(SysUser::getMobileNumber, sysUser.getMobileNumber()));
-                if (Objects.nonNull(checkMobileNumber) && checkMobileNumber.getId().longValue() != sysUser.getId().longValue()) {
-                    throw ExceptionResultEnum.ERROR.exception("用户手机号[" + sysUser.getMobileNumber() + "]在系统中已使用");
-                }
-            }
             if (Objects.isNull(sysUser.getId())) {//新增用户
                 sysUser.insertInfo(requestUser.getId());
                 sysUser.setPasswordInfo(sysUser.getMobileNumber());
                 sysUserService.save(sysUser);
                 sysUserRoleService.addUserRolePrivilege(sysUser, sysUser.getRoleIds());
             } else {//修改用户
+                sysUser.updatePasswordInfo(sysUser.getMobileNumber());
+                SysUser dbUser = this.getById(sysUser.getId());
+                Optional.ofNullable(dbUser).orElseThrow(() -> ExceptionResultEnum.USER_NO_EXISTS.exception());
+
                 List<SysUserRole> sysUserRoleList = commonCacheService.userRolePrivilegeCache(sysUser.getId());
                 List<Long> userRolesList = Arrays.asList(sysUser.getRoleIds());
                 Set<Long> dbUserRolesList = sysUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toSet());
                 int count = (int) dbUserRolesList.stream().filter(s -> !userRolesList.contains(s)).count();
-                SysUser dbUser = sysUserService.getById(sysUser.getId());
                 sysUserService.update(new UpdateWrapper<SysUser>().lambda()
                         .eq(SysUser::getId, sysUser.getId())
                         .set(SysUser::getLoginName, sysUser.getLoginName())
                         .set(SysUser::getRealName, sysUser.getRealName())
                         .set(SysUser::getCode, sysUser.getCode())
                         .set(SysUser::getMobileNumber, sysUser.getMobileNumber())
+                        .set(SysUser::getPassword, sysUser.getPassword())
+                        .set(SysUser::getGender, sysUser.getGender())
                         .set(SysUser::getOrgId, sysUser.getOrgId())
-                        .set(SysUser::getEnable, dbUser.getEnable())
                         .set(SysUser::getUpdateId, requestUser.getId())
                         .set(SysUser::getUpdateTime, System.currentTimeMillis())
                 );
@@ -304,6 +302,13 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
     @Override
     @Transactional
     public Boolean enable(SysUser sysUser) throws NoSuchAlgorithmException {
+        Optional.ofNullable(sysUser).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("参数不能为空"));
+        Optional.ofNullable(sysUser.getId()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("id不能为空"));
+        Optional.ofNullable(sysUser.getEnable()).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("启用/禁用不能为空"));
+
+        SysUser sysUserDb = this.getById(sysUser.getId());
+        Optional.ofNullable(sysUserDb).orElseThrow(() -> ExceptionResultEnum.USER_NO_EXISTS.exception());
+
         this.update(new UpdateWrapper<SysUser>().lambda().set(SysUser::getEnable, sysUser.getEnable()).eq(SysUser::getId, sysUser.getId()));
         commonCacheService.updateUserCache(sysUser.getId());
         //如果状态为禁用,需要踢下线重新登录

+ 2 - 1
sop-business/src/main/resources/mapper/SysRoleMapper.xml

@@ -7,7 +7,8 @@
             a.id,
             a.name,
             a.type,
-            a.default_role
+            a.default_role,
+            a.enable
         FROM
             sys_role a
                 JOIN

+ 3 - 3
sop-business/src/main/resources/mapper/SysUserMapper.xml

@@ -85,9 +85,8 @@
         a.remark,
         a.org_id orgId,
         b.name orgName
-        FROM
-        sys_user a
-        left join
+        FROM sys_user a
+        join
         sys_org b on a.org_id = b.id
         <where>
             <if test="userInfo != null and userInfo != ''">
@@ -102,6 +101,7 @@
             <if test="enable != null and enable != '' or enable == 0">
                 and a.enable = #{enable}
             </if>
+                and a.source = 'SYSTEM'
         </where>
         order by a.create_time desc
     </select>

+ 2 - 0
sop-common/src/main/java/com/qmth/sop/common/enums/ExceptionResultEnum.java

@@ -43,6 +43,8 @@ public enum ExceptionResultEnum {
 
     ORG_NO_DATA(HttpStatus.INTERNAL_SERVER_ERROR, 5000005, "没有机构数据"),
 
+    USER_NO_EXISTS(HttpStatus.INTERNAL_SERVER_ERROR, 5000006, "没有用户数据"),
+
     USER_NO_DATA(HttpStatus.INTERNAL_SERVER_ERROR, 5000009, "用户或密码不正确"),
 
     USER_ENABLE(HttpStatus.INTERNAL_SERVER_ERROR, 5000011, "用户已禁用"),

+ 0 - 1
sop-server/src/main/java/com/qmth/sop/server/api/SysUserController.java

@@ -62,5 +62,4 @@ public class SysUserController {
     public Result enable(@RequestBody SysUser sysUser) throws NoSuchAlgorithmException {
         return ResultUtil.ok(sysUserService.enable(sysUser));
     }
-
 }