|
|
@ -1,10 +1,12 @@
|
|
|
|
package com.ruoyi.flyingbook.strategy.operate;
|
|
|
|
package com.ruoyi.flyingbook.strategy.operate;
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.lark.oapi.service.bitable.v1.model.CreateAppTableRecordRespBody;
|
|
|
|
|
|
|
|
import com.lark.oapi.service.bitable.v1.model.GetAppTableRecordResp;
|
|
|
|
import com.ruoyi.common.constant.RedisConstants;
|
|
|
|
import com.ruoyi.common.constant.RedisConstants;
|
|
|
|
import com.ruoyi.common.enums.*;
|
|
|
|
import com.ruoyi.common.enums.*;
|
|
|
|
import com.ruoyi.flyingbook.LarkHelper.LarkTokenHelper;
|
|
|
|
import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper;
|
|
|
|
import com.ruoyi.flyingbook.domain.*;
|
|
|
|
import com.ruoyi.flyingbook.domain.*;
|
|
|
|
|
|
|
|
import com.ruoyi.flyingbook.domain.lark.LarkTableRequest;
|
|
|
|
import com.ruoyi.flyingbook.service.*;
|
|
|
|
import com.ruoyi.flyingbook.service.*;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
@ -13,10 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -30,7 +29,7 @@ import java.util.Map;
|
|
|
|
public class MultidimensionalTableOperate extends LarkAbstract {
|
|
|
|
public class MultidimensionalTableOperate extends LarkAbstract {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
protected LarkTokenHelper larkTokenHelper;
|
|
|
|
protected LarkTableHelper larkTokenHelper;
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
protected ILarkCompanyRelationService larkCompanyRelationService;
|
|
|
|
protected ILarkCompanyRelationService larkCompanyRelationService;
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
@ -46,22 +45,20 @@ public class MultidimensionalTableOperate extends LarkAbstract {
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private IEventLogService eventLogService;
|
|
|
|
private IEventLogService eventLogService;
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取明细行
|
|
|
|
* 新增明细行
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private static final String DETAIL = "DETAIL";
|
|
|
|
|
|
|
|
private String getDetail = "https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records/:record_id";
|
|
|
|
|
|
|
|
//新增明细行
|
|
|
|
|
|
|
|
private static final String CREATE_OPERATE = "CREATE_OPERATE";
|
|
|
|
private static final String CREATE_OPERATE = "CREATE_OPERATE";
|
|
|
|
private String addLine = "https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records";
|
|
|
|
/**
|
|
|
|
//修改明细行
|
|
|
|
* 修改明细行
|
|
|
|
|
|
|
|
*/
|
|
|
|
private static final String UPDATE_OPERATE = "UPDATE_OPERATE";
|
|
|
|
private static final String UPDATE_OPERATE = "UPDATE_OPERATE";
|
|
|
|
private String updateLine = "https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records/:record_id";
|
|
|
|
/**
|
|
|
|
//删除明细行
|
|
|
|
* 删除明细行
|
|
|
|
|
|
|
|
*/
|
|
|
|
private static final String DELETE_OPERATE = "DELETE_OPERATE";
|
|
|
|
private static final String DELETE_OPERATE = "DELETE_OPERATE";
|
|
|
|
private String deleteLine = "https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records/:record_id";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void fillTableRelation(LarkRequest request) {
|
|
|
|
private void fillTableRelation(RequestVo request) {
|
|
|
|
LarkTableRelation tableRelation = new LarkTableRelation();
|
|
|
|
LarkTableRelation tableRelation = new LarkTableRelation();
|
|
|
|
tableRelation.setLarkCompanyRelationId(request.getCompanyRelationId());
|
|
|
|
tableRelation.setLarkCompanyRelationId(request.getCompanyRelationId());
|
|
|
|
tableRelation.setFromTableId(request.getEvent().getTableId());
|
|
|
|
tableRelation.setFromTableId(request.getEvent().getTableId());
|
|
|
@ -104,7 +101,7 @@ public class MultidimensionalTableOperate extends LarkAbstract {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void preOperate(LarkRequest request) {
|
|
|
|
protected void preOperate(RequestVo request) {
|
|
|
|
Event event = request.getEvent();
|
|
|
|
Event event = request.getEvent();
|
|
|
|
if (event == null) {
|
|
|
|
if (event == null) {
|
|
|
|
throw new RuntimeException("当前事件为空");
|
|
|
|
throw new RuntimeException("当前事件为空");
|
|
|
@ -115,62 +112,36 @@ public class MultidimensionalTableOperate extends LarkAbstract {
|
|
|
|
log.error("larkCompanyRelationService.getByCompanyName:{}", errorMessage);
|
|
|
|
log.error("larkCompanyRelationService.getByCompanyName:{}", errorMessage);
|
|
|
|
throw new RuntimeException(errorMessage);
|
|
|
|
throw new RuntimeException(errorMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.setAppId(relation.getAppId());
|
|
|
|
|
|
|
|
request.setAppSecret(relation.getSecret());
|
|
|
|
//用于后续追加row时候做关联关系
|
|
|
|
//用于后续追加row时候做关联关系
|
|
|
|
request.setCompanyRelationId(relation.getId());
|
|
|
|
request.setCompanyRelationId(relation.getId());
|
|
|
|
//关联关系表填充
|
|
|
|
//关联关系表填充
|
|
|
|
this.fillTableRelation(request);
|
|
|
|
this.fillTableRelation(request);
|
|
|
|
String token = larkTokenHelper.getToken(relation.getAppId(), relation.getSecret(), "tenant_access_token");
|
|
|
|
|
|
|
|
request.setToken(token);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void businessProcessing(LarkRequest request) {
|
|
|
|
protected void businessProcessing(RequestVo request) {
|
|
|
|
request.setOperateType(DETAIL);
|
|
|
|
Event event = request.getEvent();
|
|
|
|
List<String> safeCodeList = new ArrayList<>();
|
|
|
|
LarkTableRequest detailRequest = new LarkTableRequest(request.getAppId(), request.getAppSecret(), event.getAppToken(), event.getTableId(), event.getRecordId());
|
|
|
|
safeCodeList.add("1254043");
|
|
|
|
detailRequest.setSafeCode(Arrays.asList(1254043));
|
|
|
|
JSONObject message = larkTokenHelper.getLark(getUrl(request), null, request.getToken(),safeCodeList);
|
|
|
|
GetAppTableRecordResp recordDetail = larkTokenHelper.getTableRecordDetail(detailRequest);
|
|
|
|
request.setRecord(message);
|
|
|
|
request.setRespBody(recordDetail);
|
|
|
|
for (Map.Entry<String, Long> entry : request.getTableRelationMap().entrySet()) {
|
|
|
|
for (Map.Entry<String, Long> entry : request.getTableRelationMap().entrySet()) {
|
|
|
|
String[] arr = entry.getKey().split("_");
|
|
|
|
String[] arr = entry.getKey().split("_");
|
|
|
|
request.setToAppToken(arr[0]);
|
|
|
|
request.setToAppToken(arr[0]);
|
|
|
|
request.setToTableId(arr[1]);
|
|
|
|
request.setToTableId(arr[1]);
|
|
|
|
request.setTableRelationId(entry.getValue());
|
|
|
|
request.setTableRelationId(entry.getValue());
|
|
|
|
Map<Object, Map<Object, Object>> body = getBody(request);
|
|
|
|
send(request);
|
|
|
|
String url = getUrl(request);
|
|
|
|
|
|
|
|
if (StringUtils.isBlank(url)) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
send(request.getOperateType(), url, body, request);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
protected String getUrl(LarkRequest request) {
|
|
|
|
|
|
|
|
Event event = request.getEvent();
|
|
|
|
|
|
|
|
Map<String, String> rowRelation = request.getTableRowRelationMap().get(request.getTableRelationId());
|
|
|
|
|
|
|
|
String cacheKey = String.format("%s_%s_%s_%s", RedisConstants.MULTIPLE_TABLE_RECORD, event.getAppToken(), event.getTableId(), event.getRecordId());
|
|
|
|
|
|
|
|
redisTemplate.delete(cacheKey);
|
|
|
|
|
|
|
|
if (UPDATE_OPERATE.equals(request.getOperateType())) {
|
|
|
|
|
|
|
|
if (rowRelation.containsKey(event.getRecordId())) {
|
|
|
|
|
|
|
|
return updateLine.replace(":app_token", request.getToAppToken()).replace(":table_id", request.getToTableId()).replace(":record_id", rowRelation.get(event.getRecordId()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (CREATE_OPERATE.equals(request.getOperateType())) {
|
|
|
|
|
|
|
|
return addLine.replace(":app_token", request.getToAppToken()).replace(":table_id", request.getToTableId());
|
|
|
|
|
|
|
|
} else if (DELETE_OPERATE.equals(request.getOperateType())) {
|
|
|
|
|
|
|
|
if (rowRelation.containsKey(event.getRecordId())) {
|
|
|
|
|
|
|
|
return deleteLine.replace(":app_token", request.getToAppToken()).replace(":table_id", request.getToTableId()).replace(":record_id", rowRelation.get(event.getRecordId()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (DETAIL.equals(request.getOperateType())) {
|
|
|
|
|
|
|
|
return getDetail.replace(":app_token", event.getAppToken()).replace(":table_id", event.getTableId()).replace(":record_id", event.getRecordId());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected Map<Object, Map<Object, Object>> getBody(LarkRequest request) {
|
|
|
|
protected Map<String, Object> getBody(RequestVo request) {
|
|
|
|
Map<Object, Map<Object, Object>> result = new HashMap<>();
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
JSONObject record = request.getRecord();
|
|
|
|
GetAppTableRecordResp respBody = request.getRespBody();
|
|
|
|
String responseCode = record.getString("code");
|
|
|
|
Integer responseCode = respBody.getCode();
|
|
|
|
Map<String, String> rowMap = request.getTableRowRelationMap().getOrDefault(request.getTableRelationId(), new HashMap<>());
|
|
|
|
Map<String, String> rowMap = request.getTableRowRelationMap().getOrDefault(request.getTableRelationId(), new HashMap<>());
|
|
|
|
if ("1254043".equals(responseCode)) {
|
|
|
|
if ("1254043".equals(responseCode)) {
|
|
|
|
request.setOperateType(DELETE_OPERATE);
|
|
|
|
request.setOperateType(DELETE_OPERATE);
|
|
|
@ -185,50 +156,53 @@ public class MultidimensionalTableOperate extends LarkAbstract {
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Map<Object, Map<Object, Object>> parseMap(LarkRequest request) {
|
|
|
|
/**
|
|
|
|
Map<Object, Object> result = new HashMap<>();
|
|
|
|
* 对两个表的列进行映射
|
|
|
|
JSONObject record = request.getRecord();
|
|
|
|
*/
|
|
|
|
|
|
|
|
private Map<String, Object> parseMap(RequestVo request) {
|
|
|
|
|
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
|
Map<String, String> colRelation = request.getTableColRelationMap().getOrDefault(request.getTableRelationId(), new HashMap<>());
|
|
|
|
Map<String, String> colRelation = request.getTableColRelationMap().getOrDefault(request.getTableRelationId(), new HashMap<>());
|
|
|
|
String fields = record.getJSONObject("data").getJSONObject("record").getString("fields");
|
|
|
|
Map<String, Object> midMap = request.getRespBody().getData().getRecord().getFields();
|
|
|
|
Map<Object, Object> midMap = JSONObject.parseObject(fields, Map.class);
|
|
|
|
for (Map.Entry<String, Object> entry : midMap.entrySet()) {
|
|
|
|
for (Map.Entry<Object, Object> entry : midMap.entrySet()) {
|
|
|
|
|
|
|
|
String key = String.valueOf(entry.getKey());
|
|
|
|
String key = String.valueOf(entry.getKey());
|
|
|
|
if (!colRelation.containsKey(key)) {
|
|
|
|
if (!colRelation.containsKey(key)) {
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.put(colRelation.get(key), entry.getValue());
|
|
|
|
result.put(colRelation.get(key), entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Map<Object, Map<Object, Object>> map = new HashMap<>();
|
|
|
|
return result;
|
|
|
|
map.put("fields", result);
|
|
|
|
|
|
|
|
return map;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected void send(String type, String url, Map<Object, Map<Object, Object>> body, LarkRequest request) {
|
|
|
|
private void send(RequestVo request) {
|
|
|
|
|
|
|
|
String type = request.getOperateType();
|
|
|
|
|
|
|
|
Map<String, Object> body = getBody(request);
|
|
|
|
|
|
|
|
Event event = request.getEvent();
|
|
|
|
|
|
|
|
String cacheKey = String.format("%s_%s_%s_%s", RedisConstants.MULTIPLE_TABLE_RECORD, event.getAppToken(), event.getTableId(), event.getRecordId());
|
|
|
|
|
|
|
|
redisTemplate.delete(cacheKey);
|
|
|
|
request.setOperateInfo(String.valueOf(body));
|
|
|
|
request.setOperateInfo(String.valueOf(body));
|
|
|
|
String token = request.getToken();
|
|
|
|
|
|
|
|
if (CREATE_OPERATE.equals(type)) {
|
|
|
|
if (CREATE_OPERATE.equals(type)) {
|
|
|
|
JSONObject result = larkTokenHelper.postLark(url, body, token);
|
|
|
|
LarkTableRequest add = new LarkTableRequest(request.getAppId(), request.getAppSecret(), request.getToAppToken(), request.getToTableId(), body);
|
|
|
|
JSONObject data = result.getJSONObject("data");
|
|
|
|
CreateAppTableRecordRespBody addResp = larkTokenHelper.addTableRecord(add);
|
|
|
|
if (data != null) {
|
|
|
|
if (addResp != null) {
|
|
|
|
JSONObject record = data.getJSONObject("record");
|
|
|
|
String recordId = addResp.getRecord().getRecordId();
|
|
|
|
if (record != null && StringUtils.isNotBlank(record.getString("record_id"))) {
|
|
|
|
LarkTableRowRelation rowRelation = new LarkTableRowRelation(request.getCompanyRelationId(), event.getRecordId(), recordId, TableDetailRelationTypeEnum.ROW.getCode(), TableDetailRelationTypeEnum.ROW.getCode());
|
|
|
|
String recordId = record.getString("record_id");
|
|
|
|
iLarkTableRowRelationService.insertLarkTableRowRelation(rowRelation);
|
|
|
|
LarkTableRowRelation rowRelation = new LarkTableRowRelation(request.getCompanyRelationId(), request.getEvent().getRecordId(), recordId, TableDetailRelationTypeEnum.ROW.getCode(), TableDetailRelationTypeEnum.ROW.getCode());
|
|
|
|
|
|
|
|
iLarkTableRowRelationService.insertLarkTableRowRelation(rowRelation);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
request.setEventOperateType(EventOperateType.CREATE);
|
|
|
|
request.setEventOperateType(EventOperateType.CREATE);
|
|
|
|
} else if (UPDATE_OPERATE.equals(type)) {
|
|
|
|
} else if (UPDATE_OPERATE.equals(type)) {
|
|
|
|
request.setEventOperateType(EventOperateType.UPDATE);
|
|
|
|
request.setEventOperateType(EventOperateType.UPDATE);
|
|
|
|
larkTokenHelper.putLark(url, body, token);
|
|
|
|
LarkTableRequest update = new LarkTableRequest(request.getAppId(), request.getAppSecret(), request.getToAppToken(), request.getToTableId(),request.getToRecordId(), body);
|
|
|
|
|
|
|
|
larkTokenHelper.updateTableRecord(update);
|
|
|
|
} else if (DELETE_OPERATE.equals(type)) {
|
|
|
|
} else if (DELETE_OPERATE.equals(type)) {
|
|
|
|
request.setEventOperateType(EventOperateType.DELETE);
|
|
|
|
request.setEventOperateType(EventOperateType.DELETE);
|
|
|
|
larkTokenHelper.deleteLark(url, body, token);
|
|
|
|
LarkTableRequest delete = new LarkTableRequest(request.getAppId(), request.getAppSecret(), request.getToAppToken(), request.getToTableId(),request.getToRecordId());
|
|
|
|
|
|
|
|
larkTokenHelper.deleteTableRecord(delete);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void endHandle(LarkRequest request) {
|
|
|
|
protected void endHandle(RequestVo request) {
|
|
|
|
Event event = request.getEvent();
|
|
|
|
Event event = request.getEvent();
|
|
|
|
EventLog eventLog = null;
|
|
|
|
EventLog eventLog = null;
|
|
|
|
if (StringUtils.isBlank(request.getErrorMessage())) {
|
|
|
|
if (StringUtils.isBlank(request.getErrorMessage())) {
|
|
|
@ -247,7 +221,7 @@ public class MultidimensionalTableOperate extends LarkAbstract {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected String buildResult(LarkRequest request) {
|
|
|
|
protected String buildResult(RequestVo request) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|