package com.brilliance.isc.common.txfmod.controller;

import com.brilliance.isc.bo.Bopcod;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.doc.TextResultVo;
import com.brilliance.isc.common.exception.CommonServiceException;
import com.brilliance.isc.common.txfmod.service.TxfmodPdfService;
import com.brilliance.isc.common.txfmod.service.TxfmodService;
import com.brilliance.isc.common.txfmod.vo.TxfmodVo;
import com.brilliance.isc.vo.ResponseSet;
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 javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/txfmod")
public class TxfmodController {

    @Resource
    private TxfmodService txfmodService;

    @Resource
    private TxfmodPdfService txfmodPdfService;

    @Resource
    private SettleContext settleContext;

    // 提交核验
    @PostMapping("/subtxf")
    public ResponseSet subtxf(@RequestBody TxfmodVo txfmodVo) {
        try {
            settleContext.loadSettleSession(txfmodVo.getRec().getTransName(), "", "");
            TxfmodVo result = txfmodService.subtxf(txfmodVo);
            return ResponseSet.simpleSuccess(result);
        }  catch (Exception e) {
            return ResponseSet.fail(e.getMessage());
        } finally {
            SettleContext.removeUserSession();
        }
    }

    // 付汇取消
    @PostMapping("/calpay")
    public ResponseSet calpay(@RequestBody TxfmodVo txfmodVo) {
        try{
            settleContext.loadSettleSession(txfmodVo.getRec().getTransName(),  "", "");
            txfmodService.calpay(txfmodVo);
            return ResponseSet.simpleSuccess("付汇取消成功");
        } catch (CommonServiceException e) {
            return ResponseSet.fail(e.getMessage());
        } finally {
            SettleContext.removeUserSession();
        }
    }

    // 业务受理
    @PostMapping("/busacp")
    public ResponseSet busacp(@RequestBody TxfmodVo txfmodVo) {
        try{
            settleContext.loadSettleSession(txfmodVo.getRec().getTransName(),  "", "");
            TxfmodVo busacp = txfmodService.busacp(txfmodVo);
            return ResponseSet.simpleSuccess(busacp);
        } catch (CommonServiceException e) {
            return ResponseSet.fail(e.getMessage());
        } finally {
            SettleContext.removeUserSession();
        }
    }

    // PDF下载
    @PostMapping("/showPdf")
    public ResponseSet showPdf(@RequestBody TxfmodVo txfmodVo) {
        try{
            settleContext.loadSettleSession(txfmodVo.getRec().getTransName(),  "", "");
            TextResultVo textResultVo = txfmodPdfService.viewPdf(txfmodVo);
            return ResponseSet.simpleSuccess(textResultVo);
        } catch (CommonServiceException e) {
            return ResponseSet.fail(e.getMessage());
        } finally {
            SettleContext.removeUserSession();
        }
    }

    // 是否进行税务备案信息核验
    @PostMapping("/checkYesOrNo")
    public ResponseSet checkYesOrNo(@RequestBody TxfmodVo txfmodVo) {
        try{
            settleContext.loadSettleSession(txfmodVo.getRec().getTransName(),  "", "");
            TxfmodVo result = txfmodService.checkYesOrNo(txfmodVo);
            return ResponseSet.simpleSuccess(result);
        } catch (CommonServiceException e) {
            return ResponseSet.fail(e.getMessage());
        } finally {
            SettleContext.removeUserSession();
        }
    }

    // 获取交易编码
    @PostMapping("/selectBopcod")
    public ResponseSet selectBopcod(@RequestBody TxfmodVo txfmodVo) {
        try{
            settleContext.loadSettleSession(txfmodVo.getRec().getTransName(),  "", "");
            List<Bopcod> bopcods = txfmodService.selectBopcod();
            return ResponseSet.simpleSuccess(bopcods);
        } catch (CommonServiceException e) {
            return ResponseSet.fail(e.getMessage());
        } finally {
            SettleContext.removeUserSession();
        }

    }




}
