<?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.shopping.mapper.CategoryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="categoryResultMap" type="org.springblade.modules.shopping.entity.Category">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="value" property="value"/>
        <result column="name" property="name"/>
        <result column="seo_title" property="seoTitle"/>
        <result column="seo_keyword" property="seoKeyword"/>
        <result column="seo_desc" property="seoDesc"/>
        <result column="sort" property="sort"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <resultMap id="categoryVOResultMap" type="org.springblade.modules.shopping.vo.CategoryVO">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="value" property="value"/>
        <result column="name" property="name"/>
        <result column="sort" property="sort"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="has_children" property="hasChildren"/>
    </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>


    <select id="selectCategoryPage" resultMap="categoryResultMap">
        select * from t_category where is_deleted = 0
    </select>

    <select id="lazyList" resultMap="categoryVOResultMap">
        SELECT
        c.*,
        (
        SELECT
        CASE WHEN count( 1 ) > 0 THEN 1 ELSE 0 END
        FROM
        t_category
        WHERE
        parent_id = c.id AND is_deleted = 0
        ) AS "has_children"
        FROM
        t_category c
        WHERE c.is_deleted = 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, name as title, id as "value", id as "key" from t_category where is_deleted = 0
    </select>

</mapper>
