<?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.SecKillMapper">
    <select id="querySecSpec" resultType="com.gree.mall.manager.bean.activity.CustomSecSpecBean">
        select sas.sec_kill_spec_id,sas.goods_name,sas.img_url,sas.amount,sas.price,sa.start_time,sa.end_time,
            CASE when unix_timestamp(sa.start_time) > unix_timestamp(now()) then 1
            when now() between sa.start_time and sa.end_time then 2
            when unix_timestamp(sa.end_time) &lt; unix_timestamp(now()) then 3
            when sas.status = 0 then 4
            else 0
            end 'status'
        from sec_kill_activity_spec sas
         join sec_kill_activity sa on sas.sec_kill_id = sa.sec_kill_id
        where sa.del=0
        <if test="status!=null">

            <choose>

                <when test="status==1">
                    and unix_timestamp(sa.start_time) > unix_timestamp(now())
                </when>
                <when test="status==2">
                    and now() between sa.start_time and sa.end_time
                </when>
                <when test="status==3">
                    and unix_timestamp(sa.end_time) &lt; unix_timestamp(now())
                </when>
                <when test="status==4">
                    and sas.status = 0
                </when>
            </choose>
        </if>
        <if test="keyword!=null">
            and sas.goods_name like concat('%',#{keyword,jdbcType=VARCHAR},'%')
        </if>
        <if test="companyWechatIds != null and companyWechatIds.size > 0">
            and sa.company_id in
            <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>

        order by sas.create_time DESC
    </select>

    <select id="querySecDetail" resultType="com.gree.mall.manager.bean.activity.SecKillActivitySpecBean">
        select skas.*, gs.stock_num as stockNum
        from sec_kill_activity_spec skas
                 left join goods_spec gs on skas.goods_spec_id = gs.goods_spec_id
        where skas.sec_kill_id = #{secKillId}
    </select>

    <select id="queryActivity" resultMap="activityResult">
        select s.*, sks.sec_kill_stage_id,sks.sec_kill_id as stage_kill_id ,sks.name,sks.start_hour,sks.end_hour from
            (select ska.*,
                count(skas.sec_kill_spec_id) as specCount,
                CASE when unix_timestamp(ska.start_time) > unix_timestamp(now()) then 1
                when now() between ska.start_time and ska.end_time then 2
                when unix_timestamp(ska.end_time) &lt; unix_timestamp(now()) then 3
                else 0
                end 'activityStatus'
                from sec_kill_activity ska
            left join sec_kill_activity_spec skas on ska.sec_kill_id = skas.sec_kill_id
            where ska.del=0
            <if test="status!=null">

                <choose>

                    <when test="status==1">
                        and unix_timestamp(ska.start_time) > unix_timestamp(now())
                    </when>
                    <when test="status==2">
                        and now() between ska.start_time and ska.end_time
                    </when>
                    <when test="status==3">
                        and unix_timestamp(ska.end_time) &lt; unix_timestamp(now())
                    </when>

                </choose>
            </if>
            <if test="keyword!=null">
                and ska.sec_kill_name like concat('%',#{keyword},'%')
            </if>
            <if test="startTime!=null">
                and unix_timestamp(ska.start_time) > unix_timestamp(#{startTime})
            </if>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                and ska.company_id in
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            group by ska.sec_kill_id
        ) s left join sec_kill_stage sks on sks.sec_kill_id = s.sec_kill_id
        order by s.create_time DESC
    </select>

    <select id="countActivity" resultType="java.lang.Integer">
        select count(*)
        from sec_kill_activity ska
        where ska.del = 0
        <if test="status!=null">

            <choose>

                <when test="status==1">
                    and unix_timestamp(ska.start_time) > unix_timestamp(now())
                </when>
                <when test="status==2">
                    and now() between ska.start_time and ska.end_time
                </when>
                <when test="status==3">
                    and unix_timestamp(ska.end_time) &lt; unix_timestamp(now())
                </when>

            </choose>
        </if>
        <if test="keyword!=null">
            and ska.sec_kill_name like concat('%',#{keyword,jdbcType=VARCHAR},'%')
        </if>
        <if test="startTime!=null">
            and unix_timestamp(ska.start_time) > unix_timestamp(#{startTime})
        </if>
    </select>

    <resultMap id="activityResult" type="com.gree.mall.manager.bean.activity.SecKillActivityStageBean">
        <id property="secKillId" column="sec_kill_id"/>
        <result property="secKillName" column="sec_kill_name"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="status" column="status"/>
        <result property="specCount" column="specCount"/>
        <result property="activityStatus" column="activityStatus"/>
        <result property="del" column="del"/>
        <result property="createTime" column="create_time"/>
        <collection property="stages" ofType="com.gree.mall.manager.plus.entity.SecKillStage">
            <id property="secKillStageId" column="sec_kill_stage_id"/>
            <result property="secKillId" column="stage_kill_id"/>
            <result property="name" column="name"/>
            <result property="startHour" column="start_hour"/>
            <result property="endHour" column="end_hour"/>
        </collection>
    </resultMap>



    <select id="querySpecList" resultType="com.gree.mall.manager.bean.activity.CustomSpecBean">
        select gs.goods_id,gs.goods_spec_id,gds.goods_name, gs.price as goodsPrice,gs.stock_num,gs.name,gs.spec_value,gs.img_url,gs.share_amount as amount,gs.share_percent,
               gs.org_price,gs.sold_num as salesVolume
               from goods_spec gs left join goods gds on gs.goods_id=gds.goods_id
        where gs.del=0 and  gds.del = 0 and gds.status=1 and gds.promotion_group=0
        <if test="keyword != null and keyword !=''">
            and gds.goods_name like concat('%',#{keyword,jdbcType=VARCHAR},'%')
        </if>
        <if test="categoryId != null and categoryId !=''">
            and gds.category_id = #{categoryId}
        </if>
        <if test="companyWechatIds != null and companyWechatIds.size > 0">
            and gds.company_id in
            <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by gds.goods_id
        having count(*) =1
    </select>


    <select id="querySpecListforCoupon" resultType="com.gree.mall.manager.bean.activity.CustomSpecBean">
        select gs.goods_id,gs.goods_spec_id,gds.goods_name, gs.price as goodsPrice,gs.stock_num,gs.name,gs.spec_value,gs.img_url,gs.share_amount as amount
        from goods_spec gs left join goods gds on gs.goods_id=gds.goods_id
        where gs.del=0 and  gds.del = 0 and gds.status=1
        <if test="keyword != null and keyword !=''">
            and gds.goods_name like concat('%',#{keyword,jdbcType=VARCHAR},'%')
        </if>
        <if test="categoryId != null and categoryId !=''">
            and gds.category_id = #{categoryId}
        </if>
        <if test="companyWechatIds != null and companyWechatIds.size > 0">
            and gs.company_id in
            <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by gds.goods_id
        order by gds.create_time
    </select>




</mapper>