审批回调(待测试)

于相涌/mail
YXY 2 years ago
parent f3ec707b9a
commit efdf5c222b

@ -9,7 +9,8 @@ public enum AppType {
/**
*
*/
APPROVAL("APPROVAL", "多维表格");
MULTIDIMENSIONAL_TABLE("APPROVAL", "多维表格"),
APPROVAL_TASK("APPROVAL_TASK", "审批回调");
private final String code;
private final String info;

@ -0,0 +1,39 @@
package com.ruoyi.common.enums;
/**
*
*
* @author ruoyi
*/
public enum DataStructureEnum {
/**
*
*/
TRIP_GROUP("tripGroup", "出差审批"),
CONTACK("contact", "合同审批");
private final String code;
private final String info;
DataStructureEnum(String code, String info) {
this.code = code;
this.info = info;
}
public String getCode() {
return code;
}
public String getInfo() {
return info;
}
public static DataStructureEnum getByCode(String code){
for (DataStructureEnum value : DataStructureEnum.values()) {
if (value.getCode().equals(code)){
return value;
}
}
return null;
}
}

@ -0,0 +1,8 @@
package com.ruoyi.flyingbook.LarkHelper;
/**
* @author yuxiangyong
* @create 2023-03-18 11:09
*/
public class DataStructureHelper {
}

@ -1,7 +1,6 @@
package com.ruoyi.flyingbook.LarkHelper;
import com.lark.oapi.service.approval.v4.model.GetInstanceReq;
import com.lark.oapi.service.approval.v4.model.GetInstanceRespBody;
import com.lark.oapi.service.approval.v4.model.*;
import com.ruoyi.flyingbook.domain.lark.LarkApprovalRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@ -59,28 +59,28 @@ public class LarkApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
//这里用线程池,后续如果出现量大的情况下可以调整
// threadPool.execute(() -> {
//
// while (true) {
// List<Event> eventList = eventMapper.queryEventList(EventOperateStatus.PENDING.getCode());
// if (CollectionUtils.isEmpty(eventList)) {
// try {
// // 没有订单,休息一下
// Thread.sleep(WAIT_TIME);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// } else {
// try {
// for (Event event : eventList) {
// larkOperateFactory.execute(event);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// }
// });
threadPool.execute(() -> {
while (true) {
List<Event> eventList = eventMapper.queryEventList(EventOperateStatus.PENDING.getCode());
if (CollectionUtils.isEmpty(eventList)) {
try {
// 没有订单,休息一下
Thread.sleep(WAIT_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
try {
for (Event event : eventList) {
larkOperateFactory.execute(event);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}

@ -33,9 +33,9 @@ public class EventController extends BaseController {
result = DecryptUtil.decrypt(jsonObject.getString("encrypt"));
log.info("/event/test1/approval encrypt:{}", JSONObject.toJSONString(result));
}
// RequestVo larkRequest = new RequestVo();
// larkRequest.setMessage(result);
// larkFactory.execute(larkRequest);
RequestVo larkRequest = new RequestVo();
larkRequest.setMessage(result);
larkFactory.execute(larkRequest);
return result;
}
}

@ -35,6 +35,8 @@ public class RequestVo {
private String instance;
private GetInstanceRespBody instanceRespBody;
private String message;
private String instanceId;
private String approveName;
/**
* MultidimensionalTableOperate

@ -1,10 +1,7 @@
package com.ruoyi.flyingbook.strategy.callback;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.enums.AppType;
import com.ruoyi.common.enums.EventOperateStatus;
import com.ruoyi.common.enums.EventOperateType;
import com.ruoyi.common.enums.FlagStatus;
import com.ruoyi.common.enums.*;
import com.ruoyi.flyingbook.domain.Event;
import com.ruoyi.flyingbook.domain.EventLog;
import com.ruoyi.flyingbook.domain.RequestVo;
@ -32,8 +29,6 @@ public class ApprovalCallback extends CallbackAbstract {
private IEventService eventService;
@Autowired
private IEventLogService eventLogService;
@Autowired
private RedisTemplate redisTemplate;
@Override
protected Boolean check(RequestVo request) {
@ -69,9 +64,6 @@ public class ApprovalCallback extends CallbackAbstract {
protected void endHandle(RequestVo request) {
if (request.getEventId() == null){
return;
}
if (StringUtils.isNotBlank(request.getErrorMessage())){
}
EventLog eventLog = StringUtils.isBlank(request.getErrorMessage()) ?
new EventLog(request.getEventId(),EventOperateType.CALL_BACK.getCode(),request.getMessage())
@ -113,7 +105,7 @@ public class ApprovalCallback extends CallbackAbstract {
event.setUpdateTime(new Date());
event.setUpdateBy("System");
event.setAppId(request.getAppId());
event.setType(AppType.APPROVAL.getCode());
event.setType(EventTypeEnum.APPROVAL_TASK.getCode());
return event;
}

@ -147,7 +147,7 @@ public class MultidimensionalTableCallback extends CallbackAbstract {
event.setUpdateTime(new Date());
event.setUpdateBy("System");
event.setAppId(request.getAppId());
event.setType(EventTypeEnum.APPROVAL_TASK.getCode());
event.setType(EventTypeEnum.ROW_CHANGE.getCode());
return event;
}

@ -1,5 +1,6 @@
package com.ruoyi.flyingbook.strategy.operate;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lark.oapi.service.approval.v4.model.GetInstanceRespBody;
import com.lark.oapi.service.bitable.v1.model.CreateAppTableRecordRespBody;
@ -54,8 +55,7 @@ public class ApprovalCallbackOperate extends LarkAbstract {
LarkTableRelation tableRelation = new LarkTableRelation();
tableRelation.setLarkCompanyRelationId(request.getCompanyRelationId());
//todo yxy 表关联关系 tableId取值
tableRelation.setFromTableId(request.getEvent().getTableId());
tableRelation.setFromTableId(request.getApproveName());
tableRelation.setFlag(FlagStatus.OK.getCode());
List<LarkTableRelation> larkTableRelations = iLarkTableRelationService.selectLarkTableRelationList(tableRelation);
if (CollectionUtils.isEmpty(larkTableRelations)) {
@ -76,9 +76,10 @@ public class ApprovalCallbackOperate extends LarkAbstract {
List<LarkTableRowRelation> larkTableRowRelations = iLarkTableRowRelationService.queryListByTableRelationIdList(tableRelationIdList);
Map<Long, Map<String, String>> tableColRelationMap = new HashMap<>();
for (LarkTableRowRelation larkTableRowRelation : larkTableRowRelations) {
Map<String, String> map = new HashMap<>();
map.put(larkTableRowRelation.getFromId(), larkTableRowRelation.getToId());
if (TableDetailRelationTypeEnum.COL.getCode().equals(larkTableRowRelation.getType())) {
Map<String, String> map = tableColRelationMap.getOrDefault(larkTableRowRelation.getTableRelationId(), new HashMap<>());
map.put(larkTableRowRelation.getFromId(), larkTableRowRelation.getToId());
tableColRelationMap.put(larkTableRowRelation.getTableRelationId(), map);
}
}
@ -96,25 +97,29 @@ public class ApprovalCallbackOperate extends LarkAbstract {
if (event == null) {
throw new RuntimeException("当前事件为空");
}
LarkCompanyRelation relation = larkCompanyRelationService.getByAppIdAndType(request.getEvent().getAppId(), AppType.APPROVAL.getCode());
LarkCompanyRelation relation = larkCompanyRelationService.getByAppIdAndType(request.getEvent().getAppId(), AppType.APPROVAL_TASK.getCode());
if (relation == null) {
String errorMessage = String.format("获取app值为空 appId:{} appType:{}", request.getEvent().getAppId(), AppType.APPROVAL.getCode());
String errorMessage = String.format("获取app值为空 appId:{} appType:{}", request.getEvent().getAppId(), AppType.APPROVAL_TASK.getCode());
log.error("larkCompanyRelationService.getByCompanyName:{}", errorMessage);
throw new RuntimeException(errorMessage);
}
//用于后续追加row时候做关联关系
request.setCompanyRelationId(relation.getId());
//关联关系表填充
this.fillTableRelation(request);
request.setAppId(relation.getAppId());
request.setAppSecret(relation.getSecret());
LarkApprovalRequest detailRequest = new LarkApprovalRequest(request.getAppId(), request.getAppSecret(), event.getRecordId());
GetInstanceRespBody approvalInfoDetail = larkApprovalHelper.getApprovalInfoDetail(detailRequest);
request.setInstanceRespBody(approvalInfoDetail);
String approvalName = approvalInfoDetail.getApprovalName();
request.setApproveName(approvalName);
//关联关系表填充
this.fillTableRelation(request);
}
@Override
protected void businessProcessing(RequestVo request) {
LarkApprovalRequest detailRequest = new LarkApprovalRequest(request.getAppId(), request.getAppSecret(), request.getInstance());
GetInstanceRespBody approvalInfoDetail = larkApprovalHelper.getApprovalInfoDetail(detailRequest);
request.setInstanceRespBody(approvalInfoDetail);
for (Map.Entry<String, Long> entry : request.getTableRelationMap().entrySet()) {
String[] arr = entry.getKey().split("_");
request.setToAppToken(arr[0]);
@ -126,35 +131,61 @@ public class ApprovalCallbackOperate extends LarkAbstract {
@Override
protected Map<String, Object> getBody(RequestVo request) {
Map<String , Object> result = new HashMap<>();
JSONObject record = request.getRecord();
String responseCode = record.getString("code");
if ("0".equals(responseCode)) {
result = parseMap(request);
}
return result;
Map<String, JSONObject> bodyMap = new HashMap<>();
JSONArray paramList = JSONArray.parseArray(request.getInstanceRespBody().getForm());
for (int i = 0; i < paramList.size(); i++) {
JSONObject jsonObject = paramList.getJSONObject(i);
bodyMap.put(jsonObject.getString("name"), jsonObject);
}
private Map<String, Object> parseMap(RequestVo request) {
Map<String, Object> result = new HashMap<>();
JSONObject record = request.getRecord();
Map<String, String> colRelation = request.getTableColRelationMap().getOrDefault(request.getTableRelationId(), new HashMap<>());
for (Map.Entry<String, String> entry : colRelation.entrySet()) {
result.put(entry.getValue(), recursiveValue(record,entry.getKey()));
result.put(entry.getValue(), recursiveValue(bodyMap.get(entry.getValue()), entry.getKey()));
}
return result;
}
/**
*
* #
* ##
*
* @param record
* @param key
* @return
*/
private Object recursiveValue(JSONObject record, String key) {
String[] split = key.split(",");
for (int i = 0; i < split.length - 1; i++) {
String k = split[i];
if (key.startsWith("#")){
int index = 0;
for (index = 0; index < split.length; index++) {
String k = split[index];
if (k.startsWith("##")) {
k = k.replace("##", "");
List<String> list = new ArrayList<>();
JSONArray jsonArray = record.getJSONArray(k);
for (int i = 0; i < jsonArray.size(); i++) {
Object object = jsonArray.get(i);
if (object instanceof String) {
list.add(String.valueOf(object));
} else {
List<String> keyList = new ArrayList<>();
for (int j = index+1; j < split.length; j++) {
keyList.add(split[j]);
}
Object o = recursiveValue(jsonArray.getJSONObject(i), String.join(",",keyList));
list.add(String.valueOf(o));
}
}
return String.join(",", list);
} else if (k.startsWith("#")) {
k = k.replace("#", "");
record = record.getJSONArray(k).getJSONObject(0);
record = record.getJSONObject(k);
} else {
break;
}
}
return record.get(split[split.length-1]);
return record.get(split[index]);
}
protected void send(RequestVo request) {

@ -3,6 +3,8 @@ package com.ruoyi.flyingbook.strategy.operate;
import com.ruoyi.flyingbook.domain.RequestVo;
import com.ruoyi.flyingbook.strategy.LarkOperateAbstract;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
@ -18,6 +20,4 @@ public abstract class LarkAbstract extends LarkOperateAbstract {
*/
protected abstract Map<String,Object> getBody(RequestVo request);
}

@ -83,11 +83,13 @@ public class MultidimensionalTableOperate extends LarkAbstract {
Map<Long, Map<String, String>> tableRowRelationMap = new HashMap<>();
Map<Long, Map<String, String>> tableColRelationMap = new HashMap<>();
for (LarkTableRowRelation larkTableRowRelation : larkTableRowRelations) {
Map<String, String> map = new HashMap<>();
map.put(larkTableRowRelation.getFromId(), larkTableRowRelation.getToId());
if (TableDetailRelationTypeEnum.ROW.getCode().equals(larkTableRowRelation.getType())) {
Map<String, String> map = tableRowRelationMap.getOrDefault(larkTableRowRelation.getTableRelationId(),new HashMap<>());
map.put(larkTableRowRelation.getFromId(), larkTableRowRelation.getToId());
tableRowRelationMap.put(larkTableRowRelation.getTableRelationId(), map);
} else if (TableDetailRelationTypeEnum.COL.getCode().equals(larkTableRowRelation.getType())) {
Map<String, String> map = tableColRelationMap.getOrDefault(larkTableRowRelation.getTableRelationId(),new HashMap<>());
map.put(larkTableRowRelation.getFromId(), larkTableRowRelation.getToId());
tableColRelationMap.put(larkTableRowRelation.getTableRelationId(), map);
}
}
@ -106,9 +108,9 @@ public class MultidimensionalTableOperate extends LarkAbstract {
if (event == null) {
throw new RuntimeException("当前事件为空");
}
LarkCompanyRelation relation = larkCompanyRelationService.getByAppIdAndType(request.getEvent().getAppId(), AppType.APPROVAL.getCode());
LarkCompanyRelation relation = larkCompanyRelationService.getByAppIdAndType(request.getEvent().getAppId(), AppType.MULTIDIMENSIONAL_TABLE.getCode());
if (relation == null) {
String errorMessage = String.format("获取app值为空 appId:{} appType:{}", request.getEvent().getAppId(), AppType.APPROVAL.getCode());
String errorMessage = String.format("获取app值为空 appId:{} appType:{}", request.getEvent().getAppId(), AppType.MULTIDIMENSIONAL_TABLE.getCode());
log.error("larkCompanyRelationService.getByCompanyName:{}", errorMessage);
throw new RuntimeException(errorMessage);
}

Loading…
Cancel
Save