<?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.service.group.dao.IGroupDao">

    <cache flushInterval="3600000"/>

    <resultMap id="groupDTO" type="ink.wgink.pojo.dtos.group.GroupDTO">
        <id property="groupId" column="group_id"/>
        <result property="groupParentId" column="group_parent_id"/>
        <result property="groupParentName" column="group_parent_name"/>
        <result property="groupName" column="group_name"/>
        <result property="groupSummary" column="group_summary"/>
        <result property="groupCode" column="group_code"/>
    </resultMap>

    <resultMap id="groupPO" type="ink.wgink.pojo.pos.GroupPO">
        <id property="groupId" column="group_id"/>
        <result property="groupParentId" column="group_parent_id"/>
        <result property="groupName" column="group_name"/>
        <result property="groupSummary" column="group_summary"/>
        <result property="groupCode" column="group_code"/>
    </resultMap>

    <resultMap id="groupZTreeDTO" type="ink.wgink.pojo.dtos.ZTreeDTO">
        <id property="id" column="group_id"/>
        <result property="pId" column="group_parent_id"/>
        <result property="name" column="group_name"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_group` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `group_id` char(36) NOT NULL,
            `group_parent_id` char(36) DEFAULT '0' COMMENT '组上级ID',
            `group_name` varchar(255) DEFAULT NULL COMMENT '组名称',
            `group_summary` varchar(255) DEFAULT NULL COMMENT '组说明',
            `group_code` varchar(255) DEFAULT NULL 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`,`group_id`),
            UNIQUE KEY `group_id` (`group_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增组 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_group(
            group_id,
            group_parent_id,
            group_name,
            group_summary,
            group_code,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{groupId},
            #{groupParentId},
            #{groupName},
            #{groupSummary},
            #{groupCode},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

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

    <!-- 修改组 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            sys_group
        SET
        <if test="groupName != null and groupName != ''">
            group_name = #{groupName},
        </if>
        <if test="groupCode != null and groupCode != ''">
            group_code = #{groupCode},
        </if>
        <if test="groupSummary != null">
            group_summary = #{groupSummary},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            group_id = #{groupId}
    </update>

    <!-- ztree列表 -->
    <select id="listZTree" parameterType="map" resultMap="groupZTreeDTO" useCache="true">
        SELECT
            *
        FROM
            sys_group
        WHERE
            is_delete = 0
        <if test="groupParentId != null and groupParentId != ''">
        AND
            group_parent_id = #{groupParentId}
        </if>
    </select>

    <!-- 组列表 -->
    <select id="list" parameterType="map" resultMap="groupDTO" useCache="true">
        SELECT
            *
        FROM
            sys_group
        WHERE
            is_delete = 0
        <if test="groupParentId != null and groupParentId != ''">
        AND
            group_parent_id = #{groupParentId}
        </if>
        <if test="keywords != null and keywords != ''">
        AND
            group_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="groupIds != null and groupIds.size > 0">
        AND
            group_id IN
            <foreach collection="groupIds" index="index" open="(" separator="," close=")">
                #{groupIds[${index}]}
            </foreach>
        </if>
        <choose>
            <when test="sort != null and (sort == 'groupName' or sort == 'groupCode')">
                ORDER BY
                <if test="sort == 'groupName'">
                    group_name ${order}
                </if>
                <if test="sort == 'groupCode'">
                    group_code ${order}
                </if>
            </when>
            <otherwise>
            ORDER BY
                group_code
            </otherwise>
        </choose>
    </select>

    <!-- 组详情 -->
    <select id="get" parameterType="map" resultMap="groupDTO" useCache="false">
        SELECT
            t1.*,
            t2.group_name group_parent_name
        FROM
            sys_group t1
        LEFT JOIN
            sys_group t2
        ON
            t1.group_parent_id = t2.group_id
        AND
            t2.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="groupId != null and groupId != ''">
        AND
            t1.group_id = #{groupId}
        </if>
    </select>

    <!-- 组详情 -->
    <select id="getPO" parameterType="map" resultMap="groupPO" useCache="false">
        SELECT
            *
        FROM
            sys_group
        WHERE
            is_delete = 0
        <if test="groupId != null and groupId != ''">
        AND
            group_id = #{groupId}
        </if>
    </select>

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

    <!-- 获取最后一个子组，实际数据，包含已删除，方式编码重复 -->
    <select id="getLastByParentId" parameterType="String" resultMap="groupDTO" useCache="false">
        SELECT
            *
        FROM
            sys_group
        WHERE
            group_parent_id = #{_parameter}
        ORDER BY
            group_code desc
        LIMIT 0, 1
    </select>

    <!-- 组列表 -->
    <select id="listPO" parameterType="map" resultMap="groupPO" useCache="true">
        SELECT
            *
        FROM
            sys_group
        WHERE
            is_delete = 0
        <if test="groupIds != null and groupIds.size > 0">
            AND
            group_id IN
            <foreach collection="groupIds" index="index" open="(" separator="," close=")">
                #{groupIds[${index}]}
            </foreach>
        </if>
    </select>

</mapper>