From 59431a713e7d5e8d45508a61976961ee4351e388 Mon Sep 17 00:00:00 2001 From: YXY <932687738@qq.com> Date: Sun, 28 May 2023 11:23:54 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A3=9E=E4=B9=A6=E4=BB=BB=E5=8A=A1=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=201=E3=80=81=E5=88=9B=E5=BB=BA=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=97=A5=E7=A8=8B=202=E3=80=81=E5=88=9B=E5=BB=BA=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=97=A5=E7=A8=8B=E7=9A=84=E5=8F=82=E4=B8=8E=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../enums/TableDetailRelationTypeEnum.java | 3 +- .../com/ruoyi/common/utils/DateUtils.java | 20 ++ .../LarkHelper/LarkApprovalHelper.java | 3 +- .../LarkHelper/LarkCalendarHelper.java | 230 ++++++++++++++++++ .../flyingbook/LarkHelper/LarkTaskHelper.java | 13 +- .../domain/lark/LarkCalendarRequest.java | 165 +++++++++++++ .../operate/LarkTaskCallbackOperate.java | 155 +++++++++++- 7 files changed, 576 insertions(+), 13 deletions(-) create mode 100644 ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkCalendarHelper.java create mode 100644 ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkCalendarRequest.java diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/enums/TableDetailRelationTypeEnum.java b/ruoyi-common/src/main/java/com/ruoyi/common/enums/TableDetailRelationTypeEnum.java index 04ef31a..96997f0 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/enums/TableDetailRelationTypeEnum.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/enums/TableDetailRelationTypeEnum.java @@ -9,7 +9,8 @@ public enum TableDetailRelationTypeEnum { * */ ROW("ROW", "行"), - COL("COL", "列"); + COL("COL", "列"), + TASK_CALENDAR("TASK_CALENDAR", "任务_日历关系"); private final String code; private final String info; diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java index 0ec19a5..46d9b6f 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/DateUtils.java @@ -3,7 +3,9 @@ package com.ruoyi.common.utils; import java.lang.management.ManagementFactory; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.Instant; import java.time.LocalDateTime; +import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; @@ -145,4 +147,22 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils { DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern); return LocalDateTime.parse(time,fmt); } + + public static LocalDateTime long2ldt(long time) { + Instant instant = Instant.ofEpochMilli(time); + ZoneId zone = ZoneId.systemDefault(); + return LocalDateTime.ofInstant(instant, zone); + } + + public static String long2Str(long time, String pattern) { + Date date = new Date(time); + SimpleDateFormat sdf = new SimpleDateFormat(pattern); + return sdf.format(date); + } + + public static Long ldt2Long(LocalDateTime time) { + ZoneId zone = ZoneId.systemDefault(); + Instant instant = time.atZone(zone).toInstant(); + return instant.toEpochMilli(); + } } diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkApprovalHelper.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkApprovalHelper.java index bf2aad4..4dcbcf8 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkApprovalHelper.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkApprovalHelper.java @@ -1,5 +1,6 @@ package com.ruoyi.flyingbook.LarkHelper; +import com.alibaba.fastjson.JSONObject; import com.lark.oapi.service.approval.v4.model.*; import com.ruoyi.flyingbook.domain.lark.LarkApprovalRequest; import com.ruoyi.flyingbook.domain.lark.LarkException; @@ -35,7 +36,7 @@ public class LarkApprovalHelper extends LarkHelper{ if (getInstanceResp.getCode() == 0){ return getInstanceResp.getData(); }else { - throw new RuntimeException(new LarkException("LarkApprovalHelper.getApprovalInfoDetail",getInstanceResp.getMsg(),request).getErrorMessageBody()); + throw new RuntimeException(new LarkException("LarkApprovalHelper.getApprovalInfoDetail", JSONObject.toJSONString(getInstanceResp.getError()),request).getErrorMessageBody()); } } catch (Exception e) { throw new RuntimeException(new LarkException("LarkApprovalHelper.getApprovalInfoDetail",e.getMessage(),request).getErrorMessageBody()); diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkCalendarHelper.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkCalendarHelper.java new file mode 100644 index 0000000..fdf1f22 --- /dev/null +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkCalendarHelper.java @@ -0,0 +1,230 @@ +package com.ruoyi.flyingbook.LarkHelper; + +import com.alibaba.fastjson.JSONObject; +import com.lark.oapi.core.response.EmptyData; +import com.lark.oapi.service.calendar.v4.enums.BatchDeleteCalendarEventAttendeeUserIdTypeEnum; +import com.lark.oapi.service.calendar.v4.model.*; +import com.ruoyi.flyingbook.domain.lark.LarkCalendarRequest; +import com.ruoyi.flyingbook.domain.lark.LarkException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + + +/** + * @author yuxiangyong + * @create 2023-03-12 18:10 + */ +@Slf4j +@Component +public class LarkCalendarHelper extends LarkHelper { + + + public static void main(String[] args) throws Exception { + LarkCalendarHelper calendarHelper = new LarkCalendarHelper(); + LarkCalendarRequest calendarRequest = new LarkCalendarRequest(appId, secret); + PrimaryCalendarRespBody primaryCalendar = calendarHelper.getPrimaryCalendar(calendarRequest); + int i = 0; + } + + /** + * 查询日历 + * + * @return + */ + public PrimaryCalendarRespBody getPrimaryCalendar(LarkCalendarRequest request) { + try { + PrimaryCalendarResp primary = buildClient(request) + .calendar().calendar() + .primary( + PrimaryCalendarReq.newBuilder() + .build() + ); + if (primary.getCode() == 0) { + return primary.getData(); + } else { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", JSONObject.toJSONString(primary.getError()), request).getErrorMessageBody()); + } + } catch (Exception e) { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", e.getMessage(), request).getErrorMessageBody()); + } + } + + /** + * 查询日程 + * @param request + * @return + */ + public ListCalendarEventAttendeeRespBody getCalendarDetail(LarkCalendarRequest request) { + try { + ListCalendarEventAttendeeResp list = buildClient(request) + .calendar().calendarEventAttendee() + .list( + ListCalendarEventAttendeeReq.newBuilder() + .calendarId(request.getCalendarId()) + .eventId(request.getEventId()) + .build() + ); + if (list.getCode() == 0) { + return list.getData(); + } else { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", JSONObject.toJSONString(list.getError()), request).getErrorMessageBody()); + } + } catch (Exception e) { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", e.getMessage(), request).getErrorMessageBody()); + } + } + + /** + * 创建日程 + * + * @return + */ + public CreateCalendarEventRespBody createCalendarDetail(LarkCalendarRequest request) { + try { + CreateCalendarEventResp createCalendarEventResp = buildClient(request) + .calendar().calendarEvent() + .create( + CreateCalendarEventReq.newBuilder() + .calendarId(request.getCalendarId()) + .calendarEvent( + CalendarEvent.newBuilder() + .summary(request.getSummary()) + .needNotification(request.getNeedNotification()) + .attendeeAbility(request.getAttendeeAbility()) + .startTime( + TimeInfo.newBuilder() + .timezone(request.getTimeZone()) + .date(request.getStartDate()) + .timestamp(request.getStartDateStamp()) + .build() + ) + .endTime( + TimeInfo.newBuilder() + .timezone(request.getTimeZone()) + .date(request.getEndDate()) + .timestamp(request.getEndDateStamp()) + .build() + ) + .build() + ) + .build() + ); + if (createCalendarEventResp.getCode() == 0) { + return createCalendarEventResp.getData(); + } else { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", JSONObject.toJSONString(createCalendarEventResp.getError()), request).getErrorMessageBody()); + } + } catch (Exception e) { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", e.getMessage(), request).getErrorMessageBody()); + } + } + + /** + * 修改日程 + * + * @return + */ + public PatchCalendarEventRespBody updateCalendarDetail(LarkCalendarRequest request) { + try { + PatchCalendarEventResp patch = buildClient(request) + .calendar().calendarEvent() + .patch(PatchCalendarEventReq.newBuilder() + .calendarId(request.getCalendarId()) + .eventId(request.getEventId()) + .calendarEvent( + CalendarEvent.newBuilder() + .summary(request.getSummary()) + .needNotification(request.getNeedNotification()) + .attendeeAbility(request.getAttendeeAbility()) + .startTime( + TimeInfo.newBuilder() + .timezone(request.getTimeZone()) + .date(request.getStartDate()) + .timestamp(request.getStartDateStamp()) + .build() + ) + .endTime( + TimeInfo.newBuilder() + .timezone(request.getTimeZone()) + .date(request.getEndDate()) + .timestamp(request.getEndDateStamp()) + .build() + ) + .build() + ) + .build()); + if (patch.getCode() == 0) { + return patch.getData(); + } else { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", JSONObject.toJSONString(patch.getError()), request).getErrorMessageBody()); + } + } catch (Exception e) { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", e.getMessage(), request).getErrorMessageBody()); + } + } + + /** + * 绑定日程 + * + * @return + */ + public CreateCalendarEventAttendeeRespBody attendeesCalendarDetail(LarkCalendarRequest request) { + try { + CreateCalendarEventAttendeeResp createCalendarEventAttendeeResp = buildClient(request) + .calendar().calendarEventAttendee() + .create( + CreateCalendarEventAttendeeReq.newBuilder() + .calendarId(request.getCalendarId()) + .eventId(request.getEventId()) + .createCalendarEventAttendeeReqBody( + CreateCalendarEventAttendeeReqBody.newBuilder() + .needNotification(request.getNeedNotification()) + .attendees(request.getCalendarEventAttendeesArray()) + .build() + ) + .build() + ); + + if (createCalendarEventAttendeeResp.getCode() == 0) { + return createCalendarEventAttendeeResp.getData(); + } else { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", JSONObject.toJSONString(createCalendarEventAttendeeResp.getError()), request).getErrorMessageBody()); + } + } catch (Exception e) { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", e.getMessage(), request).getErrorMessageBody()); + } + } + + /** + * 解绑日程 + * + * @return + */ + public EmptyData unAttendeesCalendarDetail(LarkCalendarRequest request) { + try { + BatchDeleteCalendarEventAttendeeResp batchDeleteCalendarEventAttendeeResp = buildClient(request) + .calendar().calendarEventAttendee() + .batchDelete( + BatchDeleteCalendarEventAttendeeReq.newBuilder() + .calendarId(request.getCalendarId()) + .eventId(request.getEventId()) + .userIdType(BatchDeleteCalendarEventAttendeeUserIdTypeEnum.USER_ID) + .batchDeleteCalendarEventAttendeeReqBody( + BatchDeleteCalendarEventAttendeeReqBody.newBuilder() + .deleteIds(request.getCalendarEventDeleteArray()) + .build() + ) + .build() + ); + + if (batchDeleteCalendarEventAttendeeResp.getCode() == 0) { + return batchDeleteCalendarEventAttendeeResp.getData(); + } else { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", JSONObject.toJSONString(batchDeleteCalendarEventAttendeeResp.getError()), request).getErrorMessageBody()); + } + } catch (Exception e) { + throw new RuntimeException(new LarkException("LarkCalendarHelper.getPrimaryCalendar", e.getMessage(), request).getErrorMessageBody()); + } + } + +} diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkTaskHelper.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkTaskHelper.java index 7dbd308..da505fb 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkTaskHelper.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/LarkHelper/LarkTaskHelper.java @@ -1,5 +1,6 @@ package com.ruoyi.flyingbook.LarkHelper; +import com.alibaba.fastjson.JSONObject; import com.lark.oapi.service.task.v1.enums.CreateTaskUserIdTypeEnum; import com.lark.oapi.service.task.v1.model.*; import com.ruoyi.flyingbook.domain.lark.LarkException; @@ -47,7 +48,11 @@ public class LarkTaskHelper extends LarkHelper { .task(request.getTask()) .build() ); - return createTaskResp.getData(); + if (createTaskResp.getCode() == 0){ + return createTaskResp.getData(); + }else { + throw new RuntimeException(new LarkException("LarkTaskHelper.createTask", JSONObject.toJSONString(createTaskResp.getError()),request).getErrorMessageBody()); + } } catch (Exception e) { throw new RuntimeException(new LarkException("LarkTaskHelper.createTask",e.getMessage(),request).getErrorMessageBody()); } @@ -67,7 +72,11 @@ public class LarkTaskHelper extends LarkHelper { .taskId(request.getLarkTaskId()) .build() ); - return getTaskResp.getData(); + if (getTaskResp.getCode() == 0){ + return getTaskResp.getData(); + }else { + throw new RuntimeException(new LarkException("LarkTaskHelper.createTask", JSONObject.toJSONString(getTaskResp.getError()),request).getErrorMessageBody()); + } } catch (Exception e) { throw new RuntimeException(new LarkException("LarkTaskHelper.createTask",e.getMessage(),request).getErrorMessageBody()); } diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkCalendarRequest.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkCalendarRequest.java new file mode 100644 index 0000000..77319a6 --- /dev/null +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/domain/lark/LarkCalendarRequest.java @@ -0,0 +1,165 @@ +package com.ruoyi.flyingbook.domain.lark; + +import com.lark.oapi.service.calendar.v4.enums.CalendarEventEventAttendeeAbilityEnum; +import com.lark.oapi.service.calendar.v4.model.CalendarEventAttendee; +import com.lark.oapi.service.calendar.v4.model.CalendarEventAttendeeId; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * @author yuxiangyong + * @create 2023-03-18 1:05 + */ +@Data +@NoArgsConstructor +public class LarkCalendarRequest extends LarkRequest { + + /** + * + */ + private String calendarId; + /** + * + */ + private String eventId; + /** + * 操作人id + */ + private String operateId; + /** + * 关注人列表 + */ + private List followerIdList; + /** + * 日程标题 + */ + private String summary; + + /** + * 开始结束时间 + * YYYY-mm-DD + */ + private String startDate; + private String endDate; + + /** + * 开始结束时间 + * YYYY-mm-DD + */ + private String startDateStamp; + private String endDateStamp; + + /** + * 默认 Asia/Shanghai + */ + private String timeZone; + + /** + * 更新日程是否给日程参与人发送bot通知,默认false + */ + private Boolean needNotification; + + + /** + * 参与人权限 默认can_see_others + * none:无法编辑日程、无法邀请其它参与人、无法查看参与人列表 + * can_see_others:无法编辑日程、无法邀请其它参与人、可以查看参与人列表 + * can_invite_others:无法编辑日程、可以邀请其它参与人、可以查看参与人列表 + * can_modify_event:可以编辑日程、可以邀请其它参与人、可以查看参与人列表 + */ + private CalendarEventEventAttendeeAbilityEnum attendeeAbility; + + public LarkCalendarRequest(String appId, String appSecret) { + super(appId, appSecret); + } + + /** + * 查询日程 + * @param appId + * @param appSecret + * @param calendarId + * @param eventId + */ + public LarkCalendarRequest(String appId, String appSecret, String calendarId, String eventId) { + super(appId, appSecret); + this.calendarId = calendarId; + this.eventId = eventId; + } + + /** + * 创建日程 + */ + public LarkCalendarRequest(String appId, String appSecret, String calendarId, String summary, String startDate, String endDate) { + super(appId, appSecret); + this.calendarId = calendarId; + this.summary = summary; + this.startDate = startDate; + this.endDate = endDate; + this.timeZone = "Asia/Shanghai"; + this.needNotification = Boolean.FALSE; + this.attendeeAbility = CalendarEventEventAttendeeAbilityEnum.CANSEEOTHERS; + } + + /** + * 编辑日程 + */ + public LarkCalendarRequest(String appId, String appSecret, String calendarId, String eventId, String summary, String startDate, String endDate) { + super(appId, appSecret); + this.calendarId = calendarId; + this.eventId = eventId; + this.summary = summary; + this.startDate = startDate; + this.endDate = endDate; + this.timeZone = "Asia/Shanghai"; + this.needNotification = Boolean.FALSE; + this.attendeeAbility = CalendarEventEventAttendeeAbilityEnum.CANSEEOTHERS; + } + + /** + * 绑定日程 + */ + public LarkCalendarRequest(String appId, String appSecret, String calendarId, String eventId,List followerIdList,String operateId) { + super(appId, appSecret); + this.calendarId = calendarId; + this.eventId = eventId; + this.operateId = operateId; + this.followerIdList = followerIdList; + } + + /** + * 解绑日程 + */ + public LarkCalendarRequest(String appId, String appSecret, String calendarId, String eventId,List followerIdList) { + super(appId, appSecret); + this.calendarId = calendarId; + this.eventId = eventId; + this.followerIdList = followerIdList; + } + + public CalendarEventAttendee[] getCalendarEventAttendeesArray() { + int size = followerIdList.size(); + CalendarEventAttendee[] calendarEventAttendees = new CalendarEventAttendee[size]; + for (int i = 0; i < followerIdList.size(); i++) { + CalendarEventAttendee calendarEventAttendee = new CalendarEventAttendee(); + calendarEventAttendee.setIsOptional(Boolean.TRUE); + calendarEventAttendee.setUserId(followerIdList.get(i)); + calendarEventAttendee.setOperateId(operateId); + calendarEventAttendees[i] = calendarEventAttendee; + } + return calendarEventAttendees; + } + + public CalendarEventAttendeeId[] getCalendarEventDeleteArray() { + int size = followerIdList.size(); + CalendarEventAttendeeId[] calendarEventAttendees = new CalendarEventAttendeeId[size]; + for (int i = 0; i < followerIdList.size(); i++) { + CalendarEventAttendeeId calendarEventAttendee = new CalendarEventAttendeeId(); + calendarEventAttendee.setType("user"); + calendarEventAttendee.setUserId(followerIdList.get(i)); + calendarEventAttendees[i] = calendarEventAttendee; + } + return calendarEventAttendees; + } +} diff --git a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/LarkTaskCallbackOperate.java b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/LarkTaskCallbackOperate.java index c8aaa95..bde2185 100644 --- a/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/LarkTaskCallbackOperate.java +++ b/ruoyi-flyingbook/src/main/java/com/ruoyi/flyingbook/strategy/operate/LarkTaskCallbackOperate.java @@ -1,19 +1,29 @@ package com.ruoyi.flyingbook.strategy.operate; -import com.ruoyi.common.enums.EventOperateStatus; -import com.ruoyi.common.enums.EventOperateType; +import cn.hutool.core.io.unit.DataUnit; +import com.lark.oapi.service.calendar.v4.model.*; +import com.lark.oapi.service.task.v1.model.Due; +import com.lark.oapi.service.task.v1.model.GetTaskRespBody; +import com.lark.oapi.service.task.v1.model.Task; +import com.ruoyi.common.enums.*; +import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.flyingbook.LarkHelper.LarkCalendarHelper; import com.ruoyi.flyingbook.LarkHelper.LarkTaskHelper; -import com.ruoyi.flyingbook.domain.Event; -import com.ruoyi.flyingbook.domain.EventLog; -import com.ruoyi.flyingbook.domain.RequestVo; +import com.ruoyi.flyingbook.domain.*; +import com.ruoyi.flyingbook.domain.lark.LarkCalendarRequest; +import com.ruoyi.flyingbook.domain.lark.LarkTaskRequest; import com.ruoyi.flyingbook.service.IEventLogService; import com.ruoyi.flyingbook.service.IEventService; +import com.ruoyi.flyingbook.service.ILarkCompanyRelationService; +import com.ruoyi.flyingbook.service.ILarkTableRowRelationService; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.Map; +import java.time.LocalDateTime; +import java.util.*; /** @@ -32,7 +42,12 @@ public class LarkTaskCallbackOperate extends LarkAbstract { private LarkTaskHelper larkTaskHelper; @Autowired private IEventService eventService; - + @Autowired + private ILarkCompanyRelationService larkCompanyRelationService; + @Autowired + private ILarkTableRowRelationService larkTableRowRelationService; + @Autowired + private LarkCalendarHelper larkCalendarHelper; @Override protected void preOperate(RequestVo request) { @@ -40,14 +55,136 @@ public class LarkTaskCallbackOperate extends LarkAbstract { if (event == null) { throw new RuntimeException("当前事件为空"); } - + LarkCompanyRelation companyRelation = larkCompanyRelationService.getByAppIdAndType(event.getAppId(), AppType.MULTIDIMENSIONAL_TABLE.getCode()); + if (companyRelation == null){ + throw new RuntimeException(String.format("eventId:{} appId:{} appType:{} 不存在对应的lark_company配置",event.getId(),event.getAppId(),AppType.MULTIDIMENSIONAL_TABLE.getCode())); + } + request.setAppId(companyRelation.getAppId()); + request.setAppSecret(companyRelation.getSecret()); } @Override protected void businessProcessing(RequestVo request) { Event event = request.getEvent(); String larkTaskId = event.getRecordId(); -// larkTaskHelper.createTask() + //获取任务明细 + LarkTaskRequest getTaskRequest = new LarkTaskRequest(request.getAppId(),request.getAppSecret(),larkTaskId); + GetTaskRespBody taskInfo = larkTaskHelper.getTaskInfo(getTaskRequest); + Task task = taskInfo.getTask(); + //任务的关注人及负责人取交集 + String[] collaboratorIds = task.getCollaboratorIds(); + String operator = collaboratorIds[0]; + String[] followerIds = task.getFollowerIds(); + //待绑定日程用户 + String[] taskFollowerArray = getArrayIntersection(collaboratorIds, followerIds); + //待解除绑定日程用户 + List taskUnFollowerList = new ArrayList<>(); + //判断当前任务是否已经存在与日历的关系 + LarkTableRowRelation taskCalendarRelation = getTaskCalendarByTaskId(larkTaskId); + //待更新日历的id + String calendarId = getCalendarInfo(request.getAppId(), request.getAppSecret()); + String eventId = null; + if (taskCalendarRelation == null){ + //关系为空,创建日程 + String summary = task.getSummary(); + if (StringUtils.isNotBlank(task.getCompleteTime()) && !task.getCompleteTime().equals("0")){ + summary += "✔"; + } + Due due = task.getDue(); + LarkCalendarRequest calendarRequest = null; + if (due == null){ + String time = DateUtils.ldt2str(LocalDateTime.now(), DateUtils.YYYY_MM_DD); + calendarRequest = new LarkCalendarRequest(request.getAppId(), request.getAppSecret(), calendarId, summary, task.getDescription(), time, time); + }else { + Boolean isAllDay = due.getIsAllDay(); + Long time = Long.valueOf(due.getTime()); + if (isAllDay){ + String allDay = DateUtils.long2Str(time, DateUtils.YYYY_MM_DD); + calendarRequest = new LarkCalendarRequest(request.getAppId(), request.getAppSecret(), calendarId, summary, task.getDescription(), allDay, allDay); + }else { + LocalDateTime localDateTime = DateUtils.long2ldt(time); + LocalDateTime startTime = localDateTime.minusHours(2L); + calendarRequest = new LarkCalendarRequest(request.getAppId(), request.getAppSecret(), calendarId, summary, task.getDescription(), null, null); + calendarRequest.setStartDateStamp(String.valueOf(DateUtils.ldt2Long(startTime))); + calendarRequest.setEndDateStamp(due.getTime()); + } + } + CreateCalendarEventRespBody calendarDetail = larkCalendarHelper.createCalendarDetail(calendarRequest); + eventId = calendarDetail.getEvent().getEventId(); + //任务日历关系实体 + this.buildTaskCalendarRelation(larkTaskId,eventId); + }else { + eventId = taskCalendarRelation.getToId(); + LarkCalendarRequest calendarRequest = new LarkCalendarRequest(request.getAppId(), request.getAppSecret(), calendarId, eventId); + ListCalendarEventAttendeeRespBody calendarDetail = larkCalendarHelper.getCalendarDetail(calendarRequest); + for (CalendarEventAttendee item : calendarDetail.getItems()) { + taskUnFollowerList.add(item.getUserId()); + } + } + if (CollectionUtils.isNotEmpty(taskUnFollowerList)){ + LarkCalendarRequest remove = new LarkCalendarRequest(request.getAppId(), request.getAppSecret(), calendarId, eventId, taskUnFollowerList); + larkCalendarHelper.unAttendeesCalendarDetail(remove); + } + if (taskFollowerArray != null && taskFollowerArray.length > 0){ + LarkCalendarRequest add = new LarkCalendarRequest(request.getAppId(), request.getAppSecret(), calendarId, eventId, Arrays.asList(taskFollowerArray), operator); + larkCalendarHelper.attendeesCalendarDetail(add); + } + + + } + + /** + * 构建任务日历关系实体 + * @return + */ + private void buildTaskCalendarRelation(String larkTaskId,String eventId){ + LarkTableRowRelation rowRelation = new LarkTableRowRelation(); + rowRelation.setFlag(FlagStatus.OK.getCode()); + rowRelation.setType(TableDetailRelationTypeEnum.TASK_CALENDAR.getCode()); + rowRelation.setFromId(larkTaskId); + rowRelation.setToId(eventId); + larkTableRowRelationService.insertLarkTableRowRelation(rowRelation); + } + + /** + * 获取主日历信息 + * @param appId + * @param appSecret + * @return + */ + private String getCalendarInfo(String appId,String appSecret){ + LarkCalendarRequest calendarRequest = new LarkCalendarRequest(appId, appSecret); + PrimaryCalendarRespBody primaryCalendar = larkCalendarHelper.getPrimaryCalendar(calendarRequest); + UserCalendar calendar = primaryCalendar.getCalendars()[0]; + return calendar.getCalendar().getCalendarId(); + } + + /** + * 查询任务对应的日历是否已经创建 + * @return + */ + private LarkTableRowRelation getTaskCalendarByTaskId(String larkTaskId){ + LarkTableRowRelation rowRelationQuery = new LarkTableRowRelation(); + rowRelationQuery.setType(TableDetailRelationTypeEnum.TASK_CALENDAR.getCode()); + rowRelationQuery.setSubType(TableDetailRelationTypeEnum.TASK_CALENDAR.getCode()); + rowRelationQuery.setFromId(larkTaskId); + rowRelationQuery.setFlag(FlagStatus.OK.getCode()); + List larkTableRowRelations = larkTableRowRelationService.selectLarkTableRowRelationList(rowRelationQuery); + return CollectionUtils.isNotEmpty(larkTableRowRelations) ? larkTableRowRelations.get(0) : null; + } + + /** + * 任务的操作人和关注人取交集 + * @param collaboratorIds + * @param followerIds + * @return + */ + private String[] getArrayIntersection(String[] collaboratorIds,String[] followerIds){ + new HashSet(Arrays.asList(collaboratorIds)); + Set collaboratorIdSet = collaboratorIds != null ? new HashSet(Arrays.asList(collaboratorIds)) : new HashSet(); + Set followerIdSet = followerIds != null ? new HashSet(Arrays.asList(followerIds)) : new HashSet(); + collaboratorIdSet.addAll(followerIdSet); + return collaboratorIdSet.toArray(new String[collaboratorIdSet.size()]); } @Override