Cloud3tpService.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package com.shkpr.service.aimodelpower.services;
  2. import com.global.base.log.LogLevelFlag;
  3. import com.global.base.log.LogPrintMgr;
  4. import com.global.base.tools.EncryptionUtil;
  5. import com.global.base.tools.FastJsonUtil;
  6. import com.global.base.tools.RandomUtil;
  7. import com.shkpr.service.aimodelpower.constants.ApiURI;
  8. import com.shkpr.service.aimodelpower.constants.LogFlagBusiType;
  9. import com.shkpr.service.aimodelpower.dto.ResponseCode;
  10. import com.shkpr.service.aimodelpower.dto.ResponseRes;
  11. import com.shkpr.service.aimodelpower.globalcache.GlobalData;
  12. import com.shkpr.service.aimodelpower.jsonbean.JPStrIdsSK;
  13. import com.shkpr.service.aimodelpower.jsonbean.zilaishui.JP3TPDay;
  14. import com.shkpr.service.aimodelpower.jsonbean.zilaishui.JP3TPHour;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Qualifier;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.http.*;
  19. import org.springframework.remoting.RemoteAccessException;
  20. import org.springframework.retry.annotation.Backoff;
  21. import org.springframework.retry.annotation.Retryable;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.util.StringUtils;
  24. import org.springframework.web.client.RestTemplate;
  25. import javax.annotation.PostConstruct;
  26. /**
  27. * @ClassName Cloud3tpService
  28. * @Description: TODO
  29. * @Author LX
  30. * @Date 2024/8/20
  31. * @Version V1.0
  32. **/
  33. @Service
  34. public class Cloud3tpService {
  35. final String MSG_SUCCESS = "success.";
  36. final String MSG_FAILED = "failed.";
  37. private String strClassName = "";
  38. @SuppressWarnings("all")
  39. @Autowired
  40. @Qualifier("RestTemplateEx")
  41. RestTemplate restTemplate;
  42. @Value("${cloud.3tp.water.service.address:}")
  43. private String mStrAddress;
  44. private String mDayDataSupply = "";
  45. private String mHourDataSupply = "";
  46. private HttpHeaders headers = null;
  47. @PostConstruct
  48. public void init(){
  49. strClassName = this.getClass().getSimpleName();
  50. if (!mStrAddress.endsWith("/"))
  51. mStrAddress += "/";
  52. mDayDataSupply = String.format("%s%s", mStrAddress, "PredictSupply");//预测日供水数据
  53. mHourDataSupply = String.format("%s%s", mStrAddress, "PredictHour");//预测小时供水数据
  54. headers = new HttpHeaders();
  55. headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
  56. headers.add(ApiURI.HEADER_CLIENT_TYPE, GlobalData.getInstance().getServerId());
  57. }
  58. // @Retryable(value = {RemoteAccessException.class},
  59. // maxAttempts = 0, // 首次尝试加上3次重试
  60. // backoff = @Backoff(delay = 200))
  61. public ResponseRes<String> dayDataPredictSupply(final JP3TPDay jsonParam) throws Exception{
  62. ResponseRes<String> resResult = new ResponseRes<String>();
  63. resResult.setRescode(ResponseCode.RESULT_BAD.toStrCode());
  64. resResult.setResmsg(MSG_FAILED);
  65. resResult.setResdata("");
  66. HttpEntity<String> request = new HttpEntity<>(jsonParam.toJsonStr(), headers);
  67. try {
  68. ResponseEntity<String> response = restTemplate.postForEntity(mDayDataSupply, request, String.class);
  69. if (response.getStatusCode() == HttpStatus.OK){
  70. String strBody = response.getBody();
  71. return FastJsonUtil.fromJSONByGson(strBody, ResponseRes.class);
  72. }
  73. }catch (Exception e){
  74. 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()));
  75. //达到maxAttempts次数将返回RemoteAccessException
  76. throw new RemoteAccessException("Retry...");
  77. }
  78. return resResult;
  79. }
  80. // @Retryable(value = {RemoteAccessException.class},
  81. // maxAttempts = 0, // 首次尝试加上3次重试
  82. // backoff = @Backoff(delay = 200))
  83. public ResponseRes<String> dayHourPredictSupply(final JP3TPHour jsonParam) throws Exception{
  84. ResponseRes<String> resResult = new ResponseRes<String>();
  85. resResult.setRescode(ResponseCode.RESULT_BAD.toStrCode());
  86. resResult.setResmsg(MSG_FAILED);
  87. resResult.setResdata("");
  88. HttpEntity<String> request = new HttpEntity<>(jsonParam.toJsonStr(), headers);
  89. try {
  90. ResponseEntity<String> response = restTemplate.postForEntity(mHourDataSupply, request, String.class);
  91. if (response.getStatusCode() == HttpStatus.OK){
  92. String strBody = response.getBody();
  93. return FastJsonUtil.fromJSONByGson(strBody, ResponseRes.class);
  94. }
  95. }catch (Exception e){
  96. 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()));
  97. //达到maxAttempts次数将返回RemoteAccessException
  98. throw new RemoteAccessException("Retry...");
  99. }
  100. return resResult;
  101. }
  102. }