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

import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.constants.CS;
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.DivisionReceiverChannelBalanceCashoutRQ;
import com.jeequan.jeepay.core.model.rqrs.division.DivisionReceiverChannelBalanceCashoutRS;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
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 MchDivisionReceiverChannelBalanceCashoutController extends ApiController {

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

    /** 分账账号绑定 **/
    @MethodLog(remark = "对分账用户的渠道余额发起提现API")
    @PostMapping("/api/division/receiver/channelBalanceCashout")
    public ApiRes channelBalanceCashout(){

        //获取参数 & 验签
        DivisionReceiverChannelBalanceCashoutRQ bizRQ = getRQByWithMchSign(DivisionReceiverChannelBalanceCashoutRQ.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("系统不支持该分账接口");
            }

            DivisionReceiverChannelBalanceCashoutRS bizRes = new DivisionReceiverChannelBalanceCashoutRS();

            ChannelRetMsg retMsg = divisionService.cashout(mchDivisionReceiver, bizRQ.getCashoutAmount(), mchAppConfigContext);
            if(retMsg.getChannelState() == ChannelRetMsg.ChannelState.CONFIRM_SUCCESS){
                bizRes.setState(CS.YES);
            }else{
                bizRes.setState(CS.NO);
                bizRes.setErrCode(retMsg.getChannelErrCode());
                bizRes.setErrMsg(retMsg.getChannelErrMsg());
            }

            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("系统异常");
        }
    }

}
