<?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.menu.dao.IMenuDao">

    <cache flushInterval="3600000"/>

    <resultMap id="menuDTO" type="ink.wgink.pojo.dtos.menu.MenuDTO">
        <id property="menuId" column="menu_id"/>
        <result property="menuParentId" column="menu_parent_id"/>
        <result property="menuParentName" column="menu_parent_name"/>
        <result property="menuName" column="menu_name"/>
        <result property="menuSummary" column="menu_summary"/>
        <result property="menuCode" column="menu_code"/>
        <result property="menuUrl" column="menu_url"/>
        <result property="menuType" column="menu_type"/>
        <result property="menuIcon" column="menu_icon"/>
        <result property="menuOrder" column="menu_order"/>
        <result property="menuStatus" column="menu_status"/>
        <result property="openType" column="open_type"/>
    </resultMap>

    <resultMap id="ZTreeDTO" type="ink.wgink.pojo.dtos.ZTreeDTO">
        <id property="id" column="menu_id"/>
        <result property="pId" column="menu_parent_id"/>
        <result property="name" column="menu_name"/>
        <result property="url" column="menu_url"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_menu` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `menu_id` char(36) NOT NULL,
            `menu_parent_id` char(36) DEFAULT '0' COMMENT '父ID',
            `menu_name` varchar(255) DEFAULT NULL COMMENT '菜单名称',
            `menu_summary` varchar(255) DEFAULT NULL COMMENT '菜单说明',
            `menu_code` varchar(255) DEFAULT NULL COMMENT '菜单编码',
            `menu_url` varchar(255) DEFAULT 'javascript:void(0);' COMMENT '菜单链接',
            `menu_type` int(2) DEFAULT '0' COMMENT '菜单类型',
            `menu_icon` varchar(255) DEFAULT 'fa-icon-color-white fa fa-list' COMMENT '菜单图标',
            `menu_order` int(11) DEFAULT '0' COMMENT '菜单排序',
            `menu_status` int(2) DEFAULT '0' COMMENT '菜单状态',
            `open_type` int(2) DEFAULT '1' COMMENT '打开方式：1: 默认，2: 弹窗，3: 页面',
            `gmt_create` datetime DEFAULT NULL,
            `creator` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `is_delete` int(2) DEFAULT '0',
            PRIMARY KEY (`id`,`menu_id`),
            UNIQUE KEY `menu_id` (`menu_id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增菜单 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_menu (
            menu_id,
            menu_parent_id,
            menu_name,
            menu_summary,
            menu_code,
            menu_url,
            menu_type,
            menu_icon,
            menu_order,
            menu_status,
            open_type,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES (
            #{menuId},
            #{menuParentId},
            #{menuName},
            #{menuSummary},
            #{menuCode},
            #{menuUrl},
            #{menuType},
            #{menuIcon},
            #{menuOrder},
            #{menuStatus},
            #{openType},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

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

    <!-- 修改菜单 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            sys_menu
        SET
        <if test="menuName != null and menuName != ''">
            menu_name = #{menuName},
        </if>
        <if test="menuSummary != null and menuSummary != ''">
            menu_summary = #{menuSummary},
        </if>
        <if test="menuCode != null and menuCode != ''">
            menu_code = #{menuCode},
        </if>
        <if test="menuUrl != null and menuUrl != ''">
            menu_url = #{menuUrl},
        </if>
        <if test="menuType != null">
            menu_type = #{menuType},
        </if>
        <if test="menuIcon != null and menuIcon != ''">
            menu_icon = #{menuIcon},
        </if>
        <if test="menuOrder != null and menuOrder != ''">
            menu_order = #{menuOrder},
        </if>
        <if test="menuStatus != null">
            menu_status = #{menuStatus},
        </if>
        <if test="openType != null">
            open_type = #{openType},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            menu_id = #{menuId}
    </update>

    <!-- ztree列表 -->
    <select id="listZTree" parameterType="map" resultMap="ZTreeDTO" useCache="true">
        SELECT
            *
        FROM
            sys_menu
        WHERE
            is_delete = 0
        <if test="menuParentId != null and menuParentId != ''">
        AND
            menu_parent_id = #{menuParentId}
        </if>
        ORDER BY
            menu_order asc
    </select>

    <!-- 菜单列表 -->
    <select id="list" parameterType="map" resultMap="menuDTO" useCache="true">
        SELECT
            menu_id,
            menu_parent_id,
            menu_name,
            menu_summary,
            menu_code,
            menu_url,
            menu_type,
            menu_icon,
            menu_order,
            menu_status,
            open_type
        FROM
            sys_menu t1
        WHERE
            is_delete = 0
        <if test="menuParentId != null and menuParentId != ''">
        AND
            menu_parent_id = #{menuParentId}
        </if>
        <if test="menuIds != null and menuIds.size > 0">
        AND
            menu_id IN
            <foreach collection="menuIds" index="index" open="(" separator="," close=")">
                #{menuIds[${index}]}
            </foreach>
        </if>
        <if test="menuStatus != null">
        AND
            menu_status = #{menuStatus}
        </if>
        <if test="menuType != null">
        AND
            menu_type = #{menuType}
        </if>
        <if test="keywords != null and keywords != ''">
        AND
            menu_name LIKE CONCAT('%', #{keywords}, '%')
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            gmt_create <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            gmt_create <![CDATA[ <= ]]> #{endTime}
        </if>
        <choose>
            <when test="sort != null and (sort == 'menuType' or sort == 'menuCode' or sort == 'menuName' or sort == 'menuOrder' or sort == 'menuStatus')">
                ORDER BY
                <if test="sort == 'menuType'">
                    menu_type ${order}
                </if>
                <if test="sort == 'menuCode'">
                    menu_code ${order}
                </if>
                <if test="sort == 'menuName'">
                    menu_name ${order}
                </if>
                <if test="sort == 'menuOrder'">
                    menu_order ${order}
                </if>
                <if test="sort == 'menuStatus'">
                    menu_status ${order}
                </if>
            </when>
            <otherwise>
            ORDER BY
                menu_order asc
            </otherwise>
        </choose>
    </select>

    <!-- 菜单详情 -->
    <select id="get" parameterType="map" resultMap="menuDTO" useCache="true">
        SELECT
            t1.*,
            t2.menu_name menu_parent_name
        FROM
           sys_menu t1
        LEFT JOIN
            sys_menu t2
        ON
            t1.menu_parent_id = t2.menu_id
        AND
            t2.is_delete = 0
        WHERE
            t1.is_delete = 0
        <if test="menuId != null and menuId != ''">
        AND
            t1.menu_id = #{menuId}
        </if>
    </select>

    <!-- 菜单详情 -->
    <select id="getSimple" parameterType="map" resultMap="menuDTO" useCache="true">
        SELECT
            t1.menu_id,
            t1.menu_parent_id,
            t1.menu_name,
            t1.menu_summary,
            t1.menu_code,
            t1.menu_url,
            t1.menu_type,
            t1.menu_icon,
            t1.menu_order,
            t1.menu_status,
            t1.open_type
        FROM
            sys_menu t1
        WHERE
            t1.is_delete = 0
        <if test="menuId != null and menuId != ''">
        AND
            t1.menu_id = #{menuId}
        </if>
        <if test="menuCode != null and menuCode != ''">
        AND
            t1.menu_code = #{menuCode}
        </if>
    </select>

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

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

    <!-- 通过用户获取菜单 -->
    <select id="listIdByUser" parameterType="map" resultType="String" useCache="false">
        SELECT
            t1.menu_id
        FROM
            sys_role_menu t1
        LEFT JOIN
            sys_role_user t2
        ON
            t1.role_id = t2.role_id
        WHERE
            1 = 1
        <if test="roleType != null and roleType != ''">
        AND
            t1.role_type = #{roleType}
        </if>
        <if test="userId != null and userId != ''">
        AND
            t2.user_id = #{userId}
        </if>
        GROUP BY
            t1.menu_id
    </select>

</mapper>