<?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.AppMapper">

    <select id="queryShoppingCartList" resultType="com.gree.mall.miniapp.bean.order.ShoppingCartList">
        select a.*,
               b.price,
               c.goods_name,
               b.spec_value,
               b.org_price,
               b.price * num as 'totalPrice', c.img_url 'goodsImg',
               c.promotion_full_piece

        from shopping_cart a
                 join goods_spec b on a.goods_spec_id = b.goods_spec_id
                 join goods c on c.goods_id = a.goods_id
        where a.user_id = #{userId}
    </select>


    <select id="orderStatusCount" resultType="com.gree.mall.miniapp.bean.order.OrderStatusBean">

        select sum(if(order_status = 'NOPAY', 1, 0))  noPay,
               sum(if(order_status in ('YFH','DFH'), 1, 0))    yfh,
               sum(if(order_status = 'DFH', 1, 0))    dfh,
               sum(if(order_status in ('REFUND'), 1, 0)) sh,
               sum(if(order_status = 'OVER' and comment_goods=0, 1, 0))   ywc
        from order_info
        where user_id = #{userId}
    </select>


    <select id="queryTimeList" resultType="com.gree.mall.miniapp.bean.seckill.SecKillExtends">
        <![CDATA[
        select
            ska.*, sks.start_hour, sks.end_Hour,'jxz' as 'type', '2' as 'seq',
            concat(date(now()),' ',sks.start_hour,':00:00')  'start_datetime',
            if(sks.end_hour < sks.start_hour,DATE_ADD(concat(date(now()),' ',sks.end_hour,':00:00'),INTERVAL 1 DAY)
              ,concat(date(now()),' ',sks.end_hour,':00:00')) 'end_datetime'
        from sec_kill_activity ska
                         inner join sec_kill_stage sks on ska.sec_kill_id = sks.sec_kill_id
                         left join sec_kill_activity_spec skas on skas.sec_kill_id = ska.sec_kill_id
                         left join goods gs on gs.goods_id = skas.goods_id
        where ska.start_time < now()
            and ska.company_wechat_id = #{companyWechatId}
            and ska.end_time > now()
            and ska.status = 1
            and ska.del = 0
            and skas.status = 1
            and gs.del = 0
            and gs.status = 1
            and now() between concat(date(now()),' ',sks.start_hour,':00:00')
                and if(sks.end_hour < sks.start_hour,DATE_ADD(concat(date(now()),' ',sks.end_hour,':00:00'),INTERVAL 1 DAY)
                  ,concat(date(now()),' ',sks.end_hour,':00:00'))
            GROUP BY ska.sec_kill_id, sks.sec_kill_stage_id

        union

            (select
                    ska.*,
                    sks.start_hour,
                    sks.end_Hour,
                    'wks' as 'type',
                    '3' as 'seq',
                    concat(date(now()),' ',sks.start_hour,':00:00')  'start_datetime',
                    if(sks.end_hour < sks.start_hour,DATE_ADD(concat(date(now()),' ',sks.end_hour,':00:00'),INTERVAL 1 DAY)
                      ,concat(date(now()),' ',sks.end_hour,':00:00')) 'end_datetime'
                from sec_kill_activity ska
                                 inner join sec_kill_stage sks on ska.sec_kill_id = sks.sec_kill_id
                                 left join sec_kill_activity_spec skas on skas.sec_kill_id = ska.sec_kill_id
                                 left join goods gs on gs.goods_id = skas.goods_id
                where ((ska.start_time < now()
                        and ska.end_time > now()
                        and sks.start_hour > HOUR (now()) ) or
                             ska.start_time > now())
                    and ska.status = 1
                    and ska.del = 0
                    and skas.status = 1
                    and gs.del = 0
                    and gs.status = 1
                    and ska.company_wechat_id = #{companyWechatId}
                GROUP BY ska.sec_kill_id, sks.sec_kill_stage_id
                order by ska.start_time , sks.start_hour  limit 3)

        ]]>
    </select>

    <!--
    int hour = date.get(Calendar.HOUR_OF_DAY)
    HOUR (now())
    if(10=<hour<20){
            sks.end_hour>hour and sks.start_hour<=hour
    }else{
        sks.end_hour=10 and sks.start_hour=20
    }

    <if test="hour gte 10 and hour lt 20">
                and sks.end_hour>hour and sks.start_hour<=hour
            </if>
    <if test="hour gte 20 or hour lt 10">
                and sks.end_hour=10 and sks.start_hour=20
            </if>
    -->

    <select id="querySecKillList" resultType="com.gree.mall.miniapp.bean.seckill.SecKillList">
        select a.limit_buy,
               a.content,
               a.img_url,
               b.*
        from sec_kill_activity a
                 join sec_kill_activity_spec b on a.sec_kill_id = b.sec_kill_id
        where a.status = 1
          and a.del = 0
          and start_time between #{time} and concat(#{time}, ' 23:59:59')
        order by a.sort_num desc, a.start_time desc
    </select>


    <select id="queryNewGoods" resultType="com.gree.mall.miniapp.bean.goods.GoodsNewBean">
        select
            a.*
        <if test="objId != null and objId != ''">
            ,b.obj_id
        </if>
        from goods a
        <if test="objId != null and objId != ''">
            join goods_news_category_goods b on a.goods_id = b.goods_id
        </if>
        where
            a.company_wechat_id = #{companyWechatId} and a.del=0 and a.status=1 and a.promotion_group=false
        <if test="objId != null and objId != ''">
            and b.obj_id = #{objId}
        </if>
        <if test="type != null and type != ''">
            and b.type = #{type}
        </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>
