Commit cf196751 by chenzy

【新增】深铁同步系统数据

parent 68b4835c
......@@ -14,10 +14,38 @@
<version>product-v1-1.0.4-rc1</version>
</parent>
<properties>
<auth.version>product-v2-1.0.3-rc1</auth.version>
<config.version>product-v1-1.0.4-rc1</config.version>
</properties>
<dependencies>
<dependency>
<groupId>com.keymobile.authservice</groupId>
<artifactId>security</artifactId>
<version>${auth.version}</version>
</dependency>
<dependency>
<groupId>com.keymobile</groupId>
<artifactId>config</artifactId>
<version>${config.version}</version>
</dependency>
<dependency>
<groupId>com.keymobile.authservice</groupId>
<artifactId>component</artifactId>
<version>${auth.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
......
package com.keymobile.syncdata;
import com.keymobile.authservice.component.SecurityConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.keymobile.syncdata"}, excludeFilters = {
@ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE, value= SecurityConfig.class)
})
@ComponentScan(basePackages = {"com.keymobile.syncdata","com.keymobile.config.logging", "com.keymobile.config.naming",
"com.keymobile.config.redisclient", "com.keymobile.authservice.component"})
@PropertySource(value = "classpath:/application.yml")
public class SyncDataApplication {
......
package com.keymobile.syncdata.api;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.keymobile.syncdata.properties.SystemProperties;
import com.keymobile.syncdata.service.SyncDataService;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/syncdata")
public class SyncDataController {
@Autowired
private SyncDataService syncDataService;
@RequestMapping("/syncSystemData")
public void SyncSystemData() {
syncDataService.syncSystemData();
}
@RequestMapping("/getSystemData")
public List<Document> getSystemData() {
return syncDataService.getSystemData();
}
}
package com.keymobile.syncdata.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "sycn.system")
@Component
public class SystemProperties {
private String systemUrl;
private String xHWID;
private String xHWAPPKEY;
public String getSystemUrl() {
return systemUrl;
}
public void setSystemUrl(String systemUrl) {
this.systemUrl = systemUrl;
}
public String getxHWID() {
return xHWID;
}
public void setxHWID(String xHWID) {
this.xHWID = xHWID;
}
public String getxHWAPPKEY() {
return xHWAPPKEY;
}
public void setxHWAPPKEY(String xHWAPPKEY) {
this.xHWAPPKEY = xHWAPPKEY;
}
}
package com.keymobile.syncdata.service;
import com.keymobile.syncdata.properties.SystemProperties;
import org.bson.Document;
import java.util.List;
public interface SyncDataService {
void syncSystemData();
List<Document> getSystemData();
}
package com.keymobile.syncdata.service.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.keymobile.syncdata.properties.SystemProperties;
import com.keymobile.syncdata.service.SyncDataService;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
@Service
@Slf4j
public class SyncDataServiceImpl implements SyncDataService {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private SystemProperties systemProperties;
@Override
public void syncSystemData() {
log.info("连接系统数据抽取接口");
String url = systemProperties.getSystemUrl();
String requestBody = "{\"limit\": 2000,\"offset\": 0}";
// 创建 HttpClient 实例
HttpClient client = HttpClient.newHttpClient();
// 创建 HttpRequest 实例
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("X-HW-ID", systemProperties.getxHWID())
.header("X-HW-APPKEY", systemProperties.getxHWAPPKEY())
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
try {
// 发送请求并获取响应
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// 打印响应码和响应体
System.out.println("Response Code: " + response.statusCode());
String jsonBody = response.body();
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonBody);
JsonNode retJSON = jsonNode.get("retJSON");
if (retJSON != null) {
JsonNode totalsArray = retJSON.get("Totals");
if (totalsArray != null && totalsArray.isArray() && totalsArray.size() > 0) {
JsonNode totalObject = totalsArray.get(0);
int totalValue = totalObject.get("TOTAL").asInt();
System.out.println("TOTAL 的值是: " + totalValue);
}
writeSystemData(retJSON);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
@Override
public List<Document> getSystemData() {
Query query = new Query();
// 指定要返回的字段,这里只返回 name 和 age 字段
query.fields().include("FNAME").include("SYSTEM_NUM").include("ABBREVIATION").include("SYSTEM_INFO");
query.fields().exclude("_id");
List<Document> documents = mongoTemplate.find(query, Document.class, "sync_system_data");
return documents;
}
private void writeSystemData(JsonNode retJSON) {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rowsArray = retJSON.get("Rows");
if (rowsArray != null && rowsArray.isArray() && rowsArray.size() > 0) {
log.info("清理数据库数据");
mongoTemplate.dropCollection("sync_system_data");
log.info("开始数据插入");
rowsArray.forEach(rows -> {
// 使用 Jackson 解析 JSON 字符串
Map<String, Object> map = null;
try {
map = objectMapper.readValue(rows.toString(), Map.class);
// 将 Map 转换为 MongoDB 的 Document 对象
Document document = new Document(map);
// 将 Map 转换为 MongoDB 的 Document 对象
mongoTemplate.save(document, "sync_system_data");
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}
log.info("数据插入完成,共插入:{} 条数据", rowsArray.size());
}
}
......@@ -21,3 +21,9 @@ eureka:
serviceUrl:
defaultZone: http://e0:8081/eureka/
enabled: true
sycn:
system:
systemUrl: http://roma.shenzhenmc.com/metro/mdm/query/sys_info_all
XHWID: com.roma.szmc.mdm
XHWAPPKEY: zt2HHE1haHBeq1Ps0E3cmA==
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment