From c50e176c4a6b9331d2d1c0520bafb18f54f0dee6 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期五, 08 三月 2024 17:35:10 +0800
Subject: [PATCH] 部门增加管理员

---
 src/main/java/com/mindskip/xzs/domain/enums/DeptAdminEnum.java                                     |   23 ++++
 src/main/java/com/mindskip/xzs/domain/enums/RoleEnum.java                                          |    3 
 src/main/java/com/mindskip/xzs/configuration/spring/security/RestAuthenticationProvider.java       |   26 ++--
 src/main/resources/mapper/DepartmentMapper.xml                                                     |    9 +
 src/main/java/com/mindskip/xzs/configuration/spring/security/SecurityConfigurer.java               |    3 
 src/main/java/com/mindskip/xzs/domain/User.java                                                    |  158 +------------------------------
 src/main/java/com/mindskip/xzs/service/impl/DepartmentServiceImpl.java                             |   45 +++++++-
 src/main/java/com/mindskip/xzs/configuration/spring/security/RestAuthenticationSuccessHandler.java |    2 
 src/main/java/com/mindskip/xzs/domain/vo/UpdateDeptAdminVO.java                                    |    2 
 src/main/java/com/mindskip/xzs/repository/DepartmentMapper.java                                    |    2 
 src/main/java/com/mindskip/xzs/repository/UserMapper.java                                          |    7 +
 src/main/resources/mapper/UserMapper.xml                                                           |   14 ++
 12 files changed, 116 insertions(+), 178 deletions(-)

diff --git a/src/main/java/com/mindskip/xzs/configuration/spring/security/RestAuthenticationProvider.java b/src/main/java/com/mindskip/xzs/configuration/spring/security/RestAuthenticationProvider.java
index 495d408..fd508d2 100644
--- a/src/main/java/com/mindskip/xzs/configuration/spring/security/RestAuthenticationProvider.java
+++ b/src/main/java/com/mindskip/xzs/configuration/spring/security/RestAuthenticationProvider.java
@@ -2,10 +2,13 @@
 
 
 import com.mindskip.xzs.context.WebContext;
+import com.mindskip.xzs.domain.enums.DeptAdminEnum;
 import com.mindskip.xzs.domain.enums.RoleEnum;
 import com.mindskip.xzs.domain.enums.UserStatusEnum;
+import com.mindskip.xzs.repository.DepartmentMapper;
 import com.mindskip.xzs.service.AuthenticationService;
 import com.mindskip.xzs.service.UserService;
+import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.authentication.AuthenticationProvider;
 import org.springframework.security.authentication.BadCredentialsException;
@@ -29,25 +32,13 @@
  * @date 2021/12/25 9:45
  */
 @Component
+@RequiredArgsConstructor
 public class RestAuthenticationProvider implements AuthenticationProvider {
 
     private final AuthenticationService authenticationService;
     private final UserService userService;
     private final WebContext webContext;
-
-    /**
-     * Instantiates a new Rest authentication provider.
-     *
-     * @param authenticationService the authentication service
-     * @param userService           the user service
-     * @param webContext            the web context
-     */
-    @Autowired
-    public RestAuthenticationProvider(AuthenticationService authenticationService, UserService userService, WebContext webContext) {
-        this.authenticationService = authenticationService;
-        this.userService = userService;
-        this.webContext = webContext;
-    }
+    private final DepartmentMapper departmentMapper;
 
     @Override
     public Authentication authenticate(Authentication authentication) throws AuthenticationException {
@@ -69,7 +60,14 @@
             throw new LockedException("鐢ㄦ埛琚鐢�");
         }
 
+//        // 鏌ヨ璇ョ敤鎴锋槸涓嶆槸閮ㄩ棬绠$悊鍛�
+//        Integer num = departmentMapper.countByAdminId(user.getId());
+
         ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();
+        // 璧嬩簣閮ㄩ棬绠$悊鍛樿鑹�
+        if (DeptAdminEnum.YES.getValue().equals(user.getDeptAdmin())) {
+            grantedAuthorities.add(new SimpleGrantedAuthority(RoleEnum.DEPT_ADMIN.getRoleName()));
+        }
         grantedAuthorities.add(new SimpleGrantedAuthority(RoleEnum.fromCode(user.getRole()).getRoleName()));
 
         User authUser = new User(user.getUserName(), user.getPassword(), grantedAuthorities);
diff --git a/src/main/java/com/mindskip/xzs/configuration/spring/security/RestAuthenticationSuccessHandler.java b/src/main/java/com/mindskip/xzs/configuration/spring/security/RestAuthenticationSuccessHandler.java
index b696ad6..69948e8 100644
--- a/src/main/java/com/mindskip/xzs/configuration/spring/security/RestAuthenticationSuccessHandler.java
+++ b/src/main/java/com/mindskip/xzs/configuration/spring/security/RestAuthenticationSuccessHandler.java
@@ -55,6 +55,8 @@
                 com.mindskip.xzs.domain.User newUser = new com.mindskip.xzs.domain.User();
                 newUser.setUserName(user.getUserName());
                 newUser.setImagePath(user.getImagePath());
+                // 杩斿洖鏄惁閮ㄩ棬绠$悊鍛樼殑鏍囪瘑
+                newUser.setDeptAdmin(user.getDeptAdmin());
                 RestUtil.response(response, SystemCode.OK.getCode(), SystemCode.OK.getMessage(), newUser);
             }
         } else {
diff --git a/src/main/java/com/mindskip/xzs/configuration/spring/security/SecurityConfigurer.java b/src/main/java/com/mindskip/xzs/configuration/spring/security/SecurityConfigurer.java
index a3db86f..62b472d 100644
--- a/src/main/java/com/mindskip/xzs/configuration/spring/security/SecurityConfigurer.java
+++ b/src/main/java/com/mindskip/xzs/configuration/spring/security/SecurityConfigurer.java
@@ -80,7 +80,8 @@
                     .authorizeRequests()
                     .antMatchers(securityIgnoreUrls.toArray(ignores)).permitAll()
                     .antMatchers("/api/admin/department/list", "/api/admin/video/getList","/api/admin/user/conversion","/api/admin/examPaperGrade/updates").permitAll()
-                    .antMatchers("/api/admin/**").hasRole(RoleEnum.ADMIN.getName())
+                    // todo 璁剧疆閮ㄩ棬绠$悊鍛樺彲浠ョ湅鐨勮姹�
+                    .antMatchers("/api/admin/**").hasAnyRole(RoleEnum.ADMIN.getName(), RoleEnum.DEPT_ADMIN.getName())
                     .antMatchers("/api/student/**").hasRole(RoleEnum.STUDENT.getName())
                     .anyRequest().permitAll()
                     .and().exceptionHandling().accessDeniedHandler(restAccessDeniedHandler)
diff --git a/src/main/java/com/mindskip/xzs/domain/User.java b/src/main/java/com/mindskip/xzs/domain/User.java
index cce1041..6f1628a 100644
--- a/src/main/java/com/mindskip/xzs/domain/User.java
+++ b/src/main/java/com/mindskip/xzs/domain/User.java
@@ -1,8 +1,11 @@
 package com.mindskip.xzs.domain;
 
+import lombok.Data;
+
 import java.io.Serializable;
 import java.util.Date;
 
+@Data
 public class User implements Serializable {
 
     private static final long serialVersionUID = -7797183521247423117L;
@@ -71,155 +74,8 @@
     private String wxOpenId;
     private String deptIds;
 
-    public Integer getId() {
-        return id;
-    }
-
-    public void setId(Integer id) {
-        this.id = id;
-    }
-
-    public String getUserUuid() {
-        return userUuid;
-    }
-
-    public void setUserUuid(String userUuid) {
-        this.userUuid = userUuid == null ? null : userUuid.trim();
-    }
-
-    public String getUserName() {
-        return userName;
-    }
-
-    public void setUserName(String userName) {
-        this.userName = userName == null ? null : userName.trim();
-    }
-
-    public String getPassword() {
-        return password;
-    }
-
-    public void setPassword(String password) {
-        this.password = password == null ? null : password.trim();
-    }
-
-    public String getRealName() {
-        return realName;
-    }
-
-    public void setRealName(String realName) {
-        this.realName = realName == null ? null : realName.trim();
-    }
-
-    public Integer getAge() {
-        return age;
-    }
-
-    public void setAge(Integer age) {
-        this.age = age;
-    }
-
-    public Integer getSex() {
-        return sex;
-    }
-
-    public void setSex(Integer sex) {
-        this.sex = sex;
-    }
-
-    public Date getBirthDay() {
-        return birthDay;
-    }
-
-    public void setBirthDay(Date birthDay) {
-        this.birthDay = birthDay;
-    }
-
-    public Integer getUserLevel() {
-        return userLevel;
-    }
-
-    public void setUserLevel(Integer userLevel) {
-        this.userLevel = userLevel;
-    }
-
-    public String getPhone() {
-        return phone;
-    }
-
-    public void setPhone(String phone) {
-        this.phone = phone == null ? null : phone.trim();
-    }
-
-    public Integer getRole() {
-        return role;
-    }
-
-    public void setRole(Integer role) {
-        this.role = role;
-    }
-
-    public Integer getStatus() {
-        return status;
-    }
-
-    public void setStatus(Integer status) {
-        this.status = status;
-    }
-
-    public String getImagePath() {
-        return imagePath;
-    }
-
-    public void setImagePath(String imagePath) {
-        this.imagePath = imagePath == null ? null : imagePath.trim();
-    }
-
-    public Date getCreateTime() {
-        return createTime;
-    }
-
-    public void setCreateTime(Date createTime) {
-        this.createTime = createTime;
-    }
-
-    public Date getModifyTime() {
-        return modifyTime;
-    }
-
-    public void setModifyTime(Date modifyTime) {
-        this.modifyTime = modifyTime;
-    }
-
-    public Date getLastActiveTime() {
-        return lastActiveTime;
-    }
-
-    public void setLastActiveTime(Date lastActiveTime) {
-        this.lastActiveTime = lastActiveTime;
-    }
-
-    public Boolean getDeleted() {
-        return deleted;
-    }
-
-    public void setDeleted(Boolean deleted) {
-        this.deleted = deleted;
-    }
-
-    public String getWxOpenId() {
-        return wxOpenId;
-    }
-
-    public void setWxOpenId(String wxOpenId) {
-        this.wxOpenId = wxOpenId == null ? null : wxOpenId.trim();
-    }
-
-    public String getDeptIds() {
-        return deptIds;
-    }
-
-    public void setDeptIds(String deptIds) {
-        this.deptIds = deptIds;
-    }
+    /**
+     * 鏄惁閮ㄩ棬绠$悊鍛�
+     */
+    private String deptAdmin;
 }
diff --git a/src/main/java/com/mindskip/xzs/domain/enums/DeptAdminEnum.java b/src/main/java/com/mindskip/xzs/domain/enums/DeptAdminEnum.java
new file mode 100644
index 0000000..f460f74
--- /dev/null
+++ b/src/main/java/com/mindskip/xzs/domain/enums/DeptAdminEnum.java
@@ -0,0 +1,23 @@
+package com.mindskip.xzs.domain.enums;
+
+import lombok.Getter;
+
+/**
+ * @author锛歺p
+ * @date锛�2024/3/8 15:37
+ */
+@Getter
+public enum DeptAdminEnum {
+    NO("0", "涓嶆槸"),
+    YES("1", "鏄�"),
+    ;
+
+    private final String value;
+
+    private final String desc;
+
+    DeptAdminEnum(String value, String desc) {
+        this.value = value;
+        this.desc = desc;
+    }
+}
diff --git a/src/main/java/com/mindskip/xzs/domain/enums/RoleEnum.java b/src/main/java/com/mindskip/xzs/domain/enums/RoleEnum.java
index a5fa365..eddee89 100644
--- a/src/main/java/com/mindskip/xzs/domain/enums/RoleEnum.java
+++ b/src/main/java/com/mindskip/xzs/domain/enums/RoleEnum.java
@@ -7,7 +7,8 @@
 public enum RoleEnum {
 
     STUDENT(1, "STUDENT"),
-    ADMIN(3, "ADMIN");
+    ADMIN(3, "ADMIN"),
+    DEPT_ADMIN(-1, "DEPT_ADMIN");
 
     int code;
     String name;
diff --git a/src/main/java/com/mindskip/xzs/domain/vo/UpdateDeptAdminVO.java b/src/main/java/com/mindskip/xzs/domain/vo/UpdateDeptAdminVO.java
index 166d9a1..7cfcd66 100644
--- a/src/main/java/com/mindskip/xzs/domain/vo/UpdateDeptAdminVO.java
+++ b/src/main/java/com/mindskip/xzs/domain/vo/UpdateDeptAdminVO.java
@@ -24,6 +24,6 @@
      * 绠$悊鍛橈紝userId
      */
     @NotNull(message = "閮ㄩ棬绠$悊鍛樹笉鑳戒负绌�")
-    private Integer deptAdmin;
+    private Integer adminId;
 
 }
diff --git a/src/main/java/com/mindskip/xzs/repository/DepartmentMapper.java b/src/main/java/com/mindskip/xzs/repository/DepartmentMapper.java
index 2e92684..b951949 100644
--- a/src/main/java/com/mindskip/xzs/repository/DepartmentMapper.java
+++ b/src/main/java/com/mindskip/xzs/repository/DepartmentMapper.java
@@ -20,4 +20,6 @@
     Department getName(String name);
 
     Department getById(Integer id);
+
+    List<Integer> selectByAdminId(Integer id);
 }
diff --git a/src/main/java/com/mindskip/xzs/repository/UserMapper.java b/src/main/java/com/mindskip/xzs/repository/UserMapper.java
index 695001c..89aaf6f 100644
--- a/src/main/java/com/mindskip/xzs/repository/UserMapper.java
+++ b/src/main/java/com/mindskip/xzs/repository/UserMapper.java
@@ -135,4 +135,11 @@
     List<User> getUserByLevel(@Param("userLevel") Integer userLevel);
 
     User getUserByRealName(@Param("realName") String realName);
+
+    /**
+     * 淇敼閮ㄩ棬绠$悊鍛樻爣璇�
+     * @param id
+     * @param deptAdmin
+     */
+    void updateDeptAdmin(Integer id, String deptAdmin);
 }
diff --git a/src/main/java/com/mindskip/xzs/service/impl/DepartmentServiceImpl.java b/src/main/java/com/mindskip/xzs/service/impl/DepartmentServiceImpl.java
index 0c737eb..d138d98 100644
--- a/src/main/java/com/mindskip/xzs/service/impl/DepartmentServiceImpl.java
+++ b/src/main/java/com/mindskip/xzs/service/impl/DepartmentServiceImpl.java
@@ -5,11 +5,13 @@
 import com.github.pagehelper.PageInfo;
 import com.mindskip.xzs.domain.Department;
 import com.mindskip.xzs.domain.UserDepartment;
+import com.mindskip.xzs.domain.enums.DeptAdminEnum;
 import com.mindskip.xzs.domain.vo.BaseSelect;
 import com.mindskip.xzs.domain.vo.UpdateDeptAdminVO;
 import com.mindskip.xzs.repository.BaseMapper;
 import com.mindskip.xzs.repository.DepartmentMapper;
 import com.mindskip.xzs.repository.UserDepartmentMapper;
+import com.mindskip.xzs.repository.UserMapper;
 import com.mindskip.xzs.service.DepartmentService;
 import com.mindskip.xzs.utility.ModelMapperSingle;
 import com.mindskip.xzs.viewmodel.admin.department.DepartmentResponseVM;
@@ -27,11 +29,13 @@
 
     private final DepartmentMapper departmentMapper;
     private final UserDepartmentMapper userDepartmentMapper;
+    private final UserMapper userMapper;
 
-    public DepartmentServiceImpl(BaseMapper<Department> baseMapper, DepartmentMapper departmentMapper, UserDepartmentMapper userDepartmentMapper) {
+    public DepartmentServiceImpl(BaseMapper<Department> baseMapper, DepartmentMapper departmentMapper, UserDepartmentMapper userDepartmentMapper, UserMapper userMapper) {
         super(baseMapper);
         this.departmentMapper = departmentMapper;
         this.userDepartmentMapper = userDepartmentMapper;
+        this.userMapper = userMapper;
     }
 
     @Override
@@ -54,7 +58,24 @@
     @Override
     public Integer update(Department department) {
 //        userDepartmentMapper.removeByDepartmentId(department.getId());
-        return departmentMapper.update(department);
+        Department entity = departmentMapper.getById(department.getId());
+        if (Objects.isNull(entity)) {
+            throw new RuntimeException("璇ラ儴闂ㄤ笉瀛樺湪");
+        }
+        if (Objects.nonNull(department.getAdminId())) {
+            if (! Objects.equals(entity.getAdminId(), department.getAdminId())) {
+                // 鍙栨秷鍘熷厛鐨勭鐞嗗憳鏍囪瘑
+                List<Integer> depIds = departmentMapper.selectByAdminId(entity.getAdminId());
+                depIds.remove(department.getId());
+                if (depIds.size() < 1) {
+                    userMapper.updateDeptAdmin(entity.getAdminId(), DeptAdminEnum.NO.getValue());
+                }
+            }
+            // 鏍囪瘑閮ㄩ棬绠$悊鍛�
+            userMapper.updateDeptAdmin(department.getAdminId(), DeptAdminEnum.YES.getValue());
+        }
+        Integer update = departmentMapper.update(department);
+        return update;
     }
 
     @Override
@@ -91,11 +112,23 @@
 
     @Override
     public void updateAdmin(UpdateDeptAdminVO form) {
-        Department department = departmentMapper.getById(form.getId());
-        if (Objects.isNull(department)) {
+        Department entity = departmentMapper.getById(form.getId());
+        if (Objects.isNull(form)) {
             throw new RuntimeException("璇ラ儴闂ㄤ笉瀛樺湪");
         }
-        department.setAdminId(form.getDeptAdmin());
-        departmentMapper.update(department);
+        if (Objects.nonNull(form.getAdminId())) {
+            if (! Objects.equals(entity.getAdminId(), form.getAdminId())) {
+                // 鍙栨秷鍘熷厛鐨勭鐞嗗憳鏍囪瘑
+                List<Integer> depIds = departmentMapper.selectByAdminId(entity.getAdminId());
+                depIds.remove(form.getId());
+                if (depIds.size() < 1) {
+                    userMapper.updateDeptAdmin(entity.getAdminId(), DeptAdminEnum.NO.getValue());
+                }
+            }
+            // 鏍囪瘑閮ㄩ棬绠$悊鍛�
+            userMapper.updateDeptAdmin(form.getAdminId(), DeptAdminEnum.YES.getValue());
+        }
+        entity.setAdminId(form.getAdminId());
+        departmentMapper.update(entity);
     }
 }
diff --git a/src/main/resources/mapper/DepartmentMapper.xml b/src/main/resources/mapper/DepartmentMapper.xml
index 5eee8cd..5d54e83 100644
--- a/src/main/resources/mapper/DepartmentMapper.xml
+++ b/src/main/resources/mapper/DepartmentMapper.xml
@@ -13,8 +13,8 @@
     </sql>
 
     <insert id="add" parameterType="com.mindskip.xzs.domain.Department" useGeneratedKeys="true" keyProperty="id">
-        insert into t_department (name, admin_id, deleted)
-        values (#{name,jdbcType=VARCHAR}, {#{adminId}, #{deleted,jdbcType=VARCHAR})
+        insert into t_department (name, deleted)
+        values (#{name,jdbcType=VARCHAR}, #{deleted,jdbcType=VARCHAR})
     </insert>
 
     <update id="update" parameterType="com.mindskip.xzs.domain.Department">
@@ -93,4 +93,9 @@
         WHERE
             td.name = #{name} AND td.deleted = 0
     </select>
+
+    <select id="selectByAdminId" resultType="integer">
+        SELECT id FROM t_department WHERE admin_id = #{id} AND deleted = 0
+    </select>
+
 </mapper>
diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml
index 2579dda..2afc008 100644
--- a/src/main/resources/mapper/UserMapper.xml
+++ b/src/main/resources/mapper/UserMapper.xml
@@ -23,7 +23,7 @@
   </resultMap>
   <sql id="Base_Column_List">
     id, user_uuid, user_name, password, real_name, age, sex, birth_day, user_level, phone,
-    role, status, image_path, create_time, modify_time, last_active_time, deleted, wx_open_id
+    role, status, image_path, create_time, modify_time, last_active_time, deleted, wx_open_id, dept_admin
   </sql>
   <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
     select
@@ -220,6 +220,9 @@
       <if test="wxOpenId != null">
         wx_open_id = #{wxOpenId,jdbcType=VARCHAR},
       </if>
+      <if test="deptAdmin != null">
+        dept_admin = #{deptAdmin},
+      </if>
     </set>
     where id = #{id,jdbcType=INTEGER}
   </update>
@@ -241,7 +244,8 @@
       modify_time = #{modifyTime,jdbcType=TIMESTAMP},
       last_active_time = #{lastActiveTime,jdbcType=TIMESTAMP},
       deleted = #{deleted,jdbcType=BIT},
-      wx_open_id = #{wxOpenId,jdbcType=VARCHAR}
+      wx_open_id = #{wxOpenId,jdbcType=VARCHAR},
+      dept_admin = #{deptAdmin}
     where id = #{id,jdbcType=INTEGER}
   </update>
 
@@ -434,4 +438,10 @@
       </if>
     </where>
   </select>
+
+
+  <update id="updateDeptAdmin">
+    UPDATE t_user SET dept_admin = #{deptAdmin} WHERE id = #{id} AND deleted = 0
+  </update>
+
 </mapper>

--
Gitblit v1.8.0