package cn.hnthyy.thmz.mapper.thmz;

import cn.hnthyy.thmz.entity.thmz.UserMenuRelation;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 菜单与用户的关系表
 */
public interface UserMenuRelationMapper {
    /**
     * 根据单个id查询菜单关系
     *
     * @param id
     * @return
     */
    @Select("select * from t_user_menu_relation where id=#{id,jdbcType=BIGINT}")
    UserMenuRelation selectById(@Param("id") Long id);


    /**
     * 根据userId查询菜单关系
     *
     * @param userId
     * @return
     */
    @Select("select * from t_user_menu_relation where user_id=#{userId,jdbcType=BIGINT}")
    List<UserMenuRelation> selectByUserId(@Param("userId") Long userId);


    /**
     * 根据用户id集合查询菜单关系
     *
     * @param userIds
     * @return
     */
    @Select({"<script>",
            "select * from t_user_menu_relation where user_id in" +
                    "<foreach item='item' index='index' collection='userIds' open='(' separator=',' close=')'>" +
                    "#{item}" +
                    "</foreach>" +
                    "</script>"})
    List<UserMenuRelation> selectByUserIds(@Param("userIds") List<Long> userIds);


    /**
     * 新增菜单关系
     *
     * @param userMenuRelation
     * @return
     */
    @Insert("INSERT INTO t_user_menu_relation(user_id,menu_id,create_time,create_id) values(#{userId,jdbcType=BIGINT},#{menuId,jdbcType=BIGINT},#{createTime,jdbcType=TIMESTAMP},#{createId,jdbcType=BIGINT})")
    int insertUserMenuRelation(UserMenuRelation userMenuRelation);

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

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


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


}
