package com.jeequan.jeepay.pay.ctrl.transfer;

import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.IConfigContextQueryService;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.rqrs.transfer.QueryTransferOrderRQ;
import com.jeequan.jeepay.core.model.rqrs.transfer.QueryTransferOrderRS;
import com.jeequan.jeepay.db.entity.TransferOrderEntity;
import com.jeequan.jeepay.pay.ctrl.ApiController;
import com.jeequan.jeepay.pay.util.ApiResKit;
import com.jeequan.jeepay.service.impl.PayInterfaceConfigService;
import com.jeequan.jeepay.service.impl.TransferOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 商户转账单查询controller
*
* @author terrfly
*
* @date 2021/8/13 15:20
*/
@Slf4j
@RestController
public class QueryTransferOrderController extends ApiController {

    @Autowired private TransferOrderService transferOrderService;
    @Autowired private IConfigContextQueryService configContextQueryService;
    @Autowired private PayInterfaceConfigService payInterfaceConfigService;

    /**
     * 查单接口
     * **/
    @MethodLog(remark = "查询转账订单API")
    @RequestMapping("/api/transfer/query")
    public ApiRes queryTransferOrder(){

        //获取参数 & 验签
        QueryTransferOrderRQ rq = getRQByWithMchSign(QueryTransferOrderRQ.class);

        if(StringUtils.isAllEmpty(rq.getMchOrderNo(), rq.getTransferId())){
            throw new BizException("mchOrderNo 和 transferId不能同时为空");
        }

        TransferOrderEntity refundOrder = transferOrderService.queryMchOrder(rq.getMchNo(), rq.getMchOrderNo(), rq.getTransferId());
        if(refundOrder == null){
            throw new BizException("订单不存在");
        }

        QueryTransferOrderRS bizRes = QueryTransferOrderRS.buildByRecord(refundOrder);

        return ApiResKit.okWithSign(bizRes, configContextQueryService.queryMchApp(rq.getMchNo(), rq.getAppId()), rq.getSignType());
    }

    /**
     * 查询转账账户余额接口
     * **/
    @MethodLog(remark = "查询转账可用余额API")
    @RequestMapping("/api/transfer/balance/query")
    public ApiRes queryTransferBalance(){

        return ApiRes.ok();
//        //获取参数 & 验签
//        QueryTransferBalanceRQ bizRQ = getRQByWithMchSign(QueryTransferBalanceRQ.class);
//
//        try {
//            String mchNo = bizRQ.getMchNo();
//            String appId = bizRQ.getAppId();
//            String ifCode = bizRQ.getIfCode();
//
//            // 商户配置信息
//            MchAppConfigContext mchAppConfigContext = configContextQueryService.queryMchInfoAndAppInfoV2(mchNo, appId,bizRQ.getMchExtNo());
//            if(mchAppConfigContext == null){
//                throw new BizException("获取商户应用信息失败");
//            }
//
//            MchApp mchApp = mchAppConfigContext.getMchApp();
//
//            // 是否已正确配置
//            if(!payInterfaceConfigService.mchAppHasAvailableIfCode(appId, ifCode)){
//                throw new BizException("应用未开通此接口配置!");
//            }
//
//            ITransferService transferService = SpringBeansUtil.getBean(JeepayKit.getIfCodeOrigin(ifCode) + "TransferService", ITransferService.class);
//            if(transferService == null){
//                throw new BizException("无此转账通道接口");
//            }
//
//            // 调起上游接口
//            MutablePair<String, Long> pair = transferService.queryBalanceAmount(mchAppConfigContext, bizRQ.getIfCode());
//
//            QueryTransferBalanceRS bizRes = new QueryTransferBalanceRS();
//            bizRes.setBalanceAmount(pair.getRight());
//
//            return ApiResKit.okWithSign(bizRes, mchApp, bizRQ.getSignType());
//
//        }  catch (BizException e) {
//            return ApiRes.customFail(e.getMessage());
//
//        } catch (Exception e) {
//            log.error("系统异常：", e);
//            return ApiRes.customFail("系统异常");
//        }
    }
}
