<?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.datadictionary.IDataDictionaryDao">

    <cache/>

    <resultMap id="dictionaryDTO" type="com.cm.common.plugin.pojo.dtos.datadictionary.DataDictionaryDTO">
        <id property="dictionaryId" column="dictionary_id"/>
        <result property="dictionaryParentId" column="dictionary_parent_id"/>
        <result property="dictionaryParentName" column="dictionary_parent_name"/>
        <result property="dictionaryName" column="dictionary_name"/>
        <result property="dictionarySummary" column="dictionary_summary"/>
        <result property="dictionaryCode" column="dictionary_code"/>
        <result property="dictionarySort" column="dictionary_sort"/>
    </resultMap>

    <resultMap id="dictionaryZTreeDTO" type="com.cm.common.pojo.dtos.ZTreeDTO">
        <id property="id" column="dictionary_id"/>
        <result property="pId" column="dictionary_parent_id"/>
        <result property="name" column="dictionary_name"/>
    </resultMap>

    <!-- 新增字典 -->
    <insert id="saveDictionary" parameterType="map" flushCache="true">
        INSERT INTO data_dictionary(
            dictionary_id,
            dictionary_parent_id,
            dictionary_name,
            dictionary_summary,
            dictionary_code,
            dictionary_sort,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{dictionaryId},
            #{dictionaryParentId},
            #{dictionaryName},
            #{dictionarySummary},
            #{dictionaryCode},
            #{dictionarySort},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

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

    <!-- 修改字典 -->
    <update id="updateDictionary" parameterType="map" flushCache="true">
        UPDATE
            data_dictionary
        SET
        <if test="dictionaryName != null and dictionaryName != ''">
            dictionary_name = #{dictionaryName},
        </if>
        <if test="dictionaryCode != null and dictionaryCode != ''">
            dictionary_code = #{dictionaryCode},
        </if>
        <if test="dictionarySummary != null">
            dictionary_summary = #{dictionarySummary},
        </if>
        <if test="dictionarySort != null">
            dictionary_sort = #{dictionarySort},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            dictionary_id = #{dictionaryId}
    </update>

    <!-- ztree列表 -->
    <select id="listZTreeDictionary" parameterType="map" resultMap="dictionaryZTreeDTO" useCache="true">
        SELECT
            *
        FROM
            data_dictionary
        WHERE
            is_delete = 0
        <if test="dictionaryParentId != null and dictionaryParentId != ''">
        AND
            dictionary_parent_id = #{dictionaryParentId}
        </if>
        ORDER BY
            dictionary_sort, dictionary_code
    </select>

    <!-- 字典列表 -->
    <select id="listDictionary" parameterType="map" resultMap="dictionaryDTO" useCache="true">
        SELECT
            dictionary_id,
            dictionary_parent_id,
            dictionary_name,
            dictionary_summary,
            dictionary_code,
            dictionary_sort
        FROM
            data_dictionary
        WHERE
            is_delete = 0
        <if test="dictionaryParentId != null and dictionaryParentId != ''">
        AND
            dictionary_parent_id = #{dictionaryParentId}
        </if>
        <if test="keywords != null and keywords != ''">
        AND
            dictionary_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="dictionaryCode != null and dictionaryCode != ''">
        AND
            dictionary_code LIKE CONCAT(#{dictionaryCode}, '%')
        </if>
        <if test="dictionaryIds != null and dictionaryIds.size > 0">
        AND
            dictionary_id IN
            <foreach collection="dictionaryIds" index="index" open="(" separator="," close=")">
                #{dictionaryIds[${index}]}
            </foreach>
        </if>
        ORDER BY
            dictionary_sort, dictionary_code
    </select>

    <!-- 字典详情 -->
    <select id="getDictionary" parameterType="map" resultMap="dictionaryDTO" useCache="true">
        SELECT
            t1.*,
            t2.dictionary_name dictionary_parent_name
        FROM
            data_dictionary t1
        LEFT JOIN
            data_dictionary t2
        ON
            t1.dictionary_parent_id = t2.dictionary_id
        AND
            t2.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="dictionaryId != null and dictionaryId != ''">
        AND
            t1.dictionary_id = #{dictionaryId}
        </if>
    </select>

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

    <!-- 获取最后一个子字典，实际数据，包含已删除，方式编码重复 -->
    <select id="getLastByParentId" parameterType="String" resultMap="dictionaryDTO">
        SELECT
            *
        FROM
            data_dictionary
        WHERE
            dictionary_parent_id = #{_parameter}
        ORDER BY
            dictionary_code desc
        LIMIT 0, 1
    </select>

</mapper>