<?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.SysAreaSettingMapper">

    <resultMap type="com.vctgo.system.domain.SysAreaSetting" id="SysAreaSettingResult">
        <result property="areaId"    column="area_id"    />
        <result property="areaName"    column="area_name"    />
        <result property="areaType"    column="area_type"    />
        <result property="areaTypeName"    column="area_type_name"    />
        <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>

    <sql id="selectSysAreaSettingVo">
        select area_id, area_name, area_type, area_type_name, remark, is_deleted, create_by_id, create_by, create_time, update_by_id, update_by, update_time, tenant_id from sys_area_setting
    </sql>

    <sql id="defaultDeleteCondition">
        is_deleted = 0
    </sql>

    <select id="selectTenantAreaPageList"   resultMap="SysAreaSettingResult">
        <include refid="selectSysAreaSettingVo"/>
        <where>
            <include refid="defaultDeleteCondition"/>
            <if test="query.areaName != null  and query.areaName != ''"> and area_name like concat('%', #{query.areaName}, '%')</if>
            <if test="query.areaType != null  and query.areaType != ''"> and area_type like concat('%', #{query.areaType}, '%')</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectSysAreaSettingList"   resultMap="SysAreaSettingResult">
        <include refid="selectSysAreaSettingVo"/>
        <where>
            <include refid="defaultDeleteCondition"/>
            <if test="query.areaName != null  and query.areaName != ''"> and area_name like concat('%', #{query.areaName}, '%')</if>
            <if test="query.areaType != null  and query.areaType != ''"> and area_type like concat('%', #{query.areaType}, '%')</if>
        </where>
    </select>

    <select id="selectTenantAreaSettingList"   resultMap="SysAreaSettingResult">
        <include refid="selectSysAreaSettingVo"/>
        <where>
            <include refid="defaultDeleteCondition"/>
            <if test="query.areaName != null  and query.areaName != ''"> and area_name like concat('%', #{query.areaName}, '%')</if>
            <if test="query.areaType != null  and query.areaType != ''"> and area_type like concat('%', #{query.areaType}, '%')</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAreaListByParams"   resultMap="SysAreaSettingResult">
        <include refid="selectSysAreaSettingVo"/>
        <where>
            <include refid="defaultDeleteCondition"/>
            <if test="params.areaName != null  and params.areaName != ''"> and area_name = #{params.areaName}</if>
            <if test="params.areaId != null  and params.areaId != ''"> and area_id != #{params.areaId}</if>
        </where>
    </select>

    <select id="getAreaSettingInfo" resultType="com.vctgo.system.domain.SysAreaSetting">
        SELECT
        setting.*
        FROM
        sys_area_setting setting
        LEFT JOIN sys_re_location_area rla ON setting.area_id = rla.area_id
        WHERE
        rla.is_deleted = '0'
        AND setting.is_deleted = '0'
        <if test="locationIds != null  and locationIds != ''">
            AND rla.location_id IN
            <foreach item="id" collection="locationIds.split(',')" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="getAreaNameByIds" resultType="com.vctgo.system.domain.SysAreaSetting">
        SELECT
        area_name
        FROM
        sys_area_setting
        WHERE
        is_deleted = '0'
        AND area_id IN
        <foreach collection="areaIds" item="id" index="i" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="selectAreaNameByIds" resultType="com.vctgo.system.domain.SysAreaSetting">
        SELECT
        *
        FROM
        sys_area_setting
        WHERE
        is_deleted = '0'
        AND area_id IN
        <foreach collection="areaIds" item="id" index="i" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <update id="updateDeletedStatusByIds">
        update sys_area_setting set is_deleted = '1',
        update_by_id = #{query.updateById}, update_by = #{query.updateBy},
        update_time = #{query.updateTime} where area_id in
        <foreach item="id" collection="ids.split(',')" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <delete id="deleteSysAreaSettingByAreaIds" parameterType="String">
        delete from sys_area_setting where area_id in
        <foreach item="areaId" collection="array" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </delete>
</mapper>
