Sfoglia il codice sorgente

自来水预测数据接口 增加预测数据当前月自添加机制, 自添加机制增添泵房数据预测

1037015548@qq.com 1 anno fa
parent
commit
dd2abf58e4

+ 346 - 0
src/main/java/com/shkpr/service/aimodelpower/bizmgr/KprAimWaterCollecationBizFun.java

@@ -1,5 +1,7 @@
 package com.shkpr.service.aimodelpower.bizmgr;
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.global.base.log.LogLevelFlag;
 import com.global.base.log.LogPrintMgr;
 import com.global.base.tools.FastJsonUtil;
@@ -45,6 +47,255 @@ public class KprAimWaterCollecationBizFun {
     private static final String mStrClassName = "KprAimTapWaterBizFun";
     private static final String EMPTY_NULL = "NULL";
 
+    static JSONObject pumpObj = JSONObject.parseObject("{\n" +
+            "    \"UZD299F12944DC10957HJ\": {\n" +
+            "        \"outRoom\": {\n" +
+            "            \"pumps\": [\n" +
+            "                {\n" +
+            "                    \"label\": \"1\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"2\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"3\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"4\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"5\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"6\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"7\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                }\n" +
+            "            ]\n" +
+            "        }\n" +
+            "    },\n" +
+            "    \"UZD299F12DF5241082772\": {\n" +
+            "        \"outRoom\": {\n" +
+            "            \"pumps\": [\n" +
+            "                {\n" +
+            "                    \"label\": \"1\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"2\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"3\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"4\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"5\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"6\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"7\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"8\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"9\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"10\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                }\n" +
+            "            ]\n" +
+            "        }\n" +
+            "    },\n" +
+            "    \"UZD299F1301D04107668V\": {\n" +
+            "        \"outRoom\": {\n" +
+            "            \"pumps\": [\n" +
+            "                {\n" +
+            "                    \"label\": \"1\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"2\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"3\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"4\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"5\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"6\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"7\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"8\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                }\n" +
+            "            ]\n" +
+            "        }\n" +
+            "    },\n" +
+            "    \"UZD299F133DE5C1066A5L\": {\n" +
+            "        \"outRoom\": {\n" +
+            "            \"pumps\": [\n" +
+            "                {\n" +
+            "                    \"label\": \"1\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"2\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"3\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"4\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"5\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"6\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                }\n" +
+            "            ]\n" +
+            "        }\n" +
+            "    },\n" +
+            "    \"UZD299F1389E6010476AV\": {\n" +
+            "        \"outRoom\": {\n" +
+            "            \"pumps\": [\n" +
+            "                {\n" +
+            "                    \"label\": \"1\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"2\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"3\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"4\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"5\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"6\",\n" +
+            "                    \"status\": 0,\n" +
+            "                    \"forecastStatus\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"7\",\n" +
+            "                    \"status\": 1,\n" +
+            "                    \"forecastStatus\": 1\n" +
+            "                }\n" +
+            "            ]\n" +
+            "        }\n" +
+            "    },\n" +
+            "    \"UZD299F139CB2410181HP\": {\n" +
+            "        \"outRoom\": {\n" +
+            "            \"pumps\": [\n" +
+            "                {\n" +
+            "                    \"label\": \"1\",\n" +
+            "                    \"status\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"2\",\n" +
+            "                    \"status\": 0\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"3\"\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"4\"\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"5\"\n" +
+            "                },\n" +
+            "                {\n" +
+            "                    \"label\": \"6\"\n" +
+            "                }\n" +
+            "            ]\n" +
+            "        }\n" +
+            "    }\n" +
+            "}");
+
     public static WaterCollecationService getWaterTapWaterApi(){
         return DBMgrProxy.getInstance().applyWaterCollecationService();
     }
@@ -541,6 +792,9 @@ public class KprAimWaterCollecationBizFun {
                                                 String orgId = orgIdOptional.orElse(null);
                                                 if (!StringUtils.isEmpty(orgId)) {
                                                     //预测比对时间如果不是今年,则变为今年的时间
+                                                    //TODO 取泵号
+                                                    JSONObject pumpItem = (JSONObject)pumpObj.get(orgId);
+                                                    JSONArray pumpArray = ((JSONObject)pumpItem.get("outRoom")).getJSONArray("pumps");
                                                     //TODO 一天24小时
                                                     for (int j = 0;j<24;j++) {
                                                         //TODO 数据库操作
@@ -583,6 +837,57 @@ public class KprAimWaterCollecationBizFun {
                                                                     , String.format("Batch insertYuceDataScheduled ERROR:{%s} ",
                                                                             "新增或修改失败"));
                                                         }
+
+                                                        //TODO ③ 泵集合数据添加或修改
+                                                        // 生成[1, pumpArray.size()]范围内的随机数
+                                                        int randomNum = (int) (Math.random() * (pumpArray.size() - 1)) + 1;
+                                                        List<Double> splitRandom = splitValueRandomlyWithZeros(Double.valueOf((hourActualWaterSupply==null
+                                                                ?"0.0":hourActualWaterSupply)),pumpArray.size(),randomNum);//实际值随机数集合
+                                                        List<Double> splitRandomYuce = splitValueRandomlyWithZeros((randomWater==null
+                                                                ?0.0:randomWater),pumpArray.size(),randomNum);//预测值随机数集合
+                                                        //泵集合
+                                                        int kIndex = 0;
+                                                        for (Object obj:pumpArray){
+                                                            JSONObject itemObj = (JSONObject)obj;
+                                                            Map<String,Object> map = new LinkedHashMap<>();
+                                                            //TODO 泵状态根据值是否为0设置
+                                                            map.put("Date", dateNow.withYear(LocalDateTime.now().getYear()).toLocalDate()
+                                                                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+                                                            // 创建一个LocalTime实例,设置为指定小时,分钟和秒数为0
+                                                            // 创建一个DateTimeFormatter用于格式化时间
+                                                            map.put("Hour", time.format(formatter3));
+                                                            map.put("PumpID",itemObj.get("label"));
+
+                                                            if(splitRandom.get(kIndex)==0){
+                                                                //TODO 说明泵实际值为0,状态为关
+                                                                map.put("PumpStatus",0);
+                                                            }else{
+                                                                map.put("PumpStatus",1);
+                                                            }
+                                                            map.put("HourForecastActualWaterSupply",splitRandomYuce.get(kIndex));//预测泵的供水量
+                                                            map.put("PumpWater",splitRandom.get(kIndex));//实际泵的供水量
+                                                            if(splitRandom.get(kIndex)==0){
+                                                                //TODO 说明泵实际值为0,状态为关
+                                                                map.put("RealPumpStatus",0);
+                                                            }else{
+                                                                map.put("RealPumpStatus",1);
+                                                            }
+
+                                                            map.put("PumpEnergy",splitRandomYuce.get(kIndex)/1000*180);
+                                                            map.put("RealPumpEnergy",splitRandom.get(kIndex)/1000*200);
+                                                            map.put("LastModifyTime", LocalDateTime.now().format(formatter));
+                                                            map.put("orgId", orgId);
+                                                            //TODO 添加or修改
+                                                            Integer insertRes2 = getWaterTapApi().insertOrUpdateTbmHourwaterWatersupply(map);
+                                                            if (insertRes2 < 1) {
+                                                                LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR
+                                                                        , mStrClassName
+                                                                        , mStrClassName
+                                                                        , String.format("Batch insertOrUpdateTbmHourwaterWatersupply ERROR:{%s} ",
+                                                                                "新增或修改失败"));
+                                                            }
+                                                            kIndex++;
+                                                        }
                                                     }
                                                 }
                                             }
@@ -609,4 +914,45 @@ public class KprAimWaterCollecationBizFun {
             ex.printStackTrace();
         }
     }
+
+    //生成指定份额的随机数,并且带有0值
+    public static List<Double> splitValueRandomlyWithZeros(double totalValue, int parts, int zeroCount) {
+        if (zeroCount >= parts) {
+            throw new IllegalArgumentException("Zero count must be less than the number of parts.");
+        }
+
+        Random random = new Random();
+        List<Double> points = new ArrayList<>();
+        int nonZeroParts = parts - zeroCount;
+
+        // 添加初始点
+        points.add(0.0);
+
+        // 生成分割点
+        for (int i = 0; i < nonZeroParts - 1; i++) {
+            points.add(random.nextDouble() * totalValue);
+        }
+
+        // 添加终点
+        points.add(totalValue);
+
+        // 排序
+        Collections.sort(points);
+
+        // 计算每份的大小
+        List<Double> splits = new ArrayList<>();
+        for (int i = 1; i < points.size(); i++) {
+            splits.add(points.get(i) - points.get(i - 1));
+        }
+
+        // 添加0值份额
+        for (int i = 0; i < zeroCount; i++) {
+            splits.add(0.0);
+        }
+
+        // 打乱列表,以确保0值分布均匀
+        Collections.shuffle(splits);
+
+        return splits;
+    }
 }

+ 57 - 0
src/main/java/com/shkpr/service/aimodelpower/dbdao/shizilaishuiDataSource/WaterZILAISHUIDao.java

@@ -332,4 +332,61 @@ public class WaterZILAISHUIDao {
             return -1;
         }
     }
+
+    //TODO 添加或修改日小时开泵预测表
+    public int insertOrUpdateTbmHourwaterWatersupply(Map<String,Object> value){
+        try{
+            String sql = "INSERT INTO tb_m_hourwater_watersupply " +
+                    "(\"Date\",\"Hour\",\"PumpID\",\"PumpStatus\",\"HourForecastActualWaterSupply\"," +
+                    " \"PumpWater\",\"RealPumpStatus\",\"PumpEnergy\",\"RealPumpEnergy\",\"LastModifyTime\",\"orgId\" ) VALUES ";
+            sql +=" ( ";
+            int i = 1;
+            for (Object key : value.keySet()){
+                if(i==value.keySet().size()){
+                    if(ObjectUtils.isEmpty(value.get(key))){
+                        sql += " NULL ";
+                    }else {
+                        sql += "'" + value.get(key) + "'";
+                    }
+                }else {
+                    if(ObjectUtils.isEmpty(value.get(key))) {
+                        sql += " NULL ,";
+                    }else {
+                        sql += "'" + value.get(key) + "',";
+                    }
+                }
+                i++;
+            }
+            sql += " )";
+            sql += " ON CONFLICT (\"Date\",\"orgId\",\"Hour\",\"PumpID\") " +
+                    " DO UPDATE " +
+                    " SET \"Date\" =  '"+value.get("Date")+"' " +
+                    " , \"Hour\" =  '"+value.get("Hour")+"' "+
+                    " , \"PumpID\" =  '"+value.get("PumpID")+"' "+
+                    " , \"PumpStatus\" =  "+value.get("PumpStatus")+" ";
+            if(!ObjectUtils.isEmpty(value.get("HourForecastActualWaterSupply"))){
+                sql +=  " ,\"HourForecastActualWaterSupply\" =  '"+value.get("HourForecastActualWaterSupply")+"' ";
+            }
+            if(!ObjectUtils.isEmpty(value.get("PumpWater"))){
+                sql +=  " , \"PumpWater\" =  '"+value.get("PumpWater")+"' ";
+            }
+            if(!ObjectUtils.isEmpty(value.get("RealPumpStatus"))){
+                sql +=  " , \"RealPumpStatus\" =  "+value.get("RealPumpStatus")+" ";
+            }
+            if(!ObjectUtils.isEmpty(value.get("PumpEnergy"))){
+                sql +=  " , \"PumpEnergy\" =  '"+value.get("PumpEnergy")+"' ";
+            }
+            if(!ObjectUtils.isEmpty(value.get("RealPumpEnergy"))){
+                sql +=  " , \"RealPumpEnergy\" =  '"+value.get("RealPumpEnergy")+"' ";
+            }
+
+                sql += " , \"LastModifyTime\" =  '"+value.get("LastModifyTime")+"' " +
+                    " , \"orgId\" =  '"+value.get("orgId")+"' ";
+            JdbcTemplate pgJdbc = new JdbcTemplate(childDataSource);
+            Integer resCode = pgJdbc.update(sql);
+            return resCode;
+        }catch(Exception ex){
+            return -1;
+        }
+    }
 }

+ 21 - 0
src/main/java/com/shkpr/service/aimodelpower/dbdao/shizilaishuiDataSource/service/WaterTapWaterServiceImpl.java

@@ -289,4 +289,25 @@ public class WaterTapWaterServiceImpl implements WaterTapWaterService {
         }
         return arrRes;
     }
+
+    @Override
+    public Integer insertOrUpdateTbmHourwaterWatersupply(Map<String, Object> value) {
+        int nCode = 0;
+        String strMsg = "Success";
+        Integer arrRes = null;
+        try {
+            arrRes = waterZILAISHUIDao.insertOrUpdateTbmHourwaterWatersupply(value);
+        } catch (Exception e) {
+            nCode = LogLevelFlag.LOG_ERROR.ordinal();
+            strMsg = e.getLocalizedMessage();
+        } finally {
+//            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.valueFromInt(nCode)
+//                    , LogFlagBusiType.BUSI_CHILD_DB_TAP_WATER.toStrValue()
+//                    , mStrClassName
+//                    , String.format("Batch Insert WaterRecord:{%s} from database, code:{%s} msg:{%s} ..."
+//                            , arrRes
+//                            , nCode, strMsg));
+        }
+        return arrRes;
+    }
 }

+ 2 - 0
src/main/java/com/shkpr/service/aimodelpower/dbdao/shizilaishuiDataSource/service/intef/WaterTapWaterService.java

@@ -37,4 +37,6 @@ public interface WaterTapWaterService {
     public Integer insertOrUpdateTbmWater(Map<String,Object> value);
     //TODO 添加或修改日小时预测数据
     public Integer insertOrUpdateTbmHourWater(Map<String,Object> value);
+    //TODO 添加或修改日小时开泵预测表
+    public Integer insertOrUpdateTbmHourwaterWatersupply(Map<String,Object> value);
 }

+ 1 - 1
src/main/java/com/shkpr/service/aimodelpower/globalmgr/ScheduleTaskMgr.java

@@ -158,7 +158,7 @@ public class ScheduleTaskMgr {
 //        TimerTask timerTask1 = new TimerTask() {
 //            @Override
 //            public void run() {
-//                KprAimWaterCollecationBizFun.insertYuceHourDataScheduled(time);
+//                KprAimWaterCollecationBizFun.insertYuceHourDataScheduled(null);
 //            }
 //        };
 //        timer2.schedule(timerTask1, 10000);