package com.cyksj.model.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 微信H5支付 统一订单
 * https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_20&index=1
 * @author chan
 * @date 2021-10-10 下午9:47
 */
@Getter
@Setter
@Accessors(chain = true)
@JacksonXmlRootElement(localName = "xml")
public class UnifiedOrder {

    /**
     * 公众号appid
     */
    @JacksonXmlCData
    private String appid;

    /**
     * 商户号
     */
    @JacksonXmlCData
    @JsonProperty("mch_id")
    private String mchId;

    /**
     * 随机字符串
     */
    @JacksonXmlCData
    @JsonProperty("nonce_str")
    private String nonceStr;

    /**
     * 签名
     */
    @JacksonXmlCData
    private String sign;

    /**
     * 签名类型 支持HMAC-SHA256和MD5，默认为MD5
     */
    @JacksonXmlCData
    @JsonProperty("sign_type")
    private String signType;

    /**
     * 商品描述
     */
    @JacksonXmlCData
    private String body;

    /**
     * 附加数据 在查询API和支付通知中原样返回 用于处理订单
     */
    @JacksonXmlCData
    private String attach;

    /**
     * 商户订单号
     */
    @JacksonXmlCData
    @JsonProperty("out_trade_no")
    private String outTradeNo;

    /**
     * 订单总金额 单位为分
     */
    @JacksonXmlCData
    @JsonProperty("total_fee")
    private Integer totalFee;

    /**
     * 终端IP
     */
    @JacksonXmlCData
    @JsonProperty("spbill_create_ip")
    private String spbillCreateIp;

    /**
     * 交易结束时间
     */
    @JacksonXmlCData
    @JsonProperty("time_expire")
    private String timeExpire;

    /**
     * 通知地址
     */
    @JacksonXmlCData
    @JsonProperty("notify_url")
    private String notifyUrl;

    /**
     * 交易类型 默认为JSAPI
     */
    @JacksonXmlCData
    @JsonProperty("trade_type")
    private String tradeType;

    /**
     * 用户标识 trade_type=JSAPI 此参数必传
     */
    @JacksonXmlCData
    private String openid;

    @JacksonXmlCData
    @JsonProperty("scene_info")
    private String sceneInfo;

    public String println(String shopKey) {
        StringBuilder sb = new StringBuilder();
        sb.append("appid=" + this.appid +
                "&attach=" + this.attach +
                "&body=" + this.body +
                "&mch_id=" + this.mchId +
                "&nonce_str=" + this.nonceStr +
                "&notify_url=" + this.notifyUrl +
                "&openid=" + this.openid +
                "&out_trade_no=" + this.outTradeNo +
                "&spbill_create_ip=" + this.spbillCreateIp);
        if (this.timeExpire != null) {
            sb.append("&time_expire=" + this.timeExpire);
        }
        sb.append("&total_fee=" + totalFee +
                "&trade_type=JSAPI" +
                "&key=" + shopKey);
        return sb.toString();
    }

    public String H5Println(String shopKey) {
        return "appid=" + this.appid +
                "&attach=" + this.attach +
                "&body=" + this.body +
                "&mch_id=" + this.mchId +
                "&nonce_str=" + this.nonceStr +
                "&notify_url=" + this.notifyUrl +
                "&out_trade_no=" + this.outTradeNo +
                "&scene_info=" + this.sceneInfo +
                "&spbill_create_ip=" + this.spbillCreateIp +
                "&total_fee=" + totalFee +
                "&trade_type=MWEB" +
                "&key=" + shopKey;
    }

    public String nativePrintln(String shopKey) {
        StringBuilder sb = new StringBuilder();
        sb.append("appid=" + this.appid +
                "&attach=" + this.attach +
                "&body=" + this.body +
                "&mch_id=" + this.mchId +
                "&nonce_str=" + this.nonceStr +
                "&notify_url=" + this.notifyUrl +
                "&out_trade_no=" + this.outTradeNo +
                "&spbill_create_ip=" + this.spbillCreateIp);
        if (this.timeExpire != null) {
            sb.append("&time_expire=" + this.timeExpire);
        }
        sb.append("&total_fee=" + totalFee +
                "&trade_type=NATIVE" +
                "&key=" + shopKey);
        return sb.toString();
    }

    public String getPrintln(String tradeType, String shopKey) {
        switch (tradeType) {
            case "MWEB":
                return this.H5Println(shopKey);
            case "NATIVE":
                return this.nativePrintln(shopKey);
            default:
                return this.println(shopKey);
        }
    }
}
