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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
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.params.yspay.YspayIsvParams;
import com.jeequan.jeepay.core.model.params.yspay.YspayIsvsubMchParams;
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.thirdparty.channel.AbstractPaymentService;
import com.jeequan.jeepay.thirdparty.channel.yspay.utils.YspayKit;
import com.jeequan.jeepay.thirdparty.service.ConfigContextQueryService;
import com.jeequan.jeepay.thirdparty.util.PaywayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 银盛支付
 *
 * @author xiaoyu
 *
 * @date 2022/7/8 11:24
 */
@Slf4j
@Service
public class YspayPaymentService extends AbstractPaymentService {

    /**
     * 为了防止手续费的问题 限制银盛的最少支付金额为0.1元
     */
    public Long MIN_PAY_AMT  = 10L;

    @Autowired
    private ConfigContextQueryService configContextQueryService;

    @Override
    public String getIfCode() {
        return CS.IF_CODE.YSPAY;
    }

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

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {
        return PaywayUtil.getRealPaywayService(this, payOrder.getWayCode()).preCheck(rq, payOrder);
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        try {
            if(payOrder.getAmount() < MIN_PAY_AMT){
                throw new BizException("银盛通道最少需要支付0.1元");
            }
            return PaywayUtil.getRealPaywayService(this, payOrder.getWayCode()).pay(rq, payOrder, mchAppConfigContext);
        }catch (Exception e){
            upException(payOrder.getPayOrderId(),e);
            throw e;
        }
    }

    /** 支付下单分账登记 -服务商为商户分账下单模式 **/
    public void payOrderDivision(MchAppConfigContext mchAppConfigContext, PayOrder payOrder){
        YspayIsvParams mchParams = (YspayIsvParams) configContextQueryService.queryIsvParams(mchAppConfigContext.getMchApplyment());
        // 分账模式下走该逻辑
        if (mchParams.getPayType() != null && mchParams.getPayType().equals(YspayIsvParams.PAY_TYPE_DIVISION)) {
            JSONObject bizContent = new JSONObject();
            // 订单号
            bizContent.put("out_trade_no", payOrder.getPayOrderId());
            // 收款商户号
            bizContent.put("payee_usercode", mchAppConfigContext.getMchApplyment().getChannelMchNo());
            // 原交易订单金额 20长度位，保留2位小数
            bizContent.put("total_amount", AmountUtil.convertCent2Dollar(payOrder.getFindAmt()+""));
            // 原订单是否参与分账 01：是，02否
            bizContent.put("is_divistion", "02");
            // 是否重新分账 Y：是，N：否
            bizContent.put("is_again_division", "N");
            // 分行模式 01 ：比例，02：金额
            bizContent.put("division_mode", "02");
            // 发起分账请求
            String result = YspayKit.divisionRequest(YspayKit.DIVISION_URL, "ysepay.single.division.online.accept", mchAppConfigContext.getMchApplyment().getIsvNo(), bizContent, null, payOrder.getIfCode());
            JSONObject resJson = JSONObject.parseObject(result);
            JSONObject response = resJson.getJSONObject("ysepay_single_division_online_accept_response");
            log.info("【银盛】分账登记接口：response：{}", response);
        }
    }
}
