package com.brilliance.isc.common.api;

import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import com.brilliance.isc.bo.Pta;
import com.brilliance.isc.bo.model.Dftcre;
import com.brilliance.isc.common.api.dftcre.service.DftcreApiService;
import com.brilliance.isc.common.api.dftcre.vo.DftcreQueryVo;
import com.brilliance.isc.common.api.dftcre.vo.DftcreRequest;
import com.brilliance.isc.common.api.dftcre.vo.DftcreResponse;
import com.brilliance.isc.common.cache.pta.PtaCacheService;
import com.brilliance.isc.common.converter.DtoMapper;
import com.brilliance.isc.common.dftcre.service.DftcreService;
import com.brilliance.isc.common.esb.account.IAccountFeign;
import com.brilliance.isc.esb.model.inner.*;
import com.brilliance.isc.utils.BeanUtils;
import com.brilliance.isc.vo.component.dftcre.DftcreVo;
import com.brilliance.isc.vo.model.DfdgrpVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class DftcreApiServiceImpl implements DftcreApiService {

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

    @Autowired
    private DftcreService dftcreService;

    @Autowired
    private PtaCacheService ptaCacheService;

    @Autowired(required = false)
    private IAccountFeign accountFeign;

    @Override
    public DftcreResponse assertDftcre(DftcreRequest request) {
        request.reset();
        DftcreVo dftcreVo = request.getDftcre();
        BeanUtils.copyProperties(request.getRec(), dftcreVo.getRec());
        dftcreVo.setCbsMap(request.getCbsMap());
        dftcreVo.setPtsptaList(request.getPtsList());
        Dftcre dftcre = DtoMapper.INSTANCE.transfer(request.getDftcre());
        dftcreService.assertDftcre(dftcre);
        dftcreVo = DtoMapper.INSTANCE.transfer(dftcre);
        return DftcreResponse.builder()
               .dftcre(dftcreVo)
               .build();
    }

    /**
     *   source:dftp.0003.script
     */
    public String getDfuflg(DfdgrpVo dfdgrp){
        String dfuflg = dfdgrp.getRec().getDfuflg();
        if(!dfdgrp.getRec().isModified("dfuflg")) {
            String ptaInr = dfdgrp.getDff().getPtainr();
            Pta pta = ptaCacheService.getPtaByInr(ptaInr);
            if (pta != null) {
                String cty = Strings.mid(pta.getBic(),5,2);
                if(MdaUtils.compareTo(cty, "CN") == 0) {
                    dfuflg = "1";
                } else {
                    dfuflg = "2";
                }
            } else {
                dfuflg = "";
            }
        }
        return dfuflg;
    }

    @Override
    public String getEcifName(DftcreQueryVo vo) {
        String ecifname = "";
        try {
            String ecifno = vo.getEcifno();
            InnerReq0905 req0905 = new InnerReq0905();
            req0905.kehuno = ecifno;
            InnerReq<InnerReq0905> innerReq0905 = new InnerReq<>(req0905);
            InnerRes<InnerRes0905> innerRes0905 = accountFeign.call0905(innerReq0905);
            if (innerRes0905 != null && "AAAAAAA".equals(innerRes0905.header.code) && innerRes0905.DATA != null) {
                ecifname = innerRes0905.DATA.ptynam;
            }
        } catch (Exception e) {
            log.warn("调用ECIF0905接口出现异常,错误信息：{}", e.getMessage());
            ecifname = "";
        }
        return ecifname;
    }

    @Override
    public HashMap<String, String> getEcifCnAndEnName(DftcreQueryVo vo) {
        HashMap<String, String> result = new HashMap<>();
        String ecifCnName = "";
        String ecifEnName = "";
        try {
            String ecifno = vo.getEcifno();
            InnerReq0904 req0904 = new InnerReq0904();
            req0904.extkey = ecifno;
            InnerReq<InnerReq0904> innerReq0904 = new InnerReq<>(req0904);
            InnerRes<InnerRes0904> innerRes0904 = accountFeign.call0904(innerReq0904);
            if (innerRes0904 != null && "AAAAAAA".equals(innerRes0904.header.code) && innerRes0904.DATA != null) {
                List<InnerRes0904.Item0904> lst0904 = innerRes0904.DATA.lst0904;
                if (lst0904 != null && lst0904.size() > 0) {
                    InnerRes0904.Item0904 item0904 = lst0904.get(0);
                    ecifCnName = item0904.tbxx03;
                    ecifEnName = item0904.tbxx05;
                }
            }
        } catch (Exception e) {
            log.warn("调用ECIF0904接口出现异常,错误信息：{}", e.getMessage());
        }
        result.put("ecifCnName",ecifCnName);
        result.put("ecifEnName",ecifEnName);
        return result;
    }
}
