<?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.MgrRoleWarehouseMapper">
    <insert id="insertBatch" parameterType="object">
        insert into mgr_role_warehouse
        values
        <foreach collection="warehouseList" item="warehouseId" index="index" separator=",">
            (#{roleId}, #{warehouseId})
        </foreach>
    </insert>

    <select id="getTenantWarehouse" resultType="com.bonus.canteen.core.drp.vo.DrpWarehouseAllVO">
        SELECT d.warehouse_id,
               warehouse_name
        FROM
        <choose>
            <when test="'3'.toString() == authPO.roleType.toString()">
                (SELECT r.warehouse_id
                 FROM mgr_role_warehouse 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.del_flag = 2
                   AND o.role_code = #{authPO.roleCode}) t
                    LEFT JOIN drp_warehouse d ON d.warehouse_id = t.warehouse_id
                <where>
                    <if test="delFlag != null">
                        AND d.del_flag = #{delFlag}
                    </if>
                </where>
            </when>
            <otherwise>
                drp_warehouse d
                <where>
                    <if test="delFlag != null">
                        AND d.del_flag = #{delFlag}
                    </if>
                </where>
            </otherwise>
        </choose>
    </select>
</mapper>
