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

import com.brilliance.isc.common.api.liaccv.service.LiaccvApiService;
import com.brilliance.isc.common.api.liaccv.vo.LiaccvRequest;
import com.brilliance.isc.common.api.liaccv.vo.LiaccvResponse;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.common.vo.BizAmdInfoVo;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.common.vo.FcdVo;
import com.brilliance.isc.vo.ResponseSet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Api("保证金服务")
public class AbstractLiaccvController<T1 extends BizInfoVo, T2 extends BizAmdInfoVo> {

    @Autowired
    private LiaccvApiService liaccvApiService;

    @Autowired
    private SettleContext settleContext;

    @Autowired
    private TransactionHelpService transactionHelpService;

    /**
     *保证金试算服务
     * @param liaccvRequest
     * @return ResponseSet<LiaccvResponse>
     */
    @ApiOperation("保证金试算服务")
    public ResponseSet<LiaccvResponse> calcLiaccv(LiaccvRequest<T1, T2> liaccvRequest) {
        try {
            settleContext.loadSettleSession(liaccvRequest.getTransName(), liaccvRequest.getUserId(), liaccvRequest.getRec(), liaccvRequest.getPtsList(), liaccvRequest.getSptInr());
            return ResponseSet.simpleSuccess(liaccvApiService.calcLiaccv(liaccvRequest));
        } catch (Exception e) {
            throw e;
        } finally {
            transactionHelpService.clearLiaallSet();
            SettleContext.removeUserSession();
        }
    }

    /**
     * 增加保证金条目
     * @param liaccvRequest
     * @return ResponseSet<LiaccvResponse>
     */
    @ApiOperation("增加保证金条目")
    @Transactional(rollbackFor = {Exception.class})
    public ResponseSet<LiaccvResponse> addLiaccvg( LiaccvRequest<T1, T2> liaccvRequest) {
        try {
            settleContext.loadSettleSession(liaccvRequest.getTransName(), liaccvRequest.getUserId(), liaccvRequest.getRec(), liaccvRequest.getPtsList(), liaccvRequest.getSptInr());
            return ResponseSet.simpleSuccess(liaccvApiService.addLiaccvg(liaccvRequest));
        } catch (Exception e) {
            throw e;
        } finally {
            transactionHelpService.clearLiaallSet();
            SettleContext.removeUserSession();
        }
    }

    /**
     *Fcd列表
     * @param liaccvRequest
     * @return ResponseSet<List<FcdVo>>
     */
    @ApiOperation("Fcd列表")
    public ResponseSet<List<FcdVo>> getFcdList( LiaccvRequest<T1, T2> liaccvRequest) {
        try {
            return ResponseSet.simpleSuccess(liaccvApiService.getFcdList(liaccvRequest));
        } catch (Exception e) {
            throw e;
        } finally {

        }
    }

    /**
     * 删除保证金条目
     * @param liaccvRequest
     * @return ResponseSet<LiaccvResponse>
     */
    @ApiOperation("删除保证金条目")
    @Transactional(rollbackFor = {Exception.class})
    public ResponseSet<LiaccvResponse> deleteLiaccvg(@RequestBody LiaccvRequest<T1, T2> liaccvRequest) {
        try {
            settleContext.loadSettleSession(liaccvRequest.getTransName(), liaccvRequest.getUserId(), liaccvRequest.getRec(), liaccvRequest.getPtsList(), liaccvRequest.getSptInr());
            return ResponseSet.simpleSuccess(liaccvApiService.deleteLiaccvg(liaccvRequest));
        } catch (Exception e) {
            throw e;
        } finally {
            transactionHelpService.clearLiaallSet();
            SettleContext.removeUserSession();
        }
    }
}
