package com.brilliance.isc.common.api.trtcre.controller;

import com.brilliance.isc.bo.Pta;
import com.brilliance.isc.common.api.trtcre.service.TrtcreApiService;
import com.brilliance.isc.common.api.trtcre.vo.TrtcreQueryVo;
import com.brilliance.isc.common.api.trtcre.vo.TrtcreRequest;
import com.brilliance.isc.common.api.trtcre.vo.TrtcreResponse;
import com.brilliance.isc.common.cache.pta.PtaCacheService;
import com.brilliance.isc.common.component.TrtmodComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.finmod.service.FinmodService;
import com.brilliance.isc.common.finmod.vo.*;
import com.brilliance.isc.common.setfeg.service.SetfegService;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.vo.ResponseSet;
import com.brilliance.isc.vo.model.FinmodVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 融资服务
 * @restApi
 * @param <T>
 */
@Api("融资服务")
public abstract class AbstractTrtcreController<T extends BizInfoVo> {

    @Autowired
    private SettleContext settleContext;

    @Autowired
    private TrtcreApiService trtcreApiService;

    @Autowired
    private PtaCacheService ptaCacheService;

    @Autowired
    private TrtmodComponent trtmodComponent;

    @Autowired
    private FinmodService finmodService;

    @Autowired
    private SetfegService setfegService;

    /**
     * 融资试算入口
     * @param trtcreRequest
     * @return
     */
    public ResponseSet<TrtcreResponse> assertTrtcre(TrtcreRequest<T> trtcreRequest) {
        try {
            settleContext.loadSettleSession(trtcreRequest.getTransName(), trtcreRequest.getUserId(), trtcreRequest.getRec(), trtcreRequest.getPtsList());
            return ResponseSet.simpleSuccess(trtcreApiService.assertTrtcre(trtcreRequest));
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }
    }

    /**
     * 获取融资的业务编号
     * @param vo
     * @return
     */
    public ResponseSet<String> getRef(@RequestBody TrtcreQueryVo vo) {
        try {
            String ptainr = vo.getPtainr();
            String ownref = vo.getOwnref();
            String fintyp = vo.getFintyp();
            Pta pta = ptaCacheService.getPtaByInr(ptainr);
            if (pta == null) {
                return ResponseSet.fail("无效的客户号");
            }
            settleContext.loadSettleSession("", pta.getBranchinr(), "");
            return ResponseSet.simpleSuccess(trtmodComponent.getref(ownref, fintyp, ""));
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }
    }

    /**
     * 计算融资金额
     * @param vo
     * @return
     */
    public ResponseSet<TrtcreQueryVo> calcAmt(@RequestBody TrtcreQueryVo vo) {
        try {
            settleContext.simpleLoadSettleSession();
            return ResponseSet.simpleSuccess(trtcreApiService.calcAmt(vo));
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }
    }

    /**
     * 获取实时牌价
     * @param vo
     * @return
     */
    public ResponseSet<TrtcreQueryVo> queryExtrat(@RequestBody TrtcreQueryVo vo) {
        try {
            settleContext.simpleLoadSettleSession();
            return ResponseSet.simpleSuccess(trtcreApiService.queryExtrat(vo));
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }
    }

    /**
     * 获取账号下拉列表
     * @param vo
     * @return
     */
    public ResponseSet<TrtcreQueryVo> getActSet(@RequestBody TrtcreQueryVo vo) {
        try {
            settleContext.simpleLoadSettleSession();
            return ResponseSet.simpleSuccess(trtcreApiService.getActSet(vo));
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
            setfegService.clearFeeSet();
        }
    }

    /**
     * 核心系统提供的实时查询基准利率交易
     * @param request
     * @return
     */
    public ResponseSet<Finmod2571Response> call2571(@RequestBody Finmod2571Request request) {
        return ResponseSet.simpleSuccess(finmodService.call2571(request));
    }

    /**
     * 核心业务系统提供融资开户交易
     * @param request
     * @return
     */
    public ResponseSet<Finmod6153Response> call6153(@RequestBody FinmodVo request) {
        return ResponseSet.simpleSuccess(finmodService.call6153(request));
    }

    public ResponseSet<FinmodCPC3Response> callCPC3(@RequestBody FinmodCPC3Request request) {
        return ResponseSet.simpleSuccess(finmodService.callCPC3(request));
    }

}
