<?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.usky.system.mapper.HceCategoryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.system.domain.HceCategory">
        <id column="id" property="id" />
        <result column="pid" property="pid" />
        <result column="category_name" property="categoryName" />
        <result column="isNew" property="isNew" />
        <result column="imagePath" property="imagePath" />
        <result column="createdate" property="createdate" />
        <result column="modifydate" property="modifydate" />
        <result column="sortindex" property="sortindex" />
        <result column="status" property="status" />
        <result column="menu_name" property="menuName" />
        <result column="menu_id" property="menuId" />
        <result column="description" property="description" />
        <result column="order_num" property="orderNum" />
    </resultMap>

    <select id="selectMenuTreeAll" resultType="com.usky.system.service.vo.HceCategoryListVO">
        select distinct m.menu_id as menuId,
                        m.menu_name as menuName,
                        rm.menu_alias_name as menuAliasName,
                        m.parent_id as parentId,
                        m.order_num as orderNum,
                        m.is_frame as isFrame,
                        m.is_cache as isCache,
                        h.id as categoryId,
                        h.category_name as categoryName,
                        h.sortindex as sortindex,
                        h.imagePath as imagePath,
                        h.createdate as createdate,
                        h.modifydate as modifydate,
                        h.description as description,
                        h.status as status
        from sys_menu m
            left join sys_role_menu rm on m.menu_id = rm.menu_id
            inner JOIN hce_category h on m.menu_id = h.menu_id
        where m.menu_type in ('M', 'C')
          and m.visible = 0
          and m.status = 0
          <if test="categoryIdList != null and categoryIdList.size() > 0 ">
              and h.id in
              <foreach collection="categoryIdList" item="categoryId" index="index" open="(" close=")" separator=",">
                  #{categoryId}
              </foreach>
          </if>
          <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
              and h.createdate between #{startTime} and #{endTime}
          </if>
        order by m.parent_id, h.sortindex
    </select>
    <select id="selectMenuTreeByUserId" resultType="com.usky.system.service.vo.HceCategoryListVO">
        select distinct m.menu_id as menuId,
                        m.menu_name as menuName,
                        rm.menu_alias_name as menuAliasName,
                        m.parent_id as parentId,
                        m.order_num as orderNum,
                        m.is_frame as isFrame,
                        m.is_cache as isCache,
                        h.id as categoryId,
                        h.category_name as categoryName,
                        h.sortindex as sortindex,
                        h.imagePath as imagePath,
                        h.createdate as createdate,
                        h.modifydate as modifydate,
                        h.description as description,
                        h.status as status
        from sys_menu m
                 left join sys_role_menu rm on m.menu_id = rm.menu_id
                 left join sys_user_role ur on rm.role_id = ur.role_id
                 left join sys_role ro on ur.role_id = ro.role_id
                 left join sys_user u on ur.user_id = u.user_id
                 inner join hce_category h on m.menu_id = h.menu_id
        where u.user_id = #{userId}
          and m.menu_type in ('M', 'C')
          and m.visible = 0
          and m.status = 0
          AND ro.status = 0
        <if test="categoryIdList != null and categoryIdList.size() > 0 ">
            and h.id in
            <foreach collection="categoryIdList" item="categoryId" index="index" open="(" close=")" separator=",">
                #{categoryId}
            </foreach>
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and h.createdate between #{startTime} and #{endTime}
        </if>
        order by m.parent_id, h.sortindex
    </select>
    <select id="selectMenuTreeByUserIdOne" resultType="com.usky.system.service.vo.HceCategoryListVO">
        SELECT
            distinct m.menu_id as menuId,
                    m.menu_name as menuName,
                    t.menu_alias_name as menuAliasName,
                    m.parent_id as parentId,
                    m.order_num as orderNum,
                    m.is_frame as isFrame,
                    m.is_cache as isCache,
                    h.id as categoryId,
                    h.category_name as categoryName,
                    h.sortindex as sortindex,
                    h.imagePath as imagePath,
                    h.createdate as createdate,
                    h.modifydate as modifydate,
                    h.description as description,
                    h.status as status
        FROM
            sys_menu m
                JOIN sys_tenant_menu t on m.menu_id = t.menu_id
                inner join hce_category h on m.menu_id = h.menu_id
        WHERE m.menu_type IN ('M', 'C')
          and m.visible = 0
          and m.status = 0
          AND t.tenant_id=#{tenantId}
        <if test="categoryIdList != null and categoryIdList.size() > 0 ">
            and h.id in
            <foreach collection="categoryIdList" item="categoryId" index="index" open="(" close=")" separator=",">
                #{categoryId}
            </foreach>
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and h.createdate between #{startTime} and #{endTime}
        </if>
        ORDER BY m.parent_id, h.sortindex
    </select>
    <select id="getMenuNameList" resultType="com.usky.system.domain.HceCategory">
        SELECT
            distinct m.menu_id as menuId,
                    m.menu_name as menuName,
                    m.parent_id as parentId,
                    m.order_num as orderNum,
                    m.is_frame as isFrame,
                    m.is_cache as isCache,
                    h.id as id,
                    h.category_name as categoryName,
                    h.imagePath as imagePath,
                    h.createdate as createdate,
                    h.modifydate as modifydate,
                    h.status as status
        FROM
            sys_menu m
                inner join hce_category h on m.menu_id = h.menu_id
        WHERE m.menu_type IN ('M', 'C')
          and m.visible = 0
          and m.status = 0
          and h.id in 
          <foreach collection="categoryIdList" item="categoryId" index="index" open="(" close=")" separator=",">
              #{categoryId}
          </foreach>
    </select>
    <select id="selectPidByMenuid" resultType="integer">
        SELECT
            parent_id
        FROM
            sys_menu
        WHERE menu_id = #{menuId}
    </select>

</mapper>
