Procházet zdrojové kódy

水量调度对接实际预测接口逻辑编写 暂停原模拟预测数据添加逻辑

1037015548@qq.com před 1 rokem
rodič
revize
5558de4172

+ 73 - 15
src/main/java/com/shkpr/service/aimodelpower/bizmgr/KprAimWaterCollecationBizFun.java

@@ -15,8 +15,11 @@ import com.shkpr.service.aimodelpower.dto.TraceRunnable;
 import com.shkpr.service.aimodelpower.dto.zilaishuiModel.InPumpModel;
 import com.shkpr.service.aimodelpower.globalmgr.ThreadTaskMgr;
 import com.shkpr.service.aimodelpower.globalmgr.TraceLogMgr;
+import com.shkpr.service.aimodelpower.jsonbean.zilaishui.JP3TPDay;
+import com.shkpr.service.aimodelpower.jsonbean.zilaishui.JP3TPHour;
 import com.shkpr.service.aimodelpower.jsonbean.zilaishui.JPTbMHourWater;
 import com.shkpr.service.aimodelpower.jsonbean.zilaishui.JPTbMWater;
+import com.shkpr.service.aimodelpower.services.ServiceMgrProxy;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
@@ -638,13 +641,40 @@ public class KprAimWaterCollecationBizFun {
                                                         }
                                                         numAll += Double.valueOf(mapEntity.get("value").toString());
                                                     }
-                                                    //TODO 数据库操作
-                                                    Map<String, Object> recordAllEntity = new LinkedHashMap<>();//需要添加的实体数据,此处要用有序map
+                                                    //TODO 数据库操作 A 调用对应时间预测接口 B 在修改实际值
+
                                                     DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
-                                                    recordAllEntity.put("Date", dateNow.withYear(LocalDateTime.now().getYear()).toLocalDate().format(formatter2));
+                                                    String dateStr = dateNow.withYear(LocalDateTime.now().getYear()).toLocalDate().format(formatter2);
+
+                                                    //TODO A
+                                                    JP3TPDay tpDay = new JP3TPDay();
+                                                    tpDay.setOrgId(orgId);
+                                                    tpDay.setSdate(dateStr);
+                                                    tpDay.setEndate(dateStr);
+                                                    try {
+                                                        ResponseRes<String> tpRes = ServiceMgrProxy.getInstance()
+                                                                .applyCloud3tpServiceApi().dayDataPredictSupply(tpDay);
+                                                        if(ResponseCode.RESULT_BAD.toStrCode().equals(tpRes.getRescode())){
+                                                            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR
+                                                                    , mStrClassName
+                                                                    , mStrClassName
+                                                                    , String.format("预测日数据"+dateStr+"调用失败 ERROR:{%s} ",
+                                                                            tpRes.getResdata()));
+                                                            break;
+                                                        }
+                                                    }catch(Exception ex){
+                                                        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR
+                                                                , mStrClassName
+                                                                , mStrClassName
+                                                                , String.format("预测日数据"+dateStr+"调用异常 ERROR:{%s} ",
+                                                                        ex.getLocalizedMessage()));
+                                                    }
+                                                    //TODO B
+                                                    Map<String, Object> recordAllEntity = new LinkedHashMap<>();//需要添加的实体数据,此处要用有序map
+                                                    recordAllEntity.put("Date", dateStr);
                                                     recordAllEntity.put("Month", dateNow.withYear(LocalDateTime.now().getYear()).getMonth().getValue());
                                                     recordAllEntity.put("Week", dateNow.withYear(LocalDateTime.now().getYear()).getDayOfWeek().getValue());
-                                                    recordAllEntity.put("ActualWaterWithdrawals", "");
+//                                                    recordAllEntity.put("ActualWaterWithdrawals", "");
 
                                                     //实际从该时间查询结果中得出(当前年当前日的实际数据)
                                                     String newStartDate = dateNow.withYear(LocalDateTime.now().getYear()).format(formatter);
@@ -675,15 +705,15 @@ public class KprAimWaterCollecationBizFun {
                                                     }
 
                                                     recordAllEntity.put("ActualWaterSupply", actualWaterSupply);
-                                                    recordAllEntity.put("ForecastWaterWithdrawals", "");
+//                                                    recordAllEntity.put("ForecastWaterWithdrawals", "");
                                                     //TODO 随机数逻辑
                                                     int randomUpOrDown = ThreadLocalRandom.current().nextInt(2);//随机向上或者向下 0表示向下 1表示向上
                                                     Double randomWater = randomUpOrDown == 0 ?
                                                             numAge - (numAge * (randomCode / 100)) :
                                                             numAge + (numAge * (randomCode / 100));
-                                                    recordAllEntity.put("ForecastActualWaterSupply", randomWater);
+//                                                    recordAllEntity.put("ForecastActualWaterSupply", randomWater);
                                                     recordAllEntity.put("isAbnormal", 0);
-                                                    recordAllEntity.put("isForecast", 0);
+                                                    recordAllEntity.put("isForecast", 1);
                                                     recordAllEntity.put("LastModifyTime", LocalDateTime.now().format(formatter));
                                                     // 如果找到了org_id,就将其赋值给orgId,否则orgId为null
                                                     recordAllEntity.put("orgId", orgId);
@@ -797,23 +827,51 @@ public class KprAimWaterCollecationBizFun {
                                                     JSONArray pumpArray = ((JSONObject)pumpItem.get("outRoom")).getJSONArray("pumps");
                                                     //TODO 一天24小时
                                                     for (int j = 0;j<24;j++) {
-                                                        //TODO 数据库操作
-                                                        Map<String, Object> recordAllEntity = new LinkedHashMap<>();//需要添加的实体数据,此处要用有序map
+                                                        //TODO 数据库操作 A 调用对应时间预测接口 B 在修改实际值
                                                         DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
-                                                        recordAllEntity.put("Date", dateNow.withYear(LocalDateTime.now().getYear()).toLocalDate().format(formatter2));
+                                                        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("HH:mm:ss");
+                                                        String dateStr = dateNow.withYear(LocalDateTime.now().getYear()).toLocalDate().format(formatter2);
                                                         // 创建一个LocalTime实例,设置为指定小时,分钟和秒数为0
                                                         LocalTime time = LocalTime.of(j, 0, 0);
                                                         // 创建一个DateTimeFormatter用于格式化时间
-                                                        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("HH:mm:ss");
-                                                        recordAllEntity.put("Hour", time.format(formatter3));
-                                                        recordAllEntity.put("HourForecastWaterWithdrawals", null);
+                                                        String timeStr = time.format(formatter3);
+
+                                                        //TODO A
+                                                        JP3TPHour tpHour = new JP3TPHour();
+                                                        tpHour.setOrgId(orgId);
+                                                        tpHour.setDate(dateStr);
+                                                        try {
+                                                            ResponseRes<String> tpRes = ServiceMgrProxy.getInstance()
+                                                                    .applyCloud3tpServiceApi().dayHourPredictSupply(tpHour);
+                                                            if(ResponseCode.RESULT_BAD.toStrCode().equals(tpRes.getRescode())){
+                                                                LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR
+                                                                        , mStrClassName
+                                                                        , mStrClassName
+                                                                        , String.format("预测小时数据"+dateStr+"调用失败 ERROR:{%s} ",
+                                                                                tpRes.getResdata()));
+                                                                break;
+                                                            }
+                                                        }catch(Exception ex){
+                                                            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR
+                                                                    , mStrClassName
+                                                                    , mStrClassName
+                                                                    , String.format("预测小时数据"+dateStr+"调用异常 ERROR:{%s} ",
+                                                                            ex.getLocalizedMessage()));
+                                                        }
+
+                                                        //TODO B
+                                                        Map<String, Object> recordAllEntity = new LinkedHashMap<>();//需要添加的实体数据,此处要用有序map
+
+                                                        recordAllEntity.put("Date", dateStr);
+                                                        recordAllEntity.put("Hour", timeStr);
+//                                                        recordAllEntity.put("HourForecastWaterWithdrawals", null);
                                                         //TODO 随机数逻辑
                                                         int randomUpOrDown = ThreadLocalRandom.current().nextInt(2);//随机向上或者向下 0表示向下 1表示向上
                                                         Double randomWater = randomUpOrDown == 0 ?
                                                                 numAge - (numAge * (randomCode / 100)) :
                                                                 numAge + (numAge * (randomCode / 100));
-                                                        recordAllEntity.put("HourForecastActualWaterSupply", randomWater);
-                                                        recordAllEntity.put("HourActualWaterWithdrawals", "");
+//                                                        recordAllEntity.put("HourForecastActualWaterSupply", randomWater);
+//                                                        recordAllEntity.put("HourActualWaterWithdrawals", "");
                                                         //实际从该时间查询结果中得出
                                                         List<Map<String,Object>> newRecordAllRes = getWaterTapApi()
                                                                 .getWaterCollectionRecordAllListAll(" WHERE 1=1 " +

+ 28 - 4
src/main/java/com/shkpr/service/aimodelpower/dbdao/shizilaishuiDataSource/WaterZILAISHUIDao.java

@@ -230,8 +230,20 @@ public class WaterZILAISHUIDao {
     public int insertOrUpdateTbmWater(Map<String,Object> value){
         try{
             String sql = "INSERT INTO tb_m_water " +
-                    "(\"Date\",\"Month\",\"Week\",\"ActualWaterWithdrawals\",\"ActualWaterSupply\"," +
-                    " \"ForecastWaterWithdrawals\",\"ForecastActualWaterSupply\",\"isAbnormal\",\"isForecast\",\"LastModifyTime\",\"orgId\" ) VALUES ";
+                    "(\"Date\",\"Month\",\"Week\"";
+            if(!ObjectUtils.isEmpty(value.get("ActualWaterWithdrawals"))){
+                sql +=  " , \"ActualWaterWithdrawals\" ";
+            }
+//            if(!ObjectUtils.isEmpty(value.get("ActualWaterSupply"))){
+                sql +=  " ,\"ActualWaterSupply\"";
+//            }
+            if(!ObjectUtils.isEmpty(value.get("ForecastWaterWithdrawals"))){
+                sql +=  " , \"ForecastWaterWithdrawals\"";
+            }
+            if(!ObjectUtils.isEmpty(value.get("ForecastActualWaterSupply"))){
+                sql +=  " , \"ForecastActualWaterSupply\"";
+            }
+            sql +=",\"isAbnormal\",\"isForecast\",\"LastModifyTime\",\"orgId\" ) VALUES ";
             sql +=" ( ";
             int i = 1;
             for (Object key : value.keySet()){
@@ -285,8 +297,20 @@ public class WaterZILAISHUIDao {
     public int insertOrUpdateTbmHourWater(Map<String,Object> value){
         try{
             String sql = "INSERT INTO tb_m_hourwater " +
-                    "(\"Date\",\"Hour\",\"HourForecastWaterWithdrawals\",\"HourForecastActualWaterSupply\"," +
-                    " \"HourActualWaterWithdrawals\",\"HourActualWaterSupply\",\"LastModifyTime\",\"orgId\" ) VALUES ";
+                    "(\"Date\",\"Hour\"";
+            if(!ObjectUtils.isEmpty(value.get("HourForecastWaterWithdrawals"))){
+                sql +=  " , \"HourForecastWaterWithdrawals\" =  '"+value.get("HourForecastWaterWithdrawals")+"' ";
+            }
+            if(!ObjectUtils.isEmpty(value.get("HourForecastActualWaterSupply"))){
+                sql +=  " ,\"HourForecastActualWaterSupply\" =  '"+value.get("HourForecastActualWaterSupply")+"' ";
+            }
+            if(!ObjectUtils.isEmpty(value.get("HourActualWaterWithdrawals"))){
+                sql +=  " , \"HourActualWaterWithdrawals\" =  '"+value.get("HourActualWaterWithdrawals")+"' ";
+            }
+//            if(!ObjectUtils.isEmpty(value.get("HourActualWaterSupply"))){
+                sql +=  " , \"HourActualWaterSupply\"";
+//            }
+            sql+= ",\"LastModifyTime\",\"orgId\" ) VALUES ";
             sql +=" ( ";
             int i = 1;
             for (Object key : value.keySet()){

+ 24 - 0
src/main/java/com/shkpr/service/aimodelpower/jsonbean/zilaishui/JP3TPDay.java

@@ -0,0 +1,24 @@
+package com.shkpr.service.aimodelpower.jsonbean.zilaishui;
+
+import com.global.base.tools.FastJsonUtil;
+import lombok.Data;
+
+/**
+ * @ClassName JP3TPDay
+ * @Description: TODO 调用预测日数据接口参数模型
+ * @Author LX
+ * @Date 2024/8/20
+ * @Version V1.0
+ **/
+@Data
+public class JP3TPDay {
+    private String orgId;
+
+    private String sdate;
+
+    private String endate;
+
+    public String toJsonStr(){
+        return FastJsonUtil.toJSON(this);
+    }
+}

+ 22 - 0
src/main/java/com/shkpr/service/aimodelpower/jsonbean/zilaishui/JP3TPHour.java

@@ -0,0 +1,22 @@
+package com.shkpr.service.aimodelpower.jsonbean.zilaishui;
+
+import com.global.base.tools.FastJsonUtil;
+import lombok.Data;
+
+/**
+ * @ClassName JP3TPDay
+ * @Description: TODO 调用预测小时数据接口参数模型
+ * @Author LX
+ * @Date 2024/8/20
+ * @Version V1.0
+ **/
+@Data
+public class JP3TPHour {
+    private String orgId;
+
+    private String date;
+
+    public String toJsonStr(){
+        return FastJsonUtil.toJSON(this);
+    }
+}

+ 111 - 0
src/main/java/com/shkpr/service/aimodelpower/services/Cloud3tpService.java

@@ -0,0 +1,111 @@
+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, "PredictSupply");//预测小时供水数据
+
+        headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
+        headers.add(ApiURI.HEADER_CLIENT_TYPE, GlobalData.getInstance().getServerId());
+    }
+
+    @Retryable(value = {RemoteAccessException.class}, maxAttempts = 3, backoff = @Backoff(delay = 200L, multiplier = 1))
+    public ResponseRes<String> dayDataPredictSupply(final JP3TPDay jsonParam) throws Exception{
+        ResponseRes<String> resResult = new ResponseRes<String>();
+        resResult.setRescode(ResponseCode.RESULT_BAD.toStrCode());
+        resResult.setResmsg(MSG_FAILED);
+        resResult.setResdata("");
+
+        HttpEntity<String> request = new HttpEntity<>(jsonParam.toJsonStr(), headers);
+        try {
+            ResponseEntity<String> 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 = 3, backoff = @Backoff(delay = 200L, multiplier = 1))
+    public ResponseRes<String> dayHourPredictSupply(final JP3TPHour jsonParam) throws Exception{
+        ResponseRes<String> resResult = new ResponseRes<String>();
+        resResult.setRescode(ResponseCode.RESULT_BAD.toStrCode());
+        resResult.setResmsg(MSG_FAILED);
+        resResult.setResdata("");
+
+        HttpEntity<String> request = new HttpEntity<>(jsonParam.toJsonStr(), headers);
+        try {
+            ResponseEntity<String> 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;
+    }
+
+}

+ 4 - 0
src/main/java/com/shkpr/service/aimodelpower/services/ServiceMgrProxy.java

@@ -7,6 +7,7 @@ public class ServiceMgrProxy {
     private volatile CloudDataService dataService = null;
     private volatile CloudGWService gwService = null;
     private volatile CloudTaskService taskService= null;
+    private volatile Cloud3tpService tpService= null;
     private static volatile ServiceMgrProxy msInstance = null;
     public static ServiceMgrProxy getInstance(){
         if (msInstance == null){
@@ -30,6 +31,8 @@ public class ServiceMgrProxy {
             gwService = (CloudGWService) SpringContextUtil.getBean(CloudGWService.class);
         if (taskService == null)
             taskService = (CloudTaskService) SpringContextUtil.getBean(CloudTaskService.class);
+        if (tpService == null)
+            tpService = (Cloud3tpService) SpringContextUtil.getBean(Cloud3tpService.class);
     }
 
     public CloudBaseService applyBaseServiceApi() { return baseService; }
@@ -38,4 +41,5 @@ public class ServiceMgrProxy {
 
     public CloudGWService applyDataGWServiceApi() {return gwService;}
     public CloudTaskService applyTaskServiceApi() {return taskService;}
+    public Cloud3tpService applyCloud3tpServiceApi() {return tpService;}
 }