浩赛的一部分集成模块

This commit is contained in:
itzhang 2025-08-11 15:31:19 +08:00
parent 512375c735
commit 39842ffda2
10 changed files with 1860 additions and 2 deletions

View File

@ -0,0 +1,38 @@
package org.ssssssss.magicboot.Enum;
import java.util.HashMap;
import java.util.Map;
public enum ProductClassEnum {
核算提取("1"),
PCR("2"),
克隆及电泳("3"),
免疫学("4"),
细胞生物学("5"),
小分子化学物("6"),
耗材("7"),
仪器设备("8"),
医疗器械("9");
private final String value;
private static final Map<String, String> MAPPING = new HashMap<>();
static {
for (ProductClassEnum e : values()) {
MAPPING.put(e.name(), e.getValue());
}
}
ProductClassEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static String getValueByName(String name) {
return MAPPING.get(name);
}
}

View File

@ -0,0 +1,50 @@
package org.ssssssss.magicboot.Enum;
import java.util.HashMap;
import java.util.Map;
public enum ProductLineEnum {
DNA提取("1"),
RNA提取("2"),
质粒提取("3"),
DNA片段纯化回收("4"),
常规PCR("5"),
RTPCR("6"),
qPCR("7"),
克隆("8"),
核算电泳("9"),
免疫学("10"),
细胞生物学("11"),
小分子化合物("12"),
通用耗材("13"),
研磨仪("14"),
洗瓶机("15"),
厌氧系统("16"),
二氧化碳培养箱("17"),
电动移液器("18"),
菌落计数仪("19"),
实验服务("20");
private final String value;
private static final Map<String, String> MAPPING = new HashMap<>();
static {
for (ProductLineEnum e : values()) {
MAPPING.put(e.name(), e.getValue());
}
}
ProductLineEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static String getValueByName(String name) {
return MAPPING.get(name);
}
}

View File

@ -0,0 +1,74 @@
package org.ssssssss.magicboot.Enum;
import java.util.HashMap;
import java.util.Map;
public enum ProductUnitEnum {
("1"),
("2"),
("3"),
("4"),
("5"),
("6"),
("7"),
("8"),
("9"),
("10"),
("11"),
("12"),
("13"),
("tYfxl441I"),
("w3CIvrtuY"),
("8Allwng4S"),
("exYb8dmh9"),
KG("P8BTe41sa"),
G("8x1nD3fl9"),
mg("Wy6H1m2qf"),
ug("mDbv75Q4U"),
L("jA1z3mVN3"),
ml("q88JEo6u0"),
ul("CO2lWkaf3"),
("JL016"),
U("JL024"),
("JL026"),
抗体("JL027"),
抗体膜("JL028"),
基因样品("JL029"),
("JL030"),
爬片("JL031"),
细胞皿("JL032"),
项张("JL033"),
基因("JL037"),
("JL038"),
("JL039"),
("JL040"),
("JL036"),
("4Ny2LFeoa"),
("BTjS31OEd"),
("Z32ivmicl"),
("IMx81ewvw"),
("hwxi1faIb"),
("JL046");
private final String value;
private static final Map<String, String> MAPPING = new HashMap<>();
static {
for (ProductUnitEnum e : values()) {
MAPPING.put(e.name(), e.getValue());
}
}
ProductUnitEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public static String getValueByName(String name) {
return MAPPING.get(name);
}
}

View File

@ -451,6 +451,8 @@ public class CustomerController {
Map customerMap = new HashMap<>(); Map customerMap = new HashMap<>();
customerMap.put("data", Arrays.asList(contactMap)); customerMap.put("data", Arrays.asList(contactMap));
System.out.println(JSON.parseObject(JSON.toJSONString(customerMap)));
//开始获取token //开始获取token
String ysToken = ""; String ysToken = "";

View File

@ -0,0 +1,291 @@
package org.ssssssss.magicboot.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
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.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.ssssssss.magicboot.constant.URLConstant;
import org.ssssssss.magicboot.entity.Filter;
import org.ssssssss.magicboot.utils.CrmRequestUtil;
import org.ssssssss.magicboot.utils.YSSignUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* 部门
*
* @param
* @return null
* @Author weiloong_zhang
*/
@RestController
@RequestMapping("/api/dept")
@Slf4j
public class DeptController {
private RestTemplate restTemplate = new RestTemplate();
private static final String DEPT_URL = "https://c1.yonyoucloud.com/iuap-api-gateway/yonbip/digitalModel/OrgUnitSync/orgUnitDataSyncByDTO";
public static void main(String[] args) {
new DeptController().syncDept();
}
/**
* 部门同步
*
* @return void
* @Author weiloong_zhang
*/
@PostMapping("/sync/dept")
public void syncDept() {
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
log.info("开始同步部门");
Map getDeptReq = new HashMap();
getDeptReq.put("parentId", "2132046702701969413");
getDeptReq.put("pageSize", 100);
getDeptReq.put("pageIndex", 1);
getDeptReq.put("externalOrg", 0);
getDeptReq.put("sourceType", 1);
getDeptReq.put("codes", Arrays.asList("SKJ-BM04"));
getDeptReq.put("dr", 0);
getDeptReq.put("funcTypeCode", "orgunit");
System.out.println("部门列表请求参数为:" + JSON.toJSONString(getDeptReq));
//开始获取token
String erpToken = "";
try {
erpToken = new YSSignUtils().getYSToken();
} catch (Exception e) {
e.printStackTrace();
}
//开始发起请求
String url = DEPT_URL + "?access_token=" + erpToken;//url
JSONObject deptRes = new JSONObject();
try {
deptRes = restTemplate.postForObject(url, getDeptReq, JSONObject.class);
} catch (HttpStatusCodeException e) {
//获取响应体的错误信息
String errorBody = e.getResponseBodyAsString();
System.out.println("调用接口失败,状态码:" + e.getStatusCode());
System.out.println("错误响应:" + errorBody);
//将错误响应封装为 JSONObject可选
deptRes = JSONObject.parseObject(errorBody);
} catch (RestClientException e) {
System.out.println("调用接口失败:" + e.getMessage());
}
System.out.println(deptRes);
if (!"200".equals(deptRes.getString("code")) || deptRes.getJSONObject("data").getJSONArray("recordList").isEmpty()) {
log.info("erp请求产品失败或者没有符合条件的数据");
return;
}
log.info("erp请求产品成功继续向下执行");
//开始处理部门数据
for (Object deptObj : deptRes.getJSONObject("data").getJSONArray("recordList")) {
JSONObject deptData = JSON.parseObject(JSON.toJSONString(deptObj));
System.out.println("当前正在处理的部门为:" + deptData);
//开始处理部门数据
String deptId = deptData.getString("id");
String deptName = deptData.getString("name");
String deptCode = deptData.getString("code");
String modifyTime = deptData.getString("modifiedtime");
//开始查询日志中是否已经存在当前数据
JSONObject isLogRes = new JSONObject();
try {
isLogRes = restTemplate.getForObject("http://localhost:18080/Log/query/log_data?table=send_log_bmxx&log_type=DEPT&dataId=" + deptId + "&mark=" + modifyTime, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if (!isLogRes.getJSONArray("data").isEmpty()) {
log.info("日志不存在,将继续向下执行");
continue;
}
log.info("日志不存在,将开始执行");
//开始封装请求
Map deptMapReq = new HashMap<>();
//部门编码
deptMapReq.put("dept_code", deptData.getString("code"));
//部门名称
deptMapReq.put("name", deptData.getString("name"));
//部门简称
deptMapReq.put("department_abbreviation__c", deptData.getString("shortname"));
//部门状态
deptMapReq.put("status", "0");
//erpId
deptMapReq.put("erp_id__c", deptId);
//上级部门判断
//获取当前处理单据地等级
String pid = "999999";
String parentId = "";
//判断级别如果是2则是思科捷最上级如果不是2则是下级
if (!deptId.equals("2132046702701969413")) {
//级别小于2则获取上级部门
//需要获取一下详情以拿到上级部门ID
JSONObject getDeptSuperior = new JSONObject();
String superiorUrl = "https://c1.yonyoucloud.com/iuap-api-gateway/yonbip/digitalModel/admindept/detail" + "?access_token=" + erpToken + "&id=" + deptId;
try {
getDeptSuperior = restTemplate.getForObject(superiorUrl, JSONObject.class);
} catch (HttpStatusCodeException e) {
//获取响应体的错误信息
String errorBody = e.getResponseBodyAsString();
System.out.println("调用接口失败,状态码:" + e.getStatusCode());
System.out.println("错误响应:" + errorBody);
//将错误响应封装为 JSONObject可选
getDeptSuperior = JSONObject.parseObject(errorBody);
} catch (RestClientException e) {
System.out.println("调用接口失败:" + e.getMessage());
}
System.out.println(getDeptSuperior);
if (!"200".equals(getDeptSuperior.getString("code")) || getDeptSuperior.getJSONObject("data").isEmpty()) {
log.info("erp请求部门详情失败或者没有符合条件的数据");
continue;
}
parentId = getDeptSuperior.getJSONObject("data").getString("parent");
JSONObject getPid = crmRequestUtil.getCRMList(Arrays.asList(
new Filter("EQ", "erp_id__c", Arrays.asList(parentId)),
new Filter("EQ", "life_status", Arrays.asList("normal"))
), "DepartmentObj");
JSONObject getPidRes = new JSONObject();
try {
getPidRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getPid, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if (!"success".equals(getPidRes.getString("errorDescription")) || getPidRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
log.info("上级部门不存在");
continue;
}
pid = getPidRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("_id");
}
deptMapReq.put("parent_id", Arrays.asList(pid));
deptMapReq.put("field_1aF2M__c", parentId);
deptMapReq.put("dataObjectApiName", "DepartmentObj");
//开始查询是否已经存在重名部门
JSONObject getDept = crmRequestUtil.getCRMList(Arrays.asList(
new Filter("EQ", "name", Arrays.asList(deptName)),
new Filter("EQ", "life_status", Arrays.asList("normal"))
), "DepartmentObj");
JSONObject getDeptRes = new JSONObject();
try {
getDeptRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getDept, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
Boolean isExist = true;
if ("success".equals(getDeptRes.getString("errorDescription")) && !getDeptRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
log.info("部门存在");
String deptCrmId = getDeptRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("_id");
deptMapReq.put("_id", deptCrmId);
isExist = false;
}
JSONObject createDept = crmRequestUtil.createCRM(deptMapReq);
//开始封装日志
Map logMap = new HashMap<>();
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
logMap.put("data_name", deptName);
logMap.put("data_id", deptId);
logMap.put("mark", modifyTime);
logMap.put("send_body", JSON.toJSONString(createDept));
logMap.put("tableName", "send_log_bmxx");
JSONObject deptCrmRes = new JSONObject();
if (isExist) {
log.info("部门不存在,将执行创建");
logMap.put("log_type", "DEPT");
try {
deptCrmRes = restTemplate.postForObject(URLConstant.CREATE_CRM_MAIN, createDept, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
log.info("创建结果为:" + JSON.parseObject(JSON.toJSONString(deptCrmRes)));
} else {
log.info("部门已存在,将开始更新");
logMap.put("log_type", "DEPTUP");
try {
deptCrmRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_MAIN, createDept, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
log.info("更新结果为:" + JSON.parseObject(JSON.toJSONString(deptCrmRes)));
}
if ("success".equals(deptCrmRes.getString("errorDescription"))) {
logMap.put("log_status", "0");
logMap.put("res_body", "同步成功");
} else {
logMap.put("log_status", "1");
logMap.put("res_body", "同步失败" + deptCrmRes.getString("errorMessage"));
}
logMap.put("send_res", JSON.toJSONString(deptCrmRes));
//logMap.put("send_memo", JSON.toJSONString(categoryData));
JSONObject logRes = new JSONObject();
try {
logRes = restTemplate.postForObject("http://localhost:18080/Log/insert/log_data", logMap, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("日志插入结果为:" + logRes);
}
}
}

View File

@ -6,15 +6,18 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.ssssssss.magicboot.constant.URLConstant; import org.ssssssss.magicboot.constant.URLConstant;
import org.ssssssss.magicboot.entity.Filter; import org.ssssssss.magicboot.entity.Filter;
import org.ssssssss.magicboot.utils.CrmRequestUtil; import org.ssssssss.magicboot.utils.CrmRequestUtil;
import org.ssssssss.magicboot.utils.YSSignUtils;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
/** /**
* 院所科室 * 院所科室
@ -77,7 +80,7 @@ public class InstituteController {
//开始截取关键数据 //开始截取关键数据
String instituteId = instituteData.getString("_id"); String instituteId = instituteData.getString("_id");
String instituteName = instituteData.getString("name"); String instituteName = instituteData.getString("name");
String modifyTime = instituteData.getString("last_modified_time"); String modifyTime = instituteData.getString("create_time");
//开始查询当前数据是否已经同步过 //开始查询当前数据是否已经同步过
JSONObject isLogRes = new JSONObject(); JSONObject isLogRes = new JSONObject();
@ -94,6 +97,155 @@ public class InstituteController {
} }
log.info("日志不存在,将继续向下执行"); log.info("日志不存在,将继续向下执行");
//开始封装数据
Map institute = new HashMap<>();
institute.put("sourceUnique", "2267063578082148361");
institute.put("custdocdefid", "2157208682701520904");
institute.put("code", instituteData.getString("department_institution_id__c"));
//名称
Map instituteNameMap = new HashMap<>();
instituteNameMap.put("zh_CN", instituteName);
institute.put("name", instituteNameMap);
//简称
institute.put("shortname", instituteData.getString("department_institution_sho__c") != null ? instituteData.getString("department_institution_sho__c") : instituteName);
//排序号
institute.put("sort", instituteData.getString("sort_number__c") != null ? instituteData.getString("sort_number__c") : "");
//备注
Map remarkMap = new HashMap<>();
remarkMap.put("zh_CN", instituteData.getString("field_FgnSW__c") != null ? instituteData.getString("field_FgnSW__c") : "");
institute.put("description", remarkMap);
//业务类型
Map typeMap = new HashMap<>();
typeMap.put("zh_CN", "院所或科室");
institute.put("custdocdefid___name", typeMap);
//组织
Map orgMap = new HashMap<>();
orgMap.put("zh_CN", "企业账号级");
orgMap.put("zh_TW", "企業帳號級");
orgMap.put("en_US", "Enterprise Account");
institute.put("orgid___name", orgMap);
Map request = new HashMap<>();
request.put("matchRule", "code");
request.put("unmatchedStrategy", "add");
request.put("data", Arrays.asList(institute));
//开始获取token
String ysToken = "";
try {
ysToken = new YSSignUtils().getYSToken();
} catch (Exception e) {
e.printStackTrace();
}
//开始封装请求url
String url = "https://c1.yonyoucloud.com/iuap-api-gateway/yonbip/digitalModel/customerdoc/batchSave?access_token=" + ysToken;
JSONObject instituteYSRes = new JSONObject();
try {
instituteYSRes = restTemplate.postForObject(url, request, JSONObject.class);
} catch (HttpStatusCodeException e) {
// 获取响应体的错误信息
String errorBody = e.getResponseBodyAsString();
System.out.println("调用接口失败,状态码:" + e.getStatusCode());
System.out.println("错误响应:" + errorBody);
// 将错误响应封装为 JSONObject可选
instituteYSRes = JSONObject.parseObject(errorBody);
} catch (RestClientException e) {
System.out.println("调用接口失败:" + e.getMessage());
}
System.out.println(instituteYSRes);
//开始封装回写程序
Map backReqMap = new HashMap();
backReqMap.put("_id", instituteId);
backReqMap.put("dataObjectApiName", "department_institute__c");
//开始封装日志
Map logMap = new HashMap<>();
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
logMap.put("log_type", "KSYS");
logMap.put("syn_type", "0");
logMap.put("data_name", instituteName);
logMap.put("data_id", instituteId);
logMap.put("mark", modifyTime);
logMap.put("send_body", JSON.toJSONString(request));
logMap.put("send_res", JSON.toJSONString(instituteYSRes));
logMap.put("tableName", "send_log_khxx");
//判断是否执行成功
if ("200".equals(instituteYSRes.getString("code"))) {
log.info("同步成功");
logMap.put("log_status", "0");
logMap.put("res_body", "同步成功");
//获取ERPID
JSONObject erpMap = instituteYSRes.getJSONObject("data").getJSONArray("infos").getJSONObject(0);
backReqMap.put("field_eO8gK__c", erpMap.getString("id"));
backReqMap.put("sync_status__c", "option_sync_success__c");
backReqMap.put("response_status__c", "同步成功");
} else {
log.info("同步失败");
logMap.put("log_status", "1");
logMap.put("res_body", "同步失败:" + instituteYSRes.getJSONObject("data").getJSONArray("message").toString());
backReqMap.put("sync_status__c", "option_sync_failed__c");
backReqMap.put("response_status__c", instituteYSRes.getJSONObject("data").getJSONArray("message").toString());
}
//开始回写
JSONObject unlockReq = crmRequestUtil.unlockCRM(instituteId, "department_institute__c");
//解锁
if ("1".equals(instituteData.getString("lock_status"))) {
log.info("解锁");
JSONObject unlockRes = new JSONObject();
try {
unlockRes = restTemplate.postForObject(URLConstant.UNLOCK_CRM_URL, unlockReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("解锁" + unlockRes);
}
JSONObject backReq = crmRequestUtil.updateCRM(backReqMap);
System.out.println("回写请求为:" + JSON.parseObject(JSON.toJSONString(backReq)));
JSONObject backRes = new JSONObject();
try {
backRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_CUSTOMIZE, backReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("回写结果为:" + backRes);
logMap.put("send_memo", JSON.toJSONString(backRes));
//开始记录日志
JSONObject logRes = new JSONObject();
try {
logRes = restTemplate.postForObject("http://localhost:18080/Log/insert/log_data", logMap, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("日志插入结果为:" + logRes);
} }
} }
} }

View File

@ -0,0 +1,414 @@
package org.ssssssss.magicboot.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import groovyjarjarpicocli.CommandLine;
import lombok.extern.slf4j.Slf4j;
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.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.ssssssss.magicboot.constant.URLConstant;
import org.ssssssss.magicboot.entity.Filter;
import org.ssssssss.magicboot.utils.CrmRequestUtil;
import org.ssssssss.magicboot.utils.YSSignUtils;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.logging.SimpleFormatter;
@RestController
@RequestMapping("/api/haosai")
@Slf4j
public class PersonController {
private RestTemplate restTemplate = new RestTemplate();
private static final String PERSON_URL = "https://c1.yonyoucloud.com/iuap-api-gateway/yonbip/digitalModel/staff/listForApi";
public static void main(String[] args) {
new PersonController().syncPerson();
}
/**
* 同步人员信息
*
* @return void
* @Author weiloong_zhang
*/
@PostMapping("/sync/person")
public void syncPerson() {
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
log.info("开始同步人员信息");
//开始封装请求参数
LinkedHashMap getPersonReq = new LinkedHashMap<>();
getPersonReq.put("pageIndex", 1);
getPersonReq.put("pageSize", 1);
getPersonReq.put("objid", "");
getPersonReq.put("name", "");
getPersonReq.put("mobile", "");
getPersonReq.put("email", "");
getPersonReq.put("orgIds", Arrays.asList("2132046702701969413"));
getPersonReq.put("userId", "");
getPersonReq.put("pubts", "");
//getPersonReq.put("enable", Arrays.asList(0));
System.out.println("人员列表请求参数为:" + JSON.toJSONString(getPersonReq));
//开始获取token
String erpToken = "";
try {
erpToken = new YSSignUtils().getYSToken();
} catch (Exception e) {
e.printStackTrace();
}
//开始发起请求
String url = PERSON_URL + "?access_token=" + erpToken;//url
System.out.println("人员列表请求地址为:" + url);
JSONObject personRes = new JSONObject();
try {
personRes = restTemplate.postForObject(url, getPersonReq, JSONObject.class);
} catch (HttpStatusCodeException e) {
//获取响应体的错误信息
String errorBody = e.getResponseBodyAsString();
System.out.println("调用接口失败,状态码:" + e.getStatusCode());
System.out.println("错误响应:" + errorBody);
//将错误响应封装为 JSONObject可选
personRes = JSONObject.parseObject(errorBody);
} catch (RestClientException e) {
System.out.println("调用接口失败:" + e.getMessage());
}
System.out.println(personRes);
if (!"200".equals(personRes.getString("code")) || personRes.getJSONObject("data").getJSONArray("recordList").isEmpty()) {
log.info("erp请求产品失败或者没有符合条件的数据");
return;
}
log.info("erp请求产品成功继续向下执行");
for (Object personObj : personRes.getJSONObject("data").getJSONArray("recordList")) {
JSONObject personData = JSON.parseObject(JSON.toJSONString(personObj));
System.out.println("当前正在处理的员工数据为:" + personData);
//开始截取关键数据
String personId = personData.getString("id");
String personName = personData.getString("name");
String modifyTime = personData.getString("modifiedtime");
Integer dr = personData.getInteger("dr");
if (dr == 1) {
continue;
}
//开始查询是否已经执行过当前数据
JSONObject isLogRes = new JSONObject();
try {
isLogRes = restTemplate.getForObject("http://localhost:18080/Log/query/log_data?table=send_log_person&log_type=PERSON&dataId=" + personId + "&mark=" + modifyTime, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if (!isLogRes.getJSONArray("data").isEmpty()) {
log.info("日志存在,将不再继续向下执行");
continue;
}
log.info("日志不存在,将开始执行");
//开始封装请求
Map personMapReq = new HashMap<>();
personMapReq.put("dataObjectApiName", "PersonnelObj");
//erp编码
personMapReq.put("erp_code__c", personData.getString("code"));
//erpId
personMapReq.put("erp_id__c", personId);
//系统名
personMapReq.put("name", personName);
//姓名
personMapReq.put("full_name", personName);
//性别
String sex = personData.getString("sex") != null ? personData.getString("sex") : "";
String crmSex = "";
//男是1女是2
switch (sex) {
case "1":
crmSex = "M";
break;
case "2":
crmSex = "F";
break;
default:
crmSex = "P";
break;
}
personMapReq.put("sex", crmSex);
//生日
String birthday = personData.getString("birthdate") != null ? personData.getString("birthdate") : "";
long timestamp = 0;
if (!"".equals(birthday)) {
// 定义格式器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 解析为无时区的本地时间
LocalDateTime localDateTime = LocalDateTime.parse(birthday, formatter);
// 转为时间戳毫秒
timestamp = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
if (timestamp != 0) {
personMapReq.put("birthday", timestamp);
} else {
personMapReq.put("birthday", null);
}
//手机
String mobile = personData.getString("mobile") != null ? personData.getString("mobile") : "";
if (!"".equals(mobile)) {
mobile = mobile.replace("+86-", "");
}
personMapReq.put("phone", mobile);
//邮箱
personMapReq.put("email", personData.getString("email") != null ? personData.getString("email") : "");
//备注
personMapReq.put("note__c", personData.getString("remark") != null ? personData.getString("remark") : "");
//todo 组织
//主属部门
String dept = personData.getString("dept_id") != null ? personData.getString("dept_id") : "";
if ("".equals(dept)) {
log.info("部门为空");
continue;
}
log.info("部门不为空");
//查询部门
JSONObject getDeptReq = crmRequestUtil.getCRMList(Arrays.asList(
new Filter("EQ", "erp_id__c", Arrays.asList(dept)),
new Filter("EQ", "life_status", Arrays.asList("normal"))
), "DepartmentObj");
JSONObject getDeptRes = new JSONObject();
try {
getDeptRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getDeptReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println(getDeptRes);
if (!"success".equals(getDeptRes.getString("errorDescription")) || getDeptRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
log.info("部门不存在,将不再继续向下执行");
continue;
}
log.info("部门存在,将开始执行");
String dept_id = getDeptRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("_id");
personMapReq.put("main_department", Arrays.asList(dept_id));
//获取主职信息
JSONObject mainJob = personData.getJSONArray("mainJobList").getJSONObject(0);
//职务
String jobStr = mainJob.getString("job_id") != null ? mainJob.getString("job_id") : "";
if (!"".equals(jobStr)) {
//开始查询职务
LinkedHashMap jobMap = new LinkedHashMap();
jobMap.put("drList", Arrays.asList(0));
jobMap.put("idList", Arrays.asList(jobStr));
jobMap.put("pageIndex", 1);
jobMap.put("pageSize", 10);
JSONObject getJobReq = new JSONObject();
try {
getJobReq = restTemplate.postForObject("https://c1.yonyoucloud.com/iuap-api-gateway/yonbip/digitalModel/duty/listForIntegration?access_token=" + erpToken, jobMap, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println(getJobReq);
if ("200".equals(getJobReq.getString("code")) && !getJobReq.getJSONObject("data").getJSONArray("recordList").isEmpty()) {
JSONObject jobData = getJobReq.getJSONObject("data").getJSONArray("recordList").getJSONObject(0);
String job = jobData.getJSONObject("name").getString("zh_CN") != null ? jobData.getJSONObject("name").getString("zh_CN") : "";
personMapReq.put("position", job);
}
}
//主管
String leaderId = mainJob.getString("director") != null ? mainJob.getString("director") : "";
if (!"".equals(leaderId)) {
JSONObject getLeaderReq = crmRequestUtil.getCRMList(Arrays.asList(
new Filter("EQ", "erp_id__c", Arrays.asList(leaderId)),
new Filter("EQ", "life_status", Arrays.asList("normal"))
), "PersonnelObj");
JSONObject getLeaderRes = new JSONObject();
try {
getLeaderRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getLeaderReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if ("success".equals(getLeaderRes.getString("errorDescription")) && getLeaderRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
log.info("主管存在");
String leader_id = getLeaderRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0).getString("_id");
personMapReq.put("leader", Arrays.asList(leader_id));
}
}
String beginTime = mainJob.getString("begindate") != null ? mainJob.getString("begindate") : "";
System.out.println(beginTime);
long beginTimestamp = 0;
if (!"".equals(beginTime)) {
// 定义格式器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 解析为无时区的本地时间
LocalDateTime localDateTime = LocalDateTime.parse(beginTime, formatter);
// 转换为毫秒时间戳
beginTimestamp = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
if (beginTimestamp != 0) {
personMapReq.put("date_of_first_ployment", beginTimestamp);
} else {
personMapReq.put("date_of_first_ployment", null);
}
//开始判断是否已存在该人员数据
JSONObject getPersonFromCRM = crmRequestUtil.getCRMList(Arrays.asList(
new Filter("EQ", "name", Arrays.asList(personName)),
new Filter("EQ", "life_status", Arrays.asList("normal"))
), "PersonnelObj");
JSONObject getPersonFromCRMRes = new JSONObject();
try {
getPersonFromCRMRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getPersonFromCRM, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
Boolean isExist = true;
if ("success".equals(getPersonFromCRMRes.getString("errorDescription")) && getPersonFromCRMRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
log.info("该人员已经存在");
JSONObject personMainReq = getPersonFromCRMRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
String personCrmId = personMainReq.getString("_id");
personMapReq.put("_id", personCrmId);
isExist = false;
}
//开始封装数据
JSONObject personReq = crmRequestUtil.updateCRM(personMapReq);
System.out.println("请求为:" + JSON.parseObject(JSON.toJSONString(personReq)));
//开始封装日志
Map logMap = new HashMap<>();
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
logMap.put("data_name", personName);
logMap.put("data_id", personId);
logMap.put("mark", modifyTime);
logMap.put("send_body", JSON.toJSONString(personReq));
logMap.put("tableName", "send_log_person");
JSONObject createPersonRes = new JSONObject();
if (isExist) {
log.info("该产人员不存在,将执行创建");
logMap.put("log_type", "CP");
try {
createPersonRes = restTemplate.postForObject(URLConstant.CREATE_CRM_MAIN, personReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("创建结果为:" + JSON.parseObject(JSON.toJSONString(createPersonRes)));
} else {
log.info("该人员已存在,将执行更新");
logMap.put("log_type", "CPUP");
try {
createPersonRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_MAIN, personReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("更新结果为:" + JSON.parseObject(JSON.toJSONString(createPersonRes)));
}
if ("success".equals(createPersonRes.getString("errorDescription"))) {
logMap.put("log_status", "0");
logMap.put("res_body", "同步成功");
} else {
logMap.put("log_status", "1");
logMap.put("res_body", "同步失败" + createPersonRes.getString("errorMessage"));
}
logMap.put("send_res", JSON.toJSONString(createPersonRes));
//logMap.put("send_memo", JSON.toJSONString(categoryData));
JSONObject logRes = new JSONObject();
try {
logRes = restTemplate.postForObject("http://localhost:18080/Log/insert/log_data", logMap, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("日志插入结果为:" + logRes);
}
}
}

View File

@ -0,0 +1,271 @@
package org.ssssssss.magicboot.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
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.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.ssssssss.magicboot.constant.URLConstant;
import org.ssssssss.magicboot.entity.Filter;
import org.ssssssss.magicboot.utils.CrmRequestUtil;
import org.ssssssss.magicboot.utils.YSSignUtils;
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;
@RestController
@RequestMapping("/api/product/category")
@Slf4j
public class ProductCategoryController {
private RestTemplate restTemplate = new RestTemplate();
public static final String CATEGORY_URL = "https://c1.yonyoucloud.com/iuap-api-gateway/yonbip/digitalModel/managementclass/integration/newtree";
public static void main(String[] args) {
new ProductCategoryController().syncCategory();
}
/**
* 开始同步产品分类
*
* @return void
* @Author weiloong_zhang
*/
@PostMapping
public void syncCategory() {
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
log.info("开始同步产品分类");
//开始封装请求参数
//获取当前的时间
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String nowStr = now.format(sdf);
log.info("当前时间:{}", nowStr);
//获取两天前的时间
LocalDateTime twoDaysAgo = now.minusDays(5);//直接减去2天
String twoDaysAgoStr = twoDaysAgo.format(sdf);
log.info("两天前的时间:{}", twoDaysAgoStr);
//开始封装请求
Map getCategoryReq = new HashMap<>();
getCategoryReq.put("pageIndex", 1);
getCategoryReq.put("pageSize", 100);
//getCategoryReq.put("code", "000007");
getCategoryReq.put("endTime", nowStr);
getCategoryReq.put("beginTime", twoDaysAgoStr);
System.out.println(JSONObject.parseObject(JSON.toJSONString(getCategoryReq)));
//开始获取token
String erpToken = "";
try {
erpToken = new YSSignUtils().getYSToken();
} catch (Exception e) {
e.printStackTrace();
}
//开始发起请求
String url = CATEGORY_URL + "?access_token=" + erpToken;//url
JSONObject categoryRes = new JSONObject();
try {
categoryRes = restTemplate.postForObject(url, getCategoryReq, JSONObject.class);
} catch (HttpStatusCodeException e) {
// 获取响应体的错误信息
String errorBody = e.getResponseBodyAsString();
System.out.println("调用接口失败,状态码:" + e.getStatusCode());
System.out.println("错误响应:" + errorBody);
// 将错误响应封装为 JSONObject可选
categoryRes = JSONObject.parseObject(errorBody);
} catch (RestClientException e) {
System.out.println("调用接口失败:" + e.getMessage());
}
System.out.println(categoryRes);
if (!"200".equals(categoryRes.getString("code")) || categoryRes.getJSONObject("data").getJSONArray("recordList").isEmpty()) {
log.info("erp请求产品分类失败或者没有符合条件的数据");
return;
}
log.info("erp请求产品分类成功继续向下执行");
//开始遍历
for (Object categoryObj : categoryRes.getJSONObject("data").getJSONArray("recordList")) {
JSONObject categoryData = JSON.parseObject(JSON.toJSONString(categoryObj));
System.out.println("当前正在处理的产品分类数据为:" + categoryData);
//开始截取关键数据
String categoryId = categoryData.getString("id");
String categoryCode = categoryData.getString("code");
String categoryName = categoryData.getJSONObject("name").getString("simplifiedName");
//开始查询是否已经执行过了
JSONObject isLogRes = new JSONObject();
try {
isLogRes = restTemplate.getForObject("http://localhost:18080/Log/query/log_data?table=send_log_product&log_type=CPFL&dataId=" + categoryId + "&mark=" + categoryCode, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if (!isLogRes.getJSONArray("data").isEmpty()) {
log.info("产品分类已经执行过了,将不再继续向下执行");
continue;
}
log.info("产品分类没有执行过,将开始执行");
//开始封装处理数据
Map categoryReq = new HashMap<>();
categoryReq.put("name", categoryName);
categoryReq.put("erp_id__c", categoryId);
categoryReq.put("category_code", categoryCode);
categoryReq.put("owner", Arrays.asList("FSUID_8F452FA358F6FE84F608A2BBFCD0E4A3"));
categoryReq.put("dataObjectApiName", "ProductCategoryObj");
String remark = categoryData.getJSONObject("remark").getString("simplifiedName") != null ? categoryData.getJSONObject("remark").getString("simplifiedName") : "";
categoryReq.put("category_describe", remark);
//判断是否存在上级部门
String parentCode = categoryData.getString("parent") != null ? categoryData.getString("parent") : "";
if (!"".equals(parentCode)) {
log.info("上级部门存在,将查询上级部门");
JSONObject parentCategoryReq = crmRequestUtil.getCRMList(Arrays.asList(
new Filter("EQ", "erp_id__c", Arrays.asList(parentCode)),
new Filter("EQ", "life_status", Arrays.asList("normal"))
), "ProductCategoryObj");
JSONObject parentCategoryRes = new JSONObject();
try {
parentCategoryRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, parentCategoryReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if ("success".equals(parentCategoryRes.getString("errorDescription")) && !parentCategoryRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
log.info("上级分类存在");
JSONObject parentCategoryData = parentCategoryRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
categoryReq.put("pid", parentCategoryData.getString("_id"));
} else {
log.info("上级分类不存在,不再向下执行");
continue;
}
}
//查询是否已存在当前单据
JSONObject isExist = crmRequestUtil.getCRMList(Arrays.asList(
new Filter("EQ", "erp_id__c", Arrays.asList(categoryId)),
new Filter("EQ", "life_status", Arrays.asList("normal"))
), "ProductCategoryObj");
Boolean isExistFlag = true;
JSONObject getCategoryRes = new JSONObject();
try {
getCategoryRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, isExist, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if ("success".equals(getCategoryRes.getString("errorDescription")) && !getCategoryRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
log.info("产品分类已存在");
JSONObject getCategoryData = getCategoryRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
categoryReq.put("_id", getCategoryData.getString("_id"));
isExistFlag = false;
}
//开始创建产品分类
JSONObject createCategoryReq = crmRequestUtil.createCRM(categoryReq);
System.out.println(JSON.toJSONString(createCategoryReq));
//开始封装日志
Map logMap = new HashMap<>();
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
logMap.put("data_name", categoryName);
logMap.put("data_id", categoryId);
logMap.put("mark", categoryCode);
logMap.put("send_body", JSON.toJSONString(createCategoryReq));
logMap.put("tableName", "send_log_product");
JSONObject createCategoryRes = new JSONObject();
if (isExistFlag) {
log.info("产品分类不存在,将开始创建");
logMap.put("log_type", "CPFL");
try {
createCategoryRes = restTemplate.postForObject(URLConstant.CREATE_CRM_CUSTOMER_MAIN, createCategoryReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
log.info("创建结果为:" + JSON.parseObject(JSON.toJSONString(createCategoryRes)));
} else {
log.info("产品分类已存在,将开始更新");
logMap.put("log_type", "CPFLUP");
try {
createCategoryRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_CUSTOMIZE, createCategoryReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
log.info("更新结果为:" + JSON.parseObject(JSON.toJSONString(createCategoryRes)));
}
if ("success".equals(createCategoryRes.getString("errorDescription"))) {
logMap.put("log_status", "0");
logMap.put("res_body", "同步成功");
} else {
logMap.put("log_status", "1");
logMap.put("res_body", "同步失败" + createCategoryRes.getString("errorMessage"));
}
logMap.put("send_res", JSON.toJSONString(createCategoryRes));
//logMap.put("send_memo", JSON.toJSONString(categoryData));
JSONObject logRes = new JSONObject();
try {
logRes = restTemplate.postForObject("http://localhost:18080/Log/insert/log_data", logMap, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("日志插入结果为:" + logRes);
}
}
}

View File

@ -0,0 +1,320 @@
package org.ssssssss.magicboot.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
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.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.ssssssss.magicboot.Enum.ProductClassEnum;
import org.ssssssss.magicboot.Enum.ProductLineEnum;
import org.ssssssss.magicboot.Enum.ProductUnitEnum;
import org.ssssssss.magicboot.constant.URLConstant;
import org.ssssssss.magicboot.entity.Filter;
import org.ssssssss.magicboot.utils.CrmRequestUtil;
import org.ssssssss.magicboot.utils.YSSignUtils;
import java.math.BigDecimal;
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("/api/hs/product")
@Slf4j
public class ProductController {
private RestTemplate restTemplate = new RestTemplate();
private static final String PRODUCT_URL = "https://c1.yonyoucloud.com/iuap-api-gateway/yonbip/digitalModel/product/integration/querylist";
public static void main(String[] args) {
new ProductController().syncProduct();
}
/**
* 产品同步
*
* @return void
* @Author weiloong_zhang
*/
@PostMapping("/sync/product")
public void syncProduct() {
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
log.info("开始同步产品");
//开始封装请求参数
//获取当前的时间
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
String nowStr = now.format(sdf);
log.info("当前时间:{}", nowStr);
//获取两天前的时间
LocalDateTime twoDaysAgo = now.minusDays(5);//直接减去2天
String twoDaysAgoStr = twoDaysAgo.format(sdf);
log.info("两天前的时间:{}", twoDaysAgoStr);
//开始封装请求
Map getProductReq = new HashMap<>();
getProductReq.put("pageIndex", 1);
getProductReq.put("pageSize", 100);
getProductReq.put("code", "000008");
getProductReq.put("endTime", nowStr);
getProductReq.put("beginTime", twoDaysAgoStr);
System.out.println(JSONObject.parseObject(JSON.toJSONString(getProductReq)));
//开始获取token
String erpToken = "";
try {
erpToken = new YSSignUtils().getYSToken();
} catch (Exception e) {
e.printStackTrace();
}
//开始发起请求
String url = PRODUCT_URL + "?access_token=" + erpToken;//url
JSONObject productRes = new JSONObject();
try {
productRes = restTemplate.postForObject(url, getProductReq, JSONObject.class);
} catch (HttpStatusCodeException e) {
// 获取响应体的错误信息
String errorBody = e.getResponseBodyAsString();
System.out.println("调用接口失败,状态码:" + e.getStatusCode());
System.out.println("错误响应:" + errorBody);
// 将错误响应封装为 JSONObject可选
productRes = JSONObject.parseObject(errorBody);
} catch (RestClientException e) {
System.out.println("调用接口失败:" + e.getMessage());
}
System.out.println(productRes);
if (!"200".equals(productRes.getString("code")) || productRes.getJSONObject("data").getJSONArray("recordList").isEmpty()) {
log.info("erp请求产品失败或者没有符合条件的数据");
return;
}
log.info("erp请求产品成功继续向下执行");
for (Object productObj : productRes.getJSONObject("data").getJSONArray("recordList")) {
JSONObject productData = JSON.parseObject(JSON.toJSONString(productObj));
System.out.println("当前正在处理的产品为:" + productData);
//开始截取关键数据
String productId = productData.getString("id");
String productCode = productData.getString("code");
String productName = productData.getString("name");
//开始查询是否已经执行过了
JSONObject isLogRes = new JSONObject();
try {
isLogRes = restTemplate.getForObject("http://localhost:18080/Log/query/log_data?table=send_log_product&log_type=CP&dataId=" + productId + "&mark=" + productCode, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if (!isLogRes.getJSONArray("data").isEmpty()) {
log.info("产品已经执行过了,将不再继续向下执行");
continue;
}
log.info("产品没有执行过,将开始执行");
//开始封装数据
Map product = new HashMap<>();
product.put("dataObjectApiName", "ProductObj");
product.put("field_266q7__c", productData.getString("id"));
product.put("name", productCode);//产品货号
product.put("product_name__c", productName);//产品名称
product.put("product_spec", productData.getString("modelDescription") != null ? productData.getString("modelDescription") : "");//规格型号
//产品短码
product.put("product_short_code__c", productData.getString("model") != null ? productData.getString("model") : "");
//物料分类
String categoryCode = productData.getString("manageClass") != null ? productData.getString("manageClass") : "";
if (categoryCode.equals("")) {
log.info("物料分类为空,将不再继续向下执行");
continue;
}
JSONObject getCategory = crmRequestUtil.getCRMList(Arrays.asList(
new Filter("EQ", "erp_id__c", Arrays.asList(categoryCode)),
new Filter("EQ", "life_status", Arrays.asList("normal"))
), "ProductCategoryObj");
JSONObject categoryRes = new JSONObject();
try {
categoryRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getCategory, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if (!"success".equals(categoryRes.getString("errorDescription")) || categoryRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
log.info("产品分类不存在");
continue;
}
//产品分类存在
JSONObject categoryData = categoryRes.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
//产品分类封装
product.put("product_category_id", categoryData.getString("_id"));
//todo 产品线
String productLineStr = "";
String productLine = "";
if (!productLineStr.equals("")) {
productLine = ProductLineEnum.getValueByName(productLineStr.replace("-", ""));
}
//产品线封装
product.put("product_line", productLine);
//品牌
JSONObject productBrand = productData.getJSONObject("productCharacterDef");
//品牌封装
product.put("brand_name__c", productBrand.getString("PP0001") != null ? productBrand.getString("PP0001") : "");
//todo 产品大类
String productBigClassStr = "";
String productBigClass = "";
if (!productBigClassStr.equals("")) {
productBigClass = ProductClassEnum.getValueByName(productBigClassStr);
}
product.put("product_category__c", productBigClass);
//产品标准价格
product.put("price", productBrand.getBigDecimal("MLJ01") != null ? productBrand.getBigDecimal("MLJ01") : BigDecimal.ZERO);
//单位
String unitStr = productData.getString("unitName") != null ? productData.getString("unitName") : "";
String unit = "";
if (!unitStr.equals("")) {
unit = ProductUnitEnum.getValueByName(unitStr.replace("/", ""));
}
//单位封装
product.put("unit", unit);
//开始封装请求
JSONObject createProduct = crmRequestUtil.createCRM(product);
System.out.println("请求为:" + JSON.parseObject(JSON.toJSONString(createProduct)));
//开始判断是否已存在重名数据
JSONObject getProduct = crmRequestUtil.getCRMList(Arrays.asList(
new Filter("EQ", "name", Arrays.asList(productCode)),
new Filter("EQ", "life_status", Arrays.asList("normal"))
), "ProductObj");
JSONObject productResult = new JSONObject();
try {
productResult = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getProduct, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
Boolean isExist = true;
if ("success".equals(productResult.getString("errorDescription")) && !productResult.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
JSONObject productResultData = productResult.getJSONObject("data").getJSONArray("dataList").getJSONObject(0);
product.put("_id", productResultData.getString("_id"));
isExist = false;
}
//开始封装日志
Map logMap = new HashMap<>();
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
logMap.put("data_name", productName);
logMap.put("data_id", productId);
logMap.put("mark", productCode);
logMap.put("send_body", JSON.toJSONString(createProduct));
logMap.put("tableName", "send_log_product");
JSONObject createProductRes = new JSONObject();
if (isExist) {
log.info("该产品不存在,将执行创建");
logMap.put("log_type", "CP");
try {
createProductRes = restTemplate.postForObject(URLConstant.CREATE_CRM_MAIN, createProduct, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("创建结果为:" + JSON.parseObject(JSON.toJSONString(createProductRes)));
} else {
log.info("该产品已存在,将执行更新");
logMap.put("log_type", "CPUP");
try {
createProductRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_MAIN, createProduct, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("更新结果为:" + JSON.parseObject(JSON.toJSONString(createProductRes)));
}
if ("success".equals(createProductRes.getString("errorDescription"))) {
logMap.put("log_status", "0");
logMap.put("res_body", "同步成功");
} else {
logMap.put("log_status", "1");
logMap.put("res_body", "同步失败" + createProductRes.getString("errorMessage"));
}
logMap.put("send_res", JSON.toJSONString(createProductRes));
//logMap.put("send_memo", JSON.toJSONString(categoryData));
JSONObject logRes = new JSONObject();
try {
logRes = restTemplate.postForObject("http://localhost:18080/Log/insert/log_data", logMap, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("日志插入结果为:" + logRes);
}
}
}

View File

@ -0,0 +1,246 @@
package org.ssssssss.magicboot.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
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.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.ssssssss.magicboot.constant.URLConstant;
import org.ssssssss.magicboot.entity.Filter;
import org.ssssssss.magicboot.utils.CrmRequestUtil;
import org.ssssssss.magicboot.utils.YSSignUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* 课题组
*
* @param
* @return null
* @Author weiloong_zhang
*/
@RestController
@RequestMapping("/api/hs")
@Slf4j
public class ResearchGroupController {
private RestTemplate restTemplate = new RestTemplate();
public static void main(String[] args) {
new ResearchGroupController().syncResearchGroup();
}
/**
* 课题组同步创建
*
* @return void
* @Author weiloong_zhang
*/
@PostMapping("/sync/research")
public void syncResearchGroup() {
CrmRequestUtil crmRequestUtil = new CrmRequestUtil();
log.info("开始同步课题组");
JSONObject getResearch = crmRequestUtil.getCRMList(Arrays.asList(
new Filter("EQ", "name", Arrays.asList("保护伞课题组"))
), "topic_group__c");
JSONObject researchRes = new JSONObject();
try {
researchRes = restTemplate.postForObject(URLConstant.GET_CUSTOMIZE_LIST_URL, getResearch, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if (!"success".equals(researchRes.getString("errorDescription")) || researchRes.getJSONObject("data").getJSONArray("dataList").isEmpty()) {
log.info("该课题组不存在");
return;
}
log.info("该课题组存在,继续向下执行");
for (Object researchObj : researchRes.getJSONObject("data").getJSONArray("dataList")) {
JSONObject researchData = JSON.parseObject(JSON.toJSONString(researchObj));
System.out.println("当前正在处理数据为:" + researchData);
//开始截取关键数据
String researchId = researchData.getString("_id");
String researchName = researchData.getString("name");
String modifyTime = researchData.getString("create_time");
JSONObject isLogRes = new JSONObject();
try {
isLogRes = restTemplate.getForObject("http://localhost:18080/Log/query/log_data?table=send_log_khxx&log_type=KTZ&dataId=" + researchId + "&mark=" + modifyTime, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
if (!isLogRes.getJSONArray("data").isEmpty()) {
log.info("当前数据已经执行过了,将不再向下执行");
continue;
}
log.info("日志不存在,将继续向下执行");
//开始封装
Map researchMap = new HashMap();
researchMap.put("sourceUnique", "2141608528251453447");
researchMap.put("custdocdefid", "2137219552709181445");
//编码
researchMap.put("code", researchData.getString("topic_group_code__c") != null ? researchData.getString("topic_group_code__c") : "");
//名称
Map nameMap = new HashMap<>();
nameMap.put("zh_CN", researchData.getString("name") != null ? researchData.getString("name") : "");
researchMap.put("name", nameMap);
//简称
researchMap.put("shortname", researchData.getString("topic_group_abbreviation__c") != null ? researchData.getString("topic_group_abbreviation__c") : "");
//排序号
researchMap.put("sort", researchData.getBigDecimal("sort_number__c") != null ? researchData.getBigDecimal("sort_number__c") : "");
//描述
Map descriptionMap = new HashMap<>();
descriptionMap.put("zh_CN", researchData.getString("note__c") != null ? researchData.getString("note__c") : "");
researchMap.put("description", descriptionMap);
//业务类型
Map typeMap = new HashMap<>();
typeMap.put("zh_CN", "课题组");
researchMap.put("custdocdefid___name", typeMap);
//组织
Map orgMap = new HashMap<>();
orgMap.put("zh_CN", "企业账号级");
orgMap.put("zh_TW", "企業帳號級");
orgMap.put("en_US", "Enterprise Account");
researchMap.put("orgid___name", orgMap);
Map request = new HashMap<>();
request.put("matchRule", "code");
request.put("unmatchedStrategy", "add");
request.put("data", Arrays.asList(researchMap));
//开始获取token
String ysToken = "";
try {
ysToken = new YSSignUtils().getYSToken();
} catch (Exception e) {
e.printStackTrace();
}
//开始封装请求url
String url = "https://c1.yonyoucloud.com/iuap-api-gateway/yonbip/digitalModel/customerdoc/batchSave?access_token=" + ysToken;
JSONObject researchYSRes = new JSONObject();
try {
researchYSRes = restTemplate.postForObject(url, request, JSONObject.class);
} catch (HttpStatusCodeException e) {
// 获取响应体的错误信息
String errorBody = e.getResponseBodyAsString();
System.out.println("调用接口失败,状态码:" + e.getStatusCode());
System.out.println("错误响应:" + errorBody);
// 将错误响应封装为 JSONObject可选
researchYSRes = JSONObject.parseObject(errorBody);
} catch (RestClientException e) {
System.out.println("调用接口失败:" + e.getMessage());
}
System.out.println(researchYSRes);
//开始封装回写程序
Map backReqMap = new HashMap();
backReqMap.put("_id", researchId);
backReqMap.put("dataObjectApiName", "topic_group__c");
//开始封装日志
Map logMap = new HashMap<>();
logMap.put("log_id", UUID.randomUUID().toString().replace("-", ""));
logMap.put("log_type", "KTZ");
logMap.put("syn_type", "0");
logMap.put("data_name", researchName);
logMap.put("data_id", researchId);
logMap.put("mark", modifyTime);
logMap.put("send_body", JSON.toJSONString(request));
logMap.put("send_res", JSON.toJSONString(researchYSRes));
logMap.put("tableName", "send_log_khxx");
//判断是否执行成功
if ("200".equals(researchYSRes.getString("code"))) {
log.info("同步成功");
logMap.put("log_status", "0");
logMap.put("res_body", "同步成功");
//获取ERPID
JSONObject erpMap = researchYSRes.getJSONObject("data").getJSONArray("infos").getJSONObject(0);
backReqMap.put("erp_id__c", erpMap.getString("id"));
backReqMap.put("sync_status__c", "option_sync_success__c");
backReqMap.put("response_status__c", "同步成功");
} else {
log.info("同步失败");
logMap.put("log_status", "1");
logMap.put("res_body", "同步失败:" + researchYSRes.getJSONObject("data").getJSONArray("message").toString());
backReqMap.put("sync_status__c", "option_sync_failed__c");
backReqMap.put("response_status__c", researchYSRes.getJSONObject("data").getJSONArray("message").toString());
}
//开始回写
JSONObject unlockReq = crmRequestUtil.unlockCRM(researchId, "topic_group__c");
//解锁
if ("1".equals(researchData.getString("lock_status"))) {
log.info("解锁");
JSONObject unlockRes = new JSONObject();
try {
unlockRes = restTemplate.postForObject(URLConstant.UNLOCK_CRM_URL, unlockReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("解锁" + unlockRes);
}
JSONObject backReq = crmRequestUtil.updateCRM(backReqMap);
System.out.println("回写请求为:" + JSON.parseObject(JSON.toJSONString(backReq)));
JSONObject backRes = new JSONObject();
try {
backRes = restTemplate.postForObject(URLConstant.UPDATE_CRM_CUSTOMIZE, backReq, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("回写结果为:" + backRes);
logMap.put("send_memo", JSON.toJSONString(backRes));
//开始记录日志
JSONObject logRes = new JSONObject();
try {
logRes = restTemplate.postForObject("http://localhost:18080/Log/insert/log_data", logMap, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
System.out.println("日志插入结果为:" + logRes);
}
}
}