package cn.hnthyy.thmz.mapper.thmz;

import cn.hnthyy.thmz.entity.thmz.RoleMenuRelation;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 角色和菜单关系表
 */
public interface RoleMenuRelationMapper {
    /**
     * 根据单个id查询菜单关系
     *
     * @param id
     * @return
     */
    @Select("select * from t_role_menu_relation where id=#{id,jdbcType=BIGINT}")
    RoleMenuRelation selectById(@Param("id") Long id);


    /**
     * 根据roleId查询菜单关系
     *
     * @param roleId
     * @return
     */
    @Select("select * from t_role_menu_relation where role_id=#{roleId,jdbcType=BIGINT}")
    List<RoleMenuRelation> selectByRoleId(@Param("roleId") Long roleId);

    /**
     * 根据menuId查询角色菜单关系
     *
     * @param menuId
     * @return
     */
    @Select("select * from t_role_menu_relation where menu_id=#{menuId,jdbcType=BIGINT}")
    List<RoleMenuRelation> selectByMenuId(@Param("menuId") Long menuId);


    /**
     * 根据角色id集合查询菜单关系
     *
     * @param roleIds
     * @return
     */
    @Select({"<script>",
            "select * from t_role_menu_relation where role_id in" +
                    "<foreach item='item' index='index' collection='roleIds' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
                    "</script>"})
    List<RoleMenuRelation> selectByRoleIds(@Param("roleIds") List<Long> roleIds);


    /**
     * 新增菜单关系
     *
     * @param RoleMenuRelation
     * @return
     */
    @Insert("INSERT INTO t_role_menu_relation(role_id,menu_id,create_time,create_id) values(#{roleId,jdbcType=BIGINT},#{menuId,jdbcType=BIGINT},#{createTime,jdbcType=TIMESTAMP},#{createId,jdbcType=BIGINT})")
    int insertRoleMenuRelation(RoleMenuRelation RoleMenuRelation);

    /**
     * 删除菜单关系
     *
     * @param id
     * @return
     */
    @Delete("delete from t_role_menu_relation where id=#{id,jdbcType=BIGINT}")
    int deleteById(@Param("id") Long id);

    /**
     * 删除用户对应的所有菜单关系
     *
     * @param roleId
     * @return
     */
    @Delete("delete from t_role_menu_relation where role_id=#{roleId,jdbcType=BIGINT}")
    int deleteByRoleId(@Param("roleId") Long roleId);


    /**
     * 删除单个菜单对应的所有菜单关系
     *
     * @param menuId
     * @return
     */
    @Delete("delete from t_role_menu_relation where menu_id=#{menuId,jdbcType=BIGINT}")
    int deleteByMenuId(@Param("menuId") Long menuId);


}
