<?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.cyksj.mapper.OrderDonMapper">

    <update id="updateHasPaymentInterestRealGoods">
        update real_goods_interest_user
        set interest_status = 'hasPayment'
        where id in (select id
                     from (select distinct ru.id
                           from `real_goods_interest_user` ru
                                    left join `order_don` od on od.user_id = ru.user_id and od.goods_id = ru.goods_id
                           where od.status not in ('close', 'noPayment')
                             and ru.interest_status != 'hasPayment') s)
    </update>

    <update id="updateHasPaymentInterestRealGoodsPayTime">
        update real_goods_interest_user ru
        set ru.pay_time= (select if(pay_time is null,created_time,pay_time) from `order_don` od where od.user_id = ru.user_id and od.goods_id = ru.goods_id and od.status not in ('close','noPayment') order by money desc limit 1),
            ru.sku_id = (select sku_id from `order_don` od where od.user_id = ru.user_id and od.goods_id = ru.goods_id and od.status not in ('close','noPayment')  order by money desc limit 1)
        where interest_status = 'hasPayment'
          and pay_time is null
    </update>


    <select id="exportOrderInfo" resultType="com.cyksj.model.excel.ExcelOrderDonInfo">
        SELECT o.order_no, o.real_name ,o.price,o.phone,o.source,o.pay_time ,u1.nickname as sharedName,u2.nickname as nickName
        from order_don o  LEFT JOIN user u1 on o.shared_id = u1.id
        LEFT JOIN user u2 on o.user_id = u2.id
        where o.promotion_id = #{promotionId,jdbcType=BIGINT}
         and o.status = 'hasPayment'
        <if test="start != null and start != ''">
            and o.pay_time >= #{start,jdbcType=VARCHAR}
        </if>
        <if test="end != null and end != ''">
            and #{end,jdbcType=VARCHAR} >= o.pay_time
        </if>

    </select>

    <select id="getAppleOneOrderDons" resultType="com.cyksj.model.entity.OrderDon">
        select id,sku_id,user_id,money,is_distribute,`type` from `order_don`
        where sku_id in
        <foreach collection="skuIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and status not in
        <foreach collection="noOrderStatus" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and created_time between #{yesZeroDate} and #{thisZeroDate}
    </select>

    <select id="selectNoPaymentRealGoods" resultType="com.cyksj.model.entity.RealGoodsInterestUser">
        select s.*
        from (select od.id                     as orderId,
                     od.user_id,
                     gd.id                     as goods_id,
                     od.sku_id,
                     od.money,
                     concat(od.user_id, gd.id) as groupBy,
                     od.created_time           as orderCreatedTime
              from `order_don` od
                       left join `goods_don` gd on gd.id = od.goods_id
                       left join `real_goods_interest_user` ru on ru.user_id = od.user_id
                  and ru.goods_id = od.goods_id
              where gd.type = 2
                and od.status = 'close'
                and ru.id is null
              group by od.id, od.user_id, gd.id, od.sku_id, od.money, od.created_time) s
        where not EXISTS(
                select id from `order_don` where user_id = s.user_id and goods_id = s.goods_id and status !='close')
    </select>

    <select id="selectPlatBrokenLineMoney" resultType="com.cyksj.model.views.BrokenLineObjView">
        select min(curdate) as firstDate,
               max(curdate) as lastDate
        from (select cast(sum(od.money)/100 as decimal(10,2)) as curdateMoney,
                     date_format(od.created_time,'%Y-%m-%d') curdate from order_don od
        where od.status not in ('noPayment','close','refund')
        group by od.goods_id,date_format(od.created_time,'%Y-%m-%d'))s
        where 1 = 1
        <if test="first != null">
            and curdate >= #{first}
        </if>
        <if test="end != null">
            and curdate &lt; #{end}
        </if>
    </select>

    <select id="selectPersonDistributeBrokenLine" resultType="com.cyksj.model.views.BrokenLineObjView">
        select min(curdate) as firstDate,
               max(curdate) as lastDate
        from (select date_format(ifnull(od.created_time,vod.created_time),'%Y-%m-%d') curdate,
                     count(dw.order_id) curdateOrderCount
        from distribute_waiting_send_points dw
        left join order_don od on od.id = dw.order_id and dw.is_vip = false
        left join vip_order_don vod on vod.id = dw.order_id and dw.is_vip = true
        where dw.shared_id = #{userId}
          and dw.send_status not in ('not_exist', 'close')
        group by ifnull(od.goods_id,vod.goods_id), date_format(ifnull(od.created_time,vod.created_time), '%Y-%m-%d'))s
        where 1 = 1
        <if test="first != null">
            and curdate >= #{first}
        </if>
        <if test="end != null">
            and curdate &lt; #{end}
        </if>
    </select>

    <select id="selectChannelStatistics" resultType="com.cyksj.model.views.ChannelStatisticsView">
        select min(curdate) as firstDate,
        max(curdate) as lastDate
        from (select cast(sum(od.money)/100 as decimal(10,2)) as curdateMoney,
        date_format(od.created_time,'%Y-%m-%d') curdate from order_don od
        where od.status not in ('noPayment','close','refund')
        group by date_format(od.created_time,'%Y-%m-%d'))s
        where 1 = 1
        <if test="first != null">
            and curdate >= #{first}
        </if>
        <if test="end != null">
            and curdate &lt; #{end}
        </if>
    </select>

    <select id="selectCouponCodeUserNum" resultType="java.lang.Integer">
        select count(distinct user_id) from order_don where (ds_popularize_id = #{dsPopularizeId} or coupon_ex_code = #{exCode})
        and status not in
        <foreach collection="noOrderAllStatus" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="countPopularizeOrderDetail" resultType="com.cyksj.model.views.ChannelPopularizeView">
        select *
        from (select count(*) as orderCount, ifnull(sum(money),0) as orderMoney
        from order_don
        where popularize_id = #{popularizeId}
        <if test="startTime != null and startTime != ''">
            and created_time >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and created_time &lt; #{endTime}
        </if>
        <if test="goodsId != null">
            and goods_id = #{goodsId}
        </if>
        and status not in ('close', 'noPayment')) s1
        ,
        (select count(*) as refundCount, ifnull(sum(money),0) as refundMoney
        from order_don
        where popularize_id = #{popularizeId}
        <if test="startTime != null and startTime != ''">
            and created_time >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and created_time &lt; #{endTime}
        </if>
        <if test="goodsId != null">
            and goods_id = #{goodsId}
        </if>
        and status = 'refund')
        s2
    </select>

    <select id="selectThisMonthAppleOneMoney" resultType="java.math.BigDecimal">
        select cast(ifnull((sum(money),0) - ifnull(sum(refund_money),0))/100 as decimal(10,2)) from `order_don` where sku_id in
        <foreach collection="skuIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and status not in ('close','noPayment') and created_time between #{startDate} and #{endDate}
    </select>

    <select id="selectThisMonthDistributeOrderMoney" resultType="java.math.BigDecimal">
        select cast((ifnull(sum(money),0) - ifnull(sum(refund_money),0))/100 as decimal(10,2)) from `order_don` where sku_id in
        <foreach collection="skuIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and status not in ('close','noPayment') and is_distribute = 1
        and created_time between #{startDate} and #{endDate}
    </select>

    <select id="getOrderDetailByGoodsType" resultType="java.util.Map">
        select count(id) as orderNum,
        ifnull(sum(money),0) as money
        from order_don where goods_id in (select id from `goods_don` where type = #{type})
        and status not in
        <foreach collection="noOrderStatus" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>
    </select>

    <select id="getBackgroundDataView" resultType="com.cyksj.model.views.BackgroundDataView">
        select count(distinct user_id) as totalNumOfUser,
        count(id) as totalNumOfOrder,
        cast(ifnull(sum(money),0)/100 as decimal(11,2)) as totalOrderAmount,
        count(if(status = 'refund',1,NULL)) as totalNumOfRefundOrder,
        cast(sum(ifnull(refund_money,0))/100 as decimal(11,2)) as totalRefundAmount
        from `order_don` where status not in
        <foreach collection="noOrderStatus" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>
    </select>

    <select id="statisticsUsedBalance" resultType="java.math.BigDecimal">
        select cast(ifnull(sum(balance),0)/100 as decimal(11,2)) from order_don where status not in
        <foreach collection="noOrderAllStatus" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>
    </select>

    <select id="selectYhShopDistributeOrderDetail" resultType="java.util.Map">
        select count(*) as numOfDsOrder,
               ifnull(sum(money),0) as dsMoney
        from `order_don` where yhs_id > 0 and status not in ('close','noPayment','refund')
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>
    </select>

    <select id="selectCountActiveCodeOrderByAcId" resultType="java.lang.Integer">
        select count(distinct o.id)
        from (select user_id from corp_user where ac_id = #{acId} and user_id != 0) s
                 left join order_don o
                           on o.user_id = s.user_Id
        where o.status not in ('close', 'noPayment', 'refund')
    </select>

    <select id="selectActiveCodeOrderDetail" resultType="java.util.Map">
        select count(distinct if(o.status != 'refund', o.id, null)) as orderNum,
               sum(money) - sum(ifnull(refund_money, 0))            as orderMoney
        from (select user_id from corp_user where ac_id = #{acId} and user_id != 0) s
                 left join order_don o
                           on o.user_id = s.user_Id
        where o.status not in ('close', 'noPayment')
        <if test="startTime != null">
            and o.created_time >= #{startTime}
        </if>
        <if test="endTime != null">
            and o.created_time &lt; #{endTime}
        </if>
    </select>


    <select id="getOriRenewOrderMoney" resultType="java.math.BigDecimal">
        select cast(ifnull((sum(money) - ifnull(sum(refund_money),0))/100,0) as decimal(10,2)) from order_don
        where status not in ('close','noPayment')
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>
        and order_type = 2
    </select>

    <select id="getTotalMoney" resultType="java.math.BigDecimal">
        select cast(ifnull((sum(money) - ifnull(sum(refund_money),0))/100,0) as decimal(10,2)) from order_don
        where status not in ('close','noPayment')
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>
    </select>

    <select id="getClearRenewOrderMoney" resultType="java.math.BigDecimal">
        select cast(ifnull((sum(o.money) - ifnull(sum(o.refund_money),0))/100,0) as decimal(10,2)) from
        user_ticket_cleared_record utr inner join order_don o
        on o.user_id = utr.user_id and o.goods_id = utr.goods_id and utr.source = 'self'
        where order_type = 1 and o.created_time > utr.start_time and o.status not in ('close', 'noPayment')
        <if test="startDate != null">
            and utr.created_time >= #{startDate}
            and o.created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and utr.created_time &lt; #{endDate}
            and o.created_time &lt; #{endDate}
        </if>
    </select>

    <select id="selectDsPlatBrokenLineMoney" resultType="com.cyksj.model.views.BrokenLineObjView">
        select min(curdate) as firstDate,
        max(curdate) as lastDate
        from (select cast(sum(if(od.id is null,vod.money,od.money))/100 as decimal(10,2)) as curdateMoney,
        date_format(ifnull(od.created_time,vod.created_time),'%Y-%m-%d') curdate from (select order_id,is_vip from
        distribute_waiting_send_points where
        shared_id = #{sharedId}) dp left join order_don od on od.id = dp.order_id and dp.is_vip = false
        left join vip_order_don vod on vod.id = dp.order_id and dp.is_vip = true
        where ifnull(od.status,vod.status) not in ('noPayment','close','refund')
        group by ifnull(od.goods_id,vod.goods_id),date_format(ifnull(od.created_time,vod.created_time),'%Y-%m-%d'))s
        where 1 = 1
        <if test="first != null">
            and curdate >= #{first}
        </if>
        <if test="end != null">
            and curdate &lt; #{end}
        </if>
    </select>

    <select id="getTagUserOrdersData" resultType="com.cyksj.model.views.UserOrderDetailView">
        select count(if(status != 'refund', 1, NULL)) as numOfOrder,
        cast((sum(money) - sum(ifnull(refund_money, 0))) / 100 as decimal(10,2)) as orderMoney,
        count(if(status != 'refund' and o.goods_id = 15,1,NULL)) as numOfEpOrder,
        cast((sum(if(o.goods_id = 15,money,0)) - sum(ifnull(if(o.goods_id = 15,refund_money,0), 0))) / 100 as
        decimal(10,2)) as epOrderMoney
        from (select distinct cu.user_id,cu.created_time from (select DISTINCT relation_id from corp_user_tag where tag_id = #{tagIdStr})
        ct inner join corp_user_follow_relation cufr on cufr.id = ct.relation_id
        inner join corp_user_auth ca on ca.external_userid = cufr.corp_user_id
        inner join corp_user cu on cu.id = ca.corp_user_id
        where cu.user_id != 0
        <if test="startTime != null">
            and cu.created_time >= #{startTime}
        </if>
        <if test="endTime != null">
            and cu.created_time &lt; #{endTime}
        </if>)
        cu inner join order_don o on o.user_id = cu.user_id
        and o.`status` not in ('close','noPayment')
        AND o.money > 0
        and o.created_time > cu.created_time
        <if test="consumeStTime != null">
            and o.created_time >= #{consumeStTime}
        </if>
        <if test="consumeEtTime != null">
            and o.created_time &lt; #{consumeEtTime}
        </if>
    </select>

    <select id="getTagUserDataPage" resultType="com.cyksj.model.views.UserOrderDetailView">
        select date_format(cu.created_time, '%Y-%m-%d') as `date`,
               count(distinct cu.id)                    as numOfUser
        from (select distinct cu.id, cu.user_id, cu.created_time
              from (select DISTINCT relation_id from corp_user_tag where tag_id = #{tagIdStr}) ct
                       inner join corp_user_follow_relation cufr on cufr.id = ct.relation_id
                       inner join corp_user_auth ca on ca.external_userid = cufr.corp_user_id
                       inner join corp_user cu on cu.id = ca.corp_user_id) cu
        group by date_format(cu.created_time, '%Y-%m-%d')
        order by date_format(cu.created_time, '%Y-%m-%d') desc
    </select>

    <select id="getTagUserTimeOrderData" resultType="com.cyksj.model.views.CorpTagUserOrderDataView">
        select o.id as order_id,o.user_id,
        (select nickname from user u where u.id = o.user_id limit 1),
        o.order_no,o.goods_id,g.title,o.sku_id,sku.spec_val,
        o.money/100 as money,o.created_time
        from (select distinct cu.user_id, date_format(cu.created_time, '%Y-%m-%d') joinTime
        from (select DISTINCT relation_id from corp_user_tag where tag_id = #{tagIdStr}) ct
        inner join corp_user_follow_relation cufr on cufr.id = ct.relation_id
        inner join corp_user_auth ca on ca.external_userid = cufr.corp_user_id
        inner join corp_user cu on cu.id = ca.corp_user_id
        where cu.user_id != 0) cu inner join order_don o on o.user_id = cu.user_id
        and o.status not in ('close', 'noPayment','refund')
        and date_format(o.created_time, '%Y-%m-%d') = joinTime
        and o.money > 0
        <if test="toDate != null and toDate != ''">
            and date_format(o.created_time,'%Y-%m-%d') = #{toDate}
        </if>
        left join goods_don g on g.id = o.goods_id
        left join goods_don_sku sku on sku.id = o.sku_id
        order by o.created_time desc
    </select>

    <select id="getRealGoodsSourceData" resultType="com.cyksj.model.views.RealGoodsSourceData">
        select label_source,
        count(*) as orders,
        sum(money) as orderMoney
        from order_don o
        left join user_distribute_shared us on us.user_id = o.user_id
        where goods_id = 15
        and status not in ('close', 'noPayment', 'refund')
        <if test="startDate != null">
            and o.created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and o.created_time &lt; #{endDate}
        </if>
        <if test="isGeneral != null and isGeneral">
            and us.id is null
        </if>
        and o.label_source != ''
        group by o.label_source
    </select>

    <select id="selectMaxRefundGoodsName" resultType="java.lang.String">
        select (select title from goods_don where id = o.goods_id limit 1)
        from (select distinct order_id from refund_order_don where operator = #{operator}
        and refund_method not in ('EX_CODE'
        , 'balance')) rd inner join order_don o
        on o.id = rd.order_id
        <if test="startTime != null">
            and o.created_time >= #{startTime}
        </if>
        <if test="endTime != null">
            and o.created_time &lt; #{endTime}
        </if>
        group by o.goods_id
        order by count(*) desc limit 1
    </select>

    <select id="selectOrderRelate" resultType="com.cyksj.model.entity.OrderDonCustomerServiceRelate">
        select cms_user_id, (select nickname from cms_user where id = odr.cms_user_id limit 1) as nickname
        from order_don_customer_service_relate odr
        where order_id = #{orderId} limit 1
    </select>

    <select id="selectSubChannelOrderMoney" resultType="java.math.BigDecimal">
        select sum(o.money) - sum(ifnull(o.refund_money, 0))
        from distribute_waiting_send_points dp
                 inner join order_don o on o.id = dp.order_id
        where dp.shared_id = #{sharedId}
          and o.goods_id != 15
            and o.status not in ('close', 'noPayment')
        and o.created_time > #{invitedTime}
          and o.created_time &lt; #{limitTime}
    </select>

    <select id="getPastRecord" resultType="com.cyksj.model.views.UserOrderDetailView">
        select date_format(o.created_time, '%Y-%m-%d')                                              as `date`,
               count(if(status != 'refund', 1, NULL))                                               as numOfOrder,
               cast((ifnull(sum(money), 0) - sum(ifnull(refund_money, 0))) / 100 as decimal(10, 2)) as orderMoney
        from (select distinct cu.user_id, cu.created_time
              from (select DISTINCT relation_id from corp_user_tag where tag_id = #{tagIdStr}) ct
                       inner join corp_user_follow_relation cufr on cufr.id = ct.relation_id
                       inner join corp_user_auth ca on ca.external_userid = cufr.corp_user_id
                       inner join corp_user cu on cu.id = ca.corp_user_id
              where cu.user_id != 0) cu
                 inner join order_don o
                            on o.user_id = cu.user_id
                                and o.`status` not in ('close', 'noPayment')
                                AND money > 0
                                and o.created_time > cu.created_time
        group by date_format(o.created_time, '%Y-%m-%d')
        order by date_format(o.created_time, '%Y-%m-%d') desc
    </select>

    <select id="getNumOfTagFansUser" resultType="com.cyksj.model.views.UserOrderDetailView">
        select count(distinct cu.id) as numOfUser,
        count(distinct if(cufr.status = 0,cu.id,NULL)) as delNumOfUser
        from (select distinct relation_id from `corp_user_tag` where tag_id = #{tagIdStr}) s
        inner join corp_user_follow_relation cufr on cufr.id = s.relation_id
        inner join corp_user_auth ca on ca.external_userid = cufr.corp_user_id
        inner join corp_user cu on cu.id = ca.corp_user_id
        <where>
            <if test="startTime != null">
                cu.created_time >= #{startTime}
            </if>
            <if test="endTime != null">
                and cu.created_time &lt; #{endTime}
            </if>
        </where>
    </select>

    <select id="getFansToDayOrdersView" resultType="com.cyksj.model.views.UserOrderDetailView">
        select count(if(status != 'refund', 1, NULL))                                               as numOfOrder,
               cast((ifnull(sum(money), 0) - sum(ifnull(refund_money, 0))) / 100 as decimal(10, 2)) as orderMoney
        from (select distinct cu.user_id, cu.created_time
              from (select DISTINCT relation_id from corp_user_tag where tag_id = #{tagIdStr}) ct
                       inner join corp_user_follow_relation cufr on cufr.id = ct.relation_id
                       inner join corp_user_auth ca on ca.external_userid = cufr.corp_user_id
                       inner join corp_user cu on cu.id = ca.corp_user_id
              where cu.user_id != 0) cu
                 inner join order_don o on o.user_id = cu.user_id
            and o.`status` not in ('close', 'noPayment')
            AND money > 0
            and date_format(o.created_time, '%Y-%m-%d') = date_format(cu.created_time, '%Y-%m-%d')
            and date_format(o.created_time, '%Y-%m-%d') = #{date}
    </select>

    <select id="getPayGoodsIds" resultType="java.lang.Long">
        select distinct goods_id from order_don where status not in ('close','noPayment')
        and user_id in
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getTotalRefundAmountTime" resultType="java.math.BigDecimal">
        select cast(ifnull(sum(refund_fee),0)/100 as decimal(11,2))
        from refund_order_don
        where (refund_method != 'balance' or is_clear_balance = 1)
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>
    </select>

    <select id="getCouponUsedMoney" resultType="java.math.BigDecimal">
        select sum(money) - sum(ifnull(refund_money, 0))
        from (select id from coupon_user where coupon_id = #{couponId}) cu
                 inner join order_don o on o.coupon_user_id = cu.id and o.status not in ('close', 'noPayment')
    </select>

    <select id="checkUserHasPaidMjMirror" resultType="java.lang.Integer">
        select count(1) from order_don where user_id in
        <foreach collection="list" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        and status not in ('close','noPayment','refund') and sku_id in (select id from goods_don_sku where goods_id = 26 and
        is_mirror = 1)
    </select>

    <select id="selectUserMIrrorShopDistributeOrderDetail" resultType="java.util.Map">
        select count(*) as numOfDsOrder,
        ifnull(sum(money),0) as dsMoney
        from `order_don` o inner join user_mirror_shop_order_relate uor on uor.order_id = o.id and o.status not in ('close','noPayment','refund')
        <if test="startDate != null">
            and o.created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and o.created_time &lt; #{endDate}
        </if>
    </select>
</mapper>