<?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.vctgo.system.mapper.SysReLocationClassMapper">

    <resultMap type="com.vctgo.system.api.domain.SysReLocationClass" id="SysReLocationClassResult">
        <result property="id"    column="id"    />
        <result property="locationId"    column="location_id"    />
        <result property="classId"    column="class_id"    />
        <result property="remark"    column="remark"    />
        <result property="isDeleted"    column="is_deleted"    />
        <result property="createById"    column="create_by_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateById"    column="update_by_id"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="tenantId"    column="tenant_id"    />
    </resultMap>

    <resultMap type="com.vctgo.system.api.domain.dto.ReLocationDto" id="ReLocationResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="type"    column="type"    />
        <result property="areaId"    column="area_id"    />
        <result property="classId"    column="class_id"    />
        <result property="tenantId"    column="tenant_id"    />
    </resultMap>

    <sql id="selectSysReLocationClassVo">
        select id, location_id, class_id, remark, is_deleted, create_by_id, create_by, create_time, update_by_id, update_by, update_time, tenant_id from sys_re_location_class
    </sql>

    <resultMap type="com.vctgo.system.api.domain.SysClassSetting" id="sysClassResult"
               extends="SysReLocationClassResult">
        <collection property="id" ofType="java.lang.String" javaType="java.util.List"
                    column="{id=id}" select="selectClass"
                    fetchType="eager"/>
    </resultMap>

    <select id="selectSysReLocationClassList" resultMap="sysClassResult">
        select * from
        <where>
            <if test="query.locationIdList != null and query.locationIdList.length >0">and location_id in
                <foreach item="id" collection="locationIdList" open="(" separator="," close=")">
                    #{id}
                </foreach></if>
            <if test="query.locationId != null "> and location_id = #{query.locationId}</if>
            <if test="query.classId != null "> and class_id = #{query.classId}</if>
            <if test="query.isDeleted != null  and query.isDeleted != ''"> and is_deleted = #{query.isDeleted}</if>
            <if test="query.createById != null "> and create_by_id = #{query.createById}</if>
            <if test="query.updateById != null "> and update_by_id = #{query.updateById}</if>
            <if test="query.tenantId != null "> and tenant_id = #{query.tenantId}</if>
        </where>
    </select>

    <select id="selectLocationInfoByParam" resultMap="ReLocationResult">
        SELECT ls.id,ls.code,ls.name,ls.type,
               area_rel.area_id,class_rel.class_id FROM sys_location_setting ls
        inner join (
            select location_id,GROUP_CONCAT(area_id) area_id from sys_re_location_area
            where tenant_id = #{query.tenantId} and is_deleted = '0'
            <if test="query.areaId != null and query.areaId != ''"> and area_id = #{query.areaId}</if>
            <if test="query.areaIdList != null and query.areaIdList.size >0 ">
                and area_id in
                <foreach collection="query.areaIdList" item="areaId" index="i" open="(" close=")" separator=",">
                    #{areaId}
                </foreach>
            </if>
            group by location_id
        ) area_rel on area_rel.location_id = ls.id
        inner join (
            select location_id,GROUP_CONCAT(class_id) class_id from sys_re_location_class
            where tenant_id = #{query.tenantId} and is_deleted = '0'
            <if test="query.classId != null and query.classId != ''"> and class_id = #{query.classId}</if>
            <if test="query.classIdList != null  and query.classIdList.size >0 ">
                and class_id in
                <foreach collection="query.classIdList" item="classId" index="i" open="(" close=")" separator=",">
                    #{classId}
                </foreach>
            </if>
            group by location_id
        ) class_rel on class_rel.location_id = ls.id
        <where>
            ls.tenant_id = #{query.tenantId} and ls.is_deleted = '0'
            <if test="query.id != null and query.id != ''"> and ls.id = #{query.id}</if>
            <if test="query.code != null and query.code != ''"> and ls.code = #{query.code}</if>
            <if test="query.name != null and query.name != ''"> and ls.name = #{query.name}</if>
            <if test="query.type != null and query.type != ''"> and ls.type = #{query.type}</if>
            <if test="query.idList != null  and query.idList.size >0 ">
                and ls.id in
                <foreach collection="query.idList" item="id" index="i" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

    <select id="staticsReLocationByClassId" resultType="java.lang.Long">
        select count(*) from sys_re_location_class
        where class_id = #{classId} and is_deleted = '0'
    </select>

    <delete id="deleteSysReLocationClassByIds" parameterType="String">
        delete from sys_re_location_class where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="updateSysReLocationClassBylocationIdAndAreaIds" parameterType="String">
        update sys_re_location_class set  is_deleted='1' where location_id= #{locationId} and class_id in
        <foreach item="id" collection="classIds" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectLocationIdByUserId" parameterType="String" resultType="com.vctgo.system.api.domain.SysReLocationClass">
        SELECT
            lc.location_id
        FROM
            sys_re_class_user cu
                INNER JOIN sys_re_location_class lc ON cu.class_id = lc.location_id
        WHERE
            cu.is_deleted = '0'
          AND lc.is_deleted = '0'
          AND cu.bind_user_id = #{userId}
    </select>

    <select id="selectLocationAndClass" resultType="com.vctgo.system.api.domain.SysReLocationClass">
        SELECT
            ls.id locationId,
            ls.NAME locationName,
            ls.attribute attribute,
            cs.id classId,
            cs.grade_in gradeIn
        FROM
            sys_location_setting ls
                LEFT JOIN sys_re_location_class lc ON lc.location_id = ls.id AND lc.is_deleted = '0'
                LEFT JOIN sys_class_setting cs ON lc.class_id = cs.id AND cs.is_deleted = '0'
        WHERE
            ls.is_deleted = '0'
    </select>
</mapper>
