package com.brilliance.isc.common.api;

import com.brilliance.mda.runtime.mda.util.Strings;
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.codetable.service.CodetableService;
import com.brilliance.isc.common.esb.account.IAccountFeign;
import com.brilliance.isc.common.liaccv.service.LiaccvService;
import com.brilliance.isc.common.liaccv.vo.LiaccvAcctVo;
import com.brilliance.isc.common.liaccv.vo.LiaccvVo;
import com.brilliance.isc.common.vo.FcdVo;
import com.brilliance.isc.esb.model.inner.InnerReq;
import com.brilliance.isc.esb.model.inner.InnerReq8023;
import com.brilliance.isc.esb.model.inner.InnerRes;
import com.brilliance.isc.esb.model.inner.InnerRes8023;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LiaccvApiServiceImpl implements LiaccvApiService {

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

    @Autowired
    private LiaccvService liaccvService;

    @Autowired
    private CodetableService codetableService;

    @Autowired(required = false)
    private IAccountFeign accountFeign;

    @Override
    public LiaccvResponse calcLiaccv(LiaccvRequest liaccvRequest) {
        liaccvRequest.reset();
        LiaccvVo liaccvVo = liaccvRequest.getLiaccvVo();
        liaccvService.assertLiaccv(liaccvVo);
        return buildLiaccvResponse(liaccvVo);
    }

    @Override
    public LiaccvResponse addLiaccvg(LiaccvRequest liaccvRequest) {
        liaccvRequest.reset();
        LiaccvVo liaccvVo = liaccvRequest.getLiaccvVo();
        liaccvService.addLiaccvg(liaccvVo);
        return buildLiaccvResponse(liaccvVo);
    }

    @Override
    public LiaccvResponse deleteLiaccvg(LiaccvRequest liaccvRequest) {
        liaccvRequest.reset();
        LiaccvVo liaccvVo = liaccvRequest.getLiaccvVo();
        liaccvService.deleteLiaccvg(liaccvVo);
        return buildLiaccvResponse(liaccvVo);
    }


    @Override
    public List<FcdVo> getFcdList(LiaccvRequest liaccvRequest) {
        liaccvRequest.reset();
        LiaccvVo liaccvVo = liaccvRequest.getLiaccvVo();
        return liaccvService.getFcdList(liaccvRequest.getTransName(), liaccvVo);
    }

    @Override
    public LiaccvAcctVo getLiaccvAccountInfo(String account) {
        //组装8023接口请求对象
        InnerReq8023 req8023 = new InnerReq8023(account);
        InnerReq<InnerReq8023> innerReq8023 = new InnerReq<>(req8023);
        LiaccvAcctVo liaccvAcctVo = new LiaccvAcctVo();
        //调用接口
        try {
            InnerRes<InnerRes8023> innerRes8023 = accountFeign.call8023(innerReq8023);
            //分析接口返回码
            if (innerRes8023 == null) {
                liaccvAcctVo.setErrorMsg(Strings.formatText("查询Account8023接口结果为空:$1", account));
                return liaccvAcctVo;
            }
            if (!"AAAAAAA".equals(innerRes8023.header.code)) {
                log.error("查询Account8023接口出错{}, {}", account, innerRes8023.header.description);
                liaccvAcctVo.setErrorMsg(Strings.formatText("查询Account8023接口结果为空:$1", account));
                return liaccvAcctVo;
            }
            //获取接口返回信息,组装liaccvAcctVo
            if (innerRes8023.DATA != null) {
                liaccvAcctVo.setZhangh(innerRes8023.DATA.zhangh);
                liaccvAcctVo.setZhuzwm(innerRes8023.DATA.zhuzwm);
                liaccvAcctVo.setHuobdh(innerRes8023.DATA.huobdh);
                if (com.brilliance.isc.utils.StringUtils.isNotEmpty(liaccvAcctVo.getHuobdh())) {
                    String cur = codetableService.getTblKey(liaccvAcctVo.getHuobdh(), "CURKEY");
                    liaccvAcctVo.setCur(cur);
                }
                liaccvAcctVo.setZhhuye(innerRes8023.DATA.zhhuye);
                liaccvAcctVo.setBliuye(innerRes8023.DATA.bliuye);
                liaccvAcctVo.setDjieye(innerRes8023.DATA.djieye);
                liaccvAcctVo.setKngzye(innerRes8023.DATA.kngzye);
                liaccvAcctVo.setQnriye(innerRes8023.DATA.qnriye);
                liaccvAcctVo.setTouzbz(innerRes8023.DATA.touzbz);
                liaccvAcctVo.setTouzje(innerRes8023.DATA.touzje);
                liaccvAcctVo.setKehhao(innerRes8023.DATA.kehhao);
                return liaccvAcctVo;
            } else {
                liaccvAcctVo.setErrorMsg("查询Account8023接口结果为空!");
                return liaccvAcctVo;
            }
        } catch (Throwable e) {
            log.error("查询Account8023接口出错", e);
            liaccvAcctVo.setErrorMsg(Strings.formatText("查询Account8023接口出错:$1", account));
            return liaccvAcctVo;
        }
    }

    private LiaccvResponse buildLiaccvResponse(LiaccvVo liaccvVo) {
        return LiaccvResponse.builder()
                .oldcur(liaccvVo.getOldcur())
                .oldamt(liaccvVo.getOldamt())
                .chgamt(liaccvVo.getChgamt())
                .concur(liaccvVo.getConcur())
                .newamt(liaccvVo.getNewamt())
                .newresamt(liaccvVo.getNewresamt())
                .relcshpct(liaccvVo.getRelcshpct())
                .pctresamt(liaccvVo.getPctresamt())
                .totcovamt(liaccvVo.getTotcovamt())
                .addinf(liaccvVo.getAddinf())
                .liaccvg(liaccvVo.getLiaccvg())
                .newrol(liaccvVo.getNewrol())
                .oldredflg(liaccvVo.getOldredflg())
                .cshpct(liaccvVo.getCshpct())
                .oldresamt(liaccvVo.getOldresamt())
                .chgresamt(liaccvVo.getChgresamt())
                .macold(liaccvVo.getMacold())
                .objmodpath(liaccvVo.getObjmodpath())
                .addbut(liaccvVo.getAddbut())
                .glebut(liaccvVo.getGlebut())
                .gleflg(liaccvVo.getGleflg())
                .cbeListJson(liaccvVo.getCbeListJson())
                .build();
    }
}
