<?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.yunzhi.marketing.redpack.mapper.TaOrgOrderMapper">

    <select id="orderListByRechargeOrRefund" resultType="com.yunzhi.marketing.redpack.entity.TaOrgOrder">
        select
        t.order_id,
        t.org_id,
        t.trade_no,
        t.amount,
        e.available_balance,
        b.name as miniAppName,
        t.is_offline,
        t.create_date,
        group_concat(c.url) as certificateUrl,
        <if test="orderType == 'refund'">
            d.audit_status,
            d.audit_result,
        </if>
        t.trading_status,
        t.pay_date
        From ta_org_order t
        left join ta_org_order_detail a on t.order_id = a.order_id
        left join ta_miniapp b on t.org_id = b.org_id
        left join ta_org_account_certificate c on t.order_id = c.order_id
        left join ta_org_account e on t.org_id = e.org_id
        <if test="orderType == 'refund'">
            left join ta_org_refund_application d on d.org_id = t.org_id and t.order_id = d.order_id
        </if>
        where
        a.item_type = #{orderType}
        <if test="orgId != null and orgId != ''">
            and t.org_id = #{orgId}
        </if>
        <if test="tradeNo != null and tradeNo != ''">
            and t.trade_no like concat('%', #{tradeNo}, '%')
        </if>
        <if test="isOffline != null">
            and t.is_offline = #{isOffline}
        </if>
        <if test="tradingStatus != null and tradingStatus != ''">
            and t.trading_status = #{tradingStatus}
        </if>
        <if test="auditStatus != null and auditStatus != ''">
            and d.audit_status = #{auditStatus}
        </if>
        <if test="startDate != null and startDate != ''">
            and t.create_date >= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and t.create_date &lt;= #{endDate}
        </if>
        <if test="payStartDate != null and payStartDate != ''">
            and t.pay_date >= #{payStartDate}
        </if>
        <if test="payEndDate != null and payEndDate != ''">
            and t.pay_date &lt;= #{payEndDate}
        </if>
        <if test="miniAppName != null and miniAppName != ''">
            and b.name like concat('%', #{miniAppName}, '%')
        </if>
        group by t.order_id order by t.create_date desc
    </select>

    <select id="orderListByConsume" resultType="com.yunzhi.marketing.redpack.entity.TaOrgOrder">
        select
        t.order_id,
        t.org_id,
        t.trade_no,
        b.name as miniAppName,
        t.amount,
        t.create_date,
        c.phone,
        t.trading_status,
        d.`name` as activityName,
        t.pay_date,
        a.target_type as consumeType
        From ta_org_order t
        left join ta_org_order_detail a on t.order_id = a.order_id
        left join ta_miniapp b on t.org_id = b.org_id
        left join ta_person c on a.receiver_id = c.person_id
        left join ta_red_packet d on a.target_id = d.activity_id
        where
        a.item_type = #{orderType}
        <if test="orgId != null and orgId != ''">
            and t.org_id = #{orgId}
        </if>
        <if test="tradeNo != null and tradeNo != ''">
            and t.trade_no like concat('%', #{tradeNo}, '%')
        </if>
        <if test="tradingStatus != null and tradingStatus != ''">
            and t.trading_status = #{tradingStatus}
        </if>
        <if test="startDate != null and startDate != ''">
            and t.create_date > #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and t.create_date &lt; #{endDate}
        </if>
        <if test="receivePhone != null and receivePhone != ''">
            and c.phone like concat('%', #{receivePhone}, '%')
        </if>
        <if test="itemType != null and itemType != ''">
            and a.item_type = #{itemType}
        </if>
        <if test="miniAppName != null and miniAppName != ''">
            and b.name like concat('%', #{miniAppName}, '%')
        </if>
        GROUP BY t.order_id order by t.create_date desc
    </select>

    <select id="rechargeListByConditionExport" resultType="com.yunzhi.marketing.redpack.entity.RechargeOrder">
        select
        t.trade_no as tradeNo,
        FORMAT(t.amount/100, 2) as amount,
        b.name as miniAppName,
        if(t.is_offline = 1, '运营手工充值', '业务线上充值') as isOffline,
        t.create_date as createDate,
        if(t.trading_status = 'processing', '待支付', if(t.trading_status = 'success', '已支付','失败')) as tradingStatus,
        if(t.is_offline = 0, t.pay_date, '') as payDate
        From ta_org_order t
        left join ta_org_order_detail a on t.order_id = a.order_id
        left join ta_miniapp b on t.org_id = b.org_id
        where
        a.item_type = #{itemType}
        <if test="tradeNo != null and tradeNo != ''">
            and t.trade_no like concat('%', #{tradeNo}, '%')
        </if>
        <if test="isOffline != null">
            and t.is_offline = #{isOffline}
        </if>
        <if test="tradingStatus != null ">
            and t.trading_status = #{tradingStatus}
        </if>
        <if test="startDate != null and startDate != ''">
            and t.create_date > #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and t.create_date &lt; #{endDate}
        </if>
        <if test="miniAppName != null and miniAppName != ''">
            and b.name like concat('%', #{miniAppName}, '%')
        </if>
        <if test="orgId != null and orgId != ''">
            and t.org_id = #{orgId}
        </if>
        GROUP BY t.order_id order by t.create_date desc
    </select>

    <select id="orderListByConsumeExport" resultType="com.yunzhi.marketing.redpack.entity.ConsumeOrder">
        select
        t.trade_no as tradeNo,
        b.name as miniAppName,
        FORMAT(t.amount/100, 2) as amount,
        t.create_date as createDate,
        c.phone as phone,
        if(t.trading_status = 'success', '成功','失败') as tradingStatus,
        "红包" as consumeType,
        d.`name` as activityName
        From ta_org_order t
        left join ta_org_order_detail a on t.order_id = a.order_id
        left join ta_miniapp b on t.org_id = b.org_id
        left join ta_person c on a.receiver_id = c.person_id
        left join ta_red_packet d on a.target_id = d.activity_id
        where
        a.item_type = #{itemType}
        <if test="tradeNo != null and tradeNo != ''">
            and t.trade_no like concat('%', #{tradeNo}, '%')
        </if>
        <if test="tradingStatus != null and tradingStatus != ''">
            and t.trading_status = #{tradingStatus}
        </if>
        <if test="startDate != null and startDate != ''">
            and t.create_date > #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and t.create_date &lt; #{endDate}
        </if>
        <if test="receivePhone != null and receivePhone != ''">
            and c.phone like concat('%', #{receivePhone}, '%')
        </if>
        <if test="miniAppName != null and miniAppName != ''">
            and b.name like concat('%', #{miniAppName}, '%')
        </if>
        <if test="orgId != null and orgId != ''">
            and t.org_id = #{orgId}
        </if>
        GROUP BY t.order_id order by t.create_date desc
    </select>

    <select id="listCzOrderByCondition" resultType="com.yunzhi.marketing.redpack.entity.TaOrgOrder">
        SELECT
          *
        FROM
          ta_org_order t
        INNER JOIN ta_org_order_detail t2 ON t.order_id = t2.order_id
          AND t2.item_type = 'recharge'
        WHERE
            t.org_id = #{orgId}
            AND t.`status` >- 1
            <if test = " orderId != null and orderId != ''">
                AND t.order_id like concat( '%',#{orderId},'%')
            </if>
            <if test = " tradeNo != null and tradeNo != ''">
                AND t.trade_no like concat( '%',#{tradeNo},'%')
            </if>
            <if test = " wxOrderId != null and wxOrderId != ''">
                AND t.wx_order_id like concat ('%',#{wxOrderId},'%')
            </if>
            <if test = " isOffline != null">
                AND t.is_offline = #{isOffline}
            </if>
            <if test = " payStartTime != null ">
                AND t.pay_date &gt;= #{payStartTime}
            </if>
            <if test = " payEndTime != null ">
                AND t.pay_date &lt;= #{payEndTime}
            </if>
            <if test = " tradingStatus != null and tradingStatus != ''">
                AND t.trading_status = #{tradingStatus}
            </if>
        ORDER BY
        t.create_date DESC
    </select>

    <select id="exportCzOrderByCondition" resultType="com.yunzhi.marketing.excel.redPacket.AccountPayRecordExport">
        SELECT
            t.trade_no,
            FORMAT(t.amount/100,2) amount,
            ( CASE t.is_offline WHEN TRUE THEN '运营手工充值' ELSE '业务线上充值' END ) isOffline,
            t.create_date,
            ( CASE t.trading_status WHEN 'success' THEN '已支付' WHEN 'processing' THEN '待支付' ELSE '失败' END ) tradingStatus,
            t.pay_date
        FROM
          ta_org_order t
        INNER JOIN ta_org_order_detail t2 ON t.order_id = t2.order_id AND t2.item_type = 'recharge'
        WHERE
            t.org_id = #{orgId}
            AND t.`status` >- 1
            <if test = " orderId != null and orderId != ''">
                AND t.order_id like concat( '%',#{orderId},'%')
            </if>
            <if test = " tradeNo != null and tradeNo != ''">
                AND t.trade_no like concat( '%',#{tradeNo},'%')
            </if>
            <if test = " wxOrderId != null and wxOrderId != ''">
                AND t.wx_order_id like concat ('%',#{wxOrderId},'%')
            </if>
            <if test = " isOffline != null">
                AND t.is_offline = #{isOffline}
            </if>
            <if test = " payStartTime != null ">
                AND t.pay_date &gt;= #{payStartTime}
            </if>
            <if test = " payEndTime != null ">
                AND t.pay_date &lt;= #{payEndTime}
            </if>
            <if test = " tradingStatus != null and tradingStatus != '' ">
                AND t.trading_status = #{tradingStatus}
            </if>
        ORDER BY
          t.create_date DESC
    </select>

    <select id="listRefundOrderByCondition" resultType="com.yunzhi.marketing.redpack.entity.TaOrgOrder">
        SELECT
            t.*,
            t3.audit_status,
            t3.audit_result
        FROM
            ta_org_order t
        INNER JOIN ta_org_order_detail t2 ON t.order_id = t2.order_id AND t2.item_type = 'refund'
        LEFT JOIN ta_org_refund_application t3 ON t.order_id = t3.order_id
        WHERE
            t.org_id = #{orgId}
            AND t.`status` > - 1
            <if test=" orderId != null and orderId != ''">
                AND t.order_id like concat( '%',#{orderId},'%')
            </if>
            <if test = " tradeNo != null and tradeNo != ''">
                AND t.trade_no like concat( '%',#{tradeNo},'%')
            </if>
            <if test = " wxOrderId != null and wxOrderId != ''">
                AND t.wx_order_id like concat ('%',#{wxOrderId},'%')
            </if>
            <if test=" auditStatus != null and auditStatus != ''">
                AND t3.audit_status = #{auditStatus}
            </if>
            <if test=" refundStartTime != null">
                AND t.create_date &gt;= #{refundStartTime}
            </if>
            <if test=" refundEndTime != null">
                AND t.create_date &lt;= #{refundEndTime}
            </if>
        ORDER BY
            t.create_date DESC
    </select>

    <select id="exportRefundOrderByCondition" resultType="com.yunzhi.marketing.excel.redPacket.AccountRefundRecordExport">
        SELECT
            t.trade_no,
            FORMAT(t.amount/100,2) amount,
            t.create_date,
            ( CASE t3.audit_status WHEN 'agree' THEN '已退款' WHEN 'unagree' THEN '已驳回' ELSE '已申请' END ) tradingStatus,
            ( CASE t3.audit_status WHEN 'agree' then  '' ELSE t3.audit_result end) audit_result
        FROM
            ta_org_order t
        INNER JOIN ta_org_order_detail t2 ON t.order_id = t2.order_id AND t2.item_type = 'refund'
        LEFT JOIN ta_org_refund_application t3 ON t.order_id = t3.order_id
        WHERE
            t.org_id = #{orgId}
            AND t.`status` > - 1
            <if test=" orderId != null and orderId != ''">
                AND t.order_id like concat( '%',#{orderId},'%')
            </if>
            <if test = " tradeNo != null and tradeNo != ''">
                AND t.trade_no like concat( '%',#{tradeNo},'%')
            </if>
            <if test = " wxOrderId != null and wxOrderId != ''">
                AND t.wx_order_id like concat ('%',#{wxOrderId},'%')
            </if>
            <if test=" auditStatus != null and auditStatus != ''">
                AND t3.audit_status = #{auditStatus}
            </if>
            <if test=" refundStartTime != null">
                AND t.create_date &gt;= #{refundStartTime}
            </if>
            <if test=" refundEndTime != null">
                AND t.create_date &lt;= #{refundEndTime}
            </if>
        ORDER BY
        t.create_date DESC
    </select>

    <select id="countTotalRechargeAmount" resultType="java.lang.Integer">
        select ifnull(sum(t.amount),0) as amount from ta_org_order t
        left join ta_org_order_detail a on t.order_id = a.order_id where t.org_id = #{orgId}
        and a.item_type = 'recharge' and t.trading_status = 'success'
    </select>

    <select id="listRedPacketRecords" resultType="com.yunzhi.marketing.redpack.entity.TaOrgOrder">
        SELECT
            t.*,
            t2.amount detailAmount,
            t3.phone,
            t3.nickname nickname,
            t3.avatarurl
        FROM
            ta_org_order t
            INNER JOIN ta_org_order_detail t2 ON t.order_id = t2.order_id
            LEFT JOIN ta_person t3 ON t2.receiver_id = t3.person_id
        WHERE
            t.org_id = #{orgId}
            AND t2.target_id = #{activityId}
            AND t2.target_type = 'RedPacket'
            AND t2.item_type = 'RedPacket'
            AND t.`status` >- 1
            <if test = " orderId != null and orderId != ''">
                AND t.order_id like concat( '%',#{orderId},'%')
            </if>
            <if test = " tradeNo != null and tradeNo != ''">
                AND t.trade_no like concat( '%',#{tradeNo},'%')
            </if>
            <if test = " wxOrderId != null and wxOrderId != ''">
                AND t.wx_order_id like concat ('%',#{wxOrderId},'%')
            </if>
            <if test="tradingStatus != null and tradingStatus != ''">
                AND t.trading_status = #{tradingStatus}
            </if>
            <if test = "phone != null and phone != ''">
                AND t3.phone like concat ('%',#{phone},'%')
            </if>
        ORDER BY
            t.create_date DESC
    </select>

    <select id = "listConsumeOrderByCondition" resultType="com.yunzhi.marketing.redpack.entity.TaOrgOrder">
        SELECT
            t.*,
            t3.phone,
            t2.item_type consumeType,
            <if test="consumeType == 'RedPacket'">
                t4.name activityName,
            </if>
            t3.avatarurl,
            t3.name,
            t3.nickname
        FROM
            ta_org_order t
            INNER JOIN ta_org_order_detail t2 ON t.order_id = t2.order_id
                <if test="consumeType != null and consumeType != ''">
                    AND t2.item_type = #{consumeType}
                </if>
            LEFT JOIN ta_person t3 on t2.receiver_id = t3.person_id
            <if test="consumeType == 'RedPacket'">
                LEFT JOIN ta_red_packet t4 ON t2.target_id = t4.activity_id
            </if>
        WHERE
            t.org_id = #{orgId}
            AND t.`status` >- 1
            <if test = " orderId != null and orderId != ''">
                AND t.order_id like concat( '%',#{orderId},'%')
            </if>
            <if test = " tradeNo != null and tradeNo != ''">
                AND t.trade_no like concat( '%',#{tradeNo},'%')
            </if>
            <if test = " wxOrderId != null and wxOrderId != ''">
                AND t.wx_order_id like concat ('%',#{wxOrderId},'%')
            </if>
            <if test = " startTime != null ">
                AND t.create_date &gt;= #{startTime}
            </if>
            <if test = " endTime != null">
                AND t.create_date &lt;= #{endTime}
            </if>
            <choose>
                <when test="tradingStatus == 'success'">
                    AND t.trading_status = 'success'
                </when>
                <when test = "tradingStatus == 'fail'">
                    AND t.trading_status == 'fail'
                </when>
            </choose>
            <if test = "phone != null and phone != ''">
                AND t3.phone like concat('%',#{phone},'%')
            </if>
        ORDER BY
            t.create_date DESC

    </select>

    <select id="exportConsumeOrderByCondition" resultType="com.yunzhi.marketing.excel.redPacket.AccountConsumeRecordExport">
        SELECT
            t.trade_no,
            FORMAT(t.amount/100,2) amount,
            ( CASE t2.item_type WHEN 'RedPacket' THEN '红包' ELSE '' END ) consumeType,
            <if test="consumeType == 'RedPacket'">
                t4.name activityName,
            </if>
            t.create_date,
            t3.phone,
            ( CASE t2.STATUS WHEN 1 THEN '成功' WHEN 0 THEN '失败' ELSE '' END ) consumeStatus
        FROM
            ta_org_order t
            INNER JOIN ta_org_order_detail t2 ON t.order_id = t2.order_id
                <if test="consumeType != null and consumeType != ''">
                    AND t2.item_type = #{consumeType}
                </if>
            LEFT JOIN ta_person t3 ON t2.receiver_id = t3.person_id
            <if test="consumeType == 'RedPacket'">
                LEFT JOIN ta_red_packet t4 ON t2.target_id = t4.activity_id
            </if>
        WHERE
            t.org_id = #{orgId}
            AND t.`status` >- 1
            <if test = " orderId != null and orderId != ''">
                AND t.order_id like concat( '%',#{orderId},'%')
            </if>
            <if test = " tradeNo != null and tradeNo != ''">
                AND t.trade_no like concat( '%',#{tradeNo},'%')
            </if>
            <if test = " wxOrderId != null and wxOrderId != ''">
                AND t.wx_order_id like concat ('%',#{wxOrderId},'%')
            </if>
            <if test = " startTime != null ">
                AND t.create_date &gt;= #{startTime}
            </if>
            <if test = " endTime != null">
                AND t.create_date &lt;= #{endTime}
            </if>
            <choose>
                <when test="tradingStatus == 'success'">
                    AND t.trading_status = 'success'
                </when>
                <when test = "tradingStatus == 'fail'">
                    AND t.trading_status == 'fail'
                </when>
            </choose>
            <if test = "phone != null and phone != ''">
                AND t3.phone like concat('%',#{phone},'%')
            </if>

        ORDER BY
            t.create_date DESC
    </select>

    <select id="orderListByRefundExport" resultType="com.yunzhi.marketing.redpack.entity.RefundOrder">
        select
        t.trade_no as tradeNo,
        FORMAT(t.amount/100, 2) as amount,
        b.name as miniAppName,
        t.create_date as createDate,
        d.audit_result as auditResult,
        if(d.audit_status = 'checking', '已申请', if(d.audit_status = 'unAgree', '已驳回','已退款')) as auditStatus
        From ta_org_order t
        left join ta_org_order_detail a on t.order_id = a.order_id
        left join ta_miniapp b on t.org_id = b.org_id
        left join ta_org_refund_application d on d.org_id = t.org_id and t.order_id = d.order_id
        where
        a.item_type = #{itemType}
        <if test="tradeNo != null and tradeNo != ''">
            and t.trade_no like concat('%', #{tradeNo}, '%')
        </if>
        <if test="startDate != null and startDate != ''">
            and t.create_date > #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and t.create_date &lt; #{endDate}
        </if>
        <if test="miniAppName != null and miniAppName != ''">
            and b.name like concat('%', #{miniAppName}, '%')
        </if>
        <if test="orgId != null and orgId != ''">
            and t.org_id = #{orgId}
        </if>
        <if test="auditStatus != null and auditStatus != ''">
            and d.audit_status = #{auditStatus}
        </if>
        GROUP BY t.order_id order by t.create_date desc
    </select>

    <select id = "getRedPacketSendInfo" resultType="map">

        SELECT
        count( * ) sendNum,
        COALESCE(sum( t3.amount ),0) sendTotalAmount
    FROM
        ta_org_order_detail t3
    WHERE
        t3.target_type = #{targetType}
        AND t3.target_id = #{targetId}
        and t3.org_id = #{orgId}
    </select>

</mapper>
