<?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.cm.common.plugin.dao.dataarea.IDataAreaDao">

    <cache/>

    <resultMap id="areaDTO" type="com.cm.common.plugin.pojo.dtos.dataarea.DataAreaDTO">
        <id property="areaId" column="area_id"/>
        <result property="areaParentId" column="area_parent_id"/>
        <result property="areaName" column="area_name"/>
        <result property="areaCode" column="area_code"/>
        <result property="areaCityCode" column="area_city_code"/>
        <result property="areaMergerName" column="area_merger_name"/>
        <result property="areaShortName" column="area_short_name"/>
        <result property="areaZipCode" column="area_zip_code"/>
        <result property="areaLevel" column="area_level"/>
        <result property="areaLng" column="area_lng"/>
        <result property="areaLat" column="area_lat"/>
        <result property="areaPinyin" column="area_pinyin"/>
        <result property="areaFirst" column="area_first"/>
    </resultMap>

    <resultMap id="areaZTreeDTO" type="com.cm.common.pojo.dtos.ZTreeDTO">
        <id property="id" column="area_id"/>
        <result property="pId" column="area_parent_id"/>
        <result property="name" column="area_name"/>
        <result property="title" column="area_merger_name"/>
    </resultMap>

    <!-- 新增字典 -->
    <insert id="saveArea" parameterType="map" useGeneratedKeys="true" keyColumn="areaId" flushCache="true">
        INSERT INTO data_area(
            area_parent_id,
            area_name,
            area_code,
            area_city_code,
            area_merger_name,
            area_short_name,
            area_zip_code,
            area_level,
            area_lng,
            area_lat,
            area_pinyin,
            area_first,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{areaParentId},
            #{areaName},
            #{areaCode},
            #{areaCityCode},
            #{areaMergerName},
            #{areaShortName},
            #{areaZipCode},
            #{areaLevel},
            #{areaLng},
            #{areaLat},
            #{areaPinyin},
            #{areaFirst},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除字典 -->
    <update id="removeArea" parameterType="map" flushCache="true">
        UPDATE
            data_area
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            area_id IN
        <foreach collection="areaIds" index="index" open="(" separator="," close=")">
            #{areaIds[${index}]}
        </foreach>
    </update>

    <!-- 修改字典 -->
    <update id="updateArea" parameterType="map" flushCache="true">
        UPDATE
            data_area
        SET
        <if test="areaName != null and areaName != ''">
            area_name = #{areaName},
        </if>
        <if test="areaCode != null and areaCode != ''">
            area_code = #{areaCode},
        </if>
        <if test="areaCityCode != null and areaCityCode != ''">
            area_city_code = #{areaCityCode},
        </if>
        <if test="areaMergerName != null and areaMergerName != ''">
            area_merger_name = #{areaMergerMame},
        </if>
        <if test="areaShortName != null and areaShortName != ''">
            area_short_name = #{areaShortName},
        </if>
        <if test="areaZipCode != null and areaZipCode != ''">
            area_zip_code = #{areaZipCode},
        </if>
        <if test="areaLevel != null and areaLevel != ''">
            area_level = #{areaLevel},
        </if>
        <if test="areaLng != null and areaLng != ''">
            area_lng = #{areaLng},
        </if>
        <if test="areaLat != null and areaLat != ''">
            area_lat = #{areaLat},
        </if>
        <if test="areaPinyin != null and areaPinyin != ''">
            area_pinyin = #{areaPinyin},
        </if>
        <if test="areaFirst != null and areaFirst != ''">
            area_first = #{areaFirst},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            area_id = #{areaId}
    </update>

    <!-- ztree列表 -->
    <select id="listZTreeArea" parameterType="map" resultMap="areaZTreeDTO" useCache="true">
        SELECT
            *
        FROM
            data_area
        WHERE
            is_delete = 0
        <if test="areaParentId != null and areaParentId != ''">
        AND
            area_parent_id = #{areaParentId}
        </if>
    </select>

    <!-- 字典列表 -->
    <select id="listArea" parameterType="map" resultMap="areaDTO" useCache="true">
        SELECT
            area_id,
            area_parent_id,
            area_name,
            area_code,
            area_city_code,
            area_merger_name,
            area_short_name,
            area_zip_code,
            area_level,
            area_lng,
            area_lat,
            area_pinyin,
            area_first
        FROM
            data_area
        WHERE
            is_delete = 0
        <if test="areaParentId != null and areaParentId != ''">
        AND
            area_parent_id = #{areaParentId}
        </if>
        <if test="keywords != null and keywords != ''">
        AND
            area_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="areaCode != null and areaCode != ''">
        AND
            area_code LIKE CONCAT(#{areaCode}, '%')
        </if>
        <if test="areaIds != null and areaIds.size > 0">
        AND
            area_id IN
            <foreach collection="areaIds" index="index" open="(" separator="," close=")">
                #{areaIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 字典详情 -->
    <select id="getArea" parameterType="map" resultMap="areaDTO">
        SELECT
            t1.area_id,
            t1.area_parent_id,
            t1.area_name,
            t1.area_code,
            t1.area_city_code,
            t1.area_merger_name,
            t1.area_short_name,
            t1.area_zip_code,
            t1.area_level,
            t1.area_lng,
            t1.area_lat,
            t1.area_pinyin,
            t1.area_first,
            t2.area_name area_parent_name
        FROM
            data_area t1
        LEFT JOIN
            data_area t2
        ON
            t1.area_parent_id = t2.area_id
        AND
            t2.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="areaId != null and areaId != ''">
        AND
            t1.area_id = #{areaId}
        </if>
    </select>

    <!-- 子节点数量 -->
    <select id="countByParentId" parameterType="String" resultType="Integer" useCache="true">
        SELECT
            COUNT(*)
        FROM
            data_area
        WHERE
            is_delete = 0
        AND
            area_parent_id = #{_parameter}
    </select>

    <!-- 获取最后一个子字典，实际数据，包含已删除，方式编码重复 -->
    <select id="getLastByParentId" parameterType="String" resultMap="areaDTO">
        SELECT
            area_id,
            area_parent_id,
            area_name,
            area_code,
            area_city_code,
            area_merger_name,
            area_short_name,
            area_zip_code,
            area_level,
            area_lng,
            area_lat,
            area_pinyin,
            area_first
        FROM
            data_area
        WHERE
            area_parent_id = #{_parameter}
        LIMIT 0, 1
    </select>

</mapper>