<?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.luckdraw.LuckDrawMapper">


    <select id="queryList" resultType="com.gree.mall.manager.bean.activity.luckdraw.PromotionLuckDrawList">
        select
            a.id,
            company_wechat_name,
            name,
            sum(b.total_num) as 'total',
            ifnull((select sum(if(status=1,1,0)) from promotion_luck_draw_coupon_code where promotion_luck_draw_id=a.id),0) exchangeTotal,
            ifnull((select sum(if(status=0,1,0)) from promotion_luck_draw_coupon_code where promotion_luck_draw_id=a.id),0) surplusExchange,
            ifnull((select sum(if(status=1,1,0)) from user_coupon where  coupon_id=b.id),0) useTotal,
            ifnull((select sum(if(status=0,1,0)) from user_coupon where  coupon_id=b.id),0) notUse,
            a.status,
            a.create_time

        from promotion_luck_draw a
        join promotion_luck_draw_coupon b on a.id = b.promotion_luck_draw_id
        <where>
            <if test="name != null and name != ''">
                and a.name like concat('%',#{name},'%')
            </if>
            <if test="status != null">
                and a.status = #{status}
            </if>
            <if test="companyIds != null and companyIds.size > 0">
                and a.company_id in
                <foreach item="item" index="index" collection="companyIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        group by a.id
        order by a.create_time desc
    </select>



    <select id="queryLuckDrawCodeList" resultType="com.gree.mall.manager.bean.activity.luckdraw.PromotionLuckDrawCodeBean">
          select
            a.*,
            b.coupon_name,
            b.coupon_type,
            b.discount_amount,
            c.code,
            if(c.status=0,0,if(c.status=1 and c.order_id ='',1,2)) as 'status2',
            c.user_nick_name,
            c.user_mobile,
            c.user_exchange_time,
            c.user_use_time,
            d.order_id,
            d.user_name,
            d.order_status

        from promotion_luck_draw a
        join promotion_luck_draw_coupon b on a.id = b.promotion_luck_draw_id
        join promotion_luck_draw_coupon_code c on b.id = c.promotion_luck_draw_coupon_id
        left join order_info d on c.order_id = d.order_id
        <where>
            <if test="promotionLuckDrawId != null and promotionLuckDrawId !=''">
                and a.id = #{promotionLuckDrawId}
            </if>
            <if test="promotinLuckDrawCouponId != null and promotinLuckDrawCouponId !=''">
                and b.id = #{promotinLuckDrawCouponId}
            </if>
            <if test="code != null and code != ''">
                and c.code = #{code}
            </if>
            <if test="name != null and name !=''">
                and c.user_nick_name like concat('%',#{name},'%')
            </if>
            <if test="mobile != null and mobile !=''">
                and c.user_mobile = #{mobile}
            </if>
            <if test="exchangeStartTime != null and exchangeStartTime !='' and exchangeEndTime != null and exchangeEndTime !=''">
                and c.user_exchange_time between #{exchangeStartTime} and #{exchangeEndTime}
            </if>
            <if test="useStartTime != null and useStartTime !='' and useEndTime != null and useEndTime !=''">
                and c.user_use_time between #{useStartTime} and #{useEndTime}
            </if>
            <if test="orderId != null and orderId !=''">
                and c.order_id like concat('%',#{orderId},'%')
            </if>
            <if test="couponName != null and couponName !=''">
                and b.coupon_name like concat('%',#{couponName},'%')
            </if>
            <if test="status2 != null">
                and if(c.status=0,0,if(c.status=1 and c.order_id ='',1,2)) = #{status2}
            </if>
            <if test="companyIds != null and companyIds.size > 0">
                and a.company_id in
                <foreach item="item" index="index" collection="companyIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>


</mapper>