package com.shkpr.service.aimodelpower.services; import com.global.base.log.LogLevelFlag; import com.global.base.log.LogPrintMgr; import com.global.base.tools.EncryptionUtil; import com.global.base.tools.FastJsonUtil; import com.global.base.tools.RandomUtil; import com.shkpr.service.aimodelpower.constants.ApiURI; import com.shkpr.service.aimodelpower.constants.LogFlagBusiType; import com.shkpr.service.aimodelpower.dto.ResponseCode; import com.shkpr.service.aimodelpower.dto.ResponseRes; import com.shkpr.service.aimodelpower.globalcache.GlobalData; import com.shkpr.service.aimodelpower.jsonbean.JPStrIdsSK; import com.shkpr.service.aimodelpower.jsonbean.zilaishui.JP3TPDay; import com.shkpr.service.aimodelpower.jsonbean.zilaishui.JP3TPHour; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; import org.springframework.remoting.RemoteAccessException; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import javax.annotation.PostConstruct; /** * @ClassName Cloud3tpService * @Description: TODO * @Author LX * @Date 2024/8/20 * @Version V1.0 **/ @Service public class Cloud3tpService { final String MSG_SUCCESS = "success."; final String MSG_FAILED = "failed."; private String strClassName = ""; @SuppressWarnings("all") @Autowired @Qualifier("RestTemplateEx") RestTemplate restTemplate; @Value("${cloud.3tp.water.service.address:}") private String mStrAddress; private String mDayDataSupply = ""; private String mHourDataSupply = ""; private HttpHeaders headers = null; @PostConstruct public void init(){ strClassName = this.getClass().getSimpleName(); if (!mStrAddress.endsWith("/")) mStrAddress += "/"; mDayDataSupply = String.format("%s%s", mStrAddress, "PredictSupply");//预测日供水数据 mHourDataSupply = String.format("%s%s", mStrAddress, "PredictHour");//预测小时供水数据 headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); headers.add(ApiURI.HEADER_CLIENT_TYPE, GlobalData.getInstance().getServerId()); } // @Retryable(value = {RemoteAccessException.class}, // maxAttempts = 0, // 首次尝试加上3次重试 // backoff = @Backoff(delay = 200)) public ResponseRes dayDataPredictSupply(final JP3TPDay jsonParam) throws Exception{ ResponseRes resResult = new ResponseRes(); resResult.setRescode(ResponseCode.RESULT_BAD.toStrCode()); resResult.setResmsg(MSG_FAILED); resResult.setResdata(""); HttpEntity request = new HttpEntity<>(jsonParam.toJsonStr(), headers); try { ResponseEntity response = restTemplate.postForEntity(mDayDataSupply, request, String.class); if (response.getStatusCode() == HttpStatus.OK){ String strBody = response.getBody(); return FastJsonUtil.fromJSONByGson(strBody, ResponseRes.class); } }catch (Exception e){ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_WARN, LogFlagBusiType.BUSI_CALL_DATA_AS.toStrValue(), strClassName, String.format("dayDataPredictSupply from Cloud3tpService(url:%s) failed(%s)...", mDayDataSupply, e.getMessage())); //达到maxAttempts次数将返回RemoteAccessException throw new RemoteAccessException("Retry..."); } return resResult; } // @Retryable(value = {RemoteAccessException.class}, // maxAttempts = 0, // 首次尝试加上3次重试 // backoff = @Backoff(delay = 200)) public ResponseRes dayHourPredictSupply(final JP3TPHour jsonParam) throws Exception{ ResponseRes resResult = new ResponseRes(); resResult.setRescode(ResponseCode.RESULT_BAD.toStrCode()); resResult.setResmsg(MSG_FAILED); resResult.setResdata(""); HttpEntity request = new HttpEntity<>(jsonParam.toJsonStr(), headers); try { ResponseEntity response = restTemplate.postForEntity(mHourDataSupply, request, String.class); if (response.getStatusCode() == HttpStatus.OK){ String strBody = response.getBody(); return FastJsonUtil.fromJSONByGson(strBody, ResponseRes.class); } }catch (Exception e){ LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_WARN, LogFlagBusiType.BUSI_CALL_DATA_AS.toStrValue(), strClassName, String.format("dayHourPredictSupply from Cloud3tpService(url:%s) failed(%s)...", mHourDataSupply, e.getMessage())); //达到maxAttempts次数将返回RemoteAccessException throw new RemoteAccessException("Retry..."); } return resResult; } }