package com.brilliance.isc.common.api.settle;

import com.brilliance.mda.runtime.mda.util.Strings;
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.api.settle.vo.SettleDetailRequestVo;
import com.brilliance.isc.common.api.settle.vo.SettleDetailResponseVo;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.esb.account.IAccountFeign;
import com.brilliance.isc.common.setfeg.service.SetfegService;
import com.brilliance.isc.common.setfog.service.SetfogService;
import com.brilliance.isc.common.setgll.service.SetgllService;
import com.brilliance.isc.common.setgll.vo.SetgllVo;
import com.brilliance.isc.common.setmod.service.SetmodService;
import com.brilliance.isc.common.setmod.vo.SetmodVo;
import com.brilliance.isc.esb.model.inner.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class SettleDetailApiServiceImpl implements SettleDetailApiService {

    private static Logger log = LoggerFactory.getLogger(SettleDetailApiServiceImpl.class);

    @Autowired
    private SetmodService setmodService;

    @Autowired
    private SetfegService setfegService;

    @Autowired
    private SetfogService setfogService;

    @Autowired
    private SetgllService setgllService;

    @Autowired
    private CodetableService codetableService;

    @Autowired(required = false)
    private IAccountFeign accountFeign;

    private static Map<String, String> maps = new HashMap<>();

    @PostConstruct
    public void init() {
        maps.put("0", "正常");
        maps.put("1", "销户");
        maps.put("2", "只收不付冻结");
        maps.put("3", "封闭冻结");
        maps.put("4", "删除");
        maps.put("5", "未使用");
        maps.put("6", "结清");
        maps.put("7", "打印");
        maps.put("8", "碰库");
        maps.put("9", "不动户");
        maps.put("A", "不动户转收益");
        maps.put("B", "死亡户");
        maps.put("C", "报案户");
        maps.put("D", "请与开户行接洽");
        maps.put("E", "不能在他行销记户");
        maps.put("F", "准客户");
        maps.put("G", "未复核");
        maps.put("R", "被当日冲正");
        maps.put("S", "被隔日冲正");
        maps.put("J", "禁用");
        maps.put("Y", "预销户");
        maps.put("Z", "质押冻结");
        maps.put("T", "凭证在途");
    }

    @Override
    public SettleDetailResponseVo calcSettleDetail(SettleDetailRequestVo payDetailRequestVo) {
        payDetailRequestVo.reset();
        SetmodVo setmodVo = payDetailRequestVo.getSetmodVo();
        //1.计算party信息放到setpty等
        setmodService.ptsChgAssert(setmodVo);
        //计算国外收费
        setfogService.recalcSetfogEog(setmodVo);
        //2.根据费用条目注册对应的账务信息
        setfegService.recalcSetfegEog(setmodVo);
        //3.账务注册 根据交易码+”.settleRegister”)找交易注册类,找不到默认会取DefaultSettleRegister
        setmodService.internalPostSettleRegistersettlement(setmodVo);
        //4.试算账务条目
        for (int i = 0; i < setmodVo.getSetglg().getSetgll().size(); i++) {
            setmodVo.getSetglg().getSetgll().get(i).getSetgrp().reset();
        }
        setmodService.assertSetglg(setmodVo);
        //试算E01报送
        setmodService.assertFale01(setmodVo);
        //5.封装SettleDetailResponseVo
        return SettleDetailResponseVo.builder()
                .setglg(setmodVo.getSetglg())
                .wrksumMap(setmodVo.getWrksumMap())
                .fale01List(setmodVo.getFale01List())
                .build();
    }

    @Override
    public AccountResponseVo getCoreAccountCode(AccountRequestVo accountRequestVo) {
        //组装8091接口请求对象
        InnerReq8091 req8091 = new InnerReq8091(accountRequestVo.getAccount());
        InnerReq<InnerReq8091> innerReq8091 = new InnerReq<>(req8091);
        AccountResponseVo accountResponseVo = new AccountResponseVo();
        //调用接口
        try {
            InnerRes<InnerRes8091> innerRes8091 = accountFeign.call8091(innerReq8091);
            //分析接口返回码
            if (innerRes8091 == null) {
                accountResponseVo.setErrorMsg(Strings.formatText("查询Account8091接口结果为空:$1", accountRequestVo.getAccount()));
                return accountResponseVo;
            }
            if (!"AAAAAAA".equals(innerRes8091.header.code)) {
                log.error("查询Account8091接口出错{}, {}", accountRequestVo.getAccount(), innerRes8091.header.description);
                accountResponseVo.setErrorMsg(Strings.formatText("查询Account8091接口结果为空:$1", accountRequestVo.getAccount()));
                return accountResponseVo;
            }
            //获取接口返回信息,组装AccountResponseVo
            if (innerRes8091.DATA != null && StringUtils.isNotEmpty(innerRes8091.DATA.yewudh)) {
                accountResponseVo.setAcctCode(innerRes8091.DATA.yewudh);
                if (StringUtils.isNotEmpty(innerRes8091.DATA.zhhuye)) {
                    accountResponseVo.setAcctBalance(new BigDecimal(innerRes8091.DATA.zhhuye));
                }
                accountResponseVo.setKehhao(innerRes8091.DATA.kehhao);
                accountResponseVo.setJiluzt(maps.get(innerRes8091.DATA.jiluzt));
                return accountResponseVo;
            } else {
                accountResponseVo.setErrorMsg("查询Account8091接口结果为空!");
                return accountResponseVo;
            }
        } catch (Throwable e) {
            log.error("查询Account8091接口出错", e);
            accountResponseVo.setErrorMsg(Strings.formatText("查询Account8091接口出错:$1", accountRequestVo.getAccount()));
            return accountResponseVo;
        }
    }

    @Override
    public SetgllVo getAcctInfo(SetgllVo setgllVo) {
        setgllService.setActtrmtypAndActflg(setgllVo);
        return setgllVo;
    }

    @Override
    public AccountResponseVo getAccountNature(AccountRequestVo accountRequestVo) {
        //组装8023接口请求对象
        InnerReq8023 req8023 = new InnerReq8023(accountRequestVo.getAccount());
        InnerReq<InnerReq8023> innerReq8023 = new InnerReq<>(req8023);
        AccountResponseVo accountResponseVo = new AccountResponseVo();
        //调用接口
        try {
            InnerRes<InnerRes8023> innerRes8023 = accountFeign.call8023(innerReq8023);
            //分析接口返回码
            if (innerRes8023 == null) {
                accountResponseVo.setErrorMsg(Strings.formatText("查询Account8023接口结果为空:$1", accountRequestVo.getAccount()));
                return accountResponseVo;
            }
            if (!"AAAAAAA".equals(innerRes8023.header.code)) {
                log.error("查询Account8023接口出错{}, {}", accountRequestVo.getAccount(), innerRes8023.header.description);
                accountResponseVo.setErrorMsg(Strings.formatText("查询Account8023接口结果为空:$1", accountRequestVo.getAccount()));
                return accountResponseVo;
            }
            //获取接口返回信息,组装liaccvAcctVo
            if (innerRes8023.DATA != null) {
                accountResponseVo.setWjzhxz(innerRes8023.DATA.wjzhxz);
                String acctNatureName = codetableService.getTblLabel(accountResponseVo.getWjzhxz(), "ACTPRO");
                accountResponseVo.setAcctNatureName(acctNatureName);
                return accountResponseVo;
            } else {
                accountResponseVo.setErrorMsg("查询Account8023接口结果为空!");
                return accountResponseVo;
            }
        } catch (Throwable e) {
            log.error("查询Account8023接口出错", e);
            accountResponseVo.setErrorMsg(Strings.formatText("查询Account8023接口出错:$1", accountRequestVo.getAccount()));
            return accountResponseVo;
        }
    }

}
