699 lines
27 KiB
Java
699 lines
27 KiB
Java
|
package com.management.controller;
|
|||
|
|
|||
|
import com.alibaba.fastjson.JSON;
|
|||
|
import com.alibaba.fastjson.JSONArray;
|
|||
|
import com.alibaba.fastjson.JSONObject;
|
|||
|
import com.management.constant.CertificateConstant;
|
|||
|
import com.management.constant.URLConstant;
|
|||
|
import com.management.entity.Filter;
|
|||
|
import com.management.utils.CrmRequestUtil;
|
|||
|
import lombok.extern.slf4j.Slf4j;
|
|||
|
import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
import org.springframework.http.HttpEntity;
|
|||
|
import org.springframework.http.HttpHeaders;
|
|||
|
import org.springframework.http.MediaType;
|
|||
|
import org.springframework.web.bind.annotation.PostMapping;
|
|||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|||
|
import org.springframework.web.bind.annotation.RestController;
|
|||
|
import org.springframework.web.client.RestClientException;
|
|||
|
import org.springframework.web.client.RestTemplate;
|
|||
|
|
|||
|
import java.time.LocalDateTime;
|
|||
|
import java.time.format.DateTimeFormatter;
|
|||
|
import java.util.Arrays;
|
|||
|
import java.util.HashMap;
|
|||
|
import java.util.Map;
|
|||
|
import java.util.UUID;
|
|||
|
|
|||
|
/**
|
|||
|
* 部门集成
|
|||
|
*
|
|||
|
* @param
|
|||
|
* @return null
|
|||
|
* @Author weiloong_zhang
|
|||
|
*/
|
|||
|
@RestController
|
|||
|
@RequestMapping("/zy/api/dept")
|
|||
|
@Slf4j
|
|||
|
public class DeptController {
|
|||
|
|
|||
|
@Autowired
|
|||
|
private KDTokenController kdTokenController;
|
|||
|
|
|||
|
private RestTemplate restTemplate = new RestTemplate();
|
|||
|
|
|||
|
public static void main(String[] args) {
|
|||
|
// new DeptController().syncDept();
|
|||
|
new DeptController().syncDeptUp();
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 部门同步,新增接口
|
|||
|
*
|
|||
|
* @return void
|
|||
|
* @Author weiloong_zhang
|
|||
|
*/
|
|||
|
@PostMapping("/sync_create")
|
|||
|
public void syncDept() {
|
|||
|
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
|
|||
|
//获取当前时间
|
|||
|
LocalDateTime now = LocalDateTime.now();
|
|||
|
log.info("当前时间:{}", now);
|
|||
|
//获取两天前的时间
|
|||
|
LocalDateTime twoDaysAgo = now.minusDays(2);
|
|||
|
|
|||
|
//将时间转换样式
|
|||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|||
|
String twoDaysAgoStr = twoDaysAgo.format(formatter);
|
|||
|
|
|||
|
//开始查询部门(暂时使用固定值)
|
|||
|
Map data = new HashMap<>();
|
|||
|
data.put("createtime", "2025-06-01 00:00:00");
|
|||
|
data.put("enable", "1");
|
|||
|
// data.put("number", Arrays.asList("ZHY002.005.003.004"));
|
|||
|
// data.put("level", "3");
|
|||
|
|
|||
|
Map deptReq = new HashMap<>();
|
|||
|
deptReq.put("data", data);
|
|||
|
deptReq.put("pageNo", 1);
|
|||
|
deptReq.put("pageSize", 1999);
|
|||
|
|
|||
|
String accessToken = new KDTokenController().getKDAccessToken();
|
|||
|
|
|||
|
if (accessToken == null || accessToken.equals("")) {
|
|||
|
log.info("金蝶token为空或不存在");
|
|||
|
return;
|
|||
|
}
|
|||
|
String uuid = UUID.randomUUID().toString().replace("-", "");
|
|||
|
//开始封装请求头
|
|||
|
HttpHeaders headers = new HttpHeaders();
|
|||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|||
|
headers.set("accessToken", accessToken);
|
|||
|
headers.set("Idempotency-Key", uuid);
|
|||
|
|
|||
|
HttpEntity deptReqEntity = new HttpEntity(deptReq, headers);
|
|||
|
|
|||
|
//开始发起请求
|
|||
|
//请求地址
|
|||
|
String deptUrl = "https://lxr2.zhydsp.cn:40448/ierp/kapi/v2/f9w5/base/bos_adminorg/adminOrgQuery";
|
|||
|
String deptRes = "";
|
|||
|
|
|||
|
try {
|
|||
|
deptRes = restTemplate.postForObject(deptUrl, deptReqEntity, String.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
//将请求到的数据转换为JSONObject
|
|||
|
JSONObject deptResObj = JSON.parseObject(deptRes);
|
|||
|
|
|||
|
//开始执行判断
|
|||
|
if (deptResObj.getBooleanValue("status") != true || deptResObj.getJSONObject("data").isEmpty() || deptResObj.getJSONObject("data").getJSONArray("rows").isEmpty()) {
|
|||
|
log.info("金蝶部门数据请求失败或者为空");
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
//数据不为空,开始遍历数据
|
|||
|
JSONArray deptRows = deptResObj.getJSONObject("data").getJSONArray("rows");
|
|||
|
|
|||
|
for (Object deptObj : deptRows) {
|
|||
|
JSONObject deptData = JSON.parseObject(JSON.toJSONString(deptObj));
|
|||
|
|
|||
|
log.info("当前正在操作的数据为:{}", deptData);
|
|||
|
|
|||
|
//获取部门数据
|
|||
|
//部门id
|
|||
|
String deptId = deptData.getString("id");
|
|||
|
|
|||
|
//获取最后更新时间
|
|||
|
String createTime = deptData.getString("createtime");
|
|||
|
|
|||
|
//查询日志
|
|||
|
JSONObject getLog = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
getLog = restTemplate.getForObject("http://localhost:18085/Log/query/log_data?table=send_log_bmxx&log_type=DEPT&dataId=" + deptId + "&mark=" + createTime, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
if ("success".equals(getLog.getString("message")) && !getLog.getJSONArray("data").isEmpty()) {
|
|||
|
log.info("当前部门已经新建过了,将不再向下执行");
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
log.info("没有集成过,将继续向下执行");
|
|||
|
|
|||
|
//部门名称
|
|||
|
String deptName = deptData.getString("name");
|
|||
|
|
|||
|
if (deptName.contains("、")) {
|
|||
|
deptName = deptName.replace("、", "_");
|
|||
|
}
|
|||
|
//父部门编码
|
|||
|
JSONObject structureObj = deptData.getJSONArray("structure").getJSONObject(0);
|
|||
|
String parentDeptCode = structureObj.getString("viewparent_id");
|
|||
|
String parentDeptName = structureObj.getString("viewparent_name");
|
|||
|
//部门编码
|
|||
|
String deptCode = deptData.getString("number");
|
|||
|
|
|||
|
//开始封装数据
|
|||
|
Map deptSyncReq = new HashMap<>();
|
|||
|
deptSyncReq.put("name", deptName);
|
|||
|
deptSyncReq.put("erp_id__c", deptId);
|
|||
|
deptSyncReq.put("field_Vj6sf__c", deptCode);
|
|||
|
deptSyncReq.put("field_v711K__c", parentDeptName);
|
|||
|
deptSyncReq.put("field_31u4r__c", parentDeptCode);
|
|||
|
deptSyncReq.put("owner", Arrays.asList("FSUID_50D3C26809B163F663DC8557268A8D44"));
|
|||
|
deptSyncReq.put("dataObjectApiName", "DepartmentObj");
|
|||
|
|
|||
|
//查找父编码
|
|||
|
//根据金蝶部门父编码查询crm部门父编码
|
|||
|
JSONObject parentDeptReq = crmRequestUtil.getCRMList(Arrays.asList(
|
|||
|
new Filter("EQ", "erp_id__c", Arrays.asList(parentDeptCode))
|
|||
|
), "DepartmentObj");
|
|||
|
|
|||
|
JSONObject parentDeptRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
parentDeptRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, parentDeptReq, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
System.out.println(parentDeptRes);
|
|||
|
|
|||
|
if (!"success".equals(parentDeptRes.getString("errorDescription"))) {
|
|||
|
log.info("查询失败");
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
if (parentDeptRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|||
|
log.info("父编码为空,将不再向下执行");
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
//获取父部门和父编码
|
|||
|
JSONObject parentDeptData = parentDeptRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
|
|||
|
|
|||
|
String parentDeptId = parentDeptData.getString("_id");
|
|||
|
|
|||
|
deptSyncReq.put("parent_id", Arrays.asList(parentDeptId));
|
|||
|
//判断部门是否在crm中存在
|
|||
|
//请求封装
|
|||
|
JSONObject deptIsExistReq = crmRequestUtil.getCRMList(Arrays.asList(
|
|||
|
new Filter("EQ", "name", Arrays.asList(deptName)),
|
|||
|
new Filter("EQ", "field_v711K__c", Arrays.asList(parentDeptName)),
|
|||
|
new Filter("EQ", "field_31u4r__c", Arrays.asList(parentDeptCode))
|
|||
|
), "DepartmentObj");
|
|||
|
|
|||
|
//开始发起请求
|
|||
|
JSONObject deptIsExistRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
deptIsExistRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, deptIsExistReq, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
if ("success".equals(deptIsExistRes.getString("errorDescription")) && !deptIsExistRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|||
|
//部门已经存在,将直接进行数据更新
|
|||
|
log.info("部门已经存在,将直接进行数据更新");
|
|||
|
String dept_id = deptIsExistRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("_id");
|
|||
|
|
|||
|
deptSyncReq.put("_id", dept_id);
|
|||
|
|
|||
|
//开始执行更新
|
|||
|
JSONObject deptUpReq = crmRequestUtil.updateCRM(deptSyncReq);
|
|||
|
JSONObject deptUpRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
deptUpRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_MAIN, deptUpReq, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
log.info("部门更新状态为:{}", deptUpRes);
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
//crm中不存在该部门,继续进行新增
|
|||
|
//开始执行新建
|
|||
|
JSONObject deptCreateReq = crmRequestUtil.createCRM(deptSyncReq);
|
|||
|
|
|||
|
JSONObject deptCreateRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
deptCreateRes = restTemplate.postForObject(URLConstant.CREATE_CRM_MAIN, deptCreateReq, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
log.info("部门创建状态为:{}", deptCreateRes);
|
|||
|
|
|||
|
//开始封装日志
|
|||
|
Map logMap = new HashMap<>();
|
|||
|
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
|
|||
|
logMap.put("log_type", "DEPT");
|
|||
|
logMap.put("syn_type", "0");
|
|||
|
logMap.put("data_name", deptName);
|
|||
|
logMap.put("data_id", deptId);
|
|||
|
logMap.put("mark", createTime);
|
|||
|
logMap.put("send_body", JSON.toJSONString(deptCreateReq));
|
|||
|
logMap.put("send_res", JSON.toJSONString(deptCreateRes));
|
|||
|
logMap.put("tableName", "send_log_bmxx");
|
|||
|
|
|||
|
//判断
|
|||
|
if ("success".equals(deptCreateRes.getString("errorDescription"))) {
|
|||
|
logMap.put("log_status", "0");
|
|||
|
logMap.put("res_body", "同步成功");
|
|||
|
} else {
|
|||
|
logMap.put("log_status", "1");
|
|||
|
logMap.put("res_body", "同步失败" + deptCreateRes.getString("errorMessage"));
|
|||
|
}
|
|||
|
|
|||
|
JSONObject logRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
logRes = restTemplate.postForObject("http://localhost:18085/Log/insert/log_data", logMap, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
log.info("日志上传状态为:{}", logRes);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* 部门更新接口
|
|||
|
*
|
|||
|
* @return void
|
|||
|
* @Author weiloong_zhang
|
|||
|
*/
|
|||
|
@PostMapping("/sync_up")
|
|||
|
public void syncDeptUp() {
|
|||
|
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
|
|||
|
//获取当前时间
|
|||
|
LocalDateTime now = LocalDateTime.now();
|
|||
|
log.info("当前时间:{}", now);
|
|||
|
//获取两天前的时间
|
|||
|
LocalDateTime twoDaysAgo = now.minusDays(2);
|
|||
|
|
|||
|
//将时间转换样式
|
|||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|||
|
String twoDaysAgoStr = twoDaysAgo.format(formatter);
|
|||
|
|
|||
|
//开始查询部门(暂时使用固定值)
|
|||
|
Map data = new HashMap<>();
|
|||
|
data.put("modifytime", "2025-06-01 00:00:00");
|
|||
|
data.put("enable", "1");
|
|||
|
// data.put("number", Arrays.asList("ZHY002.005.001.006"));
|
|||
|
// data.put("level", "3");
|
|||
|
|
|||
|
Map deptReq = new HashMap<>();
|
|||
|
deptReq.put("data", data);
|
|||
|
deptReq.put("pageNo", 1);
|
|||
|
deptReq.put("pageSize", 1000);
|
|||
|
|
|||
|
String accessToken = new KDTokenController().getKDAccessToken();
|
|||
|
|
|||
|
if (accessToken == null || accessToken.equals("")) {
|
|||
|
log.info("金蝶token为空或不存在");
|
|||
|
return;
|
|||
|
}
|
|||
|
//开始封装请求头
|
|||
|
HttpHeaders headers = new HttpHeaders();
|
|||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|||
|
headers.set("accessToken", accessToken);
|
|||
|
|
|||
|
HttpEntity deptReqEntity = new HttpEntity(deptReq, headers);
|
|||
|
|
|||
|
//开始发起请求
|
|||
|
//请求地址
|
|||
|
String deptUrl = "https://lxr2.zhydsp.cn:40448/ierp/kapi/v2/f9w5/base/bos_adminorg/adminOrgQuery";
|
|||
|
String deptRes = "";
|
|||
|
|
|||
|
try {
|
|||
|
deptRes = restTemplate.postForObject(deptUrl, deptReqEntity, String.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
//将请求到的数据转换为JSONObject
|
|||
|
JSONObject deptResObj = JSON.parseObject(deptRes);
|
|||
|
System.out.println("请求结果为:" + deptRes);
|
|||
|
|
|||
|
//开始执行判断
|
|||
|
if (deptResObj.getBooleanValue("status") != true || deptResObj.getJSONObject("data").isEmpty() || deptResObj.getJSONObject("data").getJSONArray("rows").isEmpty()) {
|
|||
|
log.info("金蝶部门数据请求失败或者为空");
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
//数据不为空,开始遍历数据
|
|||
|
JSONArray deptRows = deptResObj.getJSONObject("data").getJSONArray("rows");
|
|||
|
|
|||
|
for (Object deptObj : deptRows) {
|
|||
|
JSONObject deptData = JSON.parseObject(JSON.toJSONString(deptObj));
|
|||
|
|
|||
|
log.info("当前正在操作的数据为:{}", deptData);
|
|||
|
|
|||
|
//获取部门数据
|
|||
|
//部门id
|
|||
|
String deptId = deptData.getString("id");
|
|||
|
|
|||
|
//获取最后更新时间
|
|||
|
String modifyTime = deptData.getString("modifytime");
|
|||
|
|
|||
|
//查询日志
|
|||
|
JSONObject getLog = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
getLog = restTemplate.getForObject("http://localhost:18085/Log/query/log_data?table=send_log_bmxx&log_type=DEPTUP&dataId=" + deptId + "&mark=" + modifyTime, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
if ("success".equals(getLog.getString("message")) && !getLog.getJSONArray("data").isEmpty()) {
|
|||
|
log.info("当前部门已经更新过了,将不再向下执行");
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
log.info("没有集成过,将继续向下执行");
|
|||
|
|
|||
|
//部门名称
|
|||
|
String deptName = deptData.getString("name");
|
|||
|
|
|||
|
if (deptName.contains("、")) {
|
|||
|
deptName = deptName.replace("、", "_");
|
|||
|
}
|
|||
|
//父部门编码
|
|||
|
JSONObject structureObj = deptData.getJSONArray("structure").getJSONObject(0);
|
|||
|
String parentDeptCode = structureObj.getString("viewparent_id");
|
|||
|
String parentDeptName = structureObj.getString("viewparent_name");
|
|||
|
//部门编码
|
|||
|
String deptCode = deptData.getString("number");
|
|||
|
|
|||
|
//开始封装数据
|
|||
|
Map deptSyncReq = new HashMap<>();
|
|||
|
deptSyncReq.put("name", deptName);
|
|||
|
deptSyncReq.put("erp_id__c", deptId);
|
|||
|
deptSyncReq.put("field_Vj6sf__c", deptCode);
|
|||
|
deptSyncReq.put("field_v711K__c", parentDeptName);
|
|||
|
deptSyncReq.put("field_31u4r__c", parentDeptCode);
|
|||
|
deptSyncReq.put("owner", Arrays.asList("FSUID_50D3C26809B163F663DC8557268A8D44"));
|
|||
|
deptSyncReq.put("dataObjectApiName", "DepartmentObj");
|
|||
|
|
|||
|
//查找父编码
|
|||
|
//根据金蝶部门父编码查询crm部门父编码
|
|||
|
JSONObject parentDeptReq = crmRequestUtil.getCRMList(Arrays.asList(
|
|||
|
new Filter("EQ", "erp_id__c", Arrays.asList(parentDeptCode))
|
|||
|
), "DepartmentObj");
|
|||
|
|
|||
|
JSONObject parentDeptRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
parentDeptRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, parentDeptReq, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
if (!"success".equals(parentDeptRes.getString("errorDescription")) || parentDeptRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|||
|
log.info("父编码为空,将不再向下执行");
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
//获取父部门和父编码
|
|||
|
JSONObject parentDeptData = parentDeptRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
|
|||
|
|
|||
|
String parentDeptId = parentDeptData.getString("_id");
|
|||
|
|
|||
|
deptSyncReq.put("parent_id", Arrays.asList(parentDeptId));
|
|||
|
|
|||
|
//判断部门是否在crm中存在
|
|||
|
//请求封装
|
|||
|
JSONObject deptIsExistReq = crmRequestUtil.getCRMList(Arrays.asList(
|
|||
|
new Filter("EQ", "name", Arrays.asList(deptName)),
|
|||
|
new Filter("EQ", "field_v711K__c", Arrays.asList(parentDeptName)),
|
|||
|
new Filter("EQ", "field_31u4r__c", Arrays.asList(parentDeptCode))
|
|||
|
), "DepartmentObj");
|
|||
|
|
|||
|
//开始发起请求
|
|||
|
JSONObject deptIsExistRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
deptIsExistRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, deptIsExistReq, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
if ("success".equals(deptIsExistRes.getString("errorDescription")) && deptIsExistRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|||
|
//数据不存在,将不再向下执行
|
|||
|
log.info("部门不存在,将不再向下执行");
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
//数据存在,将继续向下执行更新
|
|||
|
//部门已经存在,将直接进行数据更新
|
|||
|
log.info("部门已经存在,将直接进行数据更新");
|
|||
|
String dept_id = deptIsExistRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("_id");
|
|||
|
|
|||
|
deptSyncReq.put("_id", dept_id);
|
|||
|
|
|||
|
//开始执行更新
|
|||
|
JSONObject deptUpReq = crmRequestUtil.updateCRM(deptSyncReq);
|
|||
|
JSONObject deptUpRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
deptUpRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_MAIN, deptUpReq, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
//开始封装日志
|
|||
|
Map logMap = new HashMap<>();
|
|||
|
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
|
|||
|
logMap.put("log_type", "DEPTUP");
|
|||
|
logMap.put("syn_type", "1");
|
|||
|
logMap.put("data_name", deptName);
|
|||
|
logMap.put("data_id", deptId);
|
|||
|
logMap.put("mark", modifyTime);
|
|||
|
logMap.put("send_body", JSON.toJSONString(deptUpReq));
|
|||
|
logMap.put("send_res", JSON.toJSONString(deptUpRes));
|
|||
|
logMap.put("tableName", "send_log_bmxx");
|
|||
|
|
|||
|
//判断
|
|||
|
if ("success".equals(deptUpRes.getString("errorDescription"))) {
|
|||
|
logMap.put("log_status", "0");
|
|||
|
logMap.put("res_body", "同步成功");
|
|||
|
} else {
|
|||
|
logMap.put("log_status", "1");
|
|||
|
logMap.put("res_body", "同步失败");
|
|||
|
}
|
|||
|
|
|||
|
JSONObject logRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
logRes = restTemplate.postForObject("http://localhost:18085/Log/insert/log_data", logMap, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
log.info("日志上传状态为:{}", logRes);
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 部门删除同步
|
|||
|
*
|
|||
|
* @return void
|
|||
|
* @Author weiloong_zhang
|
|||
|
*/
|
|||
|
@PostMapping("/sync_delete")
|
|||
|
public void syncDeptDel() {
|
|||
|
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
|
|||
|
//获取当前时间
|
|||
|
LocalDateTime now = LocalDateTime.now();
|
|||
|
log.info("当前时间:{}", now);
|
|||
|
//获取两天前的时间
|
|||
|
LocalDateTime twoDaysAgo = now.minusDays(2);
|
|||
|
|
|||
|
//将时间转换样式
|
|||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|||
|
String twoDaysAgoStr = twoDaysAgo.format(formatter);
|
|||
|
|
|||
|
//开始查询部门(暂时使用固定值)
|
|||
|
Map data = new HashMap<>();
|
|||
|
data.put("modifytime", "2020-01-01 00:00:00");
|
|||
|
data.put("enable", "2");
|
|||
|
//data.put("number", Arrays.asList("ZHY003.002"));
|
|||
|
// data.put("level", "3");
|
|||
|
|
|||
|
Map deptReq = new HashMap<>();
|
|||
|
deptReq.put("data", data);
|
|||
|
deptReq.put("pageNo", 1);
|
|||
|
deptReq.put("pageSize", 1000);
|
|||
|
|
|||
|
String accessToken = kdTokenController.getKDAccessToken();
|
|||
|
|
|||
|
if (accessToken == null || accessToken.equals("")) {
|
|||
|
log.info("金蝶token为空或不存在");
|
|||
|
return;
|
|||
|
}
|
|||
|
//开始封装请求头
|
|||
|
HttpHeaders headers = new HttpHeaders();
|
|||
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|||
|
headers.set("accessToken", accessToken);
|
|||
|
|
|||
|
HttpEntity deptReqEntity = new HttpEntity(deptReq, headers);
|
|||
|
|
|||
|
//开始发起请求
|
|||
|
//请求地址
|
|||
|
String deptUrl = "https://lxr2.zhydsp.cn:40448/ierp/kapi/v2/f9w5/base/bos_adminorg/adminOrgQuery";
|
|||
|
String deptRes = "";
|
|||
|
|
|||
|
try {
|
|||
|
deptRes = restTemplate.postForObject(deptUrl, deptReqEntity, String.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
//将请求到的数据转换为JSONObject
|
|||
|
JSONObject deptResObj = JSON.parseObject(deptRes);
|
|||
|
System.out.println("请求结果为:" + deptRes);
|
|||
|
|
|||
|
//开始执行判断
|
|||
|
if (deptResObj.getBooleanValue("status") != true || deptResObj.getJSONObject("data").isEmpty() || deptResObj.getJSONObject("data").getJSONArray("rows").isEmpty()) {
|
|||
|
log.info("金蝶部门数据请求失败或者为空");
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
//数据不为空,开始遍历数据
|
|||
|
JSONArray deptRows = deptResObj.getJSONObject("data").getJSONArray("rows");
|
|||
|
|
|||
|
for (Object deptObj : deptRows) {
|
|||
|
JSONObject deptData = JSON.parseObject(JSON.toJSONString(deptObj));
|
|||
|
|
|||
|
log.info("当前正在操作的数据为:{}", deptData);
|
|||
|
|
|||
|
//获取部门数据
|
|||
|
//部门id
|
|||
|
String deptId = deptData.getString("id");
|
|||
|
|
|||
|
//获取最后更新时间
|
|||
|
String modifyTime = deptData.getString("modifytime");
|
|||
|
|
|||
|
//查询日志
|
|||
|
JSONObject getLog = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
getLog = restTemplate.getForObject("http://localhost:18085/Log/query/log_data?table=send_log_bmxx&log_type=DEPTDEL&dataId=" + deptId + "&mark=" + modifyTime, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
if ("success".equals(getLog.getString("message")) && !getLog.getJSONArray("data").isEmpty()) {
|
|||
|
log.info("当前部门已经执行过了,将不再向下执行");
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
log.info("没有集成过,将继续向下执行");
|
|||
|
|
|||
|
//部门名称
|
|||
|
String deptName = deptData.getString("name");
|
|||
|
//父部门编码
|
|||
|
JSONObject structureObj = deptData.getJSONArray("structure").getJSONObject(0);
|
|||
|
//父级全称
|
|||
|
String parentDeptName = structureObj.getString("viewparent_name");
|
|||
|
String parentDeptCode = structureObj.getString("viewparent_id");
|
|||
|
|
|||
|
//开始查询CRM
|
|||
|
JSONObject crmReq = crmRequestUtil.getCRMList(Arrays.asList(
|
|||
|
new Filter("EQ", "name", Arrays.asList(deptName)),
|
|||
|
new Filter("EQ", "field_v711K__c", Arrays.asList(parentDeptName)),
|
|||
|
new Filter("EQ", "field_31u4r__c", Arrays.asList(parentDeptCode))
|
|||
|
), "DepartmentObj");
|
|||
|
|
|||
|
JSONObject crmRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
crmRes = restTemplate.postForObject(URLConstant.GET_CRM_LIST_URL, crmReq, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
if (!"success".equals(crmRes.getString("errorDescription"))) {
|
|||
|
log.info("请求失败");
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
if (crmRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
|
|||
|
log.info("未查询到数据");
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
//数据存在,执行作废
|
|||
|
|
|||
|
JSONObject crmData = crmRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
|
|||
|
|
|||
|
String crmId = crmData.getString("_id");
|
|||
|
|
|||
|
//开始封装作废数据
|
|||
|
|
|||
|
Map delData = new HashMap<>();
|
|||
|
delData.put("object_data_id", crmId);
|
|||
|
delData.put("dataObjectApiName", "DepartmentObj");
|
|||
|
|
|||
|
Map delReq = new HashMap<>();
|
|||
|
delReq.put("corpAccessToken", crmRequestUtil.getCRMToken());
|
|||
|
delReq.put("currentOpenUserId", CertificateConstant.CURRENT_OPEN_USERID);
|
|||
|
delReq.put("corpId", CertificateConstant.CORP_ID);
|
|||
|
delReq.put("data", delData);
|
|||
|
|
|||
|
//开始请求作废接口
|
|||
|
JSONObject delRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
delRes = restTemplate.postForObject("https://open.fxiaoke.com/cgi/crm/custom/v2/data/invalid", delReq, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
//开始封装日志
|
|||
|
Map logMap = new HashMap<>();
|
|||
|
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
|
|||
|
logMap.put("log_type", "DEPTDEL");
|
|||
|
logMap.put("syn_type", "2");
|
|||
|
logMap.put("data_name", deptName);
|
|||
|
logMap.put("data_id", deptId);
|
|||
|
logMap.put("mark", modifyTime);
|
|||
|
logMap.put("send_body", JSON.toJSONString(delReq));
|
|||
|
logMap.put("send_res", JSON.toJSONString(delRes));
|
|||
|
logMap.put("tableName", "send_log_bmxx");
|
|||
|
|
|||
|
//判断
|
|||
|
if ("success".equals(delRes.getString("errorDescription"))) {
|
|||
|
logMap.put("log_status", "0");
|
|||
|
logMap.put("res_body", "同步成功");
|
|||
|
} else {
|
|||
|
logMap.put("log_status", "1");
|
|||
|
logMap.put("res_body", "同步失败" + delRes.getString("errorMessage"));
|
|||
|
}
|
|||
|
|
|||
|
JSONObject logRes = new JSONObject();
|
|||
|
|
|||
|
try {
|
|||
|
logRes = restTemplate.postForObject("http://localhost:18085/Log/insert/log_data", logMap, JSONObject.class);
|
|||
|
} catch (RestClientException e) {
|
|||
|
e.printStackTrace();
|
|||
|
}
|
|||
|
|
|||
|
log.info("日志上传状态为:{}", logRes);
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|