<?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.MgrRoleCanteenMapper">
    <insert id="insertBatch" parameterType="object">
        insert into mgr_role_canteen
        values
        <foreach collection="canteenList" item="canteen" index="index" separator=",">
            (#{roleId}, #{canteen.dataId}, #{canteen.type}, #{canteen.halfSelect})
        </foreach>
    </insert>

    <select id="getTenantCanteen" resultType="com.bonus.canteen.core.allocation.canteen.vo.AllocCanteenTreeVO">
        <choose>
            <when test="'3'.toString() == authPO.roleType.toString()">
                SELECT half_select, id, parent_id, tree_name, tree_type, biz_type
                FROM (select data_id,
                             half_select
                      from (SELECT r.data_id, r.half_select
                            FROM mgr_role_canteen 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) s
                      ORDER BY data_id, half_select) t
                         LEFT JOIN (SELECT #{merchantId} AS id,
                                           -1            AS parent_id,
                                           merchant_name AS tree_name,
                                           -1            AS tree_type,
                                           0             AS biz_type,
                                           2             AS if_del
                                    FROM merc_merchant
                                    UNION ALL
                                    SELECT area_id   AS id,
                                           super_id  AS parent_id,
                                           area_name AS tree_name,
                                           0         AS tree_type,
                                           0         AS biz_type,
                                           if_del
                                    FROM alloc_area
                                    UNION ALL
                                    SELECT canteen_id   AS id,
                                           area_id      AS parent_id,
                                           canteen_name AS tree_name,
                                           1            AS tree_type,
                                           canteen_type AS biz_type,
                                           if_del
                                    FROM alloc_canteen
                                    UNION ALL
                                    SELECT stall_id   AS id,
                                           canteen_id AS parent_id,
                                           stall_name AS tree_name,
                                           2          AS tree_type,
                                           stall_type AS biz_type,
                                           if_del
                                    FROM alloc_stall) s ON s.id = t.data_id
                <where>
                    <if test="delFlag != null">
                        AND if_del = #{delFlag}
                    </if>
                    <if test="excludeMiddle == 1">
                        AND t.half_select = 1
                    </if>
                </where>
            </when>
            <otherwise>
                SELECT id, parent_id, tree_name, tree_type, biz_type
                FROM (SELECT #{merchantId} AS id,
                             -1            AS parent_id,
                             merchant_name AS tree_name,
                             -1            AS tree_type,
                             0             AS biz_type,
                             2             AS if_del
                      FROM merc_merchant
                      UNION ALL
                      SELECT area_id   AS id,
                             super_id  AS parent_id,
                             area_name AS tree_name,
                             0         AS tree_type,
                             0         AS biz_type,
                             if_del
                      FROM alloc_area
                      UNION ALL
                      SELECT canteen_id   AS id,
                             area_id      AS parent_id,
                             canteen_name AS tree_name,
                             1            AS tree_type,
                             canteen_type AS biz_type,
                             if_del
                      FROM alloc_canteen
                      UNION ALL
                      SELECT stall_id   AS id,
                             canteen_id AS parent_id,
                             stall_name AS tree_name,
                             2          AS tree_type,
                             stall_type AS biz_type,
                             if_del
                      FROM alloc_stall) s
                <where>
                    <if test="delFlag != null">
                        AND if_del = #{delFlag}
                    </if>
                </where>
            </otherwise>
        </choose>
    </select>

    <select id="getRoleListByDataId" resultType="com.bonus.canteen.core.auth.role.entity.MgrRole">
        SELECT mrc.role_id
        FROM mgr_role_canteen mrc
        WHERE mrc.data_id = #{dataId}
          AND mrc.half_select = #{excludeMiddle}
        <if test="roleId != null">
            AND mrc.role_id != #{roleId}
        </if>
        GROUP BY mrc.role_id
    </select>
</mapper>
