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

    <resultMap id="BaseResultMap" type="com.gree.mall.manager.bean.coupon.CouponDataBean">
        <result column="totalSaleValue" property="totalSaleValue"/>
        <result column="totalDiscountValue" property="totalDiscountValue"/>
        <result column="buyGoodsCount" property="buyGoodsCount"/>
        <collection property="buyGoodsInfoList" javaType="java.util.List"
                    ofType="com.gree.mall.manager.bean.coupon.BuyGoodsInfo">
            <result column="goodsName" property="goodsName"/>
            <result column="goodsCount" property="goodsCount"/>
            <result column="memberCount" property="memberCount"/>
        </collection>
    </resultMap>

    <select id="data" resultMap="BaseResultMap">
        SELECT t1.*,
               t2.*
        FROM (
                 SELECT SUM(t1.total_amount) AS totalSaleValue,
                        SUM(t1.coupon_value) AS totalDiscountValue,
                        SUM(t2.num)          AS buyGoodsCount
                 FROM order_info t1
                          LEFT JOIN order_detail t2 ON t1.order_id = t2.order_id
                 WHERE t1.coupon_id = #{couponId}
                   AND t1.order_status NOT IN ('NOPAY', 'CLOSE')
             ) t1,
             (
                 SELECT t2.goods_name           AS goodsName,
                        SUM(t2.num)             AS goodsCount,
                        COUNT(DISTINCT user_id) AS memberCount
                 FROM order_info t1
                          LEFT JOIN order_detail t2 ON t1.order_id = t2.order_id
                 WHERE t1.coupon_id = #{couponId}
                   AND t1.order_status NOT IN ('NOPAY', 'CLOSE')
                 GROUP BY t2.goods_name
             ) t2
    </select>

    <select id="pageCoupon" resultType="com.gree.mall.manager.bean.coupon.CouponPageBean">
        select cp.*,count(if(uc.user_id is null,null,1)) as receivedCount, count(if(uc.status=1,1,NULL)) as usedCount,
        CASE when cp.flag &lt;> 'CANCEL' and cp.display_time > now() then 'WAIT'
        when cp.flag = 'CANCEL' then 'CANCEL'
        when cp.flag &lt;> 'CANCEL' and cp.obtain_end_time &lt; now() then 'END'
        when cp.flag &lt;> 'CANCEL' and cp.display_time &lt; now() and cp.obtain_end_time > now() then 'START'
        end 'releaseFlag'
        from coupon cp left join user_coupon uc on cp.coupon_id= uc.coupon_id
        <where>
            <if test="flag != null and flag != ''">
                <choose>
                    <when test='flag == "WAIT"'>
                        cp.flag &lt;> 'CANCEL' and cp.display_time > now()
                    </when>
                    <when test='flag == "CANCEL"'>
                        cp.flag = 'CANCEL'
                    </when>
                    <when test='flag == "END"'>
                        cp.flag &lt;> 'CANCEL' and cp.obtain_end_time &lt; now()
                    </when>
                    <when test='flag == "START"'>
                        cp.flag &lt;> 'CANCEL' and cp.display_time &lt; now() and cp.obtain_end_time > now()
                    </when>

                </choose>
            </if>
            <if test="couponType != null and couponType !=''">
                cp.coupon_type = #{couponType}
            </if>
            <if test="couponName != null and couponName !=''">
                cp.coupon_name like concat('%',#{couponName,jdbcType=VARCHAR},'%')
            </if>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                and cp.company_wechat_id in
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY cp.coupon_id ORDER BY cp.create_time DESC
    </select>


    <select id="exportCoupon" resultType="com.gree.mall.manager.bean.coupon.CouponExportBean">
        select
            cp.coupon_id,
            cp.coupon_amount,
            count(uc.id) receive_amount,
            count(uc.used_time) used_amount,
            cp.coupon_value,
            cp.coupon_name,
            uc.receive_time,
            uc.status,
            uc.used_time,
            ur.nick_name,
            ur.mobile
            from user_coupon uc
            left join coupon cp on cp.coupon_id= uc.coupon_id
            left join user ur on ur.user_id = uc.user_id
        <where>
            uc.self_use &lt;> 1 and uc.transfer_type=0
            <if test="flag != null and flag != ''">
                <choose>
                    <when test='flag == "WAIT"'>
                        and cp.flag &lt;> 'CANCEL' and cp.display_time > now()
                    </when>
                    <when test='flag == "CANCEL"'>
                        and cp.flag = 'CANCEL'
                    </when>
                    <when test='flag == "END"'>
                        and cp.flag &lt;> 'CANCEL' and cp.obtain_end_time &lt; now()
                    </when>
                    <when test='flag == "START"'>
                        and cp.flag &lt;> 'CANCEL' and cp.display_time &lt; now() and cp.obtain_end_time > now()
                    </when>

                </choose>
            </if>
            <if test="couponType != null and couponType !=''">
                and cp.coupon_type = #{couponType}
            </if>
            <if test="couponName != null and couponName !=''">
                and cp.coupon_name like concat('%',#{couponName,jdbcType=VARCHAR},'%')
            </if>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                and cp.company_wechat_id in
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        group by cp.coupon_id
        ORDER BY cp.create_time DESC
    </select>
    <select id="exportSelfCoupon" resultType="com.gree.mall.manager.bean.coupon.CouponExportBean">
        select uc.receive_time,uc.coupon_name, uc.status,uc.used_time,oi.order_id,oi.pay_amount,
        ur.nick_name,ur.mobile,ur.work_name,ur.work_phone,aw.`name` as websitName
        from user_coupon uc left join
        order_info oi on oi.user_coupon_id=uc.id and oi.order_status&lt;>'CLOSE'
        left join coupon cp on cp.coupon_id= uc.coupon_id
        left join user ur on ur.user_id = uc.user_id
        left join admin_company_wechat acw on acw.company_wechat_id = ur.company_wechat_id
        left join enterprise_wechat_worker eww on eww.wechat_user_id = ur.work_user_id and eww.corp_id = acw.corp_id
        LEFT JOIN admin_websit aw on eww.main_department = aw.id
        <where>
            uc.self_use = 1
            <if test="flag != null and flag != ''">
                <choose>
                    <when test='flag == "WAIT"'>
                        and cp.flag &lt;> 'CANCEL' and cp.display_time > now()
                    </when>
                    <when test='flag == "CANCEL"'>
                        and cp.flag = 'CANCEL'
                    </when>
                    <when test='flag == "END"'>
                        and cp.flag &lt;> 'CANCEL' and cp.obtain_end_time &lt; now()
                    </when>
                    <when test='flag == "START"'>
                        and cp.flag &lt;> 'CANCEL' and cp.display_time &lt; now() and cp.obtain_end_time > now()
                    </when>

                </choose>
            </if>
            <if test="couponType != null and couponType !=''">
                and cp.coupon_type = #{couponType}
            </if>
            <if test="couponName != null and couponName !=''">
                and cp.coupon_name like concat('%',#{couponName,jdbcType=VARCHAR},'%')
            </if>
            <if test="companyWechatIds != null and companyWechatIds.size > 0">
                and cp.company_wechat_id in
                <foreach item="item" index="index" collection="companyWechatIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY cp.create_time DESC
    </select>






    <select id="couponTagDetail" resultType="com.gree.mall.manager.bean.coupon.CouponTagBean">
        SELECT ct.coupon_tag_id,
               tg.`name`,
               tg.tag_id         AS tagId,
               gr.tag_group_name AS groupName,
               gr.tag_group_id   AS groupId
        FROM coupon_tag ct
                 LEFT JOIN tag tg ON tg.tag_id = ct.tag_id
                 LEFT JOIN tag_group gr ON tg.group_id = gr.tag_group_id
        WHERE ct.coupon_id = #{couponId}
        ORDER BY ct.create_time ASC

    </select>


    <select id="couponTagUser" resultType="com.gree.mall.manager.bean.coupon.CouponUserBean">
        SELECT cu.*,
               ur.nick_name,
               ur.mobile,
               ur.sex,
               ur.avatar,
               ur.type,
               ur.country,
               ur.province,
               ur.city
        from coupon_user cu
                 LEFT JOIN user ur on cu.user_id = ur.user_id
        where cu.coupon_id = #{couponId}

    </select>


    <select id="reissueCouponUser" resultType="com.gree.mall.manager.bean.coupon.CouponUserTypeBean">
        SELECT ur.user_id, ur.nick_name as userName, ur.type
        fROM user ur

        where ur.type = 'SERVICE'
         and ur.company_wechat_id = #{companyWechatId}
         and not EXISTS(
            select uc.user_id
            from user_coupon uc
            where uc.coupon_id = #{couponId}
            and uc.transfer_type = 1
            and uc.user_id = ur.user_id
        )
    </select>


</mapper>