package com.clx.performance.service.impl.thirdparty.nbbank;

import com.alibaba.fastjson.JSONObject;
import com.clx.performance.enums.PerformanceResultEnum;
import com.clx.performance.service.thirdparty.nbbank.NbBankService;
import com.clx.performance.service.thirdparty.nbbank.NbBankThirdpartyService;
import com.clx.performance.vo.pc.nbbank.NbBankOrderPayResultVO;
import com.clx.performance.vo.pc.nbbank.NbBankOrderPayStatusResultVO;
import com.msl.common.exception.ServiceSystemException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 易付通
 */
@Slf4j
@Service
public class NbBankServiceImpl implements NbBankService {

    @Autowired
    private NbBankThirdpartyService bankThirdpartyService;

    /**
     * 订单支付
     * @return
     * 第三方流水号
     */
    @Override
    public NbBankOrderPayResultVO orderPay(Integer payType) {
        if (payType == 1){
            String merSeqNo = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
            JSONObject data = bankThirdpartyService.directBankTransferOrder(merSeqNo, "EFT33021200556315", 1);

            String transSeqNo = data.getString("transSeqNo");
            String signNo = data.getString("signNo");

            NbBankOrderPayResultVO result = new NbBankOrderPayResultVO();
            result.setAmount(1);
            result.setMerSeqNo(merSeqNo);
            result.setTransSeqNo(transSeqNo);
            result.setSignNo(signNo);

            return result;
        }
        else if (payType == 2){
            String merSeqNo = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
            JSONObject data = bankThirdpartyService.unionPayDirectOrder(merSeqNo, "EFT33021200556315", 1,
                    "313332082914","86041110000076809", "黑玫瑰");

            String transSeqNo = data.getString("transSeqNo");
            NbBankOrderPayResultVO result = new NbBankOrderPayResultVO();
            result.setAmount(1);
            result.setMerSeqNo(merSeqNo);
            result.setTransSeqNo(transSeqNo);

            return result;
        }

        throw new ServiceSystemException(PerformanceResultEnum.NB_BANK_PAY_TYPE_ERROR);
    }

    /**
     * 获取交易状态
     * @return
     */
    @Override
    public NbBankOrderPayStatusResultVO getOrderStatus(String merSeqNo) {
        JSONObject data = bankThirdpartyService.queryOrder(merSeqNo);

        NbBankOrderPayStatusResultVO result = new NbBankOrderPayStatusResultVO();

        String transStatus = data.getString("transStatus");
        if (StringUtils.equals(transStatus, "00")){result.setTransStatus(1);}            //交易成功
        else if (StringUtils.equals(transStatus, "01")){result.setTransStatus(2);}       //交易失败
        else if (StringUtils.equals(transStatus, "02")){result.setTransStatus(3);}       //交易已受理
        else if (StringUtils.equals(transStatus, "80")){result.setTransStatus(4);}       //订单初始状态
        else if (StringUtils.equals(transStatus, "90")){result.setTransStatus(5);}       //交易不存在
        else if (StringUtils.equals(transStatus, "99")){result.setTransStatus(6);}       //交易超时,须发起交易结果查询

        BigDecimal trxAmt = data.getBigDecimal("trxAmt");
        BigDecimal realTrxAmt = data.getBigDecimal("realTrxAmt");
        String transSeqNo = data.getString("transSeqNo");
        String signNo = data.getString("signNo");

        result.setAmount(trxAmt==null? null : trxAmt.movePointLeft(2).intValue());
        result.setRealAmount(trxAmt==null? null : realTrxAmt.movePointLeft(2).intValue());
        result.setMerSeqNo(merSeqNo);
        result.setTransSeqNo(transSeqNo);
        result.setSignNo(signNo);

        return result;
    }

    @Override
    public void orderPayCallback(String merSeqNo, String transSeqNo,
                                 BigDecimal trxAmt, BigDecimal realTrxAmt,
                                 String transStatus,
                                 String signNo) {
        log.info("宁波银行支付回调, merSeqNo:{}, transSeqNo:{}, trxAmt:{}, realTrxAmt:{}, transSeqNo:{}, signNo:{}",
                merSeqNo, transSeqNo, trxAmt, realTrxAmt, transStatus, signNo);

        if (StringUtils.equals(transStatus, "00")){     //交易成功

        }
        if (StringUtils.equals(transStatus, "01")){     //交易失败

        }
        if (StringUtils.equals(transStatus, "02")){     //交易已受理

        }
        if (StringUtils.equals(transStatus, "80")){     //订单初始状态

        }
        if (StringUtils.equals(transStatus, "90")){     //交易不存在

        }
        if (StringUtils.equals(transStatus, "99")){     //交易超时,须发起交易结果查询

        }

    }

}
