|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package com.ruoyi.quartz.task.CJT;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.lark.oapi.core.utils.Lists;
|
|
|
|
|
import com.lark.oapi.service.bitable.v1.model.CreateAppTableRecordRespBody;
|
|
|
|
|
import com.ruoyi.common.constant.RedisConstants;
|
|
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
|
@ -22,7 +23,9 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
@ -50,6 +53,8 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
private LarkTableHelper larkTableHelper;
|
|
|
|
|
@Autowired
|
|
|
|
|
private LarkRobotHelper larkRobotHelper;
|
|
|
|
|
@Autowired
|
|
|
|
|
private CJTCreateLarkTableJob cjtCreateLarkTableJob;
|
|
|
|
|
@Value("${lark.robot.group}")
|
|
|
|
|
private String ROBOT_GROUP;
|
|
|
|
|
@Value("${sync.flag}")
|
|
|
|
@ -75,14 +80,45 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
*/
|
|
|
|
|
private static final String REQUEST_GENERATE_TOKEN_PATH = REQUEST_ROOT_PATH + "/v1/common/auth/selfBuiltApp/generateToken";
|
|
|
|
|
|
|
|
|
|
private Boolean asynchronousCheck(LocalDateTime now, String cjt) {
|
|
|
|
|
int hour = now.getHour();
|
|
|
|
|
if (hour != 1) {
|
|
|
|
|
return Boolean.FALSE;
|
|
|
|
|
}
|
|
|
|
|
//目前只给畅捷通账号二加工单开补偿劝降
|
|
|
|
|
if (AppType.SYNC_CJT_MANUFACTURE_ORDER_DATA_TO_MULTI_TABLE.getCode().equals(syncLarkAppType().getCode())
|
|
|
|
|
&& CjtAccountEnum.TWO.getCode().equals(cjt)){
|
|
|
|
|
String asynchronous = getAsynchronousKey(cjt);
|
|
|
|
|
Object cacheObject = redisCache.getCacheObject(asynchronous);
|
|
|
|
|
if (cacheObject != null) {
|
|
|
|
|
return Boolean.FALSE;
|
|
|
|
|
}
|
|
|
|
|
redisCache.setCacheObject(asynchronous, asynchronous, 2, TimeUnit.HOURS);
|
|
|
|
|
return Boolean.TRUE;
|
|
|
|
|
}else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getAsynchronousKey(String cjt){
|
|
|
|
|
return String.format("%s:%s:%s", RedisConstants.CJT_ASYNCHRONOUS, syncLarkAppType().getCode(), cjt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CJTJobContext initContext(String queryFromTime, String queryToTime, LocalDateTime now, String cjt) {
|
|
|
|
|
CJTJobContext context = new CJTJobContext();
|
|
|
|
|
Boolean asynchronous = asynchronousCheck(now, cjt);
|
|
|
|
|
context.setAsynchronous(asynchronous);
|
|
|
|
|
LocalDate localDate = LocalDate.now().minusDays(1L);
|
|
|
|
|
if (StringUtils.isEmpty(queryFromTime)) {
|
|
|
|
|
queryFromTime = DateUtils.ldt2str(now.minusMinutes(REDUCE_MINUTES), DateUtils.YYYY_MM_DD_HH_MM_SS);
|
|
|
|
|
queryFromTime = asynchronous
|
|
|
|
|
? DateUtils.startOfDate(localDate)
|
|
|
|
|
: DateUtils.ldt2str(now.minusMinutes(REDUCE_MINUTES), DateUtils.YYYY_MM_DD_HH_MM_SS);
|
|
|
|
|
}
|
|
|
|
|
context.setQueryFromTime(queryFromTime);
|
|
|
|
|
if (StringUtils.isEmpty(queryToTime)) {
|
|
|
|
|
queryToTime = DateUtils.ldt2str(now, DateUtils.YYYY_MM_DD_HH_MM_SS);
|
|
|
|
|
queryToTime = asynchronous
|
|
|
|
|
? DateUtils.endOfDate(localDate)
|
|
|
|
|
: DateUtils.ldt2str(now, DateUtils.YYYY_MM_DD_HH_MM_SS);
|
|
|
|
|
}
|
|
|
|
|
context.setQueryToTime(queryToTime);
|
|
|
|
|
context.setCjt(cjt);
|
|
|
|
@ -131,19 +167,61 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
log.info("===================== {} strat ======================", this.getClassName());
|
|
|
|
|
//初始化飞书信息及相关配置
|
|
|
|
|
initLarkInfo(context);
|
|
|
|
|
if (Boolean.TRUE.equals(context.getAsynchronous())) {
|
|
|
|
|
Boolean deleteSuccess = handleRepeatData(now, cjt, context);
|
|
|
|
|
if (!deleteSuccess){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//重置ticket
|
|
|
|
|
resetTicket(context);
|
|
|
|
|
//执行分页同步
|
|
|
|
|
sync(context);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("{} 执行失败", getClassName(), e);
|
|
|
|
|
if (Boolean.TRUE.equals(context.getAsynchronous())) {
|
|
|
|
|
String asynchronous = getAsynchronousKey(cjt);
|
|
|
|
|
redisCache.deleteObject(asynchronous);
|
|
|
|
|
}else {
|
|
|
|
|
CJTRetryRequest request = new CJTRetryRequest(now, 1, cjt);
|
|
|
|
|
redisCache.rightPush(getRetryKey(cjt), request);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
log.info("===================== {} end ======================", this.getClassName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Boolean handleRepeatData(LocalDateTime now, String cjt, CJTJobContext context) {
|
|
|
|
|
CjtAccountEnum accountEnum = CjtAccountEnum.getByCode(cjt);
|
|
|
|
|
if (accountEnum == null) {
|
|
|
|
|
return Boolean.FALSE;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
LarkCompanyRelation companyRelation = context.getCompanyRelation();
|
|
|
|
|
LarkTableRelation tableRelation = context.getTableRelation();
|
|
|
|
|
LarkTableRequest request = new LarkTableRequest(companyRelation.getAppId(),companyRelation.getSecret(),tableRelation.getToAppToken(),tableRelation.getToTableId());
|
|
|
|
|
String date = DateUtils.ldt2str(now.minusDays(1L), DateUtils.YYYY_MM_DD);
|
|
|
|
|
List<String> recordList = erpLarkRelationMapper.queryWaitDeleteRecordList(date, getRequestUrl(), cjt);
|
|
|
|
|
List<String> waitDeleteList = new ArrayList<>();
|
|
|
|
|
for (String record : recordList) {
|
|
|
|
|
waitDeleteList.add(record);
|
|
|
|
|
if (waitDeleteList.size() == 500){
|
|
|
|
|
request.setRecords(waitDeleteList.toArray(new String[0]));
|
|
|
|
|
larkTableHelper.deleteTableRecordBatch(request);
|
|
|
|
|
waitDeleteList = new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!CollectionUtils.isEmpty(waitDeleteList)){
|
|
|
|
|
request.setRecords(waitDeleteList.toArray(new String[0]));
|
|
|
|
|
larkTableHelper.deleteTableRecordBatch(request);
|
|
|
|
|
}
|
|
|
|
|
erpLarkRelationMapper.deleteRecordList(date, getRequestUrl(), cjt);
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
return Boolean.FALSE;
|
|
|
|
|
}
|
|
|
|
|
return Boolean.TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getRetryKey(String cjt) {
|
|
|
|
|
return String.format(RETRY_KEY, cjt, syncLarkAppType().getCode());
|
|
|
|
|
}
|
|
|
|
@ -181,6 +259,7 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
* 获取同步多维表格类型配置
|
|
|
|
|
*/
|
|
|
|
|
protected abstract AppType syncLarkAppType();
|
|
|
|
|
|
|
|
|
|
protected abstract TableRelationTypeEnum syncLarkType();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -197,6 +276,7 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
* 获取待查询的字段
|
|
|
|
|
*/
|
|
|
|
|
protected abstract List<String> getQueryFields();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取待查询的字段与飞书的映射关系
|
|
|
|
|
*/
|
|
|
|
@ -206,6 +286,7 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
* 获取查询条件
|
|
|
|
|
*/
|
|
|
|
|
protected abstract String getQueryKey();
|
|
|
|
|
|
|
|
|
|
protected JSONObject getQueryCondition(CJTJobContext context) {
|
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
|
@ -238,7 +319,9 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
*/
|
|
|
|
|
protected String getClassName() {
|
|
|
|
|
return this.getClass().getSimpleName();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 同步数据
|
|
|
|
@ -273,7 +356,7 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
if (!CollectionUtils.isEmpty(rows)) {
|
|
|
|
|
Map<String, String> existKeyMap = getExistKeyMap(cjtRequest, response.getColumns(), rows, context.getCjt());
|
|
|
|
|
//批量同步飞书
|
|
|
|
|
List<String> errorCodeList = syncLarkBatch(response.getColumns(), rows, existKeyMap,addRecordRequest,context.getCjt());
|
|
|
|
|
List<String> errorCodeList = syncLarkBatch(response.getColumns(), rows, existKeyMap, addRecordRequest, context.getCjt(),context.getAsynchronous());
|
|
|
|
|
if (!CollectionUtils.isEmpty(errorCodeList)) {
|
|
|
|
|
String errorKey = String.join(",", errorCodeList);
|
|
|
|
|
throw new RuntimeException(String.format("存在同步失败的记录 %s", errorKey));
|
|
|
|
@ -345,7 +428,8 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
/**
|
|
|
|
|
* 执行字段映射
|
|
|
|
|
*/
|
|
|
|
|
protected List<String> syncLarkBatch(List<String> keyList,List<List<String>> rows,Map<String, String> existKeyMap,LarkTableRequest addRecordRequest,String cjt){
|
|
|
|
|
protected List<String> syncLarkBatch(List<String> keyList, List<List<String>> rows, Map<String, String> existKeyMap
|
|
|
|
|
, LarkTableRequest addRecordRequest, String cjt,Boolean asynchronous) {
|
|
|
|
|
List<ErpLarkRelation> relationList = new ArrayList<>();
|
|
|
|
|
//查询字段与飞书字段的对应关系
|
|
|
|
|
Map<String, CJTSyncTypeRelation> queryFieldsMap = getQueryFieldsMap(cjt);
|
|
|
|
@ -353,6 +437,8 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
List<String> uniqueFields = getUniqueFields(cjt);
|
|
|
|
|
//错误唯一键
|
|
|
|
|
List<String> errorKey = new ArrayList<>();
|
|
|
|
|
//判断当前查询条件内是否存在重复数据
|
|
|
|
|
Map<String, Integer> repeatMap = new HashMap<>();
|
|
|
|
|
for (List<String> row : rows) {
|
|
|
|
|
String uniqueKey = null;
|
|
|
|
|
try {
|
|
|
|
@ -371,13 +457,17 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
body.put(larkLabel, this.changeValueType(value, cjtSyncTypeRelation));
|
|
|
|
|
}
|
|
|
|
|
uniqueKey = String.join("_", uniqueKeyList).toUpperCase();
|
|
|
|
|
if (repeatMap.containsKey(uniqueKey)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
repeatMap.put(uniqueKey, 1);
|
|
|
|
|
String larkKey = existKeyMap.get(uniqueKey);
|
|
|
|
|
addRecordRequest.setBody(body);
|
|
|
|
|
if (StringUtils.isEmpty(larkKey)) {
|
|
|
|
|
//在飞书创建一行,并根据创建返回的行id在本地保留一条映射纪律
|
|
|
|
|
CreateAppTableRecordRespBody respBody = larkTableHelper.addTableRecord(addRecordRequest);
|
|
|
|
|
larkKey = respBody.getRecord().getRecordId();
|
|
|
|
|
relationList.add(buildErpLarkRelation(uniqueKey,larkKey,cjt));
|
|
|
|
|
relationList.add(buildErpLarkRelation(uniqueKey, larkKey, cjt,asynchronous));
|
|
|
|
|
} else {
|
|
|
|
|
if (!AppType.SYNC_CJT_SALE_DISPATCH_DATA_TO_MULTI_TABLE.equals(syncLarkAppType())) {
|
|
|
|
|
//根据本地保留映射,确认飞书的更新行,更新后防止影响后续更新,需要将行id进行置空
|
|
|
|
@ -398,7 +488,7 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//构建飞书行与cjt唯一键的对应关系对象
|
|
|
|
|
private ErpLarkRelation buildErpLarkRelation(String key,String larkKey,String cjt){
|
|
|
|
|
private ErpLarkRelation buildErpLarkRelation(String key, String larkKey, String cjt,Boolean asynchronous) {
|
|
|
|
|
ErpLarkRelation erpLarkRelation = new ErpLarkRelation();
|
|
|
|
|
erpLarkRelation.setKey(key);
|
|
|
|
|
erpLarkRelation.setLarkKey(larkKey);
|
|
|
|
@ -406,7 +496,12 @@ public abstract class SyncAccountsJobAbstract {
|
|
|
|
|
erpLarkRelation.setFlag(FlagStatus.OK.getCode());
|
|
|
|
|
erpLarkRelation.setRemark(cjt);
|
|
|
|
|
erpLarkRelation.setCreateBy("SYSTEM");
|
|
|
|
|
erpLarkRelation.setCreateTime(new Date());
|
|
|
|
|
Date date = new Date();
|
|
|
|
|
if (Boolean.TRUE.equals(asynchronous)){
|
|
|
|
|
LocalDateTime localDateTime = LocalDateTime.now().minusDays(1L);
|
|
|
|
|
date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
|
|
|
|
}
|
|
|
|
|
erpLarkRelation.setCreateTime(date);
|
|
|
|
|
return erpLarkRelation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|