package com.hfkj.dao;

import com.hfkj.openapi.v1.model.TradeBillsModel;
import com.hfkj.openapi.v1.model.TradeDetailBillsModel;
import com.hfkj.openapi.v1.model.TradeRefundBillsModel;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @className: OpenApiTradeOrderMapper
 * @author: HuRui
 * @date: 2023/5/25
 **/
@Repository
public interface OpenApiTradeOrderMapper {

    @Select({
            "<script>" +
                    " SELECT " +
                    " a.pay_time payTime," +
                    " a.mer_no merchantNo," +
                    " a.platform_trade_no platformTradeNo," +
                    " a.acc_trade_no accTradeNo," +
                    " a.out_trade_no outTradeNo, " +
                    " a.pay_user_id payUserId, " +
                    " a.trans_type tradeType," +
                    " case a.pay_mode" +
                    " when \"WECHAT\" then '微信'" +
                    " when \"ALIPAY\" then '支付宝'" +
                    " when \"UQRCODEPAY\" then '云闪付'" +
                    " end payMode," +
                    " \"交易成功\" as tradeStatus," +
                    " a.trade_amount tradeAmount," +
                    " a.trade_actual_amount tradeActualAmount," +
                    " case a.refund_type" +
                    " when 1 then '全额退款' " +
                    " when 2 then '部分退款'" +
                    " end refundType," +
                    " a.refund_price refundPrice," +
                    " subject subject," +
                    " case a.create_type" +
                    " when 1 then '扫一扫'" +
                    " when 2 then '被扫'" +
                    " when 3 then 'API'" +
                    " end createType" +
                    " FROM `bs_trade_order` a" +
                    " where a.mer_no = #{merNo} " +
                    " <![CDATA[ and pay_time >= #{billDateS} ]]>" +
                    " <![CDATA[ and pay_time <= #{billDateE} ]]>" +
                    " and a.`status` in (3,6)" +
                    " GROUP BY a.id" +
            " </script>"
    })
    List<TradeBillsModel> queryTradeBills(@Param("merNo") String merNo,@Param("billDateS") String billDateS, @Param("billDateE") String billDateE);

    @Select({
            "<script>" +
            " select  " +
            " a.create_time createTime, " +
            " a.refund_no refundNo, " +
            " case a.`status` " +
            " when 1 then '退款中' " +
            " when 2 then '退款成功' " +
            " when 3 then '退款失败' " +
            " end refundStatus, " +
            " a.refund_time refundTime, " +
            " a.refund_amount refundAmount, " +
            " b.out_trade_no outTradeNo, " +
            " b.acc_trade_no accTradeNo, " +
            " b.mer_no merNo, " +
            " b.acc_settle_amount accSettleAmount, " +
            " a.refund_reason refundReason, " +
            " a.operator_name operatorName " +
            " from bs_trade_order_refund a " +
            " LEFT JOIN bs_trade_order b on b.out_trade_no = a.out_trade_no " +
            " where <![CDATA[ a.`status` <> 0 ]]> and b.mer_no = #{merNo}" +
            " <![CDATA[ and a.create_time >= #{billDateS} ]]>" +
            " <![CDATA[ and a.create_time <= #{billDateE} ]]>" +
            " ORDER BY a.create_time desc" +
            "</script>"
    })
    List<TradeRefundBillsModel> queryRefundBills(@Param("merNo") String merNo, @Param("billDateS") String billDateS, @Param("billDateE") String billDateE);

    @Select("<script>" +
            " SET @i=0;" +
            " select" +
            " @i := @i+1 serialNumber," +
            " mer_no merchantNo," +
            " mer_name merchantName," +
            " trade_time tradeTime," +
            " case trade_type" +
            " when 2 then CONCAT('-',trade_amount)" +
            " else trade_amount end tradeAmount," +
            " case trade_type" +
            " when 2 then CONCAT('-',shd_fee_amt)" +
            " else shd_fee_amt end shdFeeAmt," +
            " case trade_type" +
            " when 2 then CONCAT('-',settle_amount)" +
            " else settle_amount end settleAmount," +
            " case trade_type " +
            " when 1 then '交易支付' " +
            " when 2 then '交易退款'" +
            " end tradeType," +
            " case pay_mode " +
            " when 'WECHAT' then '微信' " +
            " when 'ALIPAY' then '支付宝'" +
            " when 'UQRCODEPAY' then '云闪付'" +
            " end payMode," +
            " out_trade_no outTradeNo," +
            " acc_trade_no accTradeNo," +
            " settle_date settleDate," +
            " CONCAT(fee_rate,'%') feeRate," +
            " org_out_trade_no orgOutTradeNo" +
            " from bs_trade_settle " +
            " where `status` = 1" +
            " and mer_no = #{merNo}" +
            " and settle_date BETWEEN #{billDateS} and #{billDateE}  "+
            " ORDER BY trade_time " +
            "</script>")
    List<TradeDetailBillsModel> queryTradeSettleBills(@Param("merNo") String merNo, @Param("billDateS") String billDateS, @Param("billDateE") String billDateE);

}
