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

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.interfaces.paychannel.IDivisionService;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.division.DivisionReceiverChannelBalanceQueryRQ;
import com.jeequan.jeepay.core.model.rqrs.division.DivisionReceiverChannelBalanceQueryRS;
import com.jeequan.jeepay.core.utils.JeepayKit;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.db.entity.MchDivisionReceiver;
import com.jeequan.jeepay.pay.ctrl.ApiController;
import com.jeequan.jeepay.pay.util.ApiResKit;
import com.jeequan.jeepay.service.impl.MchDivisionReceiverService;
import com.jeequan.jeepay.service.impl.PayInterfaceConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 分账账号 渠道余额查询
*
* @author terrfly
*
* @date 2022/05/11 10:46
*/
@Slf4j
@RestController
public class MchDivisionReceiverChannelBalanceQueryController extends ApiController {

    @Autowired private IConfigContextQueryService configContextQueryService;
    @Autowired private PayInterfaceConfigService payInterfaceConfigService;
    @Autowired private MchDivisionReceiverService mchDivisionReceiverService;

    /**  ctrl **/
    @MethodLog(remark = "查询分账用户可用余额API")
    @PostMapping("/api/division/receiver/channelBalanceQuery")
    public ApiRes query(){

        //获取参数 & 验签
        DivisionReceiverChannelBalanceQueryRQ bizRQ = getRQByWithMchSign(DivisionReceiverChannelBalanceQueryRQ.class);

        try {

            MchDivisionReceiver mchDivisionReceiver = mchDivisionReceiverService.getOne(MchDivisionReceiver.gw()
                    .eq(MchDivisionReceiver::getReceiverId, bizRQ.getReceiverId())
                    .eq(MchDivisionReceiver::getMchNo, bizRQ.getMchNo())
            );

            if(mchDivisionReceiver == null){
                throw new BizException("分账用户不存在");
            }

            //检查商户应用是否存在该接口
            String ifCode = mchDivisionReceiver.getIfCode();


            // 商户配置信息
            MchAppConfigContext mchAppConfigContext = configContextQueryService.queryMchInfoAndAppInfoV2(bizRQ.getMchNo(), bizRQ.getAppId(),bizRQ.getMchExtNo());
            if(mchAppConfigContext == null){
                throw new BizException("获取商户应用信息失败");
            }

            if(!payInterfaceConfigService.mchAppHasAvailableIfCode(bizRQ.getAppId(), ifCode)){
                throw new BizException("商户应用的支付配置不存在或已关闭");
            }

            //调起上游接口
            IDivisionService divisionService = SpringBeansUtil.getBean(JeepayKit.getIfCodeOrigin(ifCode) + "DivisionService", IDivisionService.class);
            if(divisionService == null){
                throw new BizException("系统不支持该分账接口");
            }


            Long balance = divisionService.queryBalanceAmount(mchDivisionReceiver, mchAppConfigContext);
            DivisionReceiverChannelBalanceQueryRS bizRes = new DivisionReceiverChannelBalanceQueryRS();
            bizRes.setReceiverId(bizRQ.getReceiverId());
            bizRes.setBalanceAmount(balance);

            return ApiResKit.okWithSign(bizRes, mchAppConfigContext.getMchApp(), bizRQ.getSignType());

        }  catch (BizException e) {
            return ApiRes.customFail(e.getMessage());

        } catch (Exception e) {
            log.error("系统异常：{}", e);
            return ApiRes.customFail("系统异常");
        }
    }
}
