package cn.pluss.platform.vo;

import cn.pluss.platform.entity.MerchantOrder;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

/**
 * @author DJH , 此类只用于查询，因refundTime在表中不存在
 */
@Data
@EqualsAndHashCode(callSuper = true)
//@TableName("(\n" +
//        "            SELECT t1.*, t2.updateTime refundTime, IFNULL(wo.total_fee - wo.final_fee, IFNULL(wpo.total_fee - wpo.final_fee, 0)) coupon FROM tb_pluss_merchant_order t1\n" +
//        "                LEFT JOIN tb_pluss_merchant_order t2 ON t1.orderNumber = t2.orderNumber AND t1.status = 1 AND t2.status = 2\n" +
//        "               LEFT JOIN `syblife`.ims_tiny_wmall_order wo ON t1.orderNumber = wo.transaction_id AND t1.orderType = 10\n" +
//        "               LEFT JOIN `syblife`.ims_tiny_wmall_paybill_order wpo ON t1.orderNumber = wpo.transaction_id AND t1.orderType != 10" +
//        "        ) t")
@TableName("(\n" +
        "            SELECT t1.*, t2.updateTime refundTime FROM tb_pluss_merchant_order t1\n" +
        "                LEFT JOIN tb_pluss_merchant_order t2 ON t1.orderNumber = t2.orderNumber AND t1.status = 1 AND t2.status = 2\n" +
        "        ) t")
@Accessors(chain = true)
public class MerchantOrderVO extends MerchantOrder {

    /**
     * 结算手续费
     */
    @TableField(exist = false)
    private BigDecimal settlementFee;

    /**
     * 结算手续费描述
     */
    @TableField(exist = false)
    private String settlementFeeDesc;

    /**
     * 提现手续费
     */
    @TableField(exist = false)
    private BigDecimal withdrawFee;

    /**
     * 提现手续费描述
     */
    @TableField(exist = false)
    private String withdrawFeeDesc;

    private Date refundTime;

    /**
     * 优惠金额
     */
//    private BigDecimal coupon;

    public static void setD0Fee(MerchantOrderVO item) {

        Double consumeFee = item.getConsumeFee();
        Double enterFee = item.getEnterFee();
        if (consumeFee == null) {
            return;
        }

        if (enterFee == null) {
            return;
        }

        // 交易手续费
        BigDecimal settlementFee = BigDecimal.valueOf(item.getConsumeFee()).multiply(item.getRate()).divide(BigDecimal.valueOf(10000), RoundingMode.HALF_EVEN);
        item.setSettlementFee(settlementFee.setScale(2, RoundingMode.HALF_EVEN));
        String settlementFeeDesc = item.getConsumeFee() + " × " + item.getRate().divide(BigDecimal.valueOf(100), RoundingMode.HALF_EVEN)+ "%";
        item.setSettlementFeeDesc(settlementFeeDesc);

        if (item.getSettlementType() != 0) {
            return;
        }

        item.setWithdrawFee(new BigDecimal(consumeFee - enterFee).subtract(item.getSettlementFee()).setScale(2, RoundingMode.HALF_UP));

        // 付款金额低于100，但是提现费用达到1毛，则表明当前最低提现费0.1
        if (consumeFee < 100d) {
            if (item.getWithdrawFee().compareTo(new BigDecimal("0.1")) >= 0) {
                item.setWithdrawFeeDesc("最低提现费0.1(以银行卡到账为准)");
                return;
            } else {
                item.setWithdrawFeeDesc(item.getConsumeFee() + " × 0.1%");
            }
        } else {
            item.setWithdrawFeeDesc(item.getConsumeFee() + " × 0.1%");
        }

        if (consumeFee < 10d) {
            if (item.getWithdrawFee().compareTo(new BigDecimal("0.01")) >= 0) {
                item.setWithdrawFeeDesc("最低提现费0.01(以银行卡到账为准)");
            }
        } else {
            item.setWithdrawFeeDesc(item.getConsumeFee() + " × 0.1%");
        }

    }
}
