package com.czg.order.entity;

import com.czg.constants.PayTypeConstants;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

import java.io.Serial;

import jakarta.validation.constraints.NotBlank;
import lombok.*;
import lombok.experimental.Accessors;

/**
 * 支付详情 实体类。
 *
 * @author ww
 * @since 2025-02-15
 */
@Data
@Table("tb_order_payment")
@Accessors(chain = true)
public class OrderPayment implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @Id(keyType = KeyType.Auto)
    private Long id;

    /**
     * 店铺Id
     */
    private Long shopId;

    /**
     * 支付渠道
     * {@link com.czg.constant.PayChannelCst}
     */
    private String channel;

    /**
     * 平台类型
     * {@link com.czg.PayCst.Platform}
     */
    private String platformType;
    /**
     * 来源Id 订单Id或充值id
     */
    private Long sourceId;
    /**
     * 霸王餐充值为 订单id 会员充值为 活动id
     */
    private Long relatedId;

    /**
     * 订单来源类型
     * {@link PayTypeConstants.SourceType}
     *
     */
    private String sourceType;
    /**
     * 支付类型
     * {@link PayTypeConstants.PayType}
     */
    private String payType;

    /**
     * 支付单号  唯一 自己系统的 mchOrderNo
     */
    private String orderNo;

    /**
     * 微信或支付宝支付条码
     */
    private String authCode;

    /**
     * 金额
     */
    private BigDecimal amount;

    /**
     * 三方支付单号 返回结果的 payOrderId
     */
    private String tradeNumber;

//    @Column(onUpdateValue = "now()")
    private LocalDateTime payTime;

    /**
     * 支付相应结果 json
     */
    private String respJson;

    /**
     * 支付状态
     * {@link PayTypeConstants.PayStatus}
     */
    private String payStatus;

    @Column(onInsertValue = "now()")
    private LocalDateTime createTime;

    @Column(onInsertValue = "now()", onUpdateValue = "now()")
    private LocalDateTime updateTime;

    public OrderPayment() {
    }

    /**
     * 订单专用支付
     *
     * @param sourceId 订单Id
     * @param amount   单元 元
     * @param authCode 扫码支付时必填 扫描码
     */
    public static OrderPayment orderPay(@NonNull Long shopId, @NonNull Long sourceId,
                                        @NotBlank String orderNo, @NonNull BigDecimal amount, String authCode) {
        OrderPayment orderPayment = getInstance(shopId, sourceId, PayTypeConstants.SourceType.ORDER, orderNo, amount, authCode, null);
        orderPayment.setPayType(PayTypeConstants.PayType.PAY);
        orderPayment.setPayStatus(PayTypeConstants.PayStatus.INIT);
        return orderPayment;
    }

    /**
     * 初始化支付参数
     *
     * @param sourceId   充值时为会员Id shopUserID
     *                   购买时为商品Id
     * @param sourceType {@link PayTypeConstants.SourceType}  支付来源
     * @param amount     单元 元
     * @param authCode   扫码支付时必填 扫描码
     * @param relatedId  霸王餐充值为 订单id 会员充值为 活动id  充值并支付时 为 MkShopRechargeDetail
     */
    public static OrderPayment pay(@NonNull Long shopId, @NonNull Long sourceId, @NotBlank String sourceType,
                                   @NotBlank String orderNo, @NonNull BigDecimal amount,
                                   String authCode, Long relatedId) {
        OrderPayment orderPayment = getInstance(shopId, sourceId, sourceType, orderNo, amount, authCode, relatedId);
        orderPayment.setPayType(PayTypeConstants.PayType.PAY);
        orderPayment.setPayStatus(PayTypeConstants.PayStatus.INIT);
        return orderPayment;
    }

    public static OrderPayment refund(@NonNull String channel,@NonNull Long shopId, @NonNull Long sourceId, @NotBlank String sourceType,
                                      @NotBlank String orderNo, @NonNull BigDecimal amount, Long relatedId, String platformType) {
        OrderPayment orderPayment = getInstance(shopId, sourceId, sourceType, orderNo, amount, null, relatedId);
        orderPayment.setChannel(channel);
        orderPayment.setPayType(PayTypeConstants.PayType.REFUND);
        orderPayment.setPayStatus(PayTypeConstants.PayStatus.INIT);
        orderPayment.setPlatformType(platformType);
        return orderPayment;
    }

    public static OrderPayment refundCompensate(@NonNull Long shopId, @NonNull Long sourceId, @NotBlank String sourceType,
                                                @NotBlank String orderNo, @NonNull BigDecimal amount, Long relatedId) {
        OrderPayment orderPayment = getInstance(shopId, sourceId, sourceType, orderNo, amount, null, relatedId);
        orderPayment.setPayType(PayTypeConstants.PayType.REFUND_COMPENSATE);
        orderPayment.setPayStatus(PayTypeConstants.PayStatus.INIT);
        return orderPayment;
    }

    private static OrderPayment getInstance(Long shopId, Long sourceId, String sourceType,
                                            String orderNo, BigDecimal amount, String authCode, Long relatedId) {
        OrderPayment orderPayment = new OrderPayment();
        orderPayment.shopId = shopId;
        orderPayment.sourceId = sourceId;
        orderPayment.sourceType = sourceType;
        orderPayment.orderNo = orderNo;
        orderPayment.authCode = authCode;
        orderPayment.amount = amount;
        orderPayment.relatedId = relatedId;
        return orderPayment;
    }
}
