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

    <select id="queryOrderTotalAmount" resultType="java.math.BigDecimal">
        select ifnull(sum(b.pay_amount) - sum(b.refund_amount),0) total from order_info a
            join order_detail b on a.order_id = b.order_id
            where a.order_status in ('DFH','YFH','OVER','REFUND') and a.promotion_group_id=#{promotionGroupId}
    </select>

    <select id="queryTotalShareAmount" resultType="java.math.BigDecimal">
            select sum(amount) total from order_share where 1=1
              <if test="status == null or status ==''">
                  and status not in ('CANCEL','TIMEOUT')
              </if>
              <if test="status != null and status !=''">
                  and status = #{status}
              </if>
               <if test="promotionGroupId != null and promotionGroupId !=''">
                   and promotion_group_id=#{promotionGroupId}
               </if>
              <if test="userId != null and userId !=''">
                  and work_user_id =#{userId}
              </if>

    </select>

    <select id="queryGroupSpecUser" resultType="com.gree.mall.manager.bean.activity.PromotionGroupDetail">
        select  DISTINCT user_id,user_name,user_phone from promotion_group_spec_user where promotion_group_id = #{promotionGroupId}
    </select>

    <select id="queryPtDetail" resultType="com.gree.mall.manager.bean.activity.PromotionGroupDetail">
        select
            count(1) 'orderNum',
            sum(total_num) 'goodsNum',
            sum(pay_amount) 'orderTotalAmount'
        from order_info
        where promotion_group_user_id=#{userId} and promotion_group_id = #{promotionGroupId}
        and order_status not in ('CLOSE','TIMEOUT')
    </select>

    <select id="queryTotalGoodsNum" resultType="java.lang.Integer">
        select sum(b.num) - sum(b.refund_num) total from order_info a
        join order_detail b on a.order_id=b.order_id
        where a.order_status not in ('CLOSE','TIMEOUT')
        <if test="promotionGroupId != null and promotionGroupId !=''">
            and a.promotion_group_id=#{promotionGroupId}
        </if>
        <if test="userId != null and userId !=''">
            and a.promotion_group_user_id =#{userId}
        </if>
    </select>

</mapper>