RoleMapper.java 2.67 KB
package com.uccc.admin.mapper;

import com.uccc.admin.domain.Role;
import com.uccc.pretty.common.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by bert on 2021-09-13 22:23
 */
public interface RoleMapper {
    static final String TABLE_NAME_1 = "user_role";
    static final String TABLE_NAME_2 = "roles";

    @Insert({
            "<script>",
            "insert into user_role (",
            "role_id, user_id, cts, uts ) values (",
            "#{roleId}, #{userId}, now(), now() )",
            "</script>",
    })
    int createUserRole(@Param("userId") Long userId, @Param("roleId") int roleId);

    @Delete("delete from " + TABLE_NAME_1 + " where user_id = #{user.id}")
    int deleteUserRole(@Param("user") User user);

    @Select({
            "<script>",
            "select count(id) from " + TABLE_NAME_2,
            "where name = #{name}",
            "</script>"
    })
    int selectCountByName(@Param("name") String name);

    @Insert({
            "<script>",
            "insert into " + TABLE_NAME_2,
            "(",
            "name, cts, uts, edit, en_name",
            ")",
            "values",
            "(",
            "#{role.name}, now(), now(), 1, #{role.enName}",
            ")",
            "</script>"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id")
    int createRole(@Param("role") Role role);

    @Select({
            "<script>",
            "select * from " + TABLE_NAME_2,
            "where name = #{name}",
            "</script>",
    })
    Role SelectRoleByName(@Param("name") String name);

    @Select({
            "<script>",
            "select * from " + TABLE_NAME_2,
            "<when test='name!=null'>",
            "where (name like concat('%',#{name},'%')",
            "</when>",
            "order by cts desc",
            "</script>"
    })
    List<Role> selectRoleList(@Param("name") String name);

    @Update({
            "<script>",
            "UPDATE " + TABLE_NAME_2 + " SET uts = now()",
            "<when test='role.name!=null'>",
            ", name = #{role.name}",
            "</when>",
            "<when test='role.enName!=null'>",
            ", en_name = #{role.enName}",
            "</when>",
            "where id = #{role.id}",
            "</script>"
    })
    int updateRole(@Param("role") Role role);

    @Select({
            "<script>",
            "select DISTINCT on(r.name) r.name,r.id,r.edit,r.en_name,r.cts,r.uts from users u JOIN user_role ur ON ur.user_id = u.id",
            "JOIN roles r ON ur.role_id = r.id",
            "where u.id = #{userId}",
            "</script>"
    })
    List<com.uccc.pretty.common.Role> selectRoleByUserId(@Param("userId") Long userId);
}