<?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.bonus.canteen.core.auth.role.mapper.MgrRoleAreaMapper">

    <select id="getTenantAreaIdList" resultType="java.lang.Long">
        SELECT r.data_id
        FROM mgr_role_area r
                 LEFT JOIN mgr_role o ON o.role_id = r.role_id
                 LEFT JOIN mgr_user_role u ON r.role_id = u.role_id
        WHERE u.user_id = #{authPO.userId}
          AND o.role_code = #{authPO.roleCode}
          AND o.del_flag = 2
        <if test="excludeMiddle == 1">
            AND r.half_select = 1
        </if>
    </select>

    <insert id="insertBatch">
        insert into mgr_role_area
        values
        <foreach collection="areaList" item="area" index="index" separator=",">
            (#{roleId}, #{area.dataId}, #{area.halfSelect})
        </foreach>
    </insert>

    <select id="getRoleListByDataId" resultType="com.bonus.canteen.core.auth.role.entity.MgrRole">
        SELECT mra.role_id
        FROM mgr_role_area mra
        WHERE mra.data_id = #{dataId}
          AND mra.half_select = #{excludeMiddle}
        <if test="roleIdList != null and roleIdList.size() != 0">
            AND mra.role_id NOT IN
            <foreach collection="roleIdList" item="roleId" index="index" open="(" close=")" separator=",">
                #{roleId}
            </foreach>
        </if>
        GROUP BY mra.role_id
    </select>
</mapper>