package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base;

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxPcnHttpService;
import cn.estsh.i3plus.mes.pcn.api.iservice.base.IConfigService;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesConfig;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author xiangming.liao
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Service
public class SxPcnHttpService implements ISxPcnHttpService {
    @Autowired
    private IConfigService configService;

    @Override
    public String getRequestUrl(String organizeCode, String cfgCode) {
        MesConfig configGateWay = configService.getMesConfigByCfgCode(MesPcnEnumUtil.CONFIG_TYPE.GATEWAY_IP.getCode(), organizeCode);
        MesConfig config = configService.getMesConfigByCfgCode(cfgCode, organizeCode);
        if (configGateWay == null || StringUtils.isEmpty(configGateWay.getCfgValue())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_FORMAT.getCode())
                    .setErrorDetail("配置中未配置网关，无法调用主服务接口")
                    .build();
        }
        if (config == null || StringUtils.isEmpty(config.getCfgValue())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_FORMAT.getCode())
                    .setErrorDetail("配置中未配置[%s]", cfgCode)
                    .build();
        }
        return configGateWay.getCfgValue() + config.getCfgValue();
    }
}
