package com.jeequan.jeepay.thirdparty.channel.zftpay;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.SettleDetailInfo;
import com.alipay.api.domain.SettleInfo;
import com.alipay.api.domain.SubMerchant;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.AbstractRS;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRQ;
import com.jeequan.jeepay.core.utils.AmountUtil;
import com.jeequan.jeepay.db.entity.AccountInfo;
import com.jeequan.jeepay.service.impl.AccountInfoService;
import com.jeequan.jeepay.thirdparty.channel.AbstractPaymentService;
import com.jeequan.jeepay.thirdparty.util.PaywayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 * 支付宝直付通
 * @author crystal
 * @date 2024/3/27 18:07
 */
@Service
public class ZftpayPaymentService extends AbstractPaymentService {

    @Autowired
    private AccountInfoService accountInfoService;

    /**
     * 支付宝直付通
     * @return
     */
    @Override
    public String getIfCode() {
        return CS.IF_CODE.ZFTPAY;
    }

    @Override
    public boolean isSupport(String wayCode) {
        return false;
    }

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {
        this.checkAccountBlance(payOrder);
        if(!CS.PAY_WAY_CODE_TYPE.ALIPAY.equals(payOrder.getWayCodeType())){
            throw new BizException("直付通只支持支付宝支付");
        }
        return PaywayUtil.getRealPaywayService(this, payOrder.getWayCode()).preCheck(rq, payOrder);
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        return PaywayUtil.getRealPaywayService(this, payOrder.getWayCode()).pay(rq, payOrder, mchAppConfigContext);
    }

    public static <T> T getPayModel(UnifiedOrderRQ rq, PayOrder payOrder, Class<T> clazz) {
        JSONObject result = new JSONObject();
        result.put("outTradeNo",payOrder.getPayOrderId());
        result.put("subject",payOrder.getSubject());
        result.put("body",payOrder.getBody());
        result.put("totalAmount", AmountUtil.convertCent2Dollar(payOrder.getFindAmt().toString()));
        result.put("timeExpire", DateUtil.format(payOrder.getExpiredTime(), DatePattern.NORM_DATETIME_FORMAT));
        SubMerchant subMerchant = new SubMerchant();
        subMerchant.setMerchantId(payOrder.getChannelMchNo());
        SettleInfo settleInfo = new SettleInfo();
        settleInfo.setSettlePeriodTime("30d");
        SettleDetailInfo settleDetailInfo = new SettleDetailInfo();
        //固定传值
        settleDetailInfo.setTransInType("defaultSettle");
        settleDetailInfo.setAmount(AmountUtil.convertCent2Dollar(payOrder.getFindAmt()));
        List<SettleDetailInfo> list = new ArrayList<>();
        list.add(settleDetailInfo);
        settleInfo.setSettleDetailInfos(list);
        result.put("subMerchant",subMerchant);
        result.put("settleInfo",settleInfo);
        Integer limitPay = rq.getLimitPay();
        if(limitPay != null && limitPay == 1){
            result.put("disablePayChannels","creditCard");
        }
        return result.toJavaObject(clazz);
    }

    /**
     * 值付通走的是手续费账户模式，校验手续费账户余额
     */
    public void checkAccountBlance(PayOrder payOrder){
        accountInfoService.checkBalance(payOrder.getMchNo(),AccountInfo.Type.SERVICE_CHARGE,payOrder.getMchOrderFeeAmount());
    }
}
