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

import com.brilliance.isc.bo.Bopcod;
import com.brilliance.isc.common.api.codetable.service.CodetableCommonService;
import com.brilliance.isc.common.api.codetable.vo.CodetableRequestVo;
import com.brilliance.isc.common.api.codetable.vo.CodetableResponseVo;
import com.brilliance.isc.common.api.codetable.vo.ConfigRequestVo;
import com.brilliance.isc.common.api.settle.service.SettleDetailApiService;
import com.brilliance.isc.common.api.settle.vo.AccountRequestVo;
import com.brilliance.isc.common.api.settle.vo.AccountResponseVo;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.vo.ResponseSet;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/codetable")
@Api("公共组件获取码值服务")
public class CodetableController {

    @Autowired
    private SettleContext settleContext;

    @Autowired
    private CodetableCommonService codetableCommonService;

    @Autowired
    private SettleDetailApiService settleDetailApiService;

    @PostMapping("/setmod/list")
    public ResponseSet<CodetableResponseVo> getSetmodCodetable(@RequestBody CodetableRequestVo request) {
        try {
            settleContext.simpleLoadSettleSession();
            return ResponseSet.simpleSuccess(codetableCommonService.getSetmodCodetable(request));
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }
    }

    @PostMapping("/bopcod/list")
    public ResponseSet<List<Bopcod>> getBopcodList(@RequestBody CodetableRequestVo request) {
        try {
            settleContext.simpleLoadSettleSession();
            return ResponseSet.simpleSuccess(codetableCommonService.getBopcodList(request));
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }
    }

    @PostMapping("/bopcod/get")
    public ResponseSet<List<Bopcod>> getBopcod(@RequestBody CodetableRequestVo request) {
        try {
            settleContext.loadSettleSession(request.getTransName(), request.getUserId(),
                    request.getRec(), request.getPtsList());
            return new ResponseSet<>(codetableCommonService.getBopcod(request));
        } finally {
            SettleContext.removeUserSession();
        }
    }

    @PostMapping("/bopcnycty/list")
    public ResponseSet getBopCnyctyCodeList(@RequestBody Map<String, Object> map) {
        try {
            settleContext.simpleLoadSettleSession();
            return codetableCommonService.getBopCnyctyCodeList(map);
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }
    }

    @PostMapping("/configOpen")
    public ResponseSet openConfig(ConfigRequestVo configRequestVo) {
        try {
            return ResponseSet.simpleSuccess(codetableCommonService.opnenConfig(configRequestVo.getStbVo()));
        } catch (Exception e) {
            throw e;
        }
    }
    @PostMapping("/dsp/list")
    public ResponseSet<List<String>> getIntAcctList(@RequestBody CodetableRequestVo request) {
        try {
            String wrkbchinr = request.getWrkbchinr();
            String bchinr = request.getBchinr();
            String ptyinr = request.getPtyinr();
            String cur = request.getCur();
            String dsp = request.getDsp();
            settleContext.loadSettleSession("", wrkbchinr, bchinr);
            return ResponseSet.simpleSuccess(codetableCommonService.getPriIntAcctList(ptyinr, cur, dsp));
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }
    }

    @PostMapping("/kehuzh/list")
    public ResponseSet<Map<String, String>> getMarginAcct(@RequestBody CodetableRequestVo request) {
        String objinr = request.getObjinr();
        String objtyp = request.getObjtyp();
        return ResponseSet.simpleSuccess(codetableCommonService.getMarginAcct(objinr, objtyp));
    }

    @PostMapping("/getAccountNature")
    public ResponseSet<AccountResponseVo> getAccountNature(@RequestBody AccountRequestVo accountRequestVo) {
        return ResponseSet.simpleSuccess(settleDetailApiService.getAccountNature(accountRequestVo));
    }

    @PostMapping("/getSwtinfo")
    public ResponseSet<Map<String, String>> getSwtInfo(String ptainr) {
        try {
            settleContext.simpleLoadSettleSession();
            return ResponseSet.simpleSuccess(codetableCommonService.getSwtInfo(ptainr));
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }

    }
}
