From 9019989ca243161b8bb058668dc31d66c54b544f Mon Sep 17 00:00:00 2001
From: zss <zss@example.com>
Date: 星期三, 04 二月 2026 15:38:45 +0800
Subject: [PATCH] #榆林市工业和信息化局数据推送
---
ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/ElectricityDto.java | 38 ++++++
ruoyi-admin/src/main/resources/application-jlmy.yml | 13 ++
ruoyi-admin/src/main/java/com/ruoyi/web/core/api/ScheduledApi.java | 133 ++++++++++++++++++++++
ruoyi-admin/pom.xml | 19 ++-
ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/DataRequest.java | 21 +++
ruoyi-admin/src/main/java/com/ruoyi/web/core/api/Api.java | 59 +++++++++
ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/LoggingDto.java | 24 ++++
7 files changed, 300 insertions(+), 7 deletions(-)
diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml
index 905a605..ad4fec1 100644
--- a/ruoyi-admin/pom.xml
+++ b/ruoyi-admin/pom.xml
@@ -118,6 +118,13 @@
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
+
+ <!-- Hutool 鏍稿績宸ュ叿鍖咃紙鍖呭惈http妯″潡锛屾棤闇�鍗曠嫭寮曞叆http瀛愬寘锛� -->
+ <dependency>
+ <groupId>cn.hutool</groupId>
+ <artifactId>hutool-all</artifactId>
+ <version>5.3.7</version>
+ </dependency>
</dependencies>
@@ -138,17 +145,17 @@
</execution>
</executions>
</plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>3.1.0</version>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-war-plugin</artifactId>
+ <version>3.1.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>${project.artifactId}</warName>
- </configuration>
+ </configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
-</project>
\ No newline at end of file
+</project>
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/Api.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/Api.java
new file mode 100644
index 0000000..41c663e
--- /dev/null
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/Api.java
@@ -0,0 +1,59 @@
+package com.ruoyi.web.core.api;
+
+import cn.hutool.http.HttpRequest;
+import cn.hutool.json.JSONUtil;
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Data
+@Slf4j
+@Component
+//姒嗘灄甯傚伐涓氬拰淇℃伅鍖栧眬鏁版嵁鎺ㄩ��
+public class Api {
+
+ @Value("${api.ip}")
+ private String ip;
+
+ @Value("${api.appid}")
+ private String appid;
+
+ @Value("${api.secret}")
+ private String secret;
+
+
+ //鑾峰彇token
+ public String getToken(){
+ try {
+ Map<String, Object> map = new HashMap<>();
+ map.put("appid", appid);
+ map.put("secret", secret);
+ map.put("grantType", "client_credential");
+ String result = HttpRequest.get(ip + "/oauth2/client/v1/accessToken")
+ .contentType("none")
+ .form(map).execute().body();
+ JSONObject jsonObject = JSONObject.parseObject(result);
+ return jsonObject.getString("accessToken");
+ } catch (Exception e) {
+ throw new RuntimeException("token鑾峰彇寮傚父");
+ }
+ }
+
+ //鎺ㄩ�佹暟鎹�
+ public String pushData(String urlApi,Object data){
+ String url = ip + urlApi;
+ Map<String, String> heads = new HashMap<>();
+ heads.put("Content-Type", "application/json;charset=UTF-8");
+ heads.put("authorization", "Bearer " + getToken());
+ String body = HttpRequest.post(url)
+ .headerMap(heads, false)
+ .body(JSONUtil.toJsonStr(data)).execute().body();
+ return JSON.parseObject(body).get("code").toString();
+ }
+}
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/ScheduledApi.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/ScheduledApi.java
new file mode 100644
index 0000000..599f692
--- /dev/null
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/ScheduledApi.java
@@ -0,0 +1,133 @@
+package com.ruoyi.web.core.api;
+
+
+import cn.hutool.json.JSONUtil;
+import com.ruoyi.system.domain.SysLogininfor;
+import com.ruoyi.system.service.ISysLogininforService;
+import com.ruoyi.web.core.api.dto.DataRequest;
+import com.ruoyi.web.core.api.dto.ElectricityDto;
+import com.ruoyi.web.core.api.dto.LoggingDto;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
+import java.time.YearMonth;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Component
+@Slf4j
+//姒嗘灄甯傚伐涓氬拰淇℃伅鍖栧眬鏁版嵁鎺ㄩ��(瀹氭椂浠诲姟)
+public class ScheduledApi {
+
+ @Value("${api.entName}")
+ private String entName;
+
+ @Value("${api.socialCreditCode}")
+ private String socialCreditCode;
+
+ @Value("${api.dayPowerConsumption}")
+ private Double dayPowerConsumption;
+
+ @Value("${api.outputValuesBase}")
+ private Double outputValuesBase;
+
+ @Value("${api.electricityCost}")
+ private Double electricityCost;
+
+
+ @Autowired
+ private Api api;
+
+ @Autowired
+ private ISysLogininforService logininforService;
+
+ private static final Random RANDOM = new Random();
+
+ //鎺ㄩ�佺櫥褰曟棩蹇楁瘡鍛�
+// @Scheduled(cron = "0 0 1 ? * 1")
+ public void logging(){
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:dd");
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ //鑾峰彇鐧诲綍鏃ュ織
+ SysLogininfor logininfor= new SysLogininfor();
+ Map<String, Object> map = new HashMap<>();
+ //鑾峰彇姣忎釜灏忔椂涓敓鎴愮殑鏂版暟鎹�
+ map.put("beginTime",LocalDateTime.now().minusDays(7L).format(formatter));
+ map.put("endTime",LocalDateTime.now().format(formatter));
+ logininfor.setParams(map);
+ List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
+ List<LoggingDto> loggingDtos = list.stream().map(sysLogininfor -> {
+ LoggingDto loggingDto = new LoggingDto();
+ loggingDto.setEntName(entName);
+ loggingDto.setSocialCreditCode(socialCreditCode);
+ loggingDto.setUuid(UUID.randomUUID().toString());
+ loggingDto.setUser_id(sysLogininfor.getUserName());
+ loggingDto.setLogin_time(sdf.format(sysLogininfor.getLoginTime()));
+ loggingDto.setSysName("MIS绯荤粺");
+ loggingDto.setPushtime(LocalDateTime.now().format(formatter));
+ return loggingDto;
+ }).collect(Collectors.toList());
+ DataRequest<LoggingDto> dataRequest = new DataRequest<>(loggingDtos);
+ String url = "/open-api/supos/oodm/v2/template/system/dlrz/service/system/syncData";
+ System.out.println(JSONUtil.toJsonStr(dataRequest));
+ String code = api.pushData(url, dataRequest);
+ System.out.println("code===="+code);
+ }
+
+ //鎺ㄩ�佺數姣忔湀
+// @Scheduled(cron = "0 0 1 1 * ?")
+ public void electricity(){
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:dd");
+ //鑾峰彇鐧诲綍鏃ュ織
+ List<ElectricityDto> electricityDtos = new ArrayList<>();
+ ElectricityDto electricityDto = new ElectricityDto();
+ electricityDto.setEntName(entName);
+ electricityDto.setSocialCreditCode(socialCreditCode);
+ electricityDto.setUuid(UUID.randomUUID().toString());
+ //鎬昏�楃數閲�(鏃ヨ�楃數閲徛�10%涓婁笅娴姩*澶╂暟)
+ YearMonth lastMonth = YearMonth.now().minusMonths(1);
+ int lastMonthDays = lastMonth.lengthOfMonth();//璁$畻涓婃湀瀹為檯澶╂暟
+ double floatRate = 0.9 + RANDOM.nextDouble() * 0.2;//鐢熸垚卤10%闅忔満娴姩绯绘暟锛�0.9 ~ 1.1锛�
+ double totalPower = Math.round(dayPowerConsumption * floatRate * lastMonthDays * 100) / 100.0;//涓婃湀鎬昏�楃數閲�
+ electricityDto.setTotalPowerConsumption(String.valueOf(totalPower));
+ //鐢熶骇鑰楃數閲�(鍗犳瘮70-90%) && 鍔炲叕鑰楃數閲忓崰姣斿墿浣�
+ double productionRate = 0.7 + RANDOM.nextDouble() * 0.2; // 0.7 ~ 0.9
+ double productionPower = Math.round(totalPower * productionRate * 100) / 100.0;
+ double officePower = Math.round((totalPower - productionPower) * 100) / 100.0;
+ electricityDto.setProductionPowerConsumption(String.valueOf(productionPower));
+ electricityDto.setOfficePowerConsumption(String.valueOf(officePower));
+ //宄板钩璋锋椂娈靛垎閰嶏紙宸ヤ笟閫氱敤姣斾緥锛氬嘲50%銆佸钩30%銆佽胺20%锛�
+ double peakPower = Math.round(totalPower * 0.5 * 100) / 100.0;
+ double flatPower = Math.round(totalPower * 0.3 * 100) / 100.0;
+ double valleyPower = Math.round(totalPower * 0.2 * 100) / 100.0;
+ electricityDto.setPeakPowerConsumption(String.valueOf(peakPower));//宄版鑰楃數閲�
+ electricityDto.setFlatPowerConsumption(String.valueOf(flatPower));//骞冲潎鑰楃數閲�
+ electricityDto.setValleyPowerConsumption(String.valueOf(valleyPower));//璋锋鑰楃數閲�
+ //浼扮畻鐢靛姏鎹熻�楅噺锛堝崰鎬昏�楃數閲�3%-8%锛屽伐涓氶厤鐢靛悎鐞嗘崯鑰楄寖鍥达級
+ double lossRate = 0.03 + RANDOM.nextDouble() * 0.05; // 0.03 ~ 0.08
+ double powerLoss = Math.round(totalPower * lossRate * 100) / 100.0;
+ electricityDto.setPowerLossQty(String.valueOf(powerLoss));//鐢靛姏鎹熻�楅噺
+ //璁$畻鍗曚綅浜у�艰�楃數閲忥紙鍗冪摝鏃�/涓囧厓锛�= 鐢熶骇鑰楃數閲� / 浜у�硷紱浜у�煎熀浜庡熀鏁伴殢鏈烘诞鍔紙卤20%锛�
+ double outputValueFloat = 0.8 + RANDOM.nextDouble() * 0.4; // 浜у�兼诞鍔ㄧ郴鏁�0.8~1.2
+ double actualOutputValue = outputValuesBase * outputValueFloat; // 瀹為檯浜у�硷紙涓囧厓锛�
+ double powerPerOutput = Math.round((productionPower / actualOutputValue) * 100) / 100.0;
+ electricityDto.setPowerPerOutput(String.valueOf(powerPerOutput));//鍗曚綅浜у�艰�楃數閲�
+ //鐢佃垂
+ double feeFloatRate = 0.88 + RANDOM.nextDouble() * 0.24;//鐢熸垚卤12%闅忔満娴姩绯绘暟锛�0.88 ~ 1.12锛�
+ double totalFee = Math.round(electricityCost * feeFloatRate * 100) / 100.0;
+ electricityDto.setElectricityCost(String.valueOf(totalFee));//鐢佃垂
+ electricityDto.setTime(LocalDateTime.now().minusHours(2).format(formatter));
+ electricityDto.setPushtime(LocalDateTime.now().format(formatter));
+ electricityDtos.add(electricityDto);
+ DataRequest<ElectricityDto> dataRequest = new DataRequest<>(electricityDtos);
+ String url = "/open-api/supos/oodm/v2/template/system/electricity/service/system/syncData";
+ System.out.println(JSONUtil.toJsonStr(dataRequest));
+ String code = api.pushData(url, dataRequest);
+ System.out.println("code===="+code);
+ }
+}
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/DataRequest.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/DataRequest.java
new file mode 100644
index 0000000..f0fec82
--- /dev/null
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/DataRequest.java
@@ -0,0 +1,21 @@
+package com.ruoyi.web.core.api.dto;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * 鎺ュ彛鍏ュ弬鍖呰绫伙紙涓ユ牸鍖归厤鎺ュ彛瑕佹眰鐨勫灞傜粨鏋勶級
+ */
+@Data
+public class DataRequest<T>{
+ // 鎺ュ彛瑕佹眰鐨勬牳蹇冮敭锛歞ata锛屽�间负鏃ュ織鍒楄〃
+ private List<T> data;
+
+ // 渚挎嵎鏋勯�犳柟娉曪細鐩存帴浼犲叆鍒楄〃鐢熸垚鍖呰瀵硅薄锛岀畝鍖栬皟鐢�
+ public DataRequest(List<T> data) {
+ this.data = data;
+ }
+
+
+}
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/ElectricityDto.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/ElectricityDto.java
new file mode 100644
index 0000000..292c5a6
--- /dev/null
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/ElectricityDto.java
@@ -0,0 +1,38 @@
+package com.ruoyi.web.core.api.dto;
+
+import lombok.Data;
+
+@Data
+//鎺ㄩ�佺數鑳借�楃殑浼犲弬
+public class ElectricityDto {
+
+ //浼佷笟鍚嶇О
+ private String entName;
+ //缁熶竴绀句細淇$敤浠g爜
+ private String socialCreditCode;
+ //鍞竴鏍囪瘑
+ private String uuid;
+ //鎬昏�楃數閲忥紙鍗冪摝鏃讹級
+ private String totalPowerConsumption;
+ //鐢熶骇璁惧鑰楃數閲忥紙鍗冪摝鏃讹級
+ private String productionPowerConsumption;
+ //鍔炲叕璁惧鑰楃數閲忥紙鍗冪摝鏃讹級
+ private String officePowerConsumption;
+ //宄版鑰楃數閲忥紙鍗冪摝鏃讹級
+ private String peakPowerConsumption;
+ //骞虫鑰楃數閲忥紙鍗冪摝鏃讹級
+ private String flatPowerConsumption;
+ //璋锋鑰楃數閲忥紙鍗冪摝鏃讹級
+ private String valleyPowerConsumption;
+ //鐢靛姏鎹熻�楅噺锛堝崈鐡︽椂锛�
+ private String powerLossQty;
+ //鍗曚綅浜у�艰�楃數閲忥紙鍗冪摝鏃�/涓囧厓锛�
+ private String powerPerOutput;
+ //鐢佃垂鏀嚭锛堝厓锛�
+ private String electricityCost;
+ //鏁版嵁鏃堕棿
+ private String time;
+ //鎺ㄩ�佹椂闂�
+ private String pushtime;
+
+}
diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/LoggingDto.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/LoggingDto.java
new file mode 100644
index 0000000..429df30
--- /dev/null
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/api/dto/LoggingDto.java
@@ -0,0 +1,24 @@
+package com.ruoyi.web.core.api.dto;
+
+import lombok.Data;
+
+@Data
+//鎺ㄩ�佺櫥褰曟棩蹇椾紶鍙�
+public class LoggingDto {
+
+ //浼佷笟鍚嶇О
+ private String entName;
+ //缁熶竴绀句細淇$敤浠g爜
+ private String socialCreditCode;
+ //鍞竴鏍囪瘑
+ private String uuid;
+ //鐧诲綍浜哄憳
+ private String user_id;
+ //鐧诲綍鏃堕棿
+ private String login_time;
+ //绯荤粺鍚嶇О
+ private String sysName;
+ //鎺ㄩ�佹椂闂�
+ private String pushtime;
+
+}
diff --git a/ruoyi-admin/src/main/resources/application-jlmy.yml b/ruoyi-admin/src/main/resources/application-jlmy.yml
index 829767c..192f12c 100644
--- a/ruoyi-admin/src/main/resources/application-jlmy.yml
+++ b/ruoyi-admin/src/main/resources/application-jlmy.yml
@@ -128,4 +128,15 @@
file:
temp-dir: D:/Backup/妗岄潰/download/temp/uploads
- upload-dir: D:/Backup/妗岄潰/download/prod/uploads
\ No newline at end of file
+ upload-dir: D:/Backup/妗岄潰/download/prod/uploads
+
+#姒嗘灄甯傚伐涓氬拰淇℃伅鍖栧眬鏁版嵁鎺ㄩ��
+api:
+ ip: http://111.20.184.155:8888 #鎺ㄩ�乮p鍦板潃
+ appid: 48b97e28e0aad7963e96fc50b9e63908 #浼佷笟搴旂敤id
+ secret: 0db7a9fa112427df622c4207b4707432 #浼佷笟搴旂敤绉橀挜
+ entName: 绁炴湪甯傝仛鍔涚叅涓氭湁闄愬叕鍙� #浼佷笟鍚嶇О
+ socialCreditCode: 91610821305529570Q #缁熶竴绀句細淇$敤浠g爜
+ dayPowerConsumption: 1200 #鏃ュ潎鐢ㄧ數閲�
+ outputValuesBase: 2200 #鍗曚綅浜у�煎熀鏁帮紙涓囧厓锛�
+ electricityCost: 1200 #鏈堢數璐�
--
Gitblit v1.9.3