<?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.ruixuan.bytedance.mapper.BytedancePromoterOrderListMapper">

    <resultMap type="com.ruixuan.bytedance.entity.BytedancePromoterOrderList" id="BytedancePromoterOrderListResult">
        <result property="orderId" column="order_id"/>
        <result property="productId" column="product_id"/>
        <result property="productName" column="product_name"/>
        <result property="productImg" column="product_img"/>
        <result property="productBindUserId" column="product_bind_user_id"/>
        <result property="productBindUserName" column="product_bind_user_name"/>
        <result property="shopId" column="shop_id"/>
        <result property="shopName" column="shop_name"/>
        <result property="totalPayAmount" column="total_pay_amount"/>
        <result property="flowPoint" column="flow_point"/>
        <result property="app" column="app"/>
        <result property="updateTime" column="update_time"/>
        <result property="paySuccessTime" column="pay_success_time"/>
        <result property="settleTime" column="settle_time"/>
        <result property="payGoodsAmount" column="total_pay_amount"/>
        <result property="settledGoodsAmount" column="total_pay_amount"/>
        <result property="itemNum" column="item_num"/>
        <result property="refundTime" column="refund_time"/>
        <result property="authorShortId" column="author_short_id"/>
        <result property="authorShortName" column="author_short_name"/>
        <result property="authorShortUrl" column="author_short_url"/>
        <result property="authorBindUserId" column="author_bind_user_id"/>
        <result property="authorBindUserName" column="author_bind_user_name"/>
        <result property="colonelType" column="colonel_type"/>
        <result property="mediaType" column="media_type"/>
        <result property="commissionRate" column="commission_rate"/>
        <result property="isTrusteeship" column="is_trusteeship"/>
        <result property="goodShareType" column="good_share_type"/>
        <result property="colonelCommissionRate" column="colonel_commission_rate"/>
        <result property="colonelActivityId" column="colonel_activity_id"/>
        <result property="colonelEstimatedCommission" column="colonel_estimated_commission"/>
        <result property="colonelRealCommission" column="colonel_real_commission"/>
        <result property="colonelTechServiceFeeRate" column="colonel_tech_service_fee_rate"/>
        <result property="colonelTechServiceFee" column="colonel_tech_service_fee"/>
        <result property="colonelSettledTechServiceFee" column="colonel_settled_tech_service_fee"/>
        <result property="colonelInstitutionId" column="colonel_institution_id"/>
        <result property="colonelInstitutionName" column="colonel_institution_name"/>
        <result property="secondCommissionRate" column="second_commission_rate"/>
        <result property="secondActivityId" column="second_activity_id"/>
        <result property="secondEstimatedCommission" column="second_estimated_commission"/>
        <result property="secondRealCommission" column="second_real_commission"/>
        <result property="createTime" column="create_time"/>
        <result property="statDate" column="stat_date"/>
    </resultMap>

    <sql id="SELECTBytedancePromoterOrderListVo">
        SELECT order_id,
               product_id,
               product_name,
               product_img,
               product_bind_user_id,
               product_bind_user_name,
               shop_id,
               shop_name,
               total_pay_amount,
               flow_point,
               app,
               update_time,
               pay_success_time,
               settle_time,
               total_pay_amount,
               total_pay_amount,
               item_num,
               refund_time,
               author_short_id,
               author_short_url,
               author_bind_user_id,
               author_bind_user_name,
               colonel_type,
               media_type,
               commission_rate,
               is_trusteeship,
               good_share_type,
               colonel_commission_rate,
               colonel_activity_id,
               colonel_estimated_commission,
               colonel_real_commission,
               colonel_tech_service_fee_rate,
               colonel_tech_service_fee,
               colonel_settled_tech_service_fee,
               colonel_institution_id,
               colonel_institution_name,
               second_commission_rate,
               second_activity_id,
               second_estimated_commission,
               second_real_commission,
               create_time,
               stat_date
        FROM bytedance_promoter_order_list
    </sql>

    <select id="SELECTBytedancePromoterOrderListList"
            parameterType="com.ruixuan.bytedance.entity.BytedancePromoterOrderList"
            resultMap="BytedancePromoterOrderListResult">
        <include refid="SELECTBytedancePromoterOrderListVo"/>
        <where>
            <if test="productId != null  and productId != ''">and product_id = #{productId}</if>
            <if test="productName != null  and productName != ''">and product_name like CONCAT('%', #{productName},
                '%')
            </if>
            <if test="productImg != null  and productImg != ''">and product_img = #{productImg}</if>
            <if test="productBindUserId != null  and productBindUserId != ''">and product_bind_user_id =
                #{productBindUserId}
            </if>
            <if test="productBindUserName != null  and productBindUserName != ''">and product_bind_user_name like
                CONCAT('%', #{productBindUserName}, '%')
            </if>
            <if test="shopId != null ">and shop_id = #{shopId}</if>
            <if test="shopName != null  and shopName != ''">and shop_name like CONCAT('%', #{shopName}, '%')</if>
            <if test="totalPayAmount != null ">and total_pay_amount = #{totalPayAmount}</if>
            <if test="flowPoint != null  and flowPoint != ''">and flow_point = #{flowPoint}</if>
            <if test="app != null  and app != ''">and app = #{app}</if>
            <if test="paySuccessTime != null ">and pay_success_time = #{paySuccessTime}</if>
            <if test="settleTime != null ">and settle_time = #{settleTime}</if>
            <if test="payGoodsAmount != null ">and total_pay_amount = #{payGoodsAmount}</if>
            <if test="settledGoodsAmount != null ">and total_pay_amount = #{settledGoodsAmount}</if>
            <if test="itemNum != null ">and item_num = #{itemNum}</if>
            <if test="refundTime != null ">and refund_time = #{refundTime}</if>
            <if test="authorShortId != null  and authorShortId != ''">and author_short_id = #{authorShortId}</if>
            <if test="authorShortUrl != null  and authorShortUrl != ''">and author_short_url = #{authorShortUrl}</if>
            <if test="authorBindUserId != null  and authorBindUserId != ''">and author_bind_user_id =
                #{authorBindUserId}
            </if>
            <if test="authorBindUserName != null  and authorBindUserName != ''">and author_bind_user_name like
                CONCAT('%', #{authorBindUserName}, '%')
            </if>
            <if test="colonelType != null  and colonelType != ''">and colonel_type = #{colonelType}</if>
            <if test="mediaType != null  and mediaType != ''">and media_type = #{mediaType}</if>
            <if test="commissionRate != null ">and commission_rate = #{commissionRate}</if>
            <if test="isTrusteeship != null  and isTrusteeship != ''">and is_trusteeship = #{isTrusteeship}</if>
            <if test="goodShareType != null  and goodShareType != ''">and good_share_type = #{goodShareType}</if>
            <if test="colonelCommissionRate != null ">and colonel_commission_rate = #{colonelCommissionRate}</if>
            <if test="colonelActivityId != null ">and colonel_activity_id = #{colonelActivityId}</if>
            <if test="colonelEstimatedCommission != null ">and colonel_estimated_commission =
                #{colonelEstimatedCommission}
            </if>
            <if test="colonelRealCommission != null ">and colonel_real_commission = #{colonelRealCommission}</if>
            <if test="colonelTechServiceFeeRate != null ">and colonel_tech_service_fee_rate =
                #{colonelTechServiceFeeRate}
            </if>
            <if test="colonelTechServiceFee != null ">and colonel_tech_service_fee = #{colonelTechServiceFee}</if>
            <if test="colonelSettledTechServiceFee != null ">and colonel_settled_tech_service_fee =
                #{colonelSettledTechServiceFee}
            </if>
            <if test="colonelInstitutionId != null ">and colonel_institution_id = #{colonelInstitutionId}</if>
            <if test="colonelInstitutionName != null  and colonelInstitutionName != ''">and colonel_institution_name
                like CONCAT('%', #{colonelInstitutionName}, '%')
            </if>
            <if test="secondCommissionRate != null ">and second_commission_rate = #{secondCommissionRate}</if>
            <if test="secondActivityId != null ">and second_activity_id = #{secondActivityId}</if>
            <if test="secondEstimatedCommission != null ">and second_estimated_commission =
                #{secondEstimatedCommission}
            </if>
            <if test="secondRealCommission != null ">and second_real_commission = #{secondRealCommission}</if>
        </where>
    </select>

    <select id="SELECTBytedancePromoterOrderListByOrderId" parameterType="Long"
            resultMap="BytedancePromoterOrderListResult">
        <include refid="SELECTBytedancePromoterOrderListVo"/>
        WHERE order_id = #{orderId}
    </select>


    <select id="getBytedanceOrderList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT *
        FROM (
        SELECT
        IFNULL(author_short_id,'-') AS 'promoterId',
        IFNULL(author_short_name,'-') AS 'promoterNickName',
        count( order_id ) AS 'orderNum',
        sum( CASE WHEN flow_point != 'REFUND' THEN 1 ELSE 0 END ) AS 'validOrderNUm',
        CONCAT(round( sum( CASE WHEN flow_point != 'REFUND' THEN 1 ELSE 0 END ) / count( order_id )*100, 2 ),'%') AS
        'validRadio',
        sum( total_pay_amount )/100 AS 'payAmount',
        sum( CASE WHEN flow_point != 'REFUND' THEN total_pay_amount ELSE 0 END )/100 AS 'validPayAmount',
        sum( CASE WHEN flow_point != 'REFUND' THEN colonel_estimated_commission ELSE 0 END )/100 AS
        'regimentalPromotionAmount',
        author_bind_user_id,
        IFNULL(author_bind_user_name,'-') AS 'userName'
        FROM
        bytedance_promoter_order_list
        WHERE
        1 = 1
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        <if test="promoterId != null and promoterId != ''">
            and author_short_id = #{promoterId}
        </if>
        <if test="promoterNickName != null and promoterNickName != ''">
            and author_short_name like CONCAT(CONCAT('%',#{promoterNickName}),'%')
        </if>
        GROUP BY author_short_id) t
        ORDER BY t.orderNum desc
    </select>

    <select id="getTopOrders" resultType="com.alibaba.fastjson.JSONObject">
        SELECT *
        FROM
        (SELECT product_id AS 'itemId',
        product_name AS 'itemTitle',
        product_img AS 'imageUrl',
        count(order_id) AS 'orderNum'
        FROM bytedance_promoter_order_list
        WHERE author_short_id = #{promoterId}
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        GROUP BY product_id) t
        ORDER BY t.orderNum desc limit 3
    </select>


    <select id="bytedanceBDReportList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.collectSampleId,
        t1.collectSampleName,
        t2.promoterCount,
        t3.sampleCount,
        round(t3.sampleCount / t2.promoterCount * 100, 2) AS 'sampleRate',
        t2.promoterCount - t3.sampleCount AS 'notSampleCount',
        t1.orderNum,
        t1.validOrderNum,
        t1.validOrderRate,
        t1.orderAmount,
        t1.regimentalPromotionAmount,
        t1.totalRegimentalSettleAmount,
        t4.sendTotalCount,
        t4.sendPromoterCount,
        t4.sendItemCount,
        t1.voucherOrderNum,
        t1.voucherValidOrderNum,
        t1.voucherOrderAmount,
        t1.voucherRegimentalPromotionAmount,
        t1.voucherTotalRegimentalSettleAmount,
        IFNULL(t5.promoters_count,'-') AS 'promotersCount'
        FROM (
        SELECT author_bind_user_id AS 'collectSampleId',
        author_bind_user_name AS 'collectSampleName',
        count(order_id) AS 'orderNum',
        IFNULL(count(CASE WHEN sample_voucher_status = 2 THEN 1 ELSE 0 END), 0) AS 'voucherOrderNum',
        t0.validOrderNUm AS 'validOrderNUm',
        IFNULL(SUM(CASE WHEN sample_voucher_status = 2 THEN 1 ELSE 0 END), 0) AS 'voucherValidOrderNum',
        CONCAT(round(t0.validOrderNUm / count(order_id) * 100, 2),'%') AS 'validOrderRate',
        sum(total_pay_amount)/100 AS 'orderAmount',
        IFNULL(round(SUM(CASE WHEN sample_voucher_status = 2 THEN total_pay_amount ELSE 0 END) / 100,2), 0) AS
        'voucherOrderAmount',
        sum(colonel_estimated_commission)/100 AS 'regimentalPromotionAmount',
        IFNULL(SUM(CASE WHEN sample_voucher_status = 2 THEN colonel_estimated_commission ELSE 0 END)/100, 0)
        AS'voucherRegimentalPromotionAmount',
        sum(colonel_real_commission)/100 AS 'totalRegimentalSettleAmount',
        IFNULL(SUM(CASE WHEN sample_voucher_status = 2 THEN colonel_real_commission ELSE 0 END)/100, 0) AS
        'voucherTotalRegimentalSettleAmount'
        FROM ruixuan.bytedance_promoter_order_list t
        LEFT JOIN (
        SELECT author_bind_user_id AS 'collectSampleId',
        sum(CASE WHEN flow_point != 'REFUND' THEN 1 ELSE 0 END) AS 'validOrderNUm'
        FROM ruixuan.bytedance_promoter_order_list
        WHERE author_bind_user_id is not null
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        <if test="collectSampleId != null and collectSampleId != ''">
            and author_bind_user_id = #{collectSampleId}
        </if>
        GROUP BY author_bind_user_id
        ) t0 ON t.author_bind_user_id= t0.collectSampleId
        WHERE author_bind_user_id is not null
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        <if test="collectSampleId != null and collectSampleId != ''">
            and author_bind_user_id = #{collectSampleId}
        </if>
        GROUP BY author_bind_user_id) t1
        LEFT JOIN (
        SELECT user_id AS 'userId',
        count(distinct promoter_id) AS 'promoterCount'
        FROM kuaishou_promoter
        GROUP BY user_id) t2
        on t1.collectSampleId = t2.userId
        LEFT JOIN (
        SELECT collect_sample_id AS 'collectSampleId',
        count(distinct promoter_id) AS 'sampleCount'
        FROM kuaishou_item_collect_samples
        WHERE collect_sample_status > 2
        GROUP BY collect_sample_id) t3
        on t2.userId = t3.collectSampleId
        LEFT JOIN (
        SELECT collect_sample_id AS 'collectSampleId',
        count(1) AS 'sendTotalCount',
        count(distinct promoter_id) AS 'sendPromoterCount',
        count(distinct item_id) AS 'sendItemCount'
        FROM kuaishou_item_collect_samples
        WHERE collect_sample_status > 2
        <if test="collectSampleId != null and collectSampleId != ''">
            and collect_sample_id = #{collectSampleId}
        </if>
        <if test="start != null and start != ''">
            and create_time &gt;= str_to_date(CONCAT(date_format(#{start}, '%Y-%m-%d'), '00:00:01'), '%Y-%m-%d
            %H:%i:%s')
        </if>
        <if test="end != null and end != ''">
            and create_time &lt;= str_to_date(CONCAT(date_format(#{end}, '%Y-%m-%d'), '23:59:59'), '%Y-%m-%d %H:%i:%s')
        </if>
        GROUP BY collect_sample_id
        ) t4
        on t1.collectSampleId = t4.collectSampleId
        LEFT JOIN (
        SELECT user_id,promoters_count
        FROM user_promoters_count
        WHERE media_id = 1
        ) t5
        on t1.collectSampleId = t5.user_id
        ORDER BY t1.orderNum desc
    </select>

    <select id="anchorBytedanceOrderDetail" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t.*,
        t2.status,
        (
        case
        when t3.activity_item_status = 1 then '待审核'
        when t3.activity_item_status = 2 then '已上架'
        when t3.activity_item_status = 3 then '审核拒绝'
        when t3.activity_item_status = 4 then '活动失效'
        when t3.activity_item_status = 5 then '商品下架'
        else ''
        end
        ) AS 'activityItemStatus'
        FROM (
        SELECT
        tt.product_name AS 'itemTitle',
        tt.product_id AS 'itemId',
        tt.total_pay_amount/100 AS 'reservePrice',
        tt.product_img 'imageUrl',
        count(order_id) AS 'orderNum',
        sum( CASE WHEN flow_point != 'REFUND' THEN 1 ELSE 0 END ) AS 'validOrderNUm',
        CONCAT(round(sum(case when flow_point != 'REFUND' then 1 else 0 end) / count(order_id)*100,2),'%') as
        'validRadio',
        sum(case when flow_point != 'REFUND' then total_pay_amount else 0 end)/100 AS 'payAmount',
        CONCAT(round(colonel_commission_rate/100,2),'%') AS 'regimentalPromotionRate'
        FROM bytedance_promoter_order_list tt
        WHERE 1= 1
        <if test="promoterId != null and promoterId != ''">
            and tt.author_short_id = #{promoterId}
        </if>

        <if test="start != null and start != ''">
            and tt.pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and tt.pay_success_time &lt;= #{end}
        </if>
        <if test="itemId != null and itemId != ''">
            and tt.product_id = #{itemId}
        </if>
        <if test="itemTitle != null and itemTitle != ''">
            and tt.product_name like CONCAT(CONCAT('%',#{itemTitle}),'%')
        </if>
        GROUP BY tt.product_id) t
        LEFT JOIN
        ( SELECT item_id AS 'itemId', collect_sample_status AS 'status' FROM
        kuaishou_item_collect_samples WHERE promoter_id = #{promoterId}) t2
        on t.itemId = t2.itemId
        LEFT JOIN kuaishou_item_list t3 ON t.itemId = t3.item_id
        ORDER BY t.orderNum desc
    </select>

    <select id="bytedanceItemList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.product_bind_user_id AS 'userId',
        t1.product_bind_user_name AS 'userName',
        t1.product_id AS 'itemId',
        t1.total_pay_amount/100 AS 'reservePrice',
        t1.product_name AS 'itemTitle',
        t1.product_img AS 'imageUrl',
        count(t1.order_id) AS 'orderNum',
        sum(case when t1.flow_point != 'REFUND' then 1 else 0 end) AS 'validOrderNUm',
        CONCAT(round(sum(case when t1.flow_point != 'REFUND' then 1 else 0 end) / count(t1.order_id)*100, 2),'%') as
        'validRadio',
        CONCAT(Round(t1.commission_rate/100,2),'%') AS 'itemCommissionRate'
        FROM bytedance_promoter_order_list t1
        WHERE 1 = 1
        <if test="start != null and start != ''">
            and t1.pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and t1.pay_success_time &lt;= #{end}
        </if>
        <if test="itemId != null and itemId != ''">
            and t1.product_id = #{itemId}
        </if>
        <if test="itemTitle != null and itemTitle != ''">
            and t1.product_name like CONCAT(CONCAT('%',#{itemTitle}),'%')
        </if>
        <if test="userId != null and userId != ''">
            and t1.product_bind_user_id = #{userId}
        </if>
        GROUP BY t1.product_id
        ORDER BY ${fieId} ${sort}
    </select>

    <select id="adminBytedanceReportList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT *
        FROM (
        SELECT
        t2.product_bind_user_id AS 'userId',
        t2.product_bind_user_name AS 'userName',
        count(distinct t2.product_id) AS 'itemCount',
        count(t2.order_id) AS 'orderNum',
        sum(case when t2.flow_point != 'REFUND' then 1 else 0 end) AS 'validOrderNUm',
        CONCAT(round(sum(case when t2.flow_point != 'REFUND' then 1 else 0 end) / count(t2.order_id)*100, 2),'%') as
        'validRadio',
        sum(case when t2.flow_point = 'REFUND' then 1 else 0 end) AS 'invalidOrderNUm',
        sum(case when t2.flow_point != 'REFUND' then t2.total_pay_amount else 0 end)/100 AS 'payAmount',
        sum(case when t2.flow_point != 'REFUND' then t2.colonel_estimated_commission
        else 0 end)/100 AS 'regimentalPromotionAmount',
        sum(case when t2.flow_point != 'REFUND' then t2.colonel_real_commission else 0 end)/100 as
        'totalRegimentalSettleAmount'
        FROM bytedance_promoter_order_list t2
        WHERE product_bind_user_id is not null
        <if test="start != null and start != ''">
            and t2.pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and t2.pay_success_time &lt;= #{end}
        </if>
        GROUP BY t2.product_bind_user_id) t
        LEFT JOIN
        (
        SELECT item_create_id,count(1) AS 'sendSampleCount' FROM kuaishou_item_collect_samples
        WHERE courier_number_time >= CONCAT(#{start},' 00:00:00')
        and courier_number_time &lt;= CONCAT(#{end},' 23:59:59')
        GROUP BY item_create_id
        ) t1 on t.userId = t1.item_create_id
        WHERE 1 =1
        <if test="userId != null and userId != ''">
            and t.userId = #{userId}
        </if>
        ORDER BY t.orderNum desc
    </select>

    <select id="adminBytedanceReportListByBind" resultType="com.alibaba.fastjson.JSONObject">
        SELECT * FROM (
        SELECT
        t1.user_id AS 'userId',
        t1.user_name AS 'userName',
        count(distinct t1.item_id) AS 'itemCount',
        count(t2.order_id) AS 'orderNum',
        sum(case when t2.flow_point != 'REFUND' then 1 else 0 end) AS 'validOrderNUm',
        CONCAT(round(sum(case when t2.flow_point != 'REFUND' then 1 else 0 end) / count(t2.order_id)*100, 2),'%') as
        'validRadio',
        sum(case when t2.flow_point = 'REFUND' then 1 else 0 end) AS 'invalidOrderNUm',
        sum(case when t2.flow_point != 'REFUND' then t2.total_pay_amount else 0 end)/100 AS 'payAmount',
        sum(case when t2.flow_point != 'REFUND' then t2.colonel_estimated_commission
        else 0 end)/100 AS 'regimentalPromotionAmount',
        sum(case when t2.flow_point != 'REFUND' then t2.colonel_real_commission else 0 end)/100 as
        'totalRegimentalSettleAmount'
        FROM kuaishou_item_list t1
        LEFT JOIN (SELECT product_id,
        order_id,
        flow_point,
        total_pay_amount,
        colonel_estimated_commission,
        colonel_real_commission
        FROM bytedance_promoter_order_list
        WHERE pay_success_time &gt;= #{start}
        ) t2
        on t1.item_id = t2.product_id
        WHERE t1.create_time >= CONCAT(#{start},' 00:00:00')
        and t1.create_time &lt;= CONCAT(#{end},' 23:59:59')
        <if test="userId != null and userId != ''">
            and t1.userId = #{userId}
        </if>
        GROUP BY t1.user_id ) t LEFT JOIN (
        SELECT t1.user_id,count(distinct courier_number) AS 'sendSampleCount' FROM kuaishou_item_list t1 LEFT JOIN
        kuaishou_item_collect_samples t2
        on t1.item_id = t2.item_id
        WHERE t1.create_time >= CONCAT(#{start},' 00:00:00')
        and t1.create_time &lt;= CONCAT(#{end},' 23:59:59')
        and t2.courier_number_time >= CONCAT(#{start},' 00:00:00')
        GROUP BY t1.user_id
        ) t2 on t.userId = t2.user_id ORDER BY t.orderNum desc
    </select>


    <select id="getBytedanceTopPromoterByItemId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        *
        FROM
        (
        SELECT author_short_id AS 'promoterId',
        author_short_name AS 'promoterNickName',
        count(order_id) AS 'orderNum'
        FROM bytedance_promoter_order_list
        WHERE product_id = #{itemId}
        AND author_bind_user_id is not null
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        GROUP BY author_bind_user_id) t
        ORDER BY t.orderNum desc
        limit 3
    </select>


    <select id="bytedanceOrderStatistics" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        DATE_FORMAT(t.pay_success_time , '%Y-%m-%d') AS 'date',
        count(t.order_id) AS 'orderNum'
        FROM (
        SELECT
        order_id,
        pay_success_time
        FROM bytedance_promoter_order_list
        WHERE 1 = 1
        <if test="promoterId != null and promoterId != ''">
            and author_short_id = #{promoterId}
        </if>
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        ) t
        GROUP BY DATE_FORMAT(t.pay_success_time , '%Y-%m-%d')
        ORDER BY DATE_FORMAT(t.pay_success_time , '%Y-%m-%d') asc
    </select>


    <select id="bytedanceItemDetail" resultType="com.alibaba.fastjson.JSONObject">
        SELECT *
        FROM (
        SELECT
        author_short_id AS 'promoterId',
        author_short_name AS 'promoterNickName',
        count(order_id) AS 'orderNum',
        sum(case when flow_point = 'REFUND' then total_pay_amount else 0 end) AS 'payAmount',
        CONCAT(round(colonel_commission_rate/100,2),'%') AS 'regimentalPromotionRate',
        sum(case when flow_point = 'REFUND' then 1 else 0 end) AS 'validOrderNUm',
        CONCAT(round(sum(case when flow_point != 'REFUND' then 1 else 0 end) / count(order_id)*100, 2),'%') as
        'validRadio'
        FROM bytedance_promoter_order_list
        WHERE 1= 1
        <if test="itemId != null and itemId != ''">
            and product_id = #{itemId}
        </if>
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        <if test="promoterId != null and promoterId != ''">
            and author_short_id = #{promoterId}
        </if>
        <if test="promoterNickName != null and promoterNickName != ''">
            and author_bind_user_name like CONCAT(CONCAT('%',#{promoterNickName}),'%')
        </if>
        GROUP BY author_bind_user_id) t
        ORDER BY t.orderNum desc
    </select>

    <select id="bytedanceBDTotal" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t2.sampleCount,
        t1.orderNum,
        t1.validOrderNum,
        t1.validOrderRate,
        t1.orderAmount,
        t1.regimentalPromotionAmount,
        t1.totalRegimentalSettleAmount
        FROM (
        SELECT
        count(order_id) AS 'orderNum',
        t0.validOrderNUm,
        CONCAT(round(t0.validOrderNUm / count(order_id) * 100, 2),'%') AS 'validOrderRate',
        sum(total_pay_amount)/100 AS 'orderAmount',
        sum(colonel_estimated_commission)/100 AS 'regimentalPromotionAmount',
        sum(colonel_real_commission)/100 AS 'totalRegimentalSettleAmount'
        FROM ruixuan.bytedance_promoter_order_list t
        LEFT JOIN (
        SELECT author_bind_user_id AS 'collectSampleId',
        sum(CASE WHEN flow_point != 'REFUND' THEN 1 ELSE 0 END) AS 'validOrderNUm'
        FROM ruixuan.bytedance_promoter_order_list
        WHERE author_bind_user_id is not null
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        <if test="collectSampleId != null and collectSampleId != ''">
            and author_bind_user_id = #{collectSampleId}
        </if>
        GROUP BY author_bind_user_id
        ) t0 ON t.author_bind_user_id= t0.collectSampleId
        WHERE 1 = 1
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        <if test="collectSampleId != null and collectSampleId != ''">
            and author_bind_user_id = #{collectSampleId}
        </if>
        ) t1
        join (
        SELECT
        count(distinct item_id) AS 'sampleCount'
        FROM kuaishou_item_collect_samples
        WHERE collect_sample_status > 2
        <if test="collectSampleId != null and collectSampleId != ''">
            and collect_sample_id = #{collectSampleId}
        </if>
        ) t2
    </select>

    <select id="bytedanceBDDetailList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        t1.promoterId,
        t1.promoterName,
        t1.promoterUrl,
        t2.sampleCount,
        t1.orderNum,
        t1.validOrderNum,
        t1.validOrderRate,
        t1.orderAmount,
        t1.regimentalPromotionAmount,
        t1.totalRegimentalSettleAmount
        FROM (
        SELECT
        author_short_id AS 'promoterId',
        author_short_name AS 'promoterName',
        author_short_url AS 'promoterUrl',
        count(order_id) AS 'orderNum',
        t0.validOrderNUm,
        CONCAT(round(t0.validOrderNUm / count(order_id) * 100, 2),'%') AS 'validOrderRate',
        sum(total_pay_amount)/100 AS 'orderAmount',
        sum(colonel_estimated_commission)/100 AS 'regimentalPromotionAmount',
        sum(colonel_real_commission)/100 AS 'totalRegimentalSettleAmount'
        FROM ruixuan.bytedance_promoter_order_list t
        LEFT JOIN (
        SELECT author_bind_user_id AS 'collectSampleId',
        sum(CASE WHEN flow_point != 'REFUND' THEN 1 ELSE 0 END) AS 'validOrderNUm'
        FROM ruixuan.bytedance_promoter_order_list
        WHERE author_short_id is not null
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        <if test="collectSampleId != null and collectSampleId != ''">
            and author_bind_user_id = #{collectSampleId}
        </if>
        GROUP BY author_bind_user_id
        ) t0 ON t.author_bind_user_id= t0.collectSampleId
        WHERE 1 = 1
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        <if test="collectSampleId != null and collectSampleId != ''">
            and author_bind_user_id = #{collectSampleId}
        </if>
        GROUP BY author_short_id) t1
        LEFT JOIN (
        SELECT
        promoter_id AS 'promoterId',
        count(distinct item_id) AS 'sampleCount'
        FROM kuaishou_item_collect_samples
        WHERE collect_sample_status > 2
        <if test="collectSampleId != null and collectSampleId != ''">
            and collect_sample_id = #{collectSampleId}
        </if>
        GROUP BY promoter_id) t2
        on t1.promoterId = t2.promoterId
        ORDER BY ${fieId} ${sort}
    </select>

    <select id="bytedanceUserItemTotal" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        count( t2.order_id ) AS 'orderNum',
        sum( CASE WHEN t2.flow_point != 'REFUND' THEN 1 ELSE 0 END ) AS 'validOrderNUm',
        CONCAT(round( sum( CASE WHEN t2.flow_point != 'REFUND' THEN 1 ELSE 0 END ) / count( t2.order_id )*100, 2),'%')
        AS 'validRadio',
        sum( CASE WHEN t2.flow_point = 'REFUND' THEN 1 ELSE 0 END ) AS 'invalidOrderNUm',
        sum( CASE WHEN t2.flow_point != 'REFUND' THEN t2.total_pay_amount ELSE 0 END ) AS 'payAmount',
        sum( CASE WHEN t2.flow_point != 'REFUND' THEN t2.colonel_estimated_commission ELSE 0 END ) AS
        'regimentalPromotionAmount',
        sum( CASE WHEN t2.flow_point != 'REFUND' THEN t2.colonel_real_commission ELSE 0 END ) AS
        'totalRegimentalSettleAmount'
        FROM bytedance_promoter_order_list t2
        WHERE
        1 = 1
        <if test="start != null and start != ''">
            and t2.pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and t2.pay_success_time &lt;= #{end}
        </if>
        <if test="userId != null and userId != ''">
            AND t2.product_bind_user_id = #{userId}
        </if>
    </select>


    <select id="bytedanceUserItemDetail" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        product_bind_user_id AS 'user_id',
        product_bind_user_name AS 'userName',
        product_id AS 'itemId',
        product_name AS 'itemTitle',
        product_img AS 'imageUrl',
        total_pay_amount/100 AS 'reservePrice',
        count( order_id ) AS 'orderNum',
        sum( CASE WHEN flow_point != 'REFUND' THEN 1 ELSE 0 END ) AS 'validOrderNUm',
        CONCAT(round( sum( CASE WHEN flow_point != 'REFUND' THEN 1 ELSE 0 END ) / count( order_id )*100, 2),'%') AS
        'validRadio',
        sum( CASE WHEN flow_point = 'REFUND' THEN 1 ELSE 0 END ) AS 'invalidOrderNUm',
        round( sum( CASE WHEN flow_point != 'REFUND' THEN total_pay_amount ELSE 0 END )/ 100, 2 ) AS 'payAmount',
        CONCAT(round(colonel_commission_rate/100,2),'%') AS 'regimentalPromotionRate',
        round( sum( CASE WHEN flow_point != 'REFUND' THEN colonel_estimated_commission ELSE 0 END ) / 100, 2 ) AS
        'regimentalPromotionAmount',
        round( sum( CASE WHEN flow_point != 'REFUND' THEN colonel_real_commission ELSE 0 END ) / 100, 2 ) AS
        'totalRegimentalSettleAmount',
        CONCAT(round(colonel_commission_rate/100,2),'%') AS 'itemCommissionRate'
        FROM bytedance_promoter_order_list
        WHERE
        product_bind_user_id is not null
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        <if test="itemId != null and itemId != ''">
            and product_id = #{itemId}
        </if>
        <if test="itemTitle != null and itemTitle != ''">
            and product_name like CONCAT(CONCAT('%',#{itemTitle}),'%')
        </if>
        <if test="userId != null and userId != ''">
            and product_bind_user_id = #{userId}
        </if>
        GROUP BY product_id
        ORDER BY orderNum desc
    </select>

    <select id="exportBytedancePromoter" resultType="com.alibaba.fastjson.JSONObject">
        SELECT *
        FROM (
        SELECT
        author_short_id AS 'promoterId',
        author_short_name AS 'promoterNickName',
        product_id AS 'itemId',
        product_name AS 'itemTitle',
        product_img AS 'imageUrl',
        count(order_id) AS 'orderNum',
        sum(case when flow_point != 'REFUND' then 1 else 0 end) AS 'validOrderNUm',
        CONCAT(round(sum(case when flow_point != 'REFUND' then 1 else 0 end) / count(order_id)*100, 2),'%') AS
        'validRadio'
        FROM bytedance_promoter_order_list
        WHERE author_short_id is not null
        <if test="start != null and start != ''">
            and pay_success_time &gt;= #{start}
        </if>
        <if test="end != null and end != ''">
            and pay_success_time &lt;= #{end}
        </if>
        <if test="promoterId != null and promoterId != ''">
            and author_short_id = #{promoterId}
        </if>
        <if test="promoterNickName != null and promoterNickName != ''">
            and author_short_name like CONCAT(CONCAT('%',#{promoterNickName}),'%')
        </if>
        <if test="itemId != null and itemId != ''">
            and product_id = #{itemId}
        </if>
        <if test="itemTitle != null and itemTitle != ''">
            and product_name like CONCAT(CONCAT('%',#{itemTitle}),'%')
        </if>
        GROUP BY author_short_id,product_id) t
        ORDER BY t.promoterId,t.itemId desc
    </select>

</mapper>