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

import cn.estsh.i3plus.ext.mes.api.base.ISxMesHttpService;
import cn.estsh.i3plus.mes.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.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 wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Service
public class SxMesHttpService implements ISxMesHttpService {

    @Autowired
    private IConfigService configService;

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