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

import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.Bch;
import com.brilliance.isc.bo.PilotBchBop;
import com.brilliance.isc.common.api.bch.service.BchService;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.fecism.vo.CalcFeeVo;
import com.brilliance.isc.common.setfeg.vo.SetfelVo;
import com.brilliance.isc.mda.dao.PilotBchBopMapper;
import com.brilliance.isc.vo.ResponseSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
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.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bopcalcom")
public class BopCalPilotController {

    @Autowired
    private SettleContext settleContext;

    @Autowired
    private CodetableService codetableService;

    @Autowired
    private PilotBchBopMapper pilotBchBopMapper;

    @Autowired
    private BchService bchService;

    @PostMapping("/bopcalcom/pilotbopbch")
    public ResponseSet<String> bopPilotBch(@RequestBody Map<String, String> params) {
        try {
            settleContext.simpleLoadSettleSession();
            //通过试点行列表，判断组件中是否能进行申报管理数据录入以及维护交易处理是否能增删改 N-不允许  Y-允许
            String enableflg = "N";

            //子系统下线开启后，判断试点机构
            //获取经办机构
            String branch = params.get("ownextkey");
            if(MdaUtils.isEmpty(branch)&&!MdaUtils.isEmpty(SettleContext.getUserSession().getWrkbch())){
                branch = SettleContext.getUserSession().getWrkbch().getBranch();
            }
            //若无经办机构，取柜员归属机构
            if(MdaUtils.isEmpty(branch)){
                branch = SettleContext.getUserSession().getOrgBch().getBranch();
            }

            //查询是否为试点行
            //总行直接返回 N 不允许做增删改
            if("1000".equals(branch)){
                enableflg = "N";
            }else{
                //非总行查询记账机构
                Bch accbranch = bchService.getDirectAccBch(branch);
                if(accbranch != null){
                    //机构为试点行时才启用 随业务申报选项 以及 维护交易功能
                    PilotBchBop pilotBchBop= pilotBchBopMapper.selectPilotBchBopByBranch(accbranch.getBranch());
                    if(pilotBchBop != null){
                        if("Y".equals(pilotBchBop.getIsPilot())){
                            enableflg = "Y";
                        }
                    }
                }
            }

            return ResponseSet.simpleSuccess(enableflg);
        } catch (Exception e) {
            throw e;
        } finally {
            SettleContext.removeUserSession();
        }
    }


}
