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

import com.brilliance.isc.bo.Pta;
import com.brilliance.isc.common.api.dftcre.service.DftcreApiService;
import com.brilliance.isc.common.api.dftcre.vo.DftcreQueryVo;
import com.brilliance.isc.common.api.dftcre.vo.DftcreRequest;
import com.brilliance.isc.common.api.dftcre.vo.DftcreResponse;
import com.brilliance.isc.common.cache.pta.PtaCacheService;
import com.brilliance.isc.common.component.DftmodComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.converter.DtoMapper;
import com.brilliance.isc.common.dftcre.service.DftcreService;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.vo.ResponseSet;
import com.brilliance.isc.vo.component.dftcre.DftcreVo;
import com.brilliance.isc.vo.model.DfdgrpVo;
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 AbstractDftcreController<T extends BizInfoVo> {

    @Autowired
    private SettleContext settleContext;

    @Autowired
    private DftcreApiService dftcreApiService;

    @Autowired
    private DftcreService dftcreService;

    @Autowired
    private PtaCacheService ptaCacheService;

    @Autowired
    private DftmodComponent dftmodComponent;

    /**
     * 同业代付试算入口
     * @param dftcreRequest
     * @return
     */
    public ResponseSet<DftcreResponse> assertDftcre(DftcreRequest<T> dftcreRequest) {
        try {
            settleContext.loadSettleSession(dftcreRequest.getTransName(), dftcreRequest.getUserId(), dftcreRequest.getRec(), dftcreRequest.getPtsList());
            return ResponseSet.simpleSuccess(dftcreApiService.assertDftcre(dftcreRequest));
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }
    }

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

    /**
     * 获取同业代付名称
     * @param dftcreVo
     * @return
     */
    public ResponseSet<String> getRecNam(@RequestBody DftcreVo dftcreVo) {
        return ResponseSet.simpleSuccess(dftcreService.defaultDfdgrpRecNam(DtoMapper.INSTANCE.transfer(dftcreVo)));
    }

    public ResponseSet<String> getDfuflg(@RequestBody DfdgrpVo dfdgrpVo) {
        return ResponseSet.simpleSuccess(dftcreApiService.getDfuflg(dfdgrpVo));
    }

    public ResponseSet<String> getEcifName(@RequestBody DftcreQueryVo vo) {
        return ResponseSet.simpleSuccess(dftcreApiService.getEcifName(vo));
    }
}
