package com.jeequan.jeepay.core.model.openapi.biz.h5;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.openapi.ParamException;
import com.jeequan.jeepay.core.model.openapi.AccessReq;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRQ;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * TODO
 * h5支付参数封装
 * @author crystal
 * @date 2024/4/10 13:34
 */
@Data
public class H5BizReq extends UnifiedOrderRQ {

    private String payType;

    /**
     * 参数校验
     */
    public UnifiedOrderRQ preCheck() {
        if(StringUtils.isEmpty(this.getCurrency())){
            throw new ParamException("货币类型[currency]参数不能为空");
        }
        if(StringUtils.isEmpty(this.getMchOrderNo())){
            throw new ParamException("商户请求流水号[mchOrderNo]参数不能为空");
        }
        if(this.getAmount() == null || this.getAmount() < 0){
            throw new ParamException("交易金额[amount]参数不能为空或参数格式不合法");
        }
        if(this.getMchOrderNo().length() > 32 ){
            throw new ParamException("商户请求流水号[mchOrderNo]长度不允许超过32位");
        }
        if(StringUtils.isEmpty(this.getSubject())){
            throw new ParamException("订单标题[subject]参数不能为空");
        }
        if(StringUtils.isEmpty(this.getStoreId())){
            throw new ParamException("门店ID[storeId]参数不能为空");
        }
        if(StringUtils.isEmpty(this.getPayType())){
            throw new ParamException("支付方式[payType]参数不能为空");
        }
        if(!CS.PAY_WAY_CODE_TYPE.ALIPAY.equals(this.getPayType()) && !CS.PAY_WAY_CODE_TYPE.WECHAT.equals(this.getPayType())){
            throw new ParamException("支付方式[payType]参数有误");
        }
        if(this.getExpiredTime() != null && (this.getExpiredTime() < 5 || this.getExpiredTime() > 1440)){
            throw new ParamException("订单过期时间[expiredTime]参数需要介于5-1440分钟之间");
        }
        if(StringUtils.isEmpty(this.getClientIp())){
            throw new ParamException("付款方客户端ip[clientIp]参数不能为空");
        }
        if(this.getLimitPay() == null){
            this.setLimitPay(-1);
        }
        if(CS.PAY_WAY_CODE_TYPE.ALIPAY.equals(this.getPayType())){
            this.setWayCode(CS.PAY_WAY_CODE.ALI_WAP);
        }else if(CS.PAY_WAY_CODE_TYPE.WECHAT.equals(this.getPayType())){
            this.setWayCode(CS.PAY_WAY_CODE.WX_LITE);
        }else{
            throw new ParamException("未知的支付方式[payType]参数");
        }
        return this;
    }
}
