Commit 3318458f by chenzy

【新增】用户,组织相关

parent 91838df6
...@@ -57,7 +57,29 @@ ...@@ -57,7 +57,29 @@
<artifactId>spring-boot-starter-mail</artifactId> <artifactId>spring-boot-starter-mail</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.16.1</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.13</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.3</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.keymobile.syncdata.actor;
import akka.actor.AbstractActor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.keymobile.syncdata.util.ObjectUtil;
import com.keymobile.syncdata.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class DataSyncActor extends AbstractActor {
private final MongoTemplate mongoTemplate = SpringUtil.getBean(MongoTemplate.class);
private Map<String, Map<String, Object>> urlMap = new HashMap<>();
private List<Map<String, Object>> urlList = new ArrayList<Map<String, Object>>();
@Override
public Receive createReceive() {
return receiveBuilder()
.match(SyncDataMessage.class, this::syncData)
.build();
}
public void syncData(SyncDataMessage syncDataMessage) {
// 抽取所有数据
getAllDataFromUrl(syncDataMessage);
// 将list转为Map,key id, value
// urlMap = urlList.stream()
// .collect(Collectors.toMap(map -> String.valueOf(map.get("USER_ID")), map -> map));
writeUserData(syncDataMessage);
}
private void writeUserData(SyncDataMessage syncDataMessage) {
mongoTemplate.dropCollection(syncDataMessage.getCollectionName());
mongoTemplate.insert(urlList, syncDataMessage.getCollectionName());
log.info("数据插入完成,共插入:{} 条数据", urlList.size());
}
public void getAllDataFromUrl(SyncDataMessage syncDataMessage) {
String url = syncDataMessage.getUrl();
String collectionName = syncDataMessage.getCollectionName();
Map<String, Integer> requestBody = syncDataMessage.getRequestBody();
log.info("连接数据抽取接口, url = {}; collectionName = {}", url, collectionName);
// 创建 HttpClient 实例
HttpClient client = HttpClient.newHttpClient();
// 创建 HttpRequest 实例
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("X-HW-ID", syncDataMessage.getXHWID())
.header("X-HW-APPKEY", syncDataMessage.getXHWAPPKEY())
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(ObjectUtil.toJson(requestBody)))
.build();
int currentTotal = requestBody.getOrDefault("limit", 0) + requestBody.getOrDefault("offset", 0);
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");
int totalValue = -1;
if (totalsArray != null && totalsArray.isArray() && totalsArray.size() > 0) {
JsonNode totalObject = totalsArray.get(0);
totalValue = totalObject.get("TOTAL").asInt();
System.out.println("TOTAL 的值是: " + totalValue);
}
writeData(retJSON);
log.info("抽取:{} 条数据", currentTotal);
if (totalValue > currentTotal) {
requestBody.put("offset", currentTotal);
syncDataMessage.setRequestBody(requestBody);
getAllDataFromUrl(syncDataMessage);
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
private void writeData(JsonNode retJSON) {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rowsArray = retJSON.get("Rows");
if (rowsArray != null && rowsArray.isArray() && rowsArray.size() > 0) {
rowsArray.forEach(rows -> {
Map<String, Object> map = null;
try {
map = objectMapper.readValue(rows.toString(), Map.class);
urlList.add(map);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}
}
}
package com.keymobile.syncdata.actor;
import lombok.Data;
import java.util.Map;
@Data
public class SyncDataMessage {
private String url;
private Map<String, Integer> requestBody;
private String collectionName;
private String xHWID;
private String xHWAPPKEY;
}
package com.keymobile.syncdata.api;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SearchType;
import co.elastic.clients.elasticsearch._types.aggregations.*;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.json.JsonData;
import com.keymobile.syncdata.dto.LoginStats;
import com.keymobile.syncdata.persistent.EsLog;
import com.keymobile.syncdata.util.DateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.elasticsearch.client.RequestOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.data.redis.core.convert.Bucket;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@RestController
@Tag(name = "ES基本统计信息")
@RequestMapping(path = "/es")
public class EsStatsCtrl {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private ElasticsearchClient elasticsearchClient;
@Operation(summary = "获取访问数")
@RequestMapping(value = "/stats/visitCounts", method = RequestMethod.GET)
public Long getVisitCounts() {
BoolQuery.Builder bqb = new BoolQuery.Builder();
bqb.must(new MatchQuery.Builder().field("logger").query("sso.API").build()._toQuery());
bqb.must(new MatchQuery.Builder().field("message").query("登录了系统").build()._toQuery());
NativeQuery nativeSearchQuery = new NativeQueryBuilder().withQuery(bqb.build()._toQuery()).build();
return elasticsearchTemplate.count(nativeSearchQuery, EsLog.class);
}
@Operation(summary = "统计系统访问趋势")
@RequestMapping(value = "getTrendStats", method = RequestMethod.GET)
public List<LoginStats> getTrendStats() throws IOException {
Date e_date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(e_date);
cal.add(Calendar.DATE, -7);
Date s_date = cal.getTime();
String startDay = DateUtil.formatDate(s_date, "yyyy-MM-dd");
String endtDay = DateUtil.formatDate(e_date, "yyyy-MM-dd");
List<LoginStats> dataList = new ArrayList<LoginStats>();
BoolQuery.Builder boolQueryBuilder = new BoolQuery.Builder();
// BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(new MatchQuery.Builder().field("logger").query("sso.API").build()._toQuery());
boolQueryBuilder.must(new MatchQuery.Builder().field("message").query("登录了系统").build()._toQuery());
boolQueryBuilder.must(new RangeQuery.Builder().field("@timestamp")
.gte(JsonData.fromJson(startDay + " 00:00:00.000"))
.lte(JsonData.fromJson(endtDay + " 23:59:59.999"))
.format("yyyy-MM-dd HH:mm:ss.SSS")
.build()._toQuery());
Aggregation aggregation = AggregationBuilders.dateHistogram(dateHistogram ->
dateHistogram.field("@timestamp")
.format("yyyy-MM-dd HH:mm:ss.SSSZ")
.minDocCount(0)
.extendedBounds(bounds ->
bounds.min(FieldDateMath.of(f -> f.expr(startDay + " 00:00:00.000" + "Z")))
.max(FieldDateMath.of(f -> f.expr(endtDay + " 23:59:59.999" + "Z"))))
.calendarInterval(CalendarInterval.Day)
);
// Query query = NativeQuery.builder()
// .withQuery(boolQueryBuilder.build()._toQuery())
// .withAggregation("statistics",aggregation)
// .build();
SearchRequest searchRequest = SearchRequest.of(sr ->
sr.index("default-logstash")
.size(0)
.aggregations("statistics", aggregation)
.query(co.elastic.clients.elasticsearch._types.query_dsl.Query.of(q -> q.bool(boolQueryBuilder.build())))
.searchType(SearchType.DfsQueryThenFetch)
.size(0));
SearchResponse<Void> searchResponse = elasticsearchClient.search(searchRequest, Void.class);
Aggregate statsAggregate = searchResponse.aggregations().get("statistics");
DateHistogramAggregate dateHistogramAggregate = statsAggregate.dateHistogram();
if (null != dateHistogramAggregate){
for (DateHistogramBucket bucket : dateHistogramAggregate.buckets().array()) {
LoginStats stats = new LoginStats();
stats.setXaxisName(bucket.keyAsString());
stats.setCount(bucket.docCount());
dataList.add(stats);
}
}
return dataList;
}
}
package com.keymobile.syncdata.api; package com.keymobile.syncdata.api;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import com.keymobile.syncdata.persistent.EsLog;
import com.keymobile.syncdata.service.StaticLoginData; import com.keymobile.syncdata.service.StaticLoginData;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.web.bind.annotation.RestController; import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController @RestController
@RequestMapping("/static") @RequestMapping("/static")
public class StaticDataController { public class StaticDataController {
@Autowired @Autowired
private StaticLoginData staticLoginData; private StaticLoginData staticLoginData;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
// @GetMapping("/totalUserCount")
// public Long getTotalUserCount() {
// return staticLoginData.getTotalUserCount();
// }
@GetMapping("/totalUserCount") @Operation(summary = "获取访问数")
public Long getTotalUserCount() { @RequestMapping(value = "/totalUserCount", method = RequestMethod.GET)
return staticLoginData.getTotalUserCount(); public Long getVisitCounts() {
BoolQuery.Builder bqb = new BoolQuery.Builder();
bqb.must(new MatchQuery.Builder().field("logger").query("sso.API").build()._toQuery());
bqb.must(new MatchQuery.Builder().field("message").query("登录了系统").build()._toQuery());
NativeQuery nativeSearchQuery = new NativeQueryBuilder().withQuery(bqb.build()._toQuery()).build();
return elasticsearchTemplate.count(nativeSearchQuery, EsLog.class);
} }
@GetMapping("/todayUserCount") @GetMapping("/todayUserCount")
public Long todayUserCount() { public Long todayUserCount() {
return staticLoginData.getTodayUserCount(); return staticLoginData.getTodayUserCount();
} }
@PostMapping("test")
public String test(@RequestBody Map<String, String> body) {
return body.toString();
}
} }
package com.keymobile.syncdata.api; package com.keymobile.syncdata.api;
import com.fasterxml.jackson.core.JsonProcessingException; import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.databind.JsonNode; import cn.hutool.core.lang.tree.Tree;
import com.fasterxml.jackson.databind.ObjectMapper; import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.keymobile.syncdata.properties.SystemProperties; import com.keymobile.syncdata.properties.SystemProperties;
import com.keymobile.syncdata.service.SyncDataService; import com.keymobile.syncdata.service.SyncDataService;
import jakarta.annotation.Nullable;
import org.bson.Document; import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.*;
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 @RestController
@RequestMapping("/") @RequestMapping("/")
...@@ -30,14 +26,90 @@ public class SyncDataController { ...@@ -30,14 +26,90 @@ public class SyncDataController {
@Autowired @Autowired
private SyncDataService syncDataService; private SyncDataService syncDataService;
public static final Map<String, Integer> defaultRequestBody = Map.of("limit", 2000, "offset", 0);
@Autowired
private SystemProperties systemProperties;
@Autowired
private MongoTemplate mongoTemplate;
private static final String LOCK_NAME = "user_data_sync_lock";
private static final int BATCH_SIZE = 100;
@GetMapping("/syncSystemData") @PostMapping("/syncSystemData")
public void SyncSystemData() { public void SyncSystemData(@RequestBody Map<String, Integer> requestBody) {
syncDataService.syncSystemData(); requestBody = requestBody != null && !requestBody.isEmpty() ? requestBody : defaultRequestBody;
syncDataService.syncSystemData(requestBody);
} }
@GetMapping("/getSystemData") @GetMapping("/getSystemData")
public List<Document> getSystemData() { public List<Document> getSystemData() {
return syncDataService.getSystemData(); return syncDataService.getSystemData();
} }
@PostMapping("/syncData")
public void SyncData(@RequestParam String type, @Nullable @RequestBody Map<String, Integer> requestBody) {
requestBody = requestBody != null && !requestBody.isEmpty() ? requestBody : new HashMap<>(defaultRequestBody);
switch (type) {
case "user":
syncDataService.syncData(systemProperties.getUserUrl(), requestBody, "sync_user_data");
break;
case "org":
syncDataService.syncData(systemProperties.getOrgUrl(), requestBody, "sync_organization_data");
break;
}
}
@GetMapping("/getOrgWithTree")
public List<Tree<String>> getOrgWithTree() {
List<TreeNode> nodeList = CollUtil.newArrayList();
List<Map> syncOrganizationData = mongoTemplate.findAll(Map.class, "sync_organization_data");
syncOrganizationData.forEach(map -> {
String deptId = (String) map.get("DEPT_ID");
String partDeptId = (String) map.get("PART_DEPT_ID");
String deptDesc = (String) map.get("DEPT_DESC");
TreeNode<String> longTreeNode = new TreeNode<>(deptId, partDeptId, deptDesc, 0);
HashMap<String, Object> hashMap = new HashMap<>();
longTreeNode.setExtra(hashMap);
nodeList.add(longTreeNode);
});
//配置
TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
// 最大递归深度
treeNodeConfig.setDeep(10);
List<Tree<String>> build = TreeUtil.<TreeNode, String>build(nodeList, null, treeNodeConfig,
(treeNode, tree) -> {
tree.setId((String) treeNode.getId());
tree.setParentId((String) treeNode.getParentId());
tree.setName(treeNode.getName());
tree.putAll(treeNode.getExtra());
});
return build;
}
@GetMapping("/getUserByOrgId")
public List<Document> getUserByOrgId(@RequestParam String orgId, @RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize) {
// 查询orgID及其子节点
Queue<String> queue = new LinkedList<>();
List<String> list = new ArrayList<>();
queue.add(orgId);
while (!queue.isEmpty()) {
String poll = queue.poll();
list.add(poll);
List<Map> result = mongoTemplate.find(new Query(Criteria.where("PART_DEPT_ID").is(poll)), Map.class, "sync_organization_data");
result.stream().map(map -> (String) map.get("DEPT_ID")).forEach(queue::add);
}
// 根据list中的id查询用户数据,查询出的数据orderby dept_id
Query query = new Query(Criteria.where("DEPTID").in(list));
query.fields().exclude("_id");
query.with(org.springframework.data.domain.Sort.by(org.springframework.data.domain.Sort.Direction.ASC, "DEPT_ID"));
Pageable pageable = PageRequest.of(pageNumber, pageSize);
query.with(pageable);
List<Document> documents = mongoTemplate.find(query, Document.class, "sync_user_data");
return documents;
}
} }
package com.keymobile.syncdata.dto;
public class LoginStats {
private String xaxisName;
private long count;
public String getXaxisName() {
return xaxisName;
}
public void setXaxisName(String xaxisName) {
this.xaxisName = xaxisName;
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
@Override
public String toString() {
return "InterfaceLogStats [xaxisName=" + xaxisName + ", count=" + count + "]";
}
}
package com.keymobile.syncdata.persistent;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.util.Date;
@Document(indexName="default-logstash")
public class EsLog {
@Id
private String _id;
@Field
private String message;
@Field
private String logger;
@Field
private String session;
@Field
private String user;
@Field(type = FieldType.Date, format = DateFormat.date_optional_time)
@JsonProperty(value = "@timestamp")
private Date timestamp;
public EsLog() {}
public EsLog(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getLogger() {
return logger;
}
public void setLogger(String logger) {
this.logger = logger;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public String getSession() {
return session;
}
public void setSession(String session) {
this.session = session;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
...@@ -3,12 +3,14 @@ package com.keymobile.syncdata.properties; ...@@ -3,12 +3,14 @@ package com.keymobile.syncdata.properties;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "sycn.system") @ConfigurationProperties(prefix = "sycn")
@Component @Component
public class SystemProperties { public class SystemProperties {
private String systemUrl; private String systemUrl;
private String xHWID; private String xHWID;
private String xHWAPPKEY; private String xHWAPPKEY;
private String userUrl;
private String orgUrl;
public String getSystemUrl() { public String getSystemUrl() {
return systemUrl; return systemUrl;
...@@ -33,4 +35,20 @@ public class SystemProperties { ...@@ -33,4 +35,20 @@ public class SystemProperties {
public void setxHWAPPKEY(String xHWAPPKEY) { public void setxHWAPPKEY(String xHWAPPKEY) {
this.xHWAPPKEY = xHWAPPKEY; this.xHWAPPKEY = xHWAPPKEY;
} }
public String getUserUrl() {
return userUrl;
}
public void setUserUrl(String userUrl) {
this.userUrl = userUrl;
}
public String getOrgUrl() {
return orgUrl;
}
public void setOrgUrl(String orgUrl) {
this.orgUrl = orgUrl;
}
} }
...@@ -4,10 +4,17 @@ import com.keymobile.syncdata.properties.SystemProperties; ...@@ -4,10 +4,17 @@ import com.keymobile.syncdata.properties.SystemProperties;
import org.bson.Document; import org.bson.Document;
import java.util.List; import java.util.List;
import java.util.Map;
public interface SyncDataService { public interface SyncDataService {
void syncSystemData(); void syncSystemData(Map<String, Integer> requestBody);
List<Document> getSystemData(); List<Document> getSystemData();
// void fetchUserDataFromUrl(Map<String, String> requestBody);
//
// void fetchOrgDataFromUrl(Map<String, String> requestBody);
void syncData(String url, Map<String, Integer> requestBody, String collectionName);
} }
package com.keymobile.syncdata.service.impl; package com.keymobile.syncdata.service.impl;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.keymobile.syncdata.actor.DataSyncActor;
import com.keymobile.syncdata.actor.SyncDataMessage;
import com.keymobile.syncdata.properties.SystemProperties; import com.keymobile.syncdata.properties.SystemProperties;
import com.keymobile.syncdata.service.SyncDataService; import com.keymobile.syncdata.service.SyncDataService;
import com.keymobile.syncdata.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.bson.Document; import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -28,12 +34,12 @@ public class SyncDataServiceImpl implements SyncDataService { ...@@ -28,12 +34,12 @@ public class SyncDataServiceImpl implements SyncDataService {
@Autowired @Autowired
private SystemProperties systemProperties; private SystemProperties systemProperties;
private static final ActorSystem syncDataActor = ActorSystem.create("SyncDataAkka");
@Override @Override
public void syncSystemData() { public void syncSystemData(Map<String, Integer> requestBody) {
log.info("连接系统数据抽取接口"); log.info("连接系统数据抽取接口");
String url = systemProperties.getSystemUrl(); String url = systemProperties.getSystemUrl();
String requestBody = "{\"limit\": 2000,\"offset\": 0}";
// 创建 HttpClient 实例 // 创建 HttpClient 实例
HttpClient client = HttpClient.newHttpClient(); HttpClient client = HttpClient.newHttpClient();
...@@ -44,7 +50,7 @@ public class SyncDataServiceImpl implements SyncDataService { ...@@ -44,7 +50,7 @@ public class SyncDataServiceImpl implements SyncDataService {
.header("X-HW-ID", systemProperties.getxHWID()) .header("X-HW-ID", systemProperties.getxHWID())
.header("X-HW-APPKEY", systemProperties.getxHWAPPKEY()) .header("X-HW-APPKEY", systemProperties.getxHWAPPKEY())
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody)) .POST(HttpRequest.BodyPublishers.ofString(ObjectUtil.toJson(requestBody)))
.build(); .build();
try { try {
...@@ -63,7 +69,7 @@ public class SyncDataServiceImpl implements SyncDataService { ...@@ -63,7 +69,7 @@ public class SyncDataServiceImpl implements SyncDataService {
int totalValue = totalObject.get("TOTAL").asInt(); int totalValue = totalObject.get("TOTAL").asInt();
System.out.println("TOTAL 的值是: " + totalValue); System.out.println("TOTAL 的值是: " + totalValue);
} }
writeSystemData(retJSON); writeData(retJSON, "sync_system_data");
} }
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -80,13 +86,26 @@ public class SyncDataServiceImpl implements SyncDataService { ...@@ -80,13 +86,26 @@ public class SyncDataServiceImpl implements SyncDataService {
return documents; return documents;
} }
private void writeSystemData(JsonNode retJSON) {
@Override
public void syncData(String url, Map<String, Integer> requestBody, String collectionName) {
ActorRef dataSyncActor = syncDataActor.actorOf(Props.create(DataSyncActor.class, DataSyncActor::new));
SyncDataMessage syncDataMessage = new SyncDataMessage();
syncDataMessage.setUrl(url);
syncDataMessage.setRequestBody(requestBody);
syncDataMessage.setCollectionName(collectionName);
syncDataMessage.setXHWID(systemProperties.getxHWID());
syncDataMessage.setXHWAPPKEY(systemProperties.getxHWAPPKEY());
dataSyncActor.tell(syncDataMessage, ActorRef.noSender());
}
private void writeData(JsonNode retJSON, String collectionName) {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
JsonNode rowsArray = retJSON.get("Rows"); JsonNode rowsArray = retJSON.get("Rows");
if (rowsArray != null && rowsArray.isArray() && rowsArray.size() > 0) { if (rowsArray != null && rowsArray.isArray() && rowsArray.size() > 0) {
log.info("清理数据库数据"); log.info("清理数据库数据");
mongoTemplate.dropCollection("sync_system_data"); mongoTemplate.dropCollection(collectionName);
log.info("开始数据插入"); log.info("开始数据插入");
rowsArray.forEach(rows -> { rowsArray.forEach(rows -> {
// 使用 Jackson 解析 JSON 字符串 // 使用 Jackson 解析 JSON 字符串
...@@ -96,7 +115,7 @@ public class SyncDataServiceImpl implements SyncDataService { ...@@ -96,7 +115,7 @@ public class SyncDataServiceImpl implements SyncDataService {
// 将 Map 转换为 MongoDB 的 Document 对象 // 将 Map 转换为 MongoDB 的 Document 对象
Document document = new Document(map); Document document = new Document(map);
// 将 Map 转换为 MongoDB 的 Document 对象 // 将 Map 转换为 MongoDB 的 Document 对象
mongoTemplate.save(document, "sync_system_data"); mongoTemplate.save(document, collectionName);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
...@@ -104,4 +123,4 @@ public class SyncDataServiceImpl implements SyncDataService { ...@@ -104,4 +123,4 @@ public class SyncDataServiceImpl implements SyncDataService {
} }
log.info("数据插入完成,共插入:{} 条数据", rowsArray.size()); log.info("数据插入完成,共插入:{} 条数据", rowsArray.size());
} }
} }
\ No newline at end of file
package com.keymobile.syncdata.util;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
*/
public class DateUtil extends DateUtils {
private static String parse_pattern_dd = "yyyy-MM-dd";
private static String parse_pattern_ss = "yyyy-MM-dd HH:mm:ss";
private static String[] parsePatterns = { parse_pattern_dd, parse_pattern_ss, "yyyy-MM-dd HH:mm",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" };
/**
* 得到当前日期字符串 格式(yyyy-MM-dd)
*/
public static String getDate() {
return getDate(parse_pattern_dd);
}
/**
* 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:parse_pattern_dd "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
/**
* 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:parse_pattern_dd "HH:mm:ss" "E"
*/
public static String formatDate(Date date, Object... pattern) {
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, parse_pattern_dd);
}
return formatDate;
}
/**
* 得到日期时间字符串,转换格式(parse_pattern_ss)
*/
public static String formatDateTime(Date date) {
return formatDate(date, parse_pattern_ss);
}
/**
* 得到当前时间字符串 格式(HH:mm:ss)
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
/**
* 得到当前日期和时间字符串 格式(parse_pattern_ss)
*/
public static String getDateTime() {
return formatDate(new Date(), parse_pattern_ss);
}
/**
* 得到当前年份字符串 格式(yyyy)
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
/**
* 得到当前月份字符串 格式(MM)
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}
/**
* 得到当天字符串 格式(dd)
*/
public static String getDay() {
return formatDate(new Date(), "dd");
}
/**
* 得到当前星期字符串 格式(E)星期几
*/
public static String getWeek() {
return formatDate(new Date(), "E");
}
/**
* 日期型字符串转化为日期 格式
* { parse_pattern_dd, parse_pattern_ss, "yyyy-MM-dd HH:mm",
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" }
*/
public static Date parseDate(Object str) {
if (str == null){
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
/**
* 获取过去的天数
* @param date
* @return
*/
public static long pastDays(Date date) {
long t = System.currentTimeMillis()-date.getTime();
return t/(24*60*60*1000);
}
public static Date getDateStart(Date date) {
if(date==null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(parse_pattern_ss);
try {
date= sdf.parse(formatDate(date, parse_pattern_dd)+" 00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static Date getDateEnd(Date date) {
if(date==null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(parse_pattern_ss);
try {
date= sdf.parse(formatDate(date, parse_pattern_dd) +" 23:59:59");
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
//得到上班时间
public static Date getDateStartWork(Date date) {
if(date==null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(parse_pattern_ss);
try {
date= sdf.parse(formatDate(date, parse_pattern_dd)+" 09:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
//得到下班时间
public static Date getDateEndWork(Date date) {
if(date==null) {
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(parse_pattern_ss);
try {
date= sdf.parse(formatDate(date, parse_pattern_dd) +" 17:30:00");
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/* 获取传值日期之前几天的日期
* @param date 需要转换的日期 date 可以为NULL 此条件下则获取当前日期
* @param after 天数
* @param xFormat 转换字符串类型 (可以为NULL)
* @return
*/
public static Date getBeforeCountDate(Date date, int before) {
date = date == null ? new Date() : date;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -before);
return calendar.getTime();
}
}
package com.keymobile.syncdata.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.Closeable;
import java.io.IOException;
import java.util.*;
/**
* @author mahx
* @version 1.0
* @date 2021/11/24 15:08
*/
@Slf4j
public class ObjectUtil {
private static final ObjectMapper MAPPER = new ObjectMapper();
private ObjectUtil() {
//do nothing
}
public static ObjectMapper getMapper() {
return MAPPER;
}
public static String toString(Object object) {
if (object == null) {
return null;
}
return StringUtils.trimToEmpty(object.toString());
}
public static String trimToEmpty(Object object) {
String result = toString(object);
return result == null ? "" : result;
}
public static String toEmpty(Object object) {
return object == null ? "" : object.toString();
}
public static String toJson(Object object) {
if (object == null) {
return "";
}
try {
return MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
log.error("生成json报错", e);
return "";
}
}
public static Map<String, Object> getMap(Object value) {
if (value == null) {
return new HashMap<>(0);
}
if (value instanceof Map) {
return (Map<String, Object>) value;
}
TypeReference<Map<String, Object>> typeReference = new TypeReference<Map<String, Object>>() {
};
try {
if (value instanceof String) {
return MAPPER.readValue((String) value, typeReference);
}
return MAPPER.convertValue(value, typeReference);
} catch (IOException e) {
log.error("解析错误", e);
return new HashMap<>(0);
}
}
public static List<Map<String, Object>> getMapList(Object value) {
if (value == null) {
return new ArrayList<>(0);
}
TypeReference<List<Map<String, Object>>> typeReference = new TypeReference<List<Map<String, Object>>>() {
};
try {
if (value instanceof String) {
return MAPPER.readValue((String) value, typeReference);
}
return MAPPER.readValue(MAPPER.writeValueAsString(value), typeReference);
} catch (IOException e) {
log.error("解析错误", e);
return new ArrayList<>();
}
}
public static <T> T getObject(Object value, TypeReference<T> reference) {
if (value == null) {
return null;
}
try {
if (value instanceof String) {
return MAPPER.readValue((String) value, reference);
}
return MAPPER.convertValue(value, reference);
} catch (IOException e) {
log.error("解析错误", e);
return null;
}
}
public static <T> T getObject(Object value, Class<T> valueType) {
if (value == null) {
return null;
}
try {
if (value instanceof String) {
return MAPPER.readValue((String) value, valueType);
}
return MAPPER.convertValue(value, valueType);
} catch (IOException e) {
log.error("解析错误", e);
return null;
}
}
public static boolean getBooleanValue(Object value) {
if (value == null) {
return false;
}
return Boolean.parseBoolean(toString(value));
}
public static Long getLong(Object value) {
if (value == null) {
return null;
}
return Long.valueOf(toString(value));
}
public static long getLongValue(Object value) {
if (value == null) {
return 0L;
}
return Long.parseLong(toString(value));
}
public static int getIntValue(Object value) {
if (value == null) {
return 0;
}
return Integer.parseInt(toString(value));
}
public static Double getDouble(Object value) {
if (value == null) {
return null;
}
return Double.valueOf(toString(value));
}
public static Object toStringForCollectionOrMap(Object value) {
if (value == null) {
return null;
}
if (value instanceof Collection) {
return checkMaxLength(removeBracket(value.toString()));
}
if (value instanceof String) {
return checkMaxLength(removeBracket(value.toString()));
}
if (value instanceof Map) {
return checkMaxLength(toJson(value));
}
return value;
}
private static String removeBracket(String value) {
if ("[]".equals(value)) {
return "";
} else if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) {
return StringUtils.substring(value, 1, value.length() - 1);
}
return value;
}
private static String checkMaxLength(String value) {
if (value == null) {
return value;
}
if (value.length() > 32767) {
return value.substring(0, 32766);
} else {
return value;
}
}
public static String addBracket(String value) {
if (!(StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]"))) {
value = "[" + value + "]";
}
return value;
}
public static void close(Closeable obj) {
if (obj != null) {
try {
obj.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
package com.keymobile.syncdata.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
/**
* SpringUtil工具类.
* @author mahx
* @version 1.0
* @date 2019/12/27 16:55
*/
@Component
public class SpringUtil implements ApplicationContextAware {
private static volatile ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
private static void staticSetValue(@NonNull ApplicationContext applicationContext) {
if (SpringUtil.applicationContext == null) {
synchronized (SpringUtil.class) {
if (SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
}
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
staticSetValue(applicationContext);
}
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
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