<?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.miniapp.commonmapper.CategoryMapper">
    <select id="queryCategory" resultMap="category">
        select
            gcl.*,
            gcr.parent_id as sub_parent_id,
            gcr.category_id as sub_category_id,
            gcr.name as sub_name,
            gcr.img_url as sub_img_url,
            gcr.goods_num as sub_goods_num,
            gcr.sort_num as sub_sort_num,
            gcr.level as sub_level
        from goods_category gcl
        left join
             goods_category gcr
        on gcl.category_id=gcr.parent_id
        where gcl.del=0 and gcl.status = 1 and gcl.parent_id is NULL and gcr.del=0 and gcr.status = 1 and gcr.company_wechat_id=#{companyWechatId}
        <if test="isServiceShow != null">
            and gcl.is_service_show = #{isServiceShow}
        </if>
        order by gcl.sort_num ASC, gcr.sort_num ASC
    </select>
    <resultMap id="category" type="com.gree.mall.miniapp.bean.goods.CategoryBean">
        <id property="categoryId" column="category_id"/>
        <result property="name" column="name"/>
        <result property="imgUrl" column="img_url"/>
        <result property="goodsNum" column="goods_num"/>
        <result property="parentId" column="parent_id"/>
        <result property="sortNum" column="sort_num"/>
        <result property="level" column="level"/>
        <collection property="sub" javaType="ArrayList" column="sub_parent_id" resultMap="category"  columnPrefix="sub_">

        </collection>
    </resultMap>

    <select id="listGoods" resultType="com.gree.mall.miniapp.bean.goods.GoodsNewBean">
        SELECT
            a.goods_id,
            a.goods_name,
            a.describe_text,
            a.img_url,
            a.vedio,
            a.category_id,
            if(!ISNULL(skay.goods_price),skay.goods_price,a.org_goods_price) as org_goods_price,
            if(!ISNULL(skay.price),skay.price,a.goods_price) as goods_price,
            a.sold_num,
            a.has_spec,
            a.show_detail_stock,
            a.show_detail_sold,
            a.freight_type,
            a.freight_template_id,
            a.freight,
            a.content,
            a.sort_num,
            a.STATUS,
            a.promotion_group,
            a.marketing_id,
            a.marketing_name,
            a.del,
            a.create_time,
            a.company_wechat_id,
            a.company_name,
            a.logo,
            a.logo_start_time,
            a.logo_end_time
        FROM
        goods a
        LEFT JOIN (  SELECT skas.*
        from sec_kill_activity_spec skas
        left JOIN sec_kill_activity ska on ska.sec_kill_id=skas.sec_kill_id
        WHERE
        1=1
        and skas.status=TRUE

        and ska.status=TRUE
        and ska.del=FALSE
        AND NOW() BETWEEN ska.start_time and ska.end_time
        Limit 1) skay on skay.goods_id = a.goods_id and skay.company_wechat_id = a.company_wechat_id
        where a.company_wechat_id = #{currentCompanyWechatId} and a.del=0 and a.status=1 and promotion_group =0
        <if test="categoryId != null and categoryId != ''">
            and category_id=#{categoryId}
        </if>
        <if test="sort != null and sort==0">
            order by a.sort_num asc
        </if>
        <if test="sort != null and sort==1">
            order by a.sold_num desc
        </if>
        <if test="sort != null and sort==2">
            order by goods_price asc
        </if>
        <if test="sort != null and sort==3">
            order by goods_price desc
        </if>
        <if test="sort != null and sort==4">
            order by a.create_time desc
        </if>
    </select>


    <select id="queryGoodsCategoryByGoodsId" resultType="com.gree.mall.miniapp.plus.entity.GoodsCategory">
        select a.* from goods_category a
        join goods b on a.category_id = b.category_id
        where b.goods_id=#{goodsId}
    </select>

    <select id="brandList" resultType="com.gree.mall.miniapp.plus.entity.Brand">
        select
            DISTINCT
            a.*
        from brand a
        join goods b on a.id = b.brand_id and b.status=1
        join goods_category c on c.category_id = b.category_id
        where 1 =1
        <if test="categoryId != null and categoryId != ''">
          and c.parent_id=#{categoryId}
        </if>
        and a.company_wechat_id = #{companyWechatId}
        order by a.sort_num
    </select>

</mapper>