<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.pine.admin.modules.system.dao.SysUserRoleDao">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.pine.admin.modules.system.entity.SysUserRole" id="sysUserRoleMap">
        <result property="id" column="id"/>
        <result property="sysUserId" column="sys_user_id"/>
        <result property="sysRoleId" column="sys_role_id"/>
    </resultMap>

    <sql id="Base_Column_List">
                 id,
                     sys_user_id,
                     sys_role_id        </sql>
    <!-- 根据Id查询-->
    <select id="selectByPrimaryKey" resultMap="sysUserRoleMap" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        from sys_user_role
        where id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectByUser" resultMap="sysUserRoleMap" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        from sys_user_role
        where sys_user_id = #{userId,jdbcType=INTEGER} and sys_role_id = 1 limit 1
    </select>

    <!-- 根据Id删除-->
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
		delete from sys_user_role
		where id = #{id,jdbcType=INTEGER}
	</delete>

    <!-- 插入数据-->
    <insert id="insertSelective" parameterType="com.pine.admin.modules.system.entity.SysUserRole"
            useGeneratedKeys="true" keyProperty="id">

        insert into sys_user_role
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id !=null">id,</if>
            <if test="sysUserId !=null">sys_user_id,</if>
            <if test="sysRoleId !=null">sys_role_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id !=null">#{id},</if>
            <if test="sysUserId !=null">#{sysUserId},</if>
            <if test="sysRoleId !=null">#{sysRoleId},</if>

        </trim>
    </insert>

    <!-- 更新数据-->
    <update id="updateByPrimaryKeySelective"
            parameterType="com.pine.admin.modules.system.entity.SysUserRole">
        update sys_user_role
        <set>
            <if test="id != null">id = #{id},</if>
            <if test="sysUserId != null">sys_user_id = #{sysUserId},</if>
            <if test="sysRoleId != null">sys_role_id = #{sysRoleId},</if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <!-- 分页查询-->
    <select id="querySysUserRolesByPage" parameterType="java.util.Map"
            resultType="com.pine.admin.modules.system.entity.SysUserRole">
        select
        <include refid="Base_Column_List"/>
        from sys_user_role
        <where>
            and del_flag = 'N'
            <if test="record.id != null and !&quot;&quot;.equals(record.id)">and id = #{record.id}</if>
            <if test="record.sysUserId != null and !&quot;&quot;.equals(record.sysUserId)">and sys_user_id =
                #{record.sysUserId}
            </if>
            <if test="record.sysRoleId != null and !&quot;&quot;.equals(record.sysRoleId)">and sys_role_id =
                #{record.sysRoleId}
            </if>
        </where>
        order by id desc
    </select>

    <!-- 全部查询 -->
    <select id="queryAllSysUserRole" parameterType="java.util.Map"
            resultType="com.pine.admin.modules.system.entity.SysUserRole">
        select
        <include refid="Base_Column_List"/>
        from sys_user_role
        <where>
            and del_flag = 'N'
            <if test="id != null and !&quot;&quot;.equals(id)">and id = #{id}</if>
            <if test="sysUserId != null and !&quot;&quot;.equals(sysUserId)">and sys_user_id = #{sysUserId}</if>
            <if test="sysRoleId != null and !&quot;&quot;.equals(sysRoleId)">and sys_role_id = #{sysRoleId}</if>
        </where>
        order by id desc
    </select>

    <!-- 根据条件查询一个 -->
    <select id="querySysUserRole" parameterType="java.util.Map"
            resultType="com.pine.admin.modules.system.entity.SysUserRole">
        select
        <include refid="Base_Column_List"/>
        from sys_user_role
        <where>
            and del_flag = 'N'
            <if test="id != null and !&quot;&quot;.equals(id)">and id = #{id}</if>
            <if test="sysUserId != null and !&quot;&quot;.equals(sysUserId)">and sys_user_id = #{sysUserId}</if>
            <if test="sysRoleId != null and !&quot;&quot;.equals(sysRoleId)">and sys_role_id = #{sysRoleId}</if>
        </where>
        LIMIT 1
    </select>

    <update id="delete">
        UPDATE sys_user_role SET del_flag = 'Y'
        <where>
            AND id = #{id}
        </where>
    </update>

    <update id="delUser">
        DELETE FROM sys_user_role
        <where>
            AND sys_user_id = #{sysUserId}
        </where>
    </update>

</mapper>