<?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.GoodsSpecDetailMapper">
    <select id="querySpecSec" resultType="com.gree.mall.miniapp.bean.goods.GoodsSpecSecBean">
        select
          gs.*,
		  sec.*,
		  a.org_goods_price,
          if(sec.sec_state,1,0) as sec_type
        from goods a join goods_spec gs on a.goods_id = gs.goods_id
                 left join
             (select skas.sec_kill_id,
                     skas.sec_kill_spec_id,
                     skas.price as sec_price,
                     skas.sec_stock_num,
                     skas.limit_buy,
                     skas.amount as sec_share_amount,
                     sks.end_hour,
                     skas.goods_spec_id,
                     1          as sec_state
              from sec_kill_activity_spec skas
                       LEFT JOIN sec_kill_activity ska on ska.sec_kill_id = skas.sec_kill_id
                       LEFT JOIN sec_kill_stage sks on ska.sec_kill_id = sks.sec_kill_id
              where ska.status = 1
                and ska.del = 0
                and (now() between ska.start_time and ska.end_time)
                and CASE
        WHEN HOUR (now())>=10 and HOUR(now())&lt; 20
        THEN sks.end_hour > HOUR(now()) AND sks.start_hour &lt;= HOUR(now())
        ELSE sks.end_hour = 10
        END
                and skas.status = 1) sec
             on sec.goods_spec_id = gs.goods_spec_id
        where gs.goods_id = #{goodsId} and gs.del = 0
        GROUP BY gs.goods_spec_id
        order BY sec.sec_state desc
    </select>


    <select id="queryCommonTemplate" resultType="com.gree.mall.miniapp.plus.entity.CommonTemplate">
        SELECT
            ct.*
        FROM
            goods_template gt
                RIGHT JOIN common_template ct ON ct.common_template_id = gt.template_id and ct.type=1
        WHERE
            gt.goods_id = #{goodsId}
        order by ct.create_time desc
        limit 1
    </select>


    <select id="queryPromotionGoods" resultType="com.gree.mall.miniapp.bean.goods.PromotionGoodsBean">
        select
            a.promotion_group_id,
            a.goods_id,
            a.goods_name,
            a.goods_img_src,
            a.group_price group_price,
            b.sales_num sale_num,
            b.stock stock,
            b.org_goods_price org_goods_price
        from
        promotion_group_spec_user a
        join  promotion_group_spec b on a.promotion_group_spec_id = b.promotion_group_spec_id
        join promotion_group c on c.promotion_group_id = b.promotion_group_id
        join goods d on d.goods_id = a.goods_id
        where a.user_id=#{userId} and c.status=1 and d.del=0 and now() between a.start_time and a.end_time
        and c.company_wechat_id=#{companyWechatId}
        <if test="keyword != null and keyword !='' ">
            and a.goods_name like concat('%',#{keyword},'%')
        </if>
        <if test="goodsCategoryId != null and goodsCategoryId !=''">
            and a.goods_id in (
                select b.goods_id from promotion_group_spec_user a
                join goods b on a.goods_id = b.goods_id
                join goods_category c on c.category_id=b.category_id
                where c.parent_id = #{goodsCategoryId}
            )
        </if>
        group by a.goods_id
        order by b.sort_num,sale_num desc
    </select>

    <select id="goodsCommentList" resultType="com.gree.mall.miniapp.bean.goods.GoodsComment">
        select b.user_name,d.avatar,a.* from order_comment a
            join order_info b on a.order_id=b.order_id
            join order_detail c on c.order_id=b.order_id
            join user d on d.user_id = b.user_id
            where  a.is_show=1 and  a.company_wechat_id=#{companyWechatId} and c.goods_id=#{goodsId}
            <if test="tag != null and tag!=''">
            and a.order_id in (
				  select order_id from order_comment_tag where tag=#{tag} and company_wechat_id=#{companyWechatId}
			)
            </if>
        order by a.create_time desc
    </select>

    <select id="goodsCommentCount" resultType="com.gree.mall.miniapp.bean.goods.CommentTagCount">
        select d.tag,count(1) total from order_comment a
            join order_info b on a.order_id=b.order_id
            join order_detail c on c.order_id=b.order_id
						join order_comment_tag d on d.order_id=a.order_id
            where c.goods_id=#{goodsId} and a.is_show=1 and a.company_wechat_id=#{companyWechatId}
						group by d.tag
    </select>

    <select id="queryGoodsPackage" resultType="com.gree.mall.miniapp.bean.goods.GoodsPackageBean">
        select
            c.*,
            a.goods_name,
            b.img_url,
            b.spec_value,
            b.org_price
        from goods a
        join goods_spec b on a.goods_id=b.goods_id
        join goods_package_pop c on c.goods_spec_id=b.goods_spec_id
        left join goods_package_user_rela d on d.goods_id = a.goods_id
        where a.del=0 and a.status=true and b.del=0 and a.company_wechat_id= #{companyWechatId} and c.goods_package_id=#{goodsId}
				and (
						(a.package_user_type=0 and d.user_id=#{userId})
				        or a.package_user_type=1
						or (a.package_user_type=2 and #{userType}!='SERVICE')
						or (a.package_user_type=3 and #{userType}='SERVICE')
				)
    </select>

    <select id="queryGoodsList" resultType="com.gree.mall.miniapp.bean.goods.GoodsNewBean">
        select
            a.*
        from goods a
        where a.del=0 and a.status=true and a.promotion_group = 0
                and a.company_wechat_id= #{companyWechatId}
				and (
						(a.package_user_type=0 and exists (select 1 from goods_package_user_rela where user_id=#{userId}) )
				    or a.package_user_type=1
						or (a.package_user_type=2 and #{userType}!='SERVICE')
						or (a.package_user_type=3 and #{userType}='SERVICE')
				)
                <if test="yjhx != null">
                   and a.is_trade = if(#{yjhx} = true,'YES','NO')
                </if>
                <if test="minPrice != null and maxPrice != null">
                    and a.goods_price between #{minPrice} and #{maxPrice}
                </if>
                <if test="tags != null and tags.size > 0">
                    and a.goods_id in (
                        select goods_id from goods_tag_rela where goods_tag_name in
                    <foreach item="item" index="index" collection="tags" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                    )
                </if>
                <if test="brandIds != null and brandIds.size > 0">
                    and a.brand_id in
                    <foreach item="item" index="index" collection="brandIds" open="(" separator="," close=")">
                        #{item}
                    </foreach>

                </if>
				<if test="keyword != null and keyword != ''">
                    and a.goods_name like concat('%',#{keyword},'%')
                </if>
                <if test="categoryId != null and categoryId !=''">
                    and a.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>

</mapper>