Browse Source

机构类型

TRX 1 year ago
parent
commit
6274d677b4

+ 71 - 0
src/main/java/com/zswl/dataservice/controller/user/RoleController.java

@@ -0,0 +1,71 @@
+package com.zswl.dataservice.controller.user;
+
+import com.zswl.dataservice.model.baseParam.IDParam;
+import com.zswl.dataservice.model.user.RoleAddParam;
+import com.zswl.dataservice.model.user.RoleModel;
+import com.zswl.dataservice.model.user.RoleSearchParam;
+import com.zswl.dataservice.service.user.impl.RoleServiceImpl;
+import com.zswl.dataservice.utils.result.ResultContent;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.enums.ParameterIn;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author TRX
+ * @date 2024/6/5
+ */
+@RestController
+@RequestMapping("role")
+@Tag(name = "角色管理")
+public class RoleController {
+
+    @Autowired
+    RoleServiceImpl roleService;
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "添加-编辑角色", description = "添加-编辑角色")
+    @RequestMapping(value = "addRole", method = {RequestMethod.POST})
+    public ResultContent addRole(@RequestBody RoleAddParam param) {
+        return this.roleService.addRole(param);
+    }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "角色列表-分页查询", description = "角色列表-分页查询")
+    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
+    public ResultContent<Page<RoleModel>> page(
+            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
+            @Parameter(required = false) RoleSearchParam param) {
+        return roleService.page(param, pageable);
+    }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "得到所有角色", description = "得到所有角色")
+    @RequestMapping(value = "getAllRoles", method = {RequestMethod.GET})
+    public ResultContent getAllRoles() {
+        return this.roleService.getAllRoles("");
+    }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "删除角色", description = "删除角色")
+    @RequestMapping(value = "deleteRole", method = {RequestMethod.POST})
+    public ResultContent deleteRole(@RequestBody IDParam param) {
+        return this.roleService.deleteRole(param.getId());
+    }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "角色详情", description = "角色详情")
+    @RequestMapping(value = "getRole", method = {RequestMethod.POST})
+    public ResultContent<RoleModel> getRole(@RequestBody IDParam param) {
+        return this.roleService.getRole(param.getId());
+    }
+
+}

+ 1 - 1
src/main/java/com/zswl/dataservice/controller/user/UserController.java

@@ -24,7 +24,7 @@ import org.springframework.web.bind.annotation.*;
 @RequestMapping("/user/userInfo")
 @RestController
 @Validated
-@Tag(name = "用户管理")
+@Tag(name = "当前用户信息管理")
 public class UserController {
 
     @Autowired

+ 20 - 0
src/main/java/com/zswl/dataservice/dao/RoleDao.java

@@ -0,0 +1,20 @@
+package com.zswl.dataservice.dao;
+
+import com.zswl.dataservice.dao.extend.RoleDaoExtend;
+import com.zswl.dataservice.domain.user.Role;
+
+import java.util.List;
+
+/**
+ * @author TRX
+ * @date 2024/3/21
+ */
+public interface RoleDao extends MongoDao<Role>, RoleDaoExtend {
+
+    Role findTopById(String id);
+
+    Role findTopByName(String name);
+
+    Role findTopByCode(String code);
+
+}

+ 15 - 0
src/main/java/com/zswl/dataservice/dao/extend/RoleDaoExtend.java

@@ -0,0 +1,15 @@
+package com.zswl.dataservice.dao.extend;
+
+import com.zswl.dataservice.domain.user.Role;
+import com.zswl.dataservice.model.user.RoleSearchParam;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+
+/**
+ * @Author TRX
+ * @CreateDate: 2023/7/7
+ * @Version: 1.0
+ */
+public interface RoleDaoExtend {
+    Page<Role> page(Pageable pageable, RoleSearchParam param);
+}

+ 64 - 0
src/main/java/com/zswl/dataservice/dao/impl/RoleDaoImpl.java

@@ -0,0 +1,64 @@
+package com.zswl.dataservice.dao.impl;
+
+import com.zswl.dataservice.dao.base.BaseImpl;
+import com.zswl.dataservice.dao.extend.RoleDaoExtend;
+import com.zswl.dataservice.domain.user.Role;
+import com.zswl.dataservice.helper.DBHelper;
+import com.zswl.dataservice.model.user.RoleSearchParam;
+import com.zswl.dataservice.utils.CommonUtil;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+import org.springframework.util.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * @Author TRX
+ * @CreateDate: 2023/4/12
+ * @Version: 1.0
+ */
+public class RoleDaoImpl extends BaseImpl implements RoleDaoExtend {
+
+    @Autowired
+    private MongoTemplate mongoTemplate;
+
+    @Autowired
+    private DBHelper dbHelper;
+
+    @Override
+    public Page<Role> page(Pageable pageable, RoleSearchParam param) {
+        Criteria criteria = new Criteria();
+
+        if (!CommonUtil.longIsEmpty(param.getStartTime()) && !CommonUtil.longIsEmpty(param.getEndTime())) {
+            criteria.and("createTime").gte(param.getStartTime()).and("createTime").lte(param.getEndTime());
+        }
+
+        // 模糊搜索
+        List<Criteria> criterias = new ArrayList<>();
+        if (StringUtils.isNotEmpty(param.getName())) {
+            Pattern pattern = Pattern.compile("^.*" + param.getName() + ".*$");
+            criterias.add(Criteria.where("name").is(pattern));
+        }
+        if (StringUtils.isNotEmpty(param.getCode())) {
+            Pattern pattern = Pattern.compile("^.*" + param.getName() + ".*$");
+            criterias.add(Criteria.where("code").is(pattern));
+        }
+        if (!CollectionUtils.isEmpty(criterias)) {
+            criteria.andOperator(criterias.toArray(new Criteria[]{}));
+        }
+        criteria.and("isDelete").is(Boolean.FALSE);
+        Sort sort = buildSort(param);
+        Query query = Query.query(criteria);
+        query.with(sort);
+        return dbHelper.pages(query, pageable, Role.class);
+    }
+
+}

+ 68 - 0
src/main/java/com/zswl/dataservice/domain/user/Role.java

@@ -0,0 +1,68 @@
+package com.zswl.dataservice.domain.user;
+
+import com.zswl.dataservice.domain.base.SuperEntity;
+import com.zswl.dataservice.type.RoleDefaultType;
+import com.zswl.dataservice.type.RoleType;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.*;
+import org.springframework.data.mongodb.core.index.Indexed;
+import org.springframework.data.mongodb.core.mapping.DBRef;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+import java.util.Set;
+
+/**
+ * @author TRX
+ * @date 2024/5/30
+ */
+@Data
+@Document
+@AllArgsConstructor
+@NoArgsConstructor
+@EqualsAndHashCode(callSuper = true)
+@Builder
+public class Role extends SuperEntity {
+
+    @Schema(description = "名称")
+    private String name;
+
+    @Schema(description = "角色编码")
+    private String code;
+
+    @Schema(description = "角色类型 系统内置 自定义")
+    RoleType roleType;
+
+    @Schema(description = "默认角色的类型")
+    RoleDefaultType roleDefaultType;
+
+    @Schema(description = "是否管理角色")
+    @Indexed
+    private Boolean isAdmin = Boolean.FALSE;
+
+    @Schema(description = "角色权限列表")
+    @Indexed
+    private Set<String> auth;
+
+    @Schema(description = "权限执行的角色组id")
+    @Indexed
+    private String roleGroupId;
+
+    @Schema(description = "权限中心的角色ID")
+    private String roleId;
+
+    @Schema(description = "操作人")
+    @DBRef(lazy = true)
+    private User creator;
+
+    /**
+     * 构建角色
+     *
+     * @param id
+     * @return
+     */
+    public static Role build(String id) {
+        Role role = new Role();
+        role.setId(id);
+        return role;
+    }
+}

+ 14 - 0
src/main/java/com/zswl/dataservice/model/baseParam/OidModel.java

@@ -0,0 +1,14 @@
+package com.zswl.dataservice.model.baseParam;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * @author TRX
+ * @date 2024/6/3
+ */
+@Data
+public class OidModel implements Serializable {
+    private String oid;
+}

+ 13 - 1
src/main/java/com/zswl/dataservice/model/baseParam/SuperModel.java

@@ -28,5 +28,17 @@ public class SuperModel implements Serializable {
     private Boolean isDelete = Boolean.FALSE;
 
     @Schema(description = "所属项目ID")
-    private String epId;
+    private String oid;
+
+    @Schema(description = "排序", hidden = true)
+    private Long sort;
+
+    @Schema(description = "创建用户ID")
+    private String createUserId;
+
+    @Schema(description = "添加用户名称")
+    private String createUserName;
+
+    @Schema(description = "添加用户电话", hidden = true)
+    private String createPhone;
 }

+ 25 - 0
src/main/java/com/zswl/dataservice/model/user/RoleAddParam.java

@@ -0,0 +1,25 @@
+package com.zswl.dataservice.model.user;
+
+import com.zswl.dataservice.model.baseParam.SuperParam;
+import com.zswl.dataservice.type.RoleDefaultType;
+import com.zswl.dataservice.type.RoleType;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * @author TRX
+ * @date 2024/5/31
+ */
+@Data
+public class RoleAddParam extends SuperParam {
+    @Schema(description = "名称")
+    private String name;
+
+    @Schema(description = "角色编码")
+    private String code;
+
+    RoleType roleType = RoleType.Custom;
+
+    @Schema(description = "默认角色的类型")
+    RoleDefaultType roleDefaultType;
+}

+ 39 - 0
src/main/java/com/zswl/dataservice/model/user/RoleModel.java

@@ -0,0 +1,39 @@
+package com.zswl.dataservice.model.user;
+
+import com.zswl.dataservice.model.baseParam.SuperModel;
+import com.zswl.dataservice.type.RoleType;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.util.Set;
+
+/**
+ * @author TRX
+ * @date 2024/5/31
+ */
+@Data
+public class RoleModel extends SuperModel {
+    @Schema(description = "名称")
+    private String name;
+
+    @Schema(description = "角色编码")
+    private String code;
+
+    @Schema(description = "角色类型")
+    RoleType roleType;
+
+    private String roleTypeStr;
+
+    @Schema(description = "角色权限列表")
+    private Set<String> auth;
+
+    public String getRoleTypeStr() {
+        if (roleType != null) {
+            return roleType.getRemark();
+        }
+        return "";
+    }
+
+    @Schema(description = "是否管理角色")
+    private Boolean isAdmin = Boolean.FALSE;
+}

+ 18 - 0
src/main/java/com/zswl/dataservice/model/user/RoleSearchParam.java

@@ -0,0 +1,18 @@
+package com.zswl.dataservice.model.user;
+
+import com.zswl.dataservice.model.baseParam.SuperSearchParam;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * @author TRX
+ * @date 2024/5/31
+ */
+@Data
+public class RoleSearchParam extends SuperSearchParam {
+    @Schema(description = "名称")
+    private String name;
+
+    @Schema(description = "角色编码")
+    private String code;
+}

+ 9 - 0
src/main/java/com/zswl/dataservice/service/base/SuperService.java

@@ -7,6 +7,7 @@ import com.zswl.dataservice.domain.user.User;
 import com.zswl.dataservice.model.baseParam.SuperParam;
 import com.zswl.dataservice.model.baseParam.SuperSearchParam;
 import com.zswl.dataservice.utils.DateUtils;
+import jakarta.servlet.http.HttpServletRequest;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -25,6 +26,9 @@ public abstract class SuperService {
     @Autowired
     private UserDao userDao;
 
+    @Autowired
+    HttpServletRequest request;
+
     /**
      * 得到当前用户对象
      *
@@ -50,6 +54,11 @@ public abstract class SuperService {
         return null;
     }
 
+    public String getCurrentOid() {
+        String oid = request.getHeader("oid");
+        return oid;
+    }
+
     public void initDefaultUser(SuperParam param) {
         if (param.getId() != null && param.getId().trim().equals("")) {
             param.setId(null);

+ 170 - 0
src/main/java/com/zswl/dataservice/service/user/impl/RoleServiceImpl.java

@@ -0,0 +1,170 @@
+package com.zswl.dataservice.service.user.impl;
+
+import com.zswl.dataservice.dao.RoleDao;
+import com.zswl.dataservice.dataConfig.ResultMessage;
+import com.zswl.dataservice.domain.user.Role;
+import com.zswl.dataservice.model.user.RoleAddParam;
+import com.zswl.dataservice.model.user.RoleModel;
+import com.zswl.dataservice.model.user.RoleSearchParam;
+import com.zswl.dataservice.service.base.SuperService;
+import com.zswl.dataservice.type.RoleType;
+import com.zswl.dataservice.utils.bean.BeanUtils;
+import com.zswl.dataservice.utils.page.PageEntityUtil;
+import com.zswl.dataservice.utils.result.ResultContent;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+import org.springframework.util.Assert;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @author TRX
+ * @date 2024/6/5
+ */
+@Slf4j
+@Service
+public class RoleServiceImpl extends SuperService {
+
+    @Autowired
+    RoleDao roleDao;
+
+    /**
+     * 添加/编辑角色
+     *
+     * @param param
+     * @return
+     */
+    public ResultContent addRole(RoleAddParam param) {
+        Assert.hasText(param.getName(), "name不能为空");
+        Assert.hasText(param.getCode(), "code不能为空");
+        String oid = param.getOid();
+        if (StringUtils.isEmpty(oid)) {
+            oid = getCurrentOid();
+        }
+        param.setOid(oid);
+        if (param.getSort() == null) {
+            param.setSort(1L);
+        }
+        Role nameRole = roleDao.findTopByName(param.getName());
+        Role codeRole = roleDao.findTopByCode(param.getCode());
+        initDefaultUser(param);
+        if (StringUtils.isEmpty(param.getId())) {
+            param.setId(null);
+            // 添加
+            if (ObjectUtils.isNotEmpty(nameRole)) {
+                return ResultContent.buildFail(String.format("角色名称已存在:%s", param.getName()));
+            }
+            if (ObjectUtils.isNotEmpty(codeRole)) {
+                return ResultContent.buildFail(String.format("角色标识已存在:%s", param.getCode()));
+            }
+            Role role = new Role();
+            BeanUtils.copyProperties(param, role);
+            role.setRoleType(RoleType.Custom);
+            role.setCreateUserId(getCurrentUserId());
+            role.setIsAdmin(Boolean.FALSE);
+            roleDao.save(role);
+        } else {
+            // 编辑
+            Role role = roleDao.findTopById(param.getId());
+            if (ObjectUtils.isEmpty(role)) {
+                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
+            }
+            if (ObjectUtils.isNotEmpty(nameRole) && !nameRole.getId().equals(role.getId())) {
+                return ResultContent.buildFail(String.format("角色名称已存在:%s", param.getName()));
+            }
+            if (ObjectUtils.isNotEmpty(codeRole) && !codeRole.getId().equals(role.getId())) {
+                return ResultContent.buildFail(String.format("角色标识已存在:%s", param.getCode()));
+            }
+            // 编辑本地角色
+            BeanUtils.copyProperties(param, role);
+            roleDao.save(role);
+        }
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 删除角色
+     *
+     * @param id
+     * @return
+     */
+    public ResultContent deleteRole(String id) {
+        Role role = roleDao.findTopById(id);
+        if (ObjectUtils.isEmpty(role)) {
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
+        }
+        if (role.getIsAdmin() != null && role.getIsAdmin()) {
+            return ResultContent.buildFail(String.format("管理员角色不能删除"));
+        }
+        if (role.getRoleType() == RoleType.BuildIn) {
+            return ResultContent.buildFail(String.format("内置角色不能删除"));
+        }
+        // 删除本地角色
+        roleDao.delete(role);
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 角色详情
+     *
+     * @param id
+     * @return
+     */
+    public ResultContent<RoleModel> getRole(String id) {
+        Role role = roleDao.findTopById(id);
+        if (ObjectUtils.isEmpty(role)) {
+            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
+        }
+        RoleModel roleModel = toModel(role);
+        return ResultContent.buildSuccess(roleModel);
+    }
+
+    /**
+     * 角色列表
+     *
+     * @param param
+     * @param pageable
+     * @return
+     */
+    public ResultContent<Page<RoleModel>> page(RoleSearchParam param, Pageable pageable) {
+        initSearchParam(param);
+        param.setIsSortDesc(Boolean.FALSE);
+        Page<Role> page = roleDao.page(pageable, param);
+        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
+    }
+
+    /**
+     * 得到机构所有的角色
+     *
+     * @param oid
+     * @return
+     */
+    public ResultContent<List<RoleModel>> getAllRoles(String oid) {
+        if (StringUtils.isEmpty(oid)) {
+            oid = getCurrentOid();
+        }
+        List<Role> list = roleDao.findAll(Sort.by(Sort.Order.asc("sort"),
+                Sort.Order.asc("createTime")));
+        List<RoleModel> models = new ArrayList<>();
+        if (ObjectUtils.isNotEmpty(list)) {
+            models = list.stream().map(this::toModel).collect(Collectors.toList());
+        }
+        return ResultContent.buildSuccess(models);
+    }
+
+    public RoleModel toModel(Role role) {
+        RoleModel roleModel = new RoleModel();
+        if (ObjectUtils.isNotEmpty(role)) {
+            BeanUtils.copyProperties(role, roleModel);
+        }
+        return roleModel;
+    }
+}

+ 25 - 0
src/main/java/com/zswl/dataservice/type/RoleDefaultType.java

@@ -0,0 +1,25 @@
+package com.zswl.dataservice.type;
+
+import lombok.Getter;
+
+/**
+ * 默认角色配置
+ */
+public enum RoleDefaultType {
+    Teacher("老师", "Teacher"),
+    Student("学生", "Student"),
+    ;
+
+    // 备注
+    @Getter
+    private String remark;
+
+    // 角色的身份标识
+    @Getter
+    private String identity;
+
+    RoleDefaultType(String remark, String identity) {
+        this.remark = remark;
+        this.identity = identity;
+    }
+}

+ 19 - 0
src/main/java/com/zswl/dataservice/type/RoleType.java

@@ -0,0 +1,19 @@
+package com.zswl.dataservice.type;
+
+import lombok.Getter;
+
+/**
+ * 用户类型
+ */
+public enum RoleType {
+    BuildIn("系统内置"),
+    Custom("自定义"),
+    ;
+
+    @Getter
+    private String remark;
+
+    RoleType(String remark) {
+        this.remark = remark;
+    }
+}