审批回调(待测试)

于相涌/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 code;
private final String info; 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; package com.ruoyi.flyingbook.LarkHelper;
import com.lark.oapi.service.approval.v4.model.GetInstanceReq; import com.lark.oapi.service.approval.v4.model.*;
import com.lark.oapi.service.approval.v4.model.GetInstanceRespBody;
import com.ruoyi.flyingbook.domain.lark.LarkApprovalRequest; import com.ruoyi.flyingbook.domain.lark.LarkApprovalRequest;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;

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

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

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

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

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

@ -1,5 +1,6 @@
package com.ruoyi.flyingbook.strategy.operate; package com.ruoyi.flyingbook.strategy.operate;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.lark.oapi.service.approval.v4.model.GetInstanceRespBody; import com.lark.oapi.service.approval.v4.model.GetInstanceRespBody;
import com.lark.oapi.service.bitable.v1.model.CreateAppTableRecordRespBody; import com.lark.oapi.service.bitable.v1.model.CreateAppTableRecordRespBody;
@ -54,8 +55,7 @@ public class ApprovalCallbackOperate extends LarkAbstract {
LarkTableRelation tableRelation = new LarkTableRelation(); LarkTableRelation tableRelation = new LarkTableRelation();
tableRelation.setLarkCompanyRelationId(request.getCompanyRelationId()); tableRelation.setLarkCompanyRelationId(request.getCompanyRelationId());
//todo yxy 表关联关系 tableId取值 tableRelation.setFromTableId(request.getApproveName());
tableRelation.setFromTableId(request.getEvent().getTableId());
tableRelation.setFlag(FlagStatus.OK.getCode()); tableRelation.setFlag(FlagStatus.OK.getCode());
List<LarkTableRelation> larkTableRelations = iLarkTableRelationService.selectLarkTableRelationList(tableRelation); List<LarkTableRelation> larkTableRelations = iLarkTableRelationService.selectLarkTableRelationList(tableRelation);
if (CollectionUtils.isEmpty(larkTableRelations)) { if (CollectionUtils.isEmpty(larkTableRelations)) {
@ -76,9 +76,10 @@ public class ApprovalCallbackOperate extends LarkAbstract {
List<LarkTableRowRelation> larkTableRowRelations = iLarkTableRowRelationService.queryListByTableRelationIdList(tableRelationIdList); List<LarkTableRowRelation> larkTableRowRelations = iLarkTableRowRelationService.queryListByTableRelationIdList(tableRelationIdList);
Map<Long, Map<String, String>> tableColRelationMap = new HashMap<>(); Map<Long, Map<String, String>> tableColRelationMap = new HashMap<>();
for (LarkTableRowRelation larkTableRowRelation : larkTableRowRelations) { for (LarkTableRowRelation larkTableRowRelation : larkTableRowRelations) {
Map<String, String> map = new HashMap<>();
map.put(larkTableRowRelation.getFromId(), larkTableRowRelation.getToId());
if (TableDetailRelationTypeEnum.COL.getCode().equals(larkTableRowRelation.getType())) { 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); tableColRelationMap.put(larkTableRowRelation.getTableRelationId(), map);
} }
} }
@ -96,65 +97,95 @@ public class ApprovalCallbackOperate extends LarkAbstract {
if (event == null) { if (event == null) {
throw new RuntimeException("当前事件为空"); 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) { 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); log.error("larkCompanyRelationService.getByCompanyName:{}", errorMessage);
throw new RuntimeException(errorMessage); throw new RuntimeException(errorMessage);
} }
//用于后续追加row时候做关联关系 //用于后续追加row时候做关联关系
request.setCompanyRelationId(relation.getId()); request.setCompanyRelationId(relation.getId());
//关联关系表填充
this.fillTableRelation(request);
request.setAppId(relation.getAppId()); request.setAppId(relation.getAppId());
request.setAppSecret(relation.getSecret()); 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 @Override
protected void businessProcessing(RequestVo request) { 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()) { 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());
send( request); send(request);
} }
} }
@Override @Override
protected Map<String, Object> getBody(RequestVo request) { protected Map<String, Object> getBody(RequestVo request) {
Map<String , Object> result = new HashMap<>(); Map<String, JSONObject> bodyMap = new HashMap<>();
JSONObject record = request.getRecord(); JSONArray paramList = JSONArray.parseArray(request.getInstanceRespBody().getForm());
String responseCode = record.getString("code"); for (int i = 0; i < paramList.size(); i++) {
if ("0".equals(responseCode)) { JSONObject jsonObject = paramList.getJSONObject(i);
result = parseMap(request); bodyMap.put(jsonObject.getString("name"), jsonObject);
} }
return result;
}
private Map<String, Object> parseMap(RequestVo request) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
JSONObject record = request.getRecord();
Map<String, String> colRelation = request.getTableColRelationMap().getOrDefault(request.getTableRelationId(), new HashMap<>()); Map<String, String> colRelation = request.getTableColRelationMap().getOrDefault(request.getTableRelationId(), new HashMap<>());
for (Map.Entry<String, String> entry : colRelation.entrySet()) { 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; return result;
} }
private Object recursiveValue(JSONObject record,String key){
/**
*
* #
* ##
*
* @param record
* @param key
* @return
*/
private Object recursiveValue(JSONObject record, String key) {
String[] split = key.split(","); String[] split = key.split(",");
for (int i = 0; i < split.length - 1; i++) { int index = 0;
String k = split[i]; for (index = 0; index < split.length; index++) {
if (key.startsWith("#")){ String k = split[index];
k = k.replace("#",""); if (k.startsWith("##")) {
record = record.getJSONArray(k).getJSONObject(0); 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.getJSONObject(k);
} else {
break;
} }
} }
return record.get(split[split.length-1]); return record.get(split[index]);
} }
protected void send(RequestVo request) { 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.domain.RequestVo;
import com.ruoyi.flyingbook.strategy.LarkOperateAbstract; import com.ruoyi.flyingbook.strategy.LarkOperateAbstract;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -18,6 +20,4 @@ public abstract class LarkAbstract extends LarkOperateAbstract {
*/ */
protected abstract Map<String,Object> getBody(RequestVo request); 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>> tableRowRelationMap = new HashMap<>();
Map<Long, Map<String, String>> tableColRelationMap = new HashMap<>(); Map<Long, Map<String, String>> tableColRelationMap = new HashMap<>();
for (LarkTableRowRelation larkTableRowRelation : larkTableRowRelations) { for (LarkTableRowRelation larkTableRowRelation : larkTableRowRelations) {
Map<String, String> map = new HashMap<>();
map.put(larkTableRowRelation.getFromId(), larkTableRowRelation.getToId());
if (TableDetailRelationTypeEnum.ROW.getCode().equals(larkTableRowRelation.getType())) { 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); tableRowRelationMap.put(larkTableRowRelation.getTableRelationId(), map);
} else if (TableDetailRelationTypeEnum.COL.getCode().equals(larkTableRowRelation.getType())) { } 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); tableColRelationMap.put(larkTableRowRelation.getTableRelationId(), map);
} }
} }
@ -106,9 +108,9 @@ public class MultidimensionalTableOperate extends LarkAbstract {
if (event == null) { if (event == null) {
throw new RuntimeException("当前事件为空"); 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) { 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); log.error("larkCompanyRelationService.getByCompanyName:{}", errorMessage);
throw new RuntimeException(errorMessage); throw new RuntimeException(errorMessage);
} }

Loading…
Cancel
Save