package com.jeequan.jeepay.thirdparty.channel.dgpay.payway;

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.exception.ChannelException;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.AbstractRS;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.core.model.rqrs.payorder.UnifiedOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliBarOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.payorder.payway.AliBarOrderRS;
import com.jeequan.jeepay.thirdparty.channel.dgpay.DgPayKit;
import com.jeequan.jeepay.thirdparty.channel.dgpay.DgpayPaymentService;
import com.jeequan.jeepay.thirdparty.util.ApiResBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/*
* 斗拱 支付宝 条码支付
*
* @author pangxiaoyu
* 
* @date 2021/6/8 18:11
*/
@Slf4j
@Service("dgpayPaymentByAliBarService") //Service Name需保持全局唯一性
public class AliBar extends DgpayPaymentService {

    @Override
    public String preCheck(UnifiedOrderRQ rq, PayOrder payOrder) {

        AliBarOrderRQ bizRQ = (AliBarOrderRQ) rq;
        if(StringUtils.isEmpty(bizRQ.getAuthCode())){
            throw new BizException("用户支付条码[authCode]不可为空");
        }
        return null;
    }

    @Override
    public AbstractRS pay(UnifiedOrderRQ rq, PayOrder payOrder, MchAppConfigContext mchAppConfigContext) throws Exception {
        AliBarOrderRQ bizRQ = (AliBarOrderRQ) rq;
        AliBarOrderRS res = ApiResBuilder.buildSuccess(AliBarOrderRS.class);
        ChannelRetMsg channelRetMsg = new ChannelRetMsg();
        res.setChannelRetMsg(channelRetMsg);
        try {
            JSONObject dataParams = DgPayKit.dataParams(payOrder, getNotifyUrl());
            dataParams.put("auth_code", bizRQ.getAuthCode());
            // 设置订单是否需要分账
            if(isDivisionOrder(payOrder)) {
                dataParams.put("delay_acct_flag", "Y");
            }

            JSONObject response = DgPayKit.payRequest(mchAppConfigContext, dataParams, "/trade/payment/micropay", payOrder.getIfCode());
            JSONObject resData = response.getJSONObject("data");
            channelRetMsg.setChannelBizData(resData);
            String code = resData.getString("resp_code");
            if(DgPayKit.STATE_SUCCESS.equals(code)){
                String transStat = resData.getString("trans_stat");
                String channelOrderNo = resData.get("hf_seq_id").toString();
                String debit_type = resData.getString("debit_type");
                if(StringUtils.isEmpty(debit_type) || "0".equals(debit_type)){
                    channelRetMsg.setDrType(CS.DrType.OTHER.getType());
                }else if("D".equals(debit_type)){
                    channelRetMsg.setDrType(CS.DrType.DEBIT.getType());
                }else if("C".equals(debit_type)){
                    channelRetMsg.setDrType(CS.DrType.CREDIT.getType());
                }else{
                    channelRetMsg.setDrType(CS.DrType.OTHER.getType());
                }
                channelRetMsg.setChannelOrderId(channelOrderNo);
                if (DgPayKit.STATE_SUCCESS_S.equals(transStat)) {
                    // 交易状态成功
                    channelRetMsg.setPlatformOrderNo(resData.get("out_trans_id").toString());
                    channelRetMsg.setPlatformMchOrderNo(resData.get("party_order_id").toString());
                    channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
                    res.setPayData(resData.toString());
                }else if (DgPayKit.STATE_FAIL_F.equals(transStat)) {
                    // 交易状态失败
                    DgPayKit.channelMsgError(resData, channelRetMsg);
                    channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                }else {
                    channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
                    // 开启轮询查单
                    channelRetMsg.setNeedQuery(true);
                }

            } else if (DgPayKit.STATE_ING.equals(code)) {
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.WAITING);
                // 开启轮询查单
                channelRetMsg.setNeedQuery(true);
            } else{
                DgPayKit.channelMsgError(resData, channelRetMsg);
                channelRetMsg.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
            }
        }catch (Exception e) {
            log.error(e.getMessage());
            throw ChannelException.sysError(e.getMessage());
        }
        return res;
    }

}
