package com.czg.dto.req;

import com.alibaba.fastjson2.annotation.JSONField;
import com.czg.PayCst;
import com.czg.pay.PayNotifyRespDTO;
import lombok.Data;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author yjjie
 * @date 2025/12/23 22:21
 */
@Data
public class WechatPayNotifyDataDto {

    /**
     * 公众账号ID
     */
    private String appid;

    /**
     * 商户号
     */
    private String mchid;

    /**
     * 商户订单号
     */
    @JSONField(name = "out_trade_no")
    private String outTradeNo;

    /**
     * 微信支付订单号
     */
    @JSONField(name = "transaction_id")
    private String transactionId;

    /**
     * 交易类型
     * JSAPI：公众号支付、小程序支付
     * NATIVE：Native支付
     * APP：APP支付
     * MICROPAY：付款码支付
     * MWEB：H5支付
     * FACEPAY：刷脸支付
     */
    @JSONField(name = "trade_type")
    private String tradeType;

    /**
     * 交易状态
     * SUCCESS—支付成功
     * REFUND—转入退款
     * NOTPAY—未支付
     * CLOSED—已关闭
     * REVOKED—已撤销（刷卡支付）
     * USERPAYING--用户支付中
     * PAYERROR--支付失败(其他原因，如银行返回失败)
     */
    @JSONField(name = "trade_state")
    private String tradeState;

    /**
     * 交易状态描述
     */
    @JSONField(name = "trade_state_desc")
    private String tradeStateDesc;

    /**
     * 付款银行
     */
    @JSONField(name = "bank_type")
    private String bankType;

    /**
     * 商户数据包
     */
    private String attach;

    /**
     * 支付完成时间
     */
    @JSONField(name = "success_time")
    private String successTime;

    /**
     * 金额
     */
    private Amount amount;

    @Data
    public static class Amount {
        /**
         * 用户支付金额
         */
        @JSONField(name = "payer_total")
        private Integer payerTotal;

        /**
         * 货币类型
         */
        private String currency;

        /**
         * 总金额
         */
        private Integer total;
    }

    public boolean isSuccess() {
        return "SUCCESS".equals(tradeState);
    }

    public boolean isClosed() {
        return "CLOSED".equals(tradeState);
    }

    public Long getPayAmount() {
        return Long.valueOf(amount.getTotal());
    }

    public PayNotifyRespDTO convertToPayNotifyRespDTO() {
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(successTime);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = zonedDateTime.format(outputFormatter);
        PayNotifyRespDTO respDTO = new PayNotifyRespDTO()
                .setMchOrderNo(outTradeNo)
                .setThirdOrderNo(transactionId)
                .setAmount(getPayAmount())
                .setPlatform(PayCst.Type.WECHAT)
                .setExtData(attach)
                .setPaySuccessTime(time)
                .setErrorMsg(tradeStateDesc);

        switch (tradeState) {
            case "SUCCESS":
                respDTO.setStatus("TRADE_SUCCESS");
                break;
            case "CLOSED":
                respDTO.setStatus("TRADE_CLOSE");
                break;
                case "USERPAYING":
                respDTO.setStatus("TRADE_AWAIT");
                break;
                case "NOTPAY":
                respDTO.setStatus("TRADE_CANCEL");
                break;
            default:
                respDTO.setStatus("TRADE_FAIL");
                break;
        }

        return respDTO;
    }
}
