针对账号二加工单的补偿数据措施

畅捷通报表
YXY 1 year ago
parent 5d298ff008
commit 95fdf2b06b

@ -27,4 +27,6 @@ public class RedisConstants {
public static final String CJT_TICKET_CACHE_KEY = "TICKET_KEY";
public static final String CJT_TOKEN_CACHE_KEY = "OPEN_TOKEN";
public static final String CJT_ASYNCHRONOUS = "CJT_ASYNCHRONOUS";
}

@ -142,6 +142,18 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
return day + "天" + hour + "小时" + min + "分钟";
}
public static String startOfDate(LocalDate now){
LocalDateTime today_start = LocalDateTime.of(now,LocalTime.MIN);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return df.format(today_start);
}
public static String endOfDate(LocalDate now){
LocalDateTime today_end = LocalDateTime.of(now,LocalTime.MAX);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return df.format(today_end);
}
public static String ldt2str(LocalDateTime time, String pattern) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
return time.format(fmt);

@ -1,6 +1,7 @@
package com.ruoyi.flyingbook.LarkHelper;
import com.alibaba.fastjson.JSONObject;
import com.lark.oapi.core.response.EmptyData;
import com.lark.oapi.service.bitable.v1.model.*;
import com.ruoyi.flyingbook.domain.lark.LarkException;
import com.ruoyi.flyingbook.domain.lark.LarkTableRequest;
@ -113,6 +114,27 @@ public class LarkTableHelper extends LarkHelper{
}
}
/**
*
*/
public void deleteDataTable(LarkTableRequest request) {
try {
buildClient(request).bitable().appTable().delete(
DeleteAppTableReq.newBuilder()
.appToken(request.getAppToken())
.tableId(request.getAppTable())
.build()
);
} catch (Exception e) {
throw new RuntimeException(new LarkException("LarkTableHelper.createDataTable",e.getMessage(),request).getErrorMessageBody());
}
// if (deleteAppTableResp != null && deleteAppTableResp.getCode() == 0){
// EmptyData data = deleteAppTableResp.getData();
// }else {
// throw new RuntimeException(new LarkException("LarkTableHelper.deleteDataTable", deleteAppTableResp.getMsg(),request).getErrorMessageBody());
// }
}
/**
*
@ -180,6 +202,30 @@ public class LarkTableHelper extends LarkHelper{
}
}
public BatchDeleteAppTableRecordRespBody deleteTableRecordBatch(LarkTableRequest request) {
BatchDeleteAppTableRecordResp delete = null;
try {
delete = buildClient(request).bitable().appTableRecord().batchDelete(
BatchDeleteAppTableRecordReq.newBuilder()
.appToken(request.getAppToken())
.tableId(request.getAppTable())
.batchDeleteAppTableRecordReqBody(
BatchDeleteAppTableRecordReqBody.newBuilder()
.records(request.getRecords())
.build()
)
.build()
);
} catch (Exception e) {
throw new RuntimeException(new LarkException("LarkTableHelper.deleteTableRecordBatch",e.getMessage(),request).getErrorMessageBody());
}
if (delete != null && delete.getCode() == 0){
return delete.getData();
}else {
throw new RuntimeException(new LarkException("LarkTableHelper.deleteTableRecordBatch",delete.getMsg(),request).getErrorMessageBody());
}
}
public UpdateAppTableRecordRespBody updateTableRecord(LarkTableRequest request) {
UpdateAppTableRecordResp update = null;
try {

@ -17,6 +17,7 @@ public class LarkTableRequest extends LarkRequest{
private String appToken;
private String appTable;
private String record;
private String[] records;
private String filter;
private String tableName;
private String defaultViewName;
@ -62,6 +63,5 @@ public class LarkTableRequest extends LarkRequest{
super(appId,appSecret);
this.appToken = appToken;
this.appTable = appTable;
this.record = record;
}
}

@ -65,6 +65,8 @@ public interface ErpLarkRelationMapper
public List<ErpLarkRelation> queryListByKeyList(@Param("keyList") List<String> keyList,@Param("method") String method);
public List<ErpLarkRelation> queryListByKeyListAndRemark(@Param("keyList") List<String> keyList,@Param("method") String method,@Param("remark") String remark);
public List<String> queryWaitDeleteRecordList(@Param("currentDay") String currentDay,@Param("method") String method,@Param("remark") String remark);
public void deleteRecordList(@Param("currentDay") String currentDay,@Param("method") String method,@Param("remark") String remark);
/**
*

@ -55,10 +55,26 @@
</foreach>
</select>
<select id="queryWaitDeleteRecordList" resultType="java.lang.String">
SELECT lark_key
FROM erp_lark_relation
WHERE DATE (create_time) = #{currentDay}
and remark = #{remark}
and `method` = #{method}
</select>
<delete id="deleteRecordList">
delete FROM erp_lark_relation
WHERE DATE (create_time) = #{currentDay}
and remark = #{remark}
and `method` = #{method}
</delete>
<select id="queryListByKey" resultMap="ErpLarkRelationResult">
select id, `key`, `method`, lark_key
from erp_lark_relation
where `method` = #{method} and `key` like CONCAT('%',#{key},'%')
where `method` = #{method}
and `key` like CONCAT('%', #{key}, '%')
</select>
<insert id="insertErpLarkRelation" parameterType="com.ruoyi.flyingbook.domain.ErpLarkRelation"
@ -87,7 +103,8 @@
<if test="larkKey != null">#{larkKey},</if>
</trim>
</insert>
<insert id="batchInsert" parameterType="com.ruoyi.flyingbook.domain.ErpLarkRelation" useGeneratedKeys="true" keyProperty="id">
<insert id="batchInsert" parameterType="com.ruoyi.flyingbook.domain.ErpLarkRelation" useGeneratedKeys="true"
keyProperty="id">
insert into erp_lark_relation(`key`,`method`,create_by,create_time,flag,lark_key,remark)
values
<foreach collection="erpLarkRelationList" item="relation" separator=",">
@ -125,7 +142,7 @@
where id = #{id}
</delete>
<delete id="deleteErpLarkRelationByIds" parameterType="String">
<delete id="deleteErpLarkRelationByIds" parameterType="Long">
delete from erp_lark_relation where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}

@ -25,6 +25,11 @@ public class CJTJobContext implements Serializable {
private String queryToTime;
private String cjt;
/**
*
*/
private Boolean asynchronous = Boolean.FALSE;
/**
* ticket
*/

@ -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;
}

Loading…
Cancel
Save