<?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="ink.wgink.module.dictionary.dao.IDataDao">

    <cache flushInterval="3600000"/>

    <resultMap id="dataDTO" type="ink.wgink.module.dictionary.pojo.dtos.DataDTO">
        <id property="dataId" column="data_id"/>
        <result property="dataParentId" column="data_parent_id"/>
        <result property="dataParentName" column="data_parent_name"/>
        <result property="dataName" column="data_name"/>
        <result property="dataSummary" column="data_summary"/>
        <result property="dataCode" column="data_code"/>
        <result property="dataSort" column="data_sort"/>
    </resultMap>

    <resultMap id="dataZTreeDTO" type="ink.wgink.pojo.dtos.ZTreeDTO">
        <id property="id" column="data_id"/>
        <result property="pId" column="data_parent_id"/>
        <result property="name" column="data_name"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `data_data` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `data_id` char(36) NOT NULL,
            `data_parent_id` char(36) DEFAULT '0' COMMENT '字典上级ID',
            `data_name` varchar(255) DEFAULT NULL COMMENT '字典名称',
            `data_summary` varchar(255) DEFAULT NULL COMMENT '字典说明',
            `data_code` varchar(255) DEFAULT NULL COMMENT '字典编码',
            `data_sort` int(11) DEFAULT '0' COMMENT '字典排序',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(2) DEFAULT '0',
            PRIMARY KEY (`id`,`data_id`),
            UNIQUE KEY `data_id` (`data_id`) USING BTREE,
            KEY `is_delete_index` (`is_delete`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增字典 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO data_data(
            data_id,
            data_parent_id,
            data_name,
            data_summary,
            data_code,
            data_sort,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{dataId},
            #{dataParentId},
            #{dataName},
            #{dataSummary},
            #{dataCode},
            #{dataSort},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

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

    <!-- 修改字典 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            data_data
        SET
        <if test="dataName != null and dataName != ''">
            data_name = #{dataName},
        </if>
        <if test="dataCode != null and dataCode != ''">
            data_code = #{dataCode},
        </if>
        <if test="dataSummary != null">
            data_summary = #{dataSummary},
        </if>
        <if test="dataSort != null">
            data_sort = #{dataSort},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            data_id = #{dataId}
    </update>

    <!-- ztree列表 -->
    <select id="listZTree" parameterType="map" resultMap="dataZTreeDTO" useCache="true">
        SELECT
            *
        FROM
            data_data
        WHERE
            is_delete = 0
        <if test="dataParentId != null and dataParentId != ''">
        AND
            data_parent_id = #{dataParentId}
        </if>
        ORDER BY
            data_sort, data_code
    </select>

    <!-- 字典列表 -->
    <select id="list" parameterType="map" resultMap="dataDTO" useCache="true">
        SELECT
            data_id,
            data_parent_id,
            data_name,
            data_summary,
            data_code,
            data_sort
        FROM
            data_data
        WHERE
            is_delete = 0
        <if test="dataParentId != null and dataParentId != ''">
        AND
            data_parent_id = #{dataParentId}
        </if>
        <if test="keywords != null and keywords != ''">
        AND
            data_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="dataCode != null and dataCode != ''">
        AND
            data_code LIKE CONCAT(#{dataCode}, '%')
        </if>
        <if test="dataIds != null and dataIds.size > 0">
        AND
            data_id IN
            <foreach collection="dataIds" index="index" open="(" separator="," close=")">
                #{dataIds[${index}]}
            </foreach>
        </if>
        ORDER BY
            data_sort, data_code
    </select>

    <!-- 字典详情 -->
    <select id="get" parameterType="map" resultMap="dataDTO" useCache="true">
        SELECT
            t1.*,
            t2.data_name data_parent_name
        FROM
            data_data t1
        LEFT JOIN
            data_data t2
        ON
            t1.data_parent_id = t2.data_id
        AND
            t2.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="dataId != null and dataId != ''">
        AND
            t1.data_id = #{dataId}
        </if>
    </select>

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

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

</mapper>