liurunyu
2023-11-13 bd18a7feba1fa1b6d8c510507249451d320f2660
角色管理实现
5个文件已修改
3个文件已添加
506 ■■■■■ 已修改文件
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoBa/BaPrivilegeMapper.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoBa/BaRoleMapper.java 60 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/pojoBa/BaRole.java 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/resources/mapper/BaRoleMapper.xml 72 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/role/QueryVo.java 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/role/RoleCtrl.java 223 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/role/RoleSv.java 101 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/user/UserSv.java 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoBa/BaPrivilegeMapper.java
@@ -7,6 +7,7 @@
import java.util.List;
public interface BaPrivilegeMapper extends BaseMapper<BaPrivilege> {
     /**
     * 查询全部
     * @return List<BaPrivilege>
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoBa/BaRoleMapper.java
@@ -6,9 +6,40 @@
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
@Mapper
public interface BaRoleMapper extends BaseMapper<BaRole> {
    /**
     * 查询全部
     * @return List<BaRole>
     */
    List<BaRole> selectAll(Map<?, ?> params) ;
    /**
     * 查询某个用户所隶属于的角色
     * @param userId 用户ID
     * @return List<BaRole>
     */
    List<BaRole> selectByUserId(@Param("userId") Long userId) ;
    /**
     * 查询总数
     * @param params 查询条件
     * @return 总数
     */
    Long selectTotal(Map<?, ?> params) ;
    /**
     * 分页查询一些
     * @param params 查询条件
     * @return 实体集合
     */
    List<BaRole> selectSome(Map<?, ?> params) ;
    /**
     * insert record to table
     * @param record the record
@@ -24,15 +55,30 @@
    int insertSelective(BaRole record);
    /**
     * 查询全部
     * @return List<BaRole>
     * update record selective
     * @param record the updated record
     * @return update count
     */
    List<BaRole> selectAll() ;
    int updateByPrimaryKeySelective(BaRole record);
    /**
     * 查询某个用户所隶属于的角色
     * @param userId 用户ID
     * @return List<BaRole>
     * update record
     * @param record the updated record
     * @return update count
     */
    List<BaRole> selectByUserId(@Param("userId") Long userId) ;
    int updateByPrimaryKey(BaRole record);
    /**
     * delete by primary key
     * @param id primaryKey
     * @return deleteCount
     */
    int deleteByPrimaryKey(Long id);
    /**
     * 逻辑删除
     * @param id primaryKey
     * @return update count
     */
    int deleteLogicById(Long id);
}
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/pojoBa/BaRole.java
@@ -4,11 +4,17 @@
import com.alibaba.fastjson2.annotation.JSONField;
import com.alibaba.fastjson2.writer.ObjectWriterImplToString;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.dy.common.mybatis.envm.Deleted;
import com.dy.common.po.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import lombok.*;
import org.hibernate.validator.constraints.Length;
import java.util.List;
/**
 * 角色实体
@@ -19,22 +25,35 @@
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "角色实体")
public class BaRole implements BaseEntity {
    public static final long serialVersionUID = 202310211546001L;
    @JSONField(serializeUsing= ObjectWriterImplToString.class)
    @TableId(type = IdType.INPUT)
    @Schema(description = "实体id", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
    public Long id ;
    /**
     * 角色名称
     */
    @Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED)
    @NotEmpty(message = "名称不能为空") //不能为空也不能为null
    @Length(message = "名称不大于{max}字符,不小于{min}字符", min = 2, max = 25)
    public String name ;
    /**
     * 是否删除: 0表示未删除  1表示删除.
     */
    @Schema(description = "删除标识,表单不用填写", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
    public Deleted deleted;
    /**
     * 角色所拥有的权限
     */
    @TableField(exist = false)
    public List<BaPrivilege> priviList ;
}
pipIrr-platform/pipIrr-global/src/main/resources/mapper/BaRoleMapper.xml
@@ -13,6 +13,10 @@
    <result column="name" jdbcType="VARCHAR" property="name" />
  </resultMap>
  <resultMap id="someResultMap" extends="partResultMap" type="com.dy.pipIrrGlobal.pojoBa.BaRole">
    <collection property="priviList" fetchType="eager" column="{roleId=id}" select="com.dy.pipIrrGlobal.daoBa.BaPrivilegeMapper.selectPrivilegeByRoleId" />
  </resultMap>
  <sql id="Base_Column_List">
    id, name, deleted
  </sql>
@@ -23,8 +27,14 @@
  <select id="selectAll" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    <include refid="part_Column_List" />
    from ba_role
    where deleted!=1
    <trim prefix="and" suffixOverrides="and">
      <if test="name != null">
        name like concat('%', #{name}, '%')
      </if>
    </trim>
  </select>
  <select id="selectByUserId" resultMap="partResultMap">
@@ -35,6 +45,34 @@
    <if test="userId != null">
      and ur.userId = #{userId,jdbcType=BIGINT}
    </if>
  </select>
  <select id="selectTotal" parameterType="java.util.Map" resultType="java.lang.Long">
    select
    count(*)
    from ba_role where deleted!=1
    <trim prefix="and" suffixOverrides="and">
      <if test="name != null">
        name like concat('%', #{name}, '%')
      </if>
    </trim>
  </select>
  <select id="selectSome" parameterType="java.util.Map" resultMap="someResultMap">
    select
    <include refid="part_Column_List" />
    from ba_role where deleted!=1
    <trim prefix="and" suffixOverrides="and">
      <if test="name != null">
        name like concat('%', #{name}, '%')
      </if>
    </trim>
    order by id DESC
    <trim prefix="limit " >
      <if test="start != null and count != null">
        #{start}, #{count}
      </if>
    </trim>
  </select>
  <insert id="insert" parameterType="com.dy.pipIrrGlobal.pojoBa.BaRole">
@@ -70,4 +108,36 @@
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.dy.pipIrrGlobal.pojoBa.BaUser">
    update ba_role
    <set>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="deleted != null">
        deleted = #{deleted,typeHandler=com.dy.common.mybatis.envm.EnumCodeTypeHandler, jdbcType=TINYINT},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.dy.pipIrrGlobal.pojoBa.BaUser">
    update ba_role
    set `name` = #{name,jdbcType=VARCHAR},
     deleted = #{deleted,typeHandler=com.dy.common.mybatis.envm.EnumCodeTypeHandler, jdbcType=TINYINT}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from ba_role
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteLogicById" parameterType="java.lang.Long">
    update ba_role set deleted = 1
    where id = #{id,jdbcType=BIGINT}
  </delete>
</mapper>
pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/role/QueryVo.java
New file
@@ -0,0 +1,20 @@
package com.dy.pipIrrBase.role;
import com.dy.common.webUtil.QueryConditionVo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
@Data
@EqualsAndHashCode(callSuper = false)
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Schema(name = "角色查询条件")
public class QueryVo extends QueryConditionVo {
    @Schema(description = "角色名称")
    public String name;
}
pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/role/RoleCtrl.java
New file
@@ -0,0 +1,223 @@
package com.dy.pipIrrBase.role;
import com.dy.common.aop.SsoAop;
import com.dy.common.mybatis.envm.Deleted;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.common.webUtil.ResultCodeMsg;
import com.dy.pipIrrBase.user.QueryVo;
import com.dy.pipIrrGlobal.pojoBa.BaRole;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
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 java.util.List;
import java.util.Objects;
@Slf4j
@Tag(name = "角色管理", description = "角色增删改查等操作")
@RestController
@RequestMapping(path="role")
@SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked")
public class RoleCtrl {
    private RoleSv sv ;
    @Autowired
    private void setSv(RoleSv sv){
        this.sv = sv ;
    }
    /**
     * 客户端请求得到所有角色数据
     * @return 所有角色数据
     */
    @Operation(summary = "获得全部角色", description = "返回全部角色数据")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "返回全部角色数据(BaseResponse.content:QueryResultVo[{}])",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = BaRole.class))}
            )
    })
    @PostMapping(path = "all")
    @SsoAop("-1")
    public BaseResponse<QueryResultVo<List<BaRole>>> all(QueryVo vo){
        try {
            QueryResultVo<List<BaRole>> res = this.sv.selectAll(vo) ;
            return BaseResponseUtils.buildSuccess(res);
        } catch (Exception e) {
            log.error("查询角色异常", e);
            return BaseResponseUtils.buildException(e.getMessage()) ;
        }
    }
    /**
     * 客户端请求得到所有角色数据
     * @return 所有角色数据
     */
    @Operation(summary = "获得一页角色", description = "返回一页角色数据")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "返回一页角色数据(BaseResponse.content:QueryResultVo[{}])",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = BaRole.class))}
            )
    })
    @PostMapping(path = "some")
    @SsoAop("-1")
    public BaseResponse<QueryResultVo<List<BaRole>>> some(QueryVo vo){
        try {
            QueryResultVo<List<BaRole>> res = this.sv.selectSome(vo) ;
            return BaseResponseUtils.buildSuccess(res);
        } catch (Exception e) {
            log.error("查询角色异常", e);
            return BaseResponseUtils.buildException(e.getMessage()) ;
        }
    }
    /**
     * 得到一个角色数据
     * @return 一个角色数据
     */
    @Operation(summary = "一个角色", description = "得到一个角色数据")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "返回一个角色数据(BaseResponse.content:{})",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = BaRole.class))}
            )
    })
    @GetMapping(path = "one")
    @SsoAop("-1")
    public BaseResponse<BaRole> one(@Parameter(description = "实体id", required = true) Long id){
        return BaseResponseUtils.buildSuccess(this.sv.selectById(id));
    }
    /**
     * 保存角色
     * @param po 保存角色form表单对象
     * @return 是否成功
     */
    @Operation(summary = "保存角色", description = "提交角色数据(form表单),进行保存")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "save", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @SsoAop("-1")//@SsoAop(power = "-1")
    public BaseResponse<Boolean> save(@Parameter(description = "form表单数据", required = true) @Valid BaRole po, @Parameter(hidden = true) BindingResult bindingResult){
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        po.id = null ;
        int count;
        try {
            po.deleted = Deleted.NO ;//默认不删除
            count = this.sv.save(po);
        } catch (Exception e) {
            log.error("保存角色异常", e);
            return BaseResponseUtils.buildException(e.getMessage()) ;
        }
        if(count <= 0){
            return BaseResponseUtils.buildFail("数据库存储失败") ;
        }else{
            return BaseResponseUtils.buildSuccess(true) ;
        }
    }
    /**
     * 编辑修改角色
     * @param po 保存角色form表单对象
     * @return 是否成功
     */
    @Operation(summary = "编辑修改角色", description = "提交角色数据(form表单),进行修改")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @SsoAop("-1")//@SsoAop(power = "-1")
    public BaseResponse<Boolean> update(@Parameter(description = "form表单数据", required = true) @Valid BaRole po, @Parameter(hidden = true) BindingResult bindingResult){
        if(bindingResult != null && bindingResult.hasErrors()){
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        if(po.id == null){
            return BaseResponseUtils.buildFail("无数据实体ID") ;
        }
        int count;
        try {
            po.deleted = null ;//设置为null,不做更新
            count = this.sv.update(po);
        } catch (Exception e) {
            log.error("保存角色异常", e);
            return BaseResponseUtils.buildException(e.getMessage()) ;
        }
        if(count <= 0){
            return BaseResponseUtils.buildFail("数据库存储失败") ;
        }else{
            return BaseResponseUtils.buildSuccess(true) ;
        }
    }
    /**
     * 删除角色
     * @param id 角色ID
     * @return 是否成功
     */
    @Operation(summary = "删除角色", description = "提交角色ID,进行逻辑删除")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @GetMapping(path = "delete", consumes = MediaType.TEXT_PLAIN_VALUE)
    @SsoAop("-1")//@SsoAop(power = "-1")
    public BaseResponse<Boolean> delete(@Parameter(description = "实体id", required = true) Long id){
        if(id == null){
            return BaseResponseUtils.buildFail("id不能为空") ;
        }
        int count;
        try {
            count = this.sv.delete(id);
        } catch (Exception e) {
            log.error("保存角色异常", e);
            return BaseResponseUtils.buildException(e.getMessage()) ;
        }
        if(count <= 0){
            return BaseResponseUtils.buildFail("数据库存储失败") ;
        }else{
            return BaseResponseUtils.buildSuccess(true) ;
        }
    }
}
pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/role/RoleSv.java
New file
@@ -0,0 +1,101 @@
package com.dy.pipIrrBase.role;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pipIrrBase.user.QueryVo;
import com.dy.pipIrrGlobal.daoBa.BaRoleMapper;
import com.dy.pipIrrGlobal.pojoBa.BaRole;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class RoleSv {
    private BaRoleMapper dao;
    @Autowired
    private void setDao(BaRoleMapper dao){
        this.dao = dao;
    }
    /**
     * 得到所有角色
     * @return 所有角色集合
     */
    @SuppressWarnings("unchecked")
    public QueryResultVo<List<BaRole>> selectAll(QueryVo vo){
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(vo) ;
        QueryResultVo<List<BaRole>> rsVo = new QueryResultVo<>() ;
        rsVo.obj = this.dao.selectAll(params) ;
        return rsVo ;
    }
    /**
     * 得到一个角色
     * @param id 角色ID
     * @return 角色实体
     */
    public BaRole selectById(Long id){
        return this.dao.selectById(id) ;
    }
    /**
     * 得到一个用户
     * @param vo 查询条件值对象
     * @return 用户实体
     */
    @SuppressWarnings("unchecked")
    public QueryResultVo<List<BaRole>> selectSome(QueryVo vo){
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(vo) ;
        Long itemTotal = this.dao.selectTotal(params) ;
        QueryResultVo<List<BaRole>> rsVo = new QueryResultVo<>() ;
        rsVo.pageSize = vo.pageSize ;
        rsVo.pageCurr = vo.pageCurr ;
        rsVo.calculateAndSet(itemTotal, params);
        rsVo.obj = this.dao.selectSome(params) ;
        return rsVo ;
    }
    /**
     * 保存实体
     * @param po 实体
     * @return 影响记录数量
     */
    @Transactional
    public int save(BaRole po){
        return this.dao.insert(po) ;
    }
    /**
     * 保存修改实体
     * @param po 实体
     * @return 影响记录数量
     */
    @Transactional
    public int update(BaRole po){
        return this.dao.updateByPrimaryKeySelective(po) ;
    }
    /**
     * 保存修改实体
     * @param id 实体ID
     * @return 影响记录数量
     */
    @Transactional
    public int delete(Long id){
        return this.dao.deleteLogicById(id) ;
    }
}
pipIrr-platform/pipIrr-web/pipIrr-web-base/src/main/java/com/dy/pipIrrBase/user/UserSv.java
@@ -26,18 +26,18 @@
    }
    /**
     * 得到一个片区
     * @param id 片区ID
     * @return 片区实体
     * 得到一个用户
     * @param id 用户ID
     * @return 用户实体
     */
    public BaUser selectById(Long id){
        return this.dao.selectById(id) ;
    }
    /**
     * 得到一个片区
     * 得到一个用户
     * @param vo 查询条件值对象
     * @return 片区实体
     * @return 用户实体
     */
    @SuppressWarnings("unchecked")
    public QueryResultVo<List<BaUser>> selectSome(QueryVo vo){