Commit fd1c1ffc by chenzy

【修改】人员搜索直接调深铁接口;对接深铁OA通知

parent 37fc7968
package com.keymobile.syncdata.api;
import com.keymobile.syncdata.dto.TodoRequest;
import com.keymobile.syncdata.service.OAService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/oa")
public class OAMessageController {
@Autowired
private OAService oaService;
@GetMapping("getTodo")
@Operation(summary ="OA根据用户名查看用户待办信息")
public void getTodo( @Parameter(description = "oa账号")String oaAccount){
oaService.getTodo(oaAccount);
}
@Operation(summary ="OA根据用户名发送待办信息")
@PostMapping("sendTodo")
public void sendTodo(@RequestBody TodoRequest request){
oaService.sendTodo(request.getOaAccount(), request.getTitle(), request.getLink());
}
}
...@@ -5,12 +5,17 @@ import cn.hutool.core.lang.tree.Tree; ...@@ -5,12 +5,17 @@ import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode; import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig; import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil; import cn.hutool.core.lang.tree.TreeUtil;
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.properties.SystemProperties;
import com.keymobile.syncdata.service.SyncDataService; import com.keymobile.syncdata.service.SyncDataService;
import com.keymobile.syncdata.util.ObjectUtil;
import jakarta.annotation.Nullable; import jakarta.annotation.Nullable;
import org.bson.Document; import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
...@@ -19,6 +24,11 @@ import org.springframework.data.mongodb.core.query.Query; ...@@ -19,6 +24,11 @@ import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.support.PageableExecutionUtils; import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
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.*; import java.util.*;
@RestController @RestController
...@@ -34,6 +44,7 @@ public class SyncDataController { ...@@ -34,6 +44,7 @@ public class SyncDataController {
@Autowired @Autowired
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
private static final String LOCK_NAME = "user_data_sync_lock"; private static final String LOCK_NAME = "user_data_sync_lock";
private static final int BATCH_SIZE = 100; private static final int BATCH_SIZE = 100;
...@@ -102,32 +113,72 @@ public class SyncDataController { ...@@ -102,32 +113,72 @@ public class SyncDataController {
} }
@GetMapping("/getUserByOrgId") @GetMapping("/getUserByOrgId")
public Page<Document> getUserByOrgId(@RequestParam String orgId, @RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { public Page<Document> getUserByOrgId(@RequestParam String orgId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
// 查询orgID及其子节点 String url = systemProperties.getUserUrl();
Queue<String> queue = new LinkedList<>(); List<Document> documentList = new ArrayList<>();
List<String> list = new ArrayList<>(); Long total = 0L;
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"));
Long total = mongoTemplate.count(query, Document.class, "sync_user_data");
Pageable pageable = PageRequest.of(pageNum - 1, pageSize); Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
query.with(pageable); Map<String, String> requestBody = new HashMap<>();
List<Document> documents = mongoTemplate.find(query, Document.class, "sync_user_data"); requestBody.put("limit", String.valueOf(pageSize));
requestBody.put("offset", String.valueOf(pageNum * pageSize));
requestBody.put("deptid", orgId);
// 创建 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(ObjectUtil.toJson(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();
total = Long.valueOf(totalValue);
System.out.println("TOTAL 的值是: " + totalValue);
}
JsonNode rowsArray = retJSON.get("Rows");
if (rowsArray != null && rowsArray.isArray() && rowsArray.size() > 0) {
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 对象
documentList.add(document);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return PageableExecutionUtils.getPage(documents, pageable, () -> total); Long finalTotal = total;
return new PageImpl<>(documentList, pageable, finalTotal);
} }
@GetMapping("/getAllSyncUser") @GetMapping("/getAllSyncUser")
public Page<Document> getAllSyncUser(@RequestParam(required = false) String search, @RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize) { public Page<Document> getAllSyncUser(@RequestParam(required = false) String search, @RequestParam(defaultValue = "1", name = "pageNum") Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize) {
Query query = new Query(); Query query = new Query();
query.fields().exclude("_id"); query.fields().exclude("_id");
if (search != null && !search.isEmpty()) { if (search != null && !search.isEmpty()) {
......
package com.keymobile.syncdata.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data // 使用 Lombok 自动生成 getter/setter
public class TodoRequest {
@Schema(description = "OA账号", required = true)
private String oaAccount;
@Schema(description = "待办信息标题", required = true)
private String title;
@Schema(description = "待办信息跳转地址", required = true)
private String link;
}
\ No newline at end of file
package com.keymobile.syncdata.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "oa")
@Component
public class OAProperties {
private String url;
private String xHWID;
private String xHWAPPKEY;
private String appName;
private String modelName;
private String modelId;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
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;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public String getModelId() {
return modelId;
}
public void setModelId(String modelId) {
this.modelId = modelId;
}
}
\ No newline at end of file
package com.keymobile.syncdata.service;
public interface OAService {
void getTodo(String loginName);
// String getTodoCount();
void sendTodo(String loginName, String title, String link);
// String deleteTodo();
//
// String getTodoList();
//
// String updateTodo();
}
package com.keymobile.syncdata.service.impl;
import com.keymobile.syncdata.properties.OAProperties;
import com.keymobile.syncdata.service.OAService;
import com.keymobile.syncdata.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import static com.keymobile.syncdata.util.DateUtil.getDateTime;
@Service
public class OAServiceImpl implements OAService {
@Autowired
private OAProperties oaProperties;
static {
disableSslVerification();
}
private static void disableSslVerification() {
try
{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}
@Override
public void getTodo(String loginName) {
//定义roma请求接口
String url = oaProperties.getUrl() + "/getTodo";
//https://roma-uat.shenzhenmc.com/api/roma/oa/sendTodo
HttpHeaders headers = new HttpHeaders();
headers.set("X-HW-ID", oaProperties.getxHWID() );
headers.set("X-HW-APPKEY", oaProperties.getxHWAPPKEY() );
//经过roma接口不需要做验证
Map<String,Object> paramBody = new HashMap<String,Object>();
Map<String, String> targetsMap = new HashMap<>();
targetsMap.put("LoginName", loginName);
paramBody.put("targets", ObjectUtil.toJson(targetsMap));
// paramBody.put("targets", "{\"LoginName\":\"dengmengqian\"}");
// 定义http请求实体对象
HttpEntity<Map<String,Object>> entity = new HttpEntity<Map<String,Object>>(paramBody,headers);
// 发送请求
RestTemplate template = new RestTemplate();
ResponseEntity<Map> exchange = template.exchange(url, HttpMethod.POST, entity, Map.class);
System.out.println(entity);
System.out.println(entity.getHeaders().getContentType());
System.out.println(entity.getBody());
System.out.println(exchange.getBody());
}
@Override
public void sendTodo(String loginName, String title, String link) {
//定义roma请求接口
String url = oaProperties.getUrl() + "/sendTodo";
HttpHeaders headers = new HttpHeaders();
headers.set("X-HW-ID", oaProperties.getxHWID() );
headers.set("X-HW-APPKEY", oaProperties.getxHWAPPKEY() );
// 定义请求参数Map
Map<String,Object> paramBody = new HashMap<String,Object>();
paramBody.put("appName", oaProperties.getAppName());
paramBody.put("modelName", oaProperties.getModelName());
paramBody.put("modelId", oaProperties.getModelId());
paramBody.put("subject", title);
paramBody.put("link", link);
paramBody.put("type", "2");
Map<String, String> targetsMap = new HashMap<>();
targetsMap.put("LoginName", loginName);
paramBody.put("targets", ObjectUtil.toJson(targetsMap));
paramBody.put("createTime", getDateTime());
Map<String, String> creatorMap = new HashMap<>();
creatorMap.put("PersonNo", getUser());
paramBody.put("docCreator", ObjectUtil.toJson(creatorMap));
paramBody.put("level", "3");
// 定义http请求实体对象
HttpEntity<Map<String,Object>> entity = new HttpEntity<Map<String,Object>>(paramBody,headers);
// 发送请求
RestTemplate template = new RestTemplate();
ResponseEntity<Map> exchange = template.exchange(url, HttpMethod.POST, entity, Map.class);
System.out.println(exchange.getBody());
}
/**
* 获取用户.
* @return user
*/
private String getUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {
return auth.getName();
}
return "NO_USER";
}
}
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