package com.brilliance.isc.common.limmod.service.impl;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.context.SpringEnvContext;
import com.brilliance.isc.common.esb.cecm.ICecmFeign;
import com.brilliance.isc.common.esb.clm.IClmFeign;
import com.brilliance.isc.common.exception.CommonServiceException;
import com.brilliance.isc.common.limmod.service.LimOptionService;
import com.brilliance.isc.common.limmod.service.LimmodService;
import com.brilliance.isc.common.limmod.vo.LimmodVo;
import com.brilliance.isc.common.limmod.vo.ZsqInfoVo;
import com.brilliance.isc.common.register.lim.ILimRegister;
import com.brilliance.isc.esb.model.inner.InnerReq;
import com.brilliance.isc.esb.model.inner.InnerReqCM51;
import com.brilliance.isc.esb.model.inner.InnerRes;
import com.brilliance.isc.esb.model.inner.InnerResCM51;
import com.brilliance.isc.utils.StringUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("limZsq")
public class LimZsqServiceImpl implements LimOptionService<InnerResCM51> {


    @Autowired
    private LimmodService limmodService;
    @Autowired
    private CodetableService codetableService;
    @Autowired(required = false)
    private ICecmFeign cecmFeign;
    private final Logger log = LoggerFactory.getLogger(LimZsqServiceImpl.class);

    @Override
    public void assertLim(LimmodVo limmodVo) {
        limmodService.LimRegParstm(limmodVo);
        LimRegParstm(limmodVo);
    }

    @Override
    public void callEsb(LimmodVo limmodVo) {

        InnerReqCM51 innerResCM51 = new InnerReqCM51();

        String extkey = limmodVo.getLimpts().getWrk().getExtkey();

        if(!StringUtils.isEmpty(limmodVo.getLimpts().getOth().getExtkey())){
            extkey=limmodVo.getLimpts().getOth().getExtkey();
        }
        if (!StringUtils.isEmpty(extkey)) {
            innerResCM51.extkey = extkey.split("-")[0];
        }
        if ("B".equals(limmodVo.getLimpts().getWrk().getPtytyp())) {
            innerResCM51.extkey = limmodVo.getEcifno();
        }
        InnerReq<InnerReqCM51> innerReqCM51InnerReq = new InnerReq<>(innerResCM51);
        InnerRes<InnerResCM51> innerResCM51InnerRes = cecmFeign.callCM51(innerReqCM51InnerReq);

        if ("0".equals(innerResCM51InnerRes.header.code)) {
            responseMapping(innerResCM51InnerRes, limmodVo);
        } else {
            throw new CommonServiceException(String.format("转授权查询失败:%s", innerResCM51InnerRes.header.description));
        }

    }

    @Override
    public void limDataCal(LimmodVo limmodVo) {

    }

    @Override
    public void LimRegParstm(LimmodVo limmodVo) {
        if ("E".equals(limmodVo.getLimpts().getErrorflg())) {
            return;
        }
        limDataCal(limmodVo);
        callEsb(limmodVo);

    }

    @Override
    public void responseMapping(InnerRes<InnerResCM51> innerResCM51InnerRes, LimmodVo limmodVo) {
        List<ZsqInfoVo> zsqInfoVoList = new ArrayList<>();
        innerResCM51InnerRes.DATA.lstcm51.forEach(itemcm51 -> {
            ZsqInfoVo zsqInfoVo = new ZsqInfoVo();
            zsqInfoVo.setHxkehh(itemcm51.hxkehh);
            zsqInfoVo.setHxkemc(itemcm51.hxkemc);
            zsqInfoVoList.add(zsqInfoVo);
        });
        limmodVo.setZsqInfos(zsqInfoVoList);
    }

    @Override
    public void saveTables(LimmodVo limmodVo) {

    }

}
