<?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="org.springblade.modules.mall.mapper.CategoryInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="categoryInfoResultMap" type="org.springblade.modules.mall.entity.CategoryInfo">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="level" property="level"/>
        <result column="state" property="state"/>
        <result column="sort" property="sort"/>
        <result column="parent_id" property="parentId"/>
        <result column="is_delete" property="isDelete"/>
        <result column="create_user_id" property="createUserId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user_id" property="updateUserId"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <resultMap id="treeNodeResultMap" type="org.springblade.core.tool.node.TreeNode">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="title" property="title"/>
        <result column="value" property="value"/>
        <result column="key" property="key"/>
    </resultMap>

    <resultMap id="categoryVOResultMap" type="org.springblade.modules.mall.vo.CategoryInfoVO">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="title" property="title"/>
        <result column="sort" property="sort"/>
        <result column="state" property="state"/>
        <result column="is_delete" property="isDelete"/>
        <result column="has_children" property="hasChildren"/>
    </resultMap>

    <select id="selectCategoryInfoPage" resultMap="categoryInfoResultMap">
        select * from re_category_info where is_delete = 0
    </select>

    <select id="lazyList" resultMap="categoryVOResultMap">
        SELECT
        c.*,
        (
        SELECT
        CASE WHEN count( 1 ) > 0 THEN 1 ELSE 0 END
        FROM
        re_category_info
        WHERE
        parent_id = c.id AND is_delete = 0
        ) AS "has_children"
        FROM
        re_category_info c
        WHERE c.is_delete = 0
        <if test="param1!=null">
            and c.parent_id = #{param1}
        </if>
        ORDER BY c.sort
    </select>

    <select id="tree" resultMap="treeNodeResultMap">
        select id, parent_id, title , id as "value", id as "key" from re_category_info where is_delete = 0
    </select>
</mapper>
