<?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,
            main_name AS mainName,
            main_number AS mainNumber,
            tax_number,
            tax_percent,
            goods_number,
            is_service_show,
            update_by,
            update_time,
            company_wechat_id,
            company_name,
            create_by,
            create_time AS createTime
        FROM goods_category
        <where>
            del = 0
            <if test='level != null'>
                AND level = #{level}
            </if>
            <if test="type != null">
                and type = #{type}
            </if>
            <if test='status != null'>
                AND status = #{status}
            </if>
            <if test='name != null'>
                AND name like concat('%',#{name,jdbcType=VARCHAR},'%')
            </if>
            <if test=' companyWechatIds != null and companyWechatIds.size > 0' >
                AND company_wechat_id in
                <foreach collection="companyWechatIds" open="(" close=")" item="companyWechatId" separator=",">
                     #{companyWechatId}
                </foreach>
            </if>
        </where>
        order by sort_num asc

    </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 asc
    </select>

</mapper>