<?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.sundot.airport.system.mapper.SysAppRoleMapper">

    <resultMap type="SysAppRole" id="SysAppRoleResult">
        <result property="appId" column="app_id"/>
        <result property="roleId" column="role_id"/>
        <result property="homePage" column="home_page"/>
    </resultMap>

    <sql id="selectSysAppRoleVo">
        select app_id, role_id, home_page
        from sys_app_role
    </sql>

    <select id="selectSysAppRoleList" parameterType="SysAppRole" resultMap="SysAppRoleResult">
        <include refid="selectSysAppRoleVo"/>
        <where>
            <if test="appId != null">
                AND app_id = #{appId}
            </if>
            <if test="roleId != null">
                AND role_id = #{roleId}
            </if>
            <if test="homePage != null">
                AND home_page = #{homePage}
            </if>
        </where>
    </select>

    <select id="selectSysAppRoleByAppId" parameterType="Long" resultMap="SysAppRoleResult">
        <include refid="selectSysAppRoleVo"/>
        where app_id = #{appId}
    </select>

    <select id="selectSysAppRoleByRoleId" parameterType="Long" resultMap="SysAppRoleResult">
        <include refid="selectSysAppRoleVo"/>
        where role_id = #{roleId}
    </select>

    <insert id="insertSysAppRole" parameterType="SysAppRole">
        insert into sys_app_role
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="appId != null">app_id,</if>
            <if test="roleId != null">role_id,</if>
            <if test="homePage != null">home_page,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="appId != null">#{appId},</if>
            <if test="roleId != null">#{roleId},</if>
            <if test="homePage != null">#{homePage},</if>
        </trim>
    </insert>

    <delete id="deleteSysAppRoleByAppId" parameterType="Long">
        delete
        from sys_app_role
        where app_id = #{appId}
    </delete>

    <delete id="deleteSysAppRoleByRoleId" parameterType="Long">
        delete
        from sys_app_role
        where role_id = #{roleId}
    </delete>

    <delete id="deleteSysAppRoleByAppIds">
        delete from sys_app_role where app_id in
        <foreach item="appId" collection="array" open="(" separator="," close=")">
            #{appId}
        </foreach>
    </delete>

    <delete id="deleteSysAppRoleByRoleIds">
        delete from sys_app_role where role_id in
        <foreach item="roleId" collection="array" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </delete>

    <insert id="batchAppRole">
        insert into sys_app_role(app_id, role_id, home_page) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.appId},#{item.roleId},#{item.homePage})
        </foreach>
    </insert>

    <select id="selectSysAppRoleByRoleIds">
        <include refid="selectSysAppRoleVo"/>
        where role_id in
        <foreach collection="list" item="roleId" open="(" separator="," close=")">
            #{roleId}
        </foreach>
    </select>

</mapper>