<?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.gree.mall.manager.commonmapper.CustomGoodsCategoryMapper">

    <resultMap id="listResultMap" type="com.gree.mall.manager.bean.goods.GoodsCategoryBean">
        <id column="categoryId" property="categoryId"/>
        <collection property="children" column="{categoryId=categoryId}"
                    select="com.gree.mall.manager.commonmapper.CustomGoodsCategoryMapper.findChildrenByCategoryId">
        </collection>
    </resultMap>

    <select id="list" resultMap="listResultMap">
        SELECT
        category_id AS categoryId,
        name AS name,
        status AS status,
        img_url AS imgUrl,
        goods_num AS goodsNum,
        level AS level,
        parent_id AS parentId,
        sort_num AS sortNum,
        create_time AS createTime
        FROM goods_category
        <where>
            del = 0
            <if test='level != null'>
                AND level = #{level}
            </if>
            <if test='status != null'>
                AND status = #{status}
            </if>
            <if test='name != null'>
                AND name like concat('%',#{name,jdbcType=VARCHAR},'%')
            </if>
        </where>
        order by sort_num desc

    </select>

    <select id="findChildrenByCategoryId" resultType="com.gree.mall.manager.bean.goods.GoodsCategoryBean">
        SELECT * FROM goods_category
        WHERE parent_id = #{categoryId} AND del = 0
        order by sort_num desc
    </select>

</mapper>