<?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.dora.mapper.WechatMenuMapper">

    <resultMap id="BaseResultMap" type="com.dora.entity.WechatMenu">
        <id column="id" property="id" />
        <result column="parent_id" property="parentId" />
        <result column="menu_name" property="menuName" />
        <result column="menu_type" property="menuType" />
        <result column="menu_key" property="menuKey" />
        <result column="menu_url" property="menuUrl" />
        <result column="media_id" property="mediaId" />
        <result column="appid" property="appid" />
        <result column="pagepath" property="pagepath" />
        <result column="sort_order" property="sortOrder" />
        <result column="is_enabled" property="isEnabled" />
        <result column="description" property="description" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <sql id="Base_Column_List">
        id, parent_id, menu_name, menu_type, menu_key, menu_url, media_id, appid, pagepath,
        sort_order, is_enabled, description, create_time, update_time
    </sql>

    <select id="selectById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_menu
        WHERE id = #{id}
    </select>

    <select id="selectAllEnabled" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_menu
        WHERE is_enabled = 1
        ORDER BY parent_id ASC, sort_order ASC
    </select>

    <select id="selectByParentId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_menu
        WHERE parent_id = #{parentId}
        ORDER BY sort_order ASC
    </select>

    <select id="selectTopLevelMenus" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_menu
        WHERE parent_id = 0
        ORDER BY sort_order ASC
    </select>

    <select id="selectMenuTree" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_menu
        ORDER BY parent_id ASC, sort_order ASC
    </select>

    <select id="selectByMenuKey" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_menu
        WHERE menu_key = #{menuKey}
        LIMIT 1
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO wechat_menu (
            parent_id, menu_name, menu_type, menu_key, menu_url, media_id, appid, pagepath,
            sort_order, is_enabled, description
        ) VALUES (
            #{parentId}, #{menuName}, #{menuType}, #{menuKey}, #{menuUrl}, #{mediaId}, #{appid}, #{pagepath},
            #{sortOrder}, #{isEnabled}, #{description}
        )
    </insert>

    <update id="updateById">
        UPDATE wechat_menu
        SET menu_name = #{menuName},
            menu_type = #{menuType},
            menu_key = #{menuKey},
            menu_url = #{menuUrl},
            media_id = #{mediaId},
            appid = #{appid},
            pagepath = #{pagepath},
            sort_order = #{sortOrder},
            is_enabled = #{isEnabled},
            description = #{description},
            update_time = CURRENT_TIMESTAMP
        WHERE id = #{id}
    </update>

    <delete id="deleteById">
        DELETE FROM wechat_menu WHERE id = #{id}
    </delete>

    <delete id="deleteByParentId">
        DELETE FROM wechat_menu WHERE parent_id = #{parentId}
    </delete>

    <update id="updateStatusBatch">
        UPDATE wechat_menu
        SET is_enabled = #{isEnabled}, update_time = CURRENT_TIMESTAMP
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getMaxSortOrder" resultType="java.lang.Integer">
        SELECT MAX(sort_order) FROM wechat_menu WHERE parent_id = #{parentId}
    </select>

    <select id="countMenus" resultType="int">
        SELECT COUNT(*) FROM wechat_menu
        <where>
            <if test="parentId != null">
                AND parent_id = #{parentId}
            </if>
            <if test="isEnabled != null">
                AND is_enabled = #{isEnabled}
            </if>
        </where>
    </select>

    <select id="existsByMenuKey" resultType="boolean">
        SELECT COUNT(*) > 0 FROM wechat_menu
        WHERE menu_key = #{menuKey}
        <if test="excludeId != null">
            AND id != #{excludeId}
        </if>
    </select>

    <select id="selectMenuList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM wechat_menu
        <where>
            <if test="parentId != null">
                AND parent_id = #{parentId}
            </if>
            <if test="menuName != null and menuName != ''">
                AND menu_name LIKE CONCAT('%', #{menuName}, '%')
            </if>
            <if test="menuType != null and menuType != ''">
                AND menu_type = #{menuType}
            </if>
            <if test="isEnabled != null">
                AND is_enabled = #{isEnabled}
            </if>
        </where>
        ORDER BY parent_id ASC, sort_order ASC
        LIMIT #{offset}, #{size}
    </select>

    <select id="countMenuList" resultType="int">
        SELECT COUNT(*) FROM wechat_menu
        <where>
            <if test="parentId != null">
                AND parent_id = #{parentId}
            </if>
            <if test="menuName != null and menuName != ''">
                AND menu_name LIKE CONCAT('%', #{menuName}, '%')
            </if>
            <if test="menuType != null and menuType != ''">
                AND menu_type = #{menuType}
            </if>
            <if test="isEnabled != null">
                AND is_enabled = #{isEnabled}
            </if>
        </where>
    </select>

</mapper>
