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

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.msg.ChannelAppletPayMsg;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRQ;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * TODO
 *
 * @author crystal
 * @date 2024/4/8 11:25
 */
@Data
public class JsapiBizReq extends UnifiedOrderRQ {

   private String payType;

   private String userId;

   private String subAppid;

   /**
    * 收款方向银联推送订单时上送的前台通知地址（仅允许为外网地址），用户完成支付点击，“返回”后，银联通过浏览器POST请求到该地址。 当transType为JSAPI，payMode为UQRCODEPAY时，可选填此字段
    */
   private String frontUrl;

   /**
    * 收款方向银联推送订单时上送的前台通知地址（仅允许为外网地址），用户完成支付点击，“返回”后，银联通过浏览器POST请求到该地址。 当transType为JSAPI，payMode为UQRCODEPAY时，可选填此字段
    */
   private String frontFailUrl;

   /**
    * 是否半屏
    */
   private Boolean isScreen = false;

   /**
    * 参数校验
    */
   public UnifiedOrderRQ preCheck(AccessReq accessReq) {
      if(StringUtils.isEmpty(this.getCurrency())){
         throw new ParamException("货币类型[currency]参数不能为空");
      }
      if(StringUtils.isEmpty(this.getMchOrderNo())){
         throw new ParamException("商户请求流水号[mchOrderNo]参数不能为空");
      }
      if(!this.getIsScreen() && StringUtils.isEmpty(this.getUserId())){
         throw new ParamException("付款用户[userId]参数不能为空");
      }
      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()) && !CS.PAY_WAY_CODE_TYPE.UNIONPAY.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);
      }
      JSONObject channelExtra = new JSONObject();
      channelExtra.put("userId",userId);

      if(CS.PAY_WAY_CODE_TYPE.ALIPAY.equals(this.getPayType())){
         this.setWayCode(CS.PAY_WAY_CODE.ALI_JSAPI);
         channelExtra.put("buyerUserId",this.getUserId());
      }else if(CS.PAY_WAY_CODE_TYPE.WECHAT.equals(this.getPayType())){
         if(!this.getIsScreen() && StringUtils.isEmpty(this.getSubAppid())){
            throw new ParamException("支付类型为微信时，子商户[subAppId]参数不能为空");
         }
         this.setWayCode(CS.PAY_WAY_CODE.WX_JSAPI);
         channelExtra.put("openId",this.getUserId());
         channelExtra.put("subAppid",this.getSubAppid());
      }else{
         this.setWayCode(CS.PAY_WAY_CODE.YSF_JSAPI);
         if(StringUtils.isNotEmpty(this.getFrontUrl())){
            channelExtra.put("frontUrl",this.getFrontUrl());
         }
         if(StringUtils.isNotEmpty(this.getFrontUrl())){
            channelExtra.put("frontFailUrl",this.getFrontFailUrl());
         }
      }
      if(accessReq.getIsApplet()){
         this.setWayCode(this.getWayCode().replace("JSAPI","LITE"));
      }
      this.setChannelExtra(channelExtra.toString());
      return this;
   }

}
