<?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.manage.statistics.StatisticsExportMapper">

    <select id="getMonthNfExNumOfUser" resultType="java.lang.Integer">
        select count(distinct user_id)
        from (select user_id
              from `order_don`
              where sku_id = 4
                and status not in ('close', 'noPayment', 'refund')
                and created_time between #{lastMonth} and #{thisMonth}
              union all
              select user_id
              from `order_don`
              where sku_id = 5
                and status not in ('close', 'noPayment', 'refund')
                and created_time between #{lastThreeMonth} and #{lastTwoMonth}
              union all
              select user_id
              from `order_don`
              where sku_id = 6
                and status not in ('close', 'noPayment', 'refund')
                and created_time between #{lastYearMonth}
                  and #{lastElevMonth}
             ) s
    </select>

    <select id="getMonthNfExNumOfUserByType" resultType="java.lang.Integer">
        select count(distinct user_id)
        from (select user_id
              from `order_don`
              where sku_id = 4
                and status not in ('close', 'noPayment', 'refund')
                and created_time between #{lastMonth} and #{thisMonth}
              union all
              select user_id
              from `order_don`
              where sku_id = 5
                and status not in ('close', 'noPayment', 'refund')
                and created_time between #{lastThreeMonth} and #{lastTwoMonth}
              union all
              select user_id
              from `order_don`
              where sku_id = 6
                and status not in ('close', 'noPayment', 'refund')
                and created_time between #{lastYearMonth}
                  and #{lastElevMonth}) s
        where EXISTS(select id
                     from order_don od
                     where status not in ('close', 'refund', 'noPayment')
                       and created_time between #{thisMonth} and #{nextMonth}
                       and order_type = #{type}
                       and od.user_id = s.user_id
                       and goods_id = #{goodsId})
    </select>

    <select id="getMonthNfRenewNumOfUser" resultType="java.lang.Integer">
        select count(distinct user_id)
        from `order_don`
        where goods_id = #{goodsId}
          and order_type = 2
          and status not in ('close', 'refund', 'noPayment')
          and created_time between #{thisMonth} and #{nextMonth}
    </select>

    <select id="getAiMonthPlus" resultType="java.lang.Long">
        select id
        from `goods_don_sku`
        where goods_id = #{goodsId}
          and months = 1
    </select>

    <select id="getMonthAiExNumOfUser" resultType="java.lang.Integer">
        select count(distinct buyer_id)
        from `order_don` od
        where status not in ('close','noPayment')
        and (refund_money is null or refund_money != money)
        and created_time BETWEEN #{lastMonth} and #{thisMonth}
        and sku_id in
        <foreach collection="skuIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getMonthAiExNumOfUserByType" resultType="java.lang.Integer">
        select count(distinct buyer_id) from (select buyer_id
        from `order_don` od
        where status not in ('close','noPayment')
        and (refund_money is null or refund_money != money)
        and created_time BETWEEN #{lastMonth} and #{thisMonth}
        and sku_id in
        <foreach collection="skuIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>) s where EXISTS(select id
        from order_don
        where status not in ('close','noPayment')
        and (refund_money is null or refund_money != money)
        and created_time between #{thisMonth} and #{nextMonth}
        and order_type = #{type}
        and buyer_id = s.buyer_id
        and sku_id in
        <foreach collection="skuIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        )
    </select>

    <select id="getMonthAiRenewNumOfUser" resultType="java.lang.Integer">
        select count(distinct buyer_id)
        from `order_don` od
        where status not in ('close','noPayment')
        and (refund_money is null or refund_money != money)
        and created_time BETWEEN #{thisMonth} and #{nextMonth}
        and order_type = 2
        and sku_id in
        <foreach collection="skuIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getMonthStreamingNewNumOfUser" resultType="java.lang.Integer">
        select count(DISTINCT user_id)
        from (select user_id, created_time
              from `order_don`
              where goods_id in
                    (select goods_id
                     from `goods_don_category_relate`
                     where category = 2)
                and status not in ('close', 'refund', 'noPayment')
                and created_time BETWEEN
                  #{thisMonth} and #{nextMonth}) s
        where not EXISTS(select id
                         from order_don od
                         where status not in ('close', 'refund', 'noPayment')
                           and od.user_id = s.user_id
                           and od.created_time &lt;
                               s.created_time)
    </select>

    <select id="getMonthStreamingNewPayOtherNumOfUser" resultType="java.lang.Integer">
        select count(DISTINCT user_id)
        from (select user_id, created_time
              from `order_don`
              where goods_id in
                    (select goods_id
                     from `goods_don_category_relate`
                     where category = 2)
                and status not in ('close', 'refund', 'noPayment')
                and created_time BETWEEN
                  #{thisMonth} and #{nextMonth}) s
        where not EXISTS(select id
                         from order_don od
                         where status not in ('close', 'refund', 'noPayment')
                           and od.user_id = s.user_id
                           and od.created_time &lt;
                               s.created_time)
          and EXISTS(select id
                     from order_don od
                     where status not in ('close', 'refund', 'noPayment')
                       and od.user_id = s.user_id
                       and od.created_time between
                         #{thisMonth} and #{nextMonth}
                       and od.created_time > s.created_time)
    </select>

    <select id="getMonthStreamingNewPayAiNumOfUser" resultType="java.lang.Integer">
        select count(DISTINCT user_id)
        from (select user_id, created_time
              from `order_don`
              where goods_id in
                    (select goods_id
                     from `goods_don_category_relate`
                     where category = 2)
                and status not in ('close', 'refund', 'noPayment')
                and created_time BETWEEN
                  #{thisMonth} and #{nextMonth}) s
        where not EXISTS(select id
                         from order_don od
                         where status not in ('close', 'refund', 'noPayment')
                           and od.user_id = s.user_id
                           and od.created_time &lt;
                               s.created_time)
          and EXISTS(select id
                     from (select id, goods_id
                           from order_don od
                           where status not in ('close', 'refund', 'noPayment')
                             and od.user_id = s.user_id
                             and od.created_time between #{thisMonth} and #{nextMonth}
                           order by od.created_time limit 1,1) s
                     where s.goods_id in (select goods_id
                                          from `goods_don_category_relate`
                                          where category = 1))
    </select>

    <select id="getMonthAiNewNumOfUser" resultType="java.lang.Integer">
        select count(DISTINCT user_id)
        from (select user_id, created_time
              from `order_don`
              where goods_id in
                    (select goods_id
                     from `goods_don_category_relate`
                     where category = 1)
                and status not in ('close', 'refund', 'noPayment')
                and created_time BETWEEN
                  #{thisMonth} and #{nextMonth}) s
        where not EXISTS(select id
                         from order_don od
                         where status not in ('close', 'refund', 'noPayment')
                           and od.user_id = s.user_id
                           and od.created_time &lt;
                               s.created_time)
    </select>

    <select id="getMonthAiNewPayOtherNumOfUser" resultType="java.lang.Integer">
        select count(DISTINCT user_id)
        from (select user_id, created_time
              from `order_don`
              where goods_id in
                    (select goods_id
                     from `goods_don_category_relate`
                     where category = 1)
                and status not in ('close', 'refund', 'noPayment')
                and created_time BETWEEN
                  #{thisMonth} and #{nextMonth}) s
        where not EXISTS(select id
                         from order_don od
                         where status not in ('close', 'refund', 'noPayment')
                           and od.user_id = s.user_id
                           and od.created_time &lt;
                               s.created_time)
          and EXISTS(select id
                     from order_don od
                     where status not in ('close', 'refund', 'noPayment')
                       and od.user_id = s.user_id
                       and od.created_time between #{thisMonth} and #{nextMonth}
                       and od.created_time > s.created_time)
    </select>

    <select id="getMonthAiNewPayStreamingNumOfUser" resultType="java.lang.Integer">
        select count(DISTINCT user_id)
        from (select user_id, created_time
              from `order_don`
              where goods_id in
                    (select goods_id
                     from `goods_don_category_relate`
                     where category = 1)
                and status not in ('close', 'refund', 'noPayment')
                and created_time BETWEEN
                  #{thisMonth} and #{nextMonth}) s
        where not EXISTS(select id
                         from order_don od
                         where status not in ('close', 'refund', 'noPayment')
                           and od.user_id = s.user_id
                           and od.created_time &lt;
                               s.created_time)
          and EXISTS(select id
                     from (select id, goods_id
                           from order_don od
                           where status not in ('close', 'refund', 'noPayment')
                             and od.user_id = s.user_id
                             and od.created_time between #{thisMonth} and #{nextMonth}
                           order by od.created_time limit 1,1) s
                     where s.goods_id in (select goods_id
                                          from `goods_don_category_relate`
                                          where category = 2))
    </select>

    <select id="getMonthPrivateCorpNumOfUser" resultType="java.lang.Integer">
        select count(distinct s.user_id) from (select user_id from (select user_id,created_time from `order_don` where
        goods_id in
        (select id from goods_don
        <where>
            <if test="category!=null">
                and category = #{category}
            </if>
            <if test="type!=null">
                and `type` = #{type}
            </if>
        </where>
        )
        and status not in ('close','noPayment','refund')) s where not exists (select id from `order_don` where status
        not
        in ('close','noPayment','refund')
        and user_id= s.user_id and created_time &lt; s.created_time)) s inner join user u on u.id = s.user_id and
        u.unionid != '' inner join corp_user cu on cu.user_id =
        u.id
        inner join corp_user_auth ca on ca.corp_user_id = cu.id and ca.wx_corp_id = 2
    </select>

    <select id="getShouldApplyMoney" resultType="java.math.BigDecimal">
        select sum(points) / 1000
        from `distribute_waiting_send_points`
        where send_status = 'success'
          and created_time &lt; #{endDate}
    </select>

    <select id="getRealApplyMoney" resultType="java.math.BigDecimal">
        select sum(money) / 100
        from `distribute_money_apply`
        where `verify_status` = 'success'
          and created_time &lt; #{endDate}
    </select>

    <select id="getMonthUserSourceNumByType" resultType="java.lang.Integer">
        select count(u.id)
        from (select id from `user` where created_time >= #{startDate} and created_time &lt; #{endDate}) u
        left join user_distribute_shared us on us.user_id = u.id
        left join user_distribute ud on ud.user_id = us.shared_id
        <where>
            <choose>
                <when test="type == 1">
                    us.id is null
                </when>
                <otherwise>
                    us.id is not null
                </otherwise>
            </choose>
        </where>
    </select>

    <select id="getNfTicketNumOfUser" resultType="java.lang.Integer">
        select count(distinct user_id)
        from `order_don`
        where goods_id = #{goodsId}
          and status not in ('close', 'noPayment', 'refund')
    </select>

    <select id="getAiTicketNumOfUser" resultType="java.lang.Integer">
        select count(distinct o.user_id)
        from `order_don` o
                 left join goods_don g on g.id = o.goods_id
        where g.id in (select goods_id
                       from `goods_don_category_relate`
                       where category = 1)
          and o.status not in ('close', 'noPayment', 'refund')
    </select>

    <select id="getNfAiTicketNumOfUser" resultType="java.lang.Integer">
        select count(distinct o.user_id)
        from `order_don` o
                 left join order_don o2 on o2.user_id = o.user_id
                 left join goods_don g on g.id = o2.goods_id
        where o.goods_id = #{goodsId}
          and o.status not in ('close', 'noPayment', 'refund')
          and o2.status not in ('close', 'noPayment', 'refund')
          and g.id in (select goods_id
                       from `goods_don_category_relate`
                       where category = 1)
    </select>

    <select id="getMonthAmountOfOrderByType" resultType="java.math.BigDecimal">
        select ifnull((sum(o.money) - ifnull(sum(o.refund_money),0))/100,0) from `order_don` o left join `user_distribute_shared`
        us on us.user_id = o.user_Id
        left join `user_distribute` ud on ud.user_id = us.shared_id and ud.deleted is true
        where
        <choose>
            <when test="type == 1">
                us.id is null
            </when>
            <when test="type == 2">
                us.id is not null and ud.id is null
            </when>
            <when test="type == 3">
                ud.id is not null
            </when>
        </choose>
        and o.created_time >= #{startDate} and o.created_time &lt; #{endDate}
        and status not in ('close', 'noPayment')
    </select>

    <select id="getMonthGoodsTypeAmountOfOrderByType" resultType="java.math.BigDecimal">
        select ifnull((sum(o.money) - ifnull(sum(o.refund_money),0))/100,0) from `order_don` o left join `user_distribute_shared`
        us on us.user_id = o.user_Id
        left join `user_distribute` ud on ud.user_id = us.shared_id and ud.deleted is true
        where
        <choose>
            <when test="type == 1">
                us.id is null
            </when>
            <when test="type == 4">
                us.id is not null
                <if test="isBind != null">
                    and us.is_bind = 0
                </if>
            </when>
            <otherwise>
                us.id is not null
                <if test="isBind != null">
                    and us.is_bind = 1
                </if>
            </otherwise>
        </choose>
        <if test="startDate != null">
            and o.created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and o.created_time &lt; #{endDate}
        </if>
        and status not in ('close', 'noPayment')
        and goods_id in (select id from goods_don
        <where>
            <if test="goodsType != null">
                `type` = #{goodsType}
            </if>
            <if test="category!=null">
                and id in (select goods_id from goods_don_category_relate where category = #{category})
            </if>
        </where>
        )
    </select>

    <select id="getMonthUserSourceNumOrderByType" resultType="java.lang.Integer">
        select count(distinct s.id) from (select u.id
        from (select id from `user` where created_time >= #{startDate} and created_time &lt; #{endDate}) u
        left join user_distribute_shared us on us.user_id = u.id
        left join user_distribute ud on ud.user_id = us.shared_id
        <where>
            <choose>
                <when test="type == 1">
                    us.id is null
                </when>
                <otherwise>
                    us.id is not null
                </otherwise>
            </choose>
        </where>
        ) s left join order_don o on o.user_id = s.id where o.status not in ('close', 'noPayment','refund')
        and o.created_time >= #{startDate} and o.created_time &lt; #{endDate}
    </select>

    <select id="getAppleOneDataThisMonth" resultType="com.cyksj.model.excel.statistics.ExcelAppleOneData">
        select count(distinct if(status = 'refund',NUll,user_id)) as numOfUser,
        count(if(status = 'refund',NULL,id)) as numOfOrder,
        cast((ifnull(sum(money),0)-ifnull(sum(refund_money),0))/100 as decimal(10,2)) as money,
        count(if(type = 'EX_CODE' and status != 'refund',id,NULL)) as numOfExCodeOrder,
        count(if(is_distribute = 1 and status != 'refund',id,NULL)) as disOrder,
        cast((sum(if(is_distribute = 1,money,0))-ifnull(sum(if(is_distribute = 1,refund_money,0)),0))/100 as
        decimal(10,2)) as disMoney
        from `order_don`
        where sku_id in
        <foreach collection="skuIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and 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="getAppleOnePayMonthTypeOrderThisMonth" resultType="java.lang.Integer">
        select count(*) from `order_don` where sku_id = #{skuId}
        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="getTransferData" resultType="java.util.Map">
        select sum(if(money &lt;= 40000, money, 0)) autoMoney,
               sum(if(type = 'usdt', money, 0))     usdtMoney,
               sum(if(money > 40000 and type != 'usdt', money, 0))     manualMoney
        from distribute_money_apply
        where created_time >= #{startDate}
          and created_time &lt; #{endDate}
          and verify_status = 'success'
    </select>

    <select id="getGroupsOrderData" resultType="com.cyksj.model.dto.NatureOrderDataDto">
        select type,
        ifnull((sum(money) - ifnull(sum(refund_money),0))/100,0) as money from (select money,
        refund_money,
        (case
        when us.id is null then 1
        when us.id is not null and ud.id is null then 2
        when ud.id is not null and us.is_bind = 1 then 3
        when ud.id is not null and us.is_bind = 0 then 4
        else 1
        end) as `type`
        from `order_don` o
        left join `user_distribute_shared` us on us.user_id = o.user_Id
        left join `user_distribute` ud on ud.user_id = us.shared_id and ud.deleted is true
        where status not in ('close', 'noPayment')
        <if test="startDate != null">
            and o.created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and o.created_time &lt; #{endDate}
        </if>) s group by `type`
    </select>

    <select id="getMonthlyUserOrderDetail" resultType="com.cyksj.model.dto.MonthlyUserOrderDetailDto">
        select cast(((sum(money) - sum(ifnull(refund_money, 0))) / 100) as decimal(10,2)) as money,
               count(if(`status` != 'refund', 1, NUll))          as orders,
               1                                                    type
        from `order_don` o
                 left join user u on u.id = o.user_id

        where o.status not in ('close', 'noPayment')
          and o.created_time BETWEEN #{thisMonth} and #{nextMonth}
          and u.created_time &lt; #{thisMonth}
        union all
        select cast(((sum(money) - sum(ifnull(refund_money, 0))) / 100) as decimal(10,2)) as money,
               count(if(`status` != 'refund', 1, NUll))          as orders,
               2                                                    type
        from `order_don` o
                 left join user u on u.id = o.user_id
        where o.status not in ('close', 'noPayment')
          and o.created_time BETWEEN #{thisMonth} and #{nextMonth}
          and u.created_time BETWEEN #{thisMonth} and #{nextMonth}
    </select>

    <select id="getEpOrderNumsGroupSource" resultType="java.util.Map">
        select count(if(is_distribute = 0,1,NULL)) naturalEpOrders,count(if(is_distribute = 1,1,NULL)) distributeOrders
        from order_don
        where status not in ('close', 'noPayment', 'refund')
        and goods_id = 15
        <if test="startDate != null">
            and created_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and created_time &lt; #{endDate}
        </if>
        group by DATE_FORMAT(created_time, '%Y-%m')
    </select>

    <select id="getOverPayByGoodsId" resultType="java.lang.Integer">
        select count(DISTINCT user_id)
        from order_don o
        where created_time &lt; #{lastMonth}
        <if test="goodsId !=null">
            and goods_id = #{goodsId}
        </if>
        <if test="skuIds!=null">
            and sku_id in
            <foreach collection="skuIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and `status` not in ('close', 'noPayment', 'refund')
        and NOT EXISTS (select 1
        from order_don
        where created_time BETWEEN #{lastMonth} and #{thisMonth}
        and `status` not in ('close', 'noPayment', 'refund')
        AND user_id = o.user_id
        <if test="goodsId !=null">
            and goods_id = #{goodsId}
        </if>
        <if test="skuIds!=null">
            and sku_id in
            <foreach collection="skuIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        )
        and EXISTS (select 1
        from order_don
        where created_time BETWEEN #{thisMonth} and #{nextMonth}
        and `status` not in ('close', 'noPayment', 'refund')
        and user_id = o.user_id
        <if test="goodsId !=null">
            and goods_id = #{goodsId}
        </if>
        <if test="skuIds!=null">
            and sku_id in
            <foreach collection="skuIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        )
    </select>

    <select id="getYearNumOfRenewalsByGoodsId" resultType="java.lang.Integer">
        select count(distinct user_id)
        from order_don
        where created_time BETWEEN #{lastYearMonth} and #{lastElevMonth}
          and `status` not in ('close', 'noPayment', 'refund')
          and goods_id = #{goodsId}
    </select>

    <select id="getActualNumberOfRenewalsByGoodsId" resultType="java.lang.Integer">
        select count(distinct user_id)
        from order_don o
        where created_time BETWEEN #{lastYearMonth} and #{lastElevMonth}
          and `status` not in ('close', 'noPayment', 'refund')
          and goods_id = #{goodsId}
        and exists (select 1 from order_don where created_time > #{lastElevMonth}
            and `status` not in ('close', 'noPayment', 'refund')
            and goods_id = #{goodsId} and user_id = o.user_id limit 1)
    </select>
</mapper>