飞书机器人提醒
continuous-integration/drone/push Build is passing Details

于相涌/Lark
YXY 1 year ago
parent 56a21f01f7
commit 8a04ab6932

@ -34,9 +34,9 @@ public class LarkTableFilterScriptHelper {
StringBuilder sb = new StringBuilder("AND(");
sb.append(String.format("YEAR(CurrentValue.[%s]) = %s",paramCode,fromTime.getYear())).append(",");
sb.append(String.format("MONTH(CurrentValue.[%s]) = %s",paramCode,fromTime.getMonth().getValue())).append(",");
sb.append(String.format("DAY(CurrentValue.[%s]) = %s",paramCode,fromTime.getDayOfMonth()));
sb.append(String.format("HOUR(CurrentValue.[%s]) = %s",paramCode,fromTime.getHour()));
sb.append(String.format("HOUR(CurrentValue.[%s]) %s 30",paramCode,range));
sb.append(String.format("DAY(CurrentValue.[%s]) = %s",paramCode,fromTime.getDayOfMonth())).append(",");
sb.append(String.format("HOUR(CurrentValue.[%s]) = %s",paramCode,fromTime.getHour())).append(",");
sb.append(String.format("MINUTE(CurrentValue.[%s]) %s 30",paramCode,range));
sb.append(")");
return sb.toString();
}

@ -14,9 +14,9 @@ public class LarkTemplate extends BaseEntity {
private Long id;
/**
* id
* id
*/
private Long larkTableId;
private Long relationId;
/**
*

@ -164,7 +164,7 @@
LEFT JOIN lark_company_relation cr ON tr.lark_company_relation_id = cr.id
WHERE
tr.flag = #{flag}
AND cr.app_type = #{appType};
AND cr.app_type = #{appType}
AND tr.relation_type = #{relationType};
</select>
</mapper>

@ -6,8 +6,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<resultMap type="com.ruoyi.flyingbook.domain.LarkTemplate" id="LarkTemplateResult">
<result property="id" column="id" />
<result property="larkTableId" column="lark_table_id" />
<result property="templateTypeEnum" column="template_type" />
<result property="relationId" column="relation_id" />
<result property="templateType" column="template_type" />
<result property="subTemplateType" column="sub_template_type" />
<result property="templateContent" column="template_content" />
<result property="createBy" column="create_by" />
@ -19,14 +19,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</resultMap>
<sql id="selectLarkTemplateVo">
select id, lark_table_id, template_type, sub_template_type, template_content, create_by, create_time, update_by, update_time, flag, remark from lark_template
select id, relation_id, template_type, sub_template_type, template_content, create_by, create_time, update_by, update_time, flag, remark from lark_template
</sql>
<select id="selectLarkTemplateList" parameterType="com.ruoyi.flyingbook.domain.LarkTemplate" resultMap="LarkTemplateResult">
<include refid="selectLarkTemplateVo"/>
<where>
<if test="larkTableId != null "> and lark_table_id = #{larkTableId}</if>
<if test="templateTypeEnum != null and templateTypeEnum != ''"> and template_type = #{templateTypeEnum}</if>
<if test="relationId != null "> and relation_id = #{relationId}</if>
<if test="templateType != null and templateType != ''"> and template_type = #{templateType}</if>
<if test="subTemplateType != null and subTemplateType != ''"> and sub_template_type = #{subTemplateType}</if>
<if test="templateContent != null and templateContent != ''"> and template_content = #{templateContent}</if>
<if test="flag != null "> and flag = #{flag}</if>
@ -41,8 +41,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<insert id="insertLarkTemplate" parameterType="com.ruoyi.flyingbook.domain.LarkTemplate" useGeneratedKeys="true" keyProperty="id">
insert into lark_template
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="larkTableId != null">lark_table_id,</if>
<if test="templateTypeEnum != null and templateTypeEnum != ''">template_type,</if>
<if test="relationId != null">relation_id,</if>
<if test="templateType != null and templateType != ''">template_type,</if>
<if test="subTemplateType != null and subTemplateType != ''">sub_template_type,</if>
<if test="templateContent != null">template_content,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
@ -53,8 +53,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="remark != null">remark,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="larkTableId != null">#{larkTableId},</if>
<if test="templateTypeEnum != null and templateTypeEnum != ''">#{templateTypeEnum},</if>
<if test="relationId != null">#{relationId},</if>
<if test="templateType != null and templateType != ''">#{templateType},</if>
<if test="subTemplateType != null and subTemplateType != ''">#{subTemplateType},</if>
<if test="templateContent != null">#{templateContent},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
@ -69,8 +69,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<update id="updateLarkTemplate" parameterType="com.ruoyi.flyingbook.domain.LarkTemplate">
update lark_template
<trim prefix="SET" suffixOverrides=",">
<if test="larkTableId != null">lark_table_id = #{larkTableId},</if>
<if test="templateTypeEnum != null and templateTypeEnum != ''">template_type = #{templateTypeEnum},</if>
<if test="relationId != null">relation_id = #{relationId},</if>
<if test="templateType != null and templateType != ''">template_type = #{templateType},</if>
<if test="subTemplateType != null and subTemplateType != ''">sub_template_type = #{subTemplateType},</if>
<if test="templateContent != null">template_content = #{templateContent},</if>
<if test="createBy != null and createBy != ''">create_by = #{createBy},</if>

@ -97,7 +97,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// 过滤请求
.authorizeRequests()
// 对于登录login 验证码captchaImage 允许匿名访问
.antMatchers("/login", "/captchaImage", "/approval","/syncEmail", "/approval2").anonymous()
.antMatchers("/login", "/captchaImage", "/approval","/syncEmail","/scheduledReminders", "/approval2").anonymous()
.antMatchers(
HttpMethod.GET,
"/*.html",

@ -2,6 +2,7 @@ package com.ruoyi.quartz.controller;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.quartz.task.MailSyncTask;
import com.ruoyi.quartz.task.ScheduledRemindersTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
@ -14,11 +15,18 @@ public class MailInfoController extends BaseController {
@Autowired
private MailSyncTask mailSyncTask;
@Autowired
private ScheduledRemindersTask scheduledRemindersTask;
@PostMapping("/syncEmail")
public void syncEmail(Boolean readFlag) {
mailSyncTask.syncMail(readFlag);
}
@PostMapping("/scheduledReminders")
public void scheduledReminders() {
scheduledRemindersTask.scheduledReminders();
}
}

@ -3,6 +3,8 @@ package com.ruoyi.quartz.task;
import cn.hutool.extra.template.Template;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.internal.LinkedTreeMap;
import com.lark.oapi.service.bitable.v1.model.AppTableRecord;
import com.lark.oapi.service.bitable.v1.model.ListAppTableRecordRespBody;
import com.ruoyi.common.enums.*;
import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper;
@ -51,9 +53,13 @@ public class ScheduledRemindersTask {
@Autowired
private EventLogMapper eventLogMapper;
/**查询分页大小*/
/**
*
*/
private static final Integer PAGE_SIZE = 20;
/**查询分页大小*/
/**
*
*/
private static final String RANGE_KEY = "日期";
@ -64,6 +70,8 @@ public class ScheduledRemindersTask {
return;
}
List<EventLog> logList = new ArrayList<>();
LocalDateTime now = LocalDateTime.now();
// LocalDateTime now = LocalDateTime.of(2023, 4, 22, 10, 0);
for (LarkCompanyTableInfo larkCompanyTableInfo : larkList) {
try {
List<LarkTableConfiguration> larkConfiguration = this.getLarkConfiguration(larkCompanyTableInfo);
@ -73,18 +81,18 @@ public class ScheduledRemindersTask {
throw new RuntimeException(String.format("tableId:%s 配置子类:%s 无对应策略处理逻辑", larkTableConfiguration.getLarkTableId(), larkTableConfiguration.getSubConfigType()));
}
if (ConfigurationSubTypeEnum.REMINDER_AROUND.equals(subType)) {
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration,ConfigurationSubTypeEnum.REMINDER_BEFORE,RANGE_KEY,LocalDateTime.now());
this.sendMessage(script,larkCompanyTableInfo,logList);
script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration,ConfigurationSubTypeEnum.REMINDER_AFTER,RANGE_KEY,LocalDateTime.now());
this.sendMessage(script,larkCompanyTableInfo,logList);
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_BEFORE, RANGE_KEY, now);
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration);
script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_AFTER, RANGE_KEY, now);
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration);
} else {
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration,subType,RANGE_KEY,LocalDateTime.now());
this.sendMessage(script,larkCompanyTableInfo,logList);
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, subType, RANGE_KEY, now);
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration);
}
}
} catch (Exception e) {
log.error("ScheduledRemindersTask#scheduledReminders error request:{}", JSONObject.toJSONString(larkCompanyTableInfo), e);
new EventLog(larkCompanyTableInfo.getId(),EventOperateType.SCHEDULE_REMINDER.getCode(), JSONObject.toJSONString(larkCompanyTableInfo),null,e.getMessage());
logList.add(new EventLog(larkCompanyTableInfo.getId(), EventOperateType.SCHEDULE_REMINDER.getCode(), JSONObject.toJSONString(larkCompanyTableInfo), null, e.getMessage()));
}
}
if (CollectionUtils.isNotEmpty(logList)) {
@ -93,33 +101,37 @@ public class ScheduledRemindersTask {
log.info("scheduledRemindersTask end");
}
private void sendMessage(String script,LarkCompanyTableInfo larkCompanyTableInfo,List<EventLog> logList){
private void sendMessage(String script, LarkCompanyTableInfo larkCompanyTableInfo, List<EventLog> logList,LarkTableConfiguration larkTableConfiguration) {
String pageToken = null;
LarkTableRequest request = this.buildRequest(larkCompanyTableInfo, script);
LarkTemplate template = this.getTemplate(larkCompanyTableInfo);
LarkTemplate template = this.getTemplate(larkCompanyTableInfo,larkTableConfiguration.getId());
do {
JSONObject msg = new JSONObject();
try {
ListAppTableRecordRespBody respBody = larkTableHelper.listTableRecord(request);
if (respBody.getPageToken().equals(pageToken)){
return;
}else {
pageToken = respBody.getPageToken();
Arrays.stream(respBody.getItems()).forEach(r -> {
Map<String, Object> fields = r.getFields();
JSONObject msg = this.buildMsg(larkCompanyTableInfo,fields, template);
}
for (AppTableRecord item : respBody.getItems()) {
try {
Map<String, Object> fields = item.getFields();
msg = this.buildMsg(larkCompanyTableInfo, fields, template);
OkHttpHelper.post(larkCompanyTableInfo.getToAppToken(), msg.toString());
});
new EventLog(larkCompanyTableInfo.getId(),EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString());
logList.add(new EventLog(larkCompanyTableInfo.getId(), EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString()));
} catch (Exception e) {
log.error("ScheduledRemindersTask#scheduledReminders error request tableId:{} msg:{}", JSONObject.toJSONString(larkCompanyTableInfo), msg.toString(), e);
new EventLog(larkCompanyTableInfo.getId(),EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString(),null,e.getMessage());
logList.add(new EventLog(larkCompanyTableInfo.getId(), EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString(), null, e.getMessage()));
}
}
} while (StringUtils.isNotBlank(pageToken));
}
private LarkTemplate getTemplate(LarkCompanyTableInfo larkCompanyTableInfo){
private LarkTemplate getTemplate(LarkCompanyTableInfo larkCompanyTableInfo,Long configurationId) {
LarkTemplate larkTemplate = new LarkTemplate();
larkTemplate.setTemplateType(TemplateTypeEnum.SCHEDULED_REMINDER.getCode());
larkTemplate.setFlag(FlagStatus.OK.getCode());
larkTemplate.setLarkTableId(larkCompanyTableInfo.getId());
larkTemplate.setRelationId(configurationId);
List<LarkTemplate> larkTemplates = larkTemplateMapper.selectLarkTemplateList(larkTemplate);
if (CollectionUtils.isEmpty(larkTemplates)) {
throw new RuntimeException(String.format("当前tableId:%s %s类型的模板配置不存在", larkCompanyTableInfo.getId(), larkTemplate.getTemplateType()));
@ -153,13 +165,13 @@ public class ScheduledRemindersTask {
case TEXT:
msg.put("msg_type", templateSubTypeEnum.getCode());
String templateContent = template.getTemplateContent();
JSONArray people = JSONObject.parseArray(String.valueOf(fields.get("人员")));
List<LinkedTreeMap<String,String>> people = (List<LinkedTreeMap<String,String>>)fields.get("人员");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < people.size(); i++) {
JSONObject jsonObject = people.getJSONObject(i);
sb.append(String.format("<at user_id = \"%s\">%s</at>",jsonObject.getString("id"),jsonObject.getString("name")));
LinkedTreeMap<String,String> detail = people.get(i);
sb.append(String.format("<at user_id = \"%s\">%s</at>", detail.get("id"), detail.get("name")));
}
templateContent.replaceAll("@name",sb.toString()).replaceAll("#name",String.valueOf(fields.get("学生姓名")));
templateContent = templateContent.replaceAll("#name", String.valueOf(fields.get("学生姓名")));
JSONObject content = new JSONObject();
content.put("text", sb.append(templateContent).toString());
msg.put("content", content);
@ -171,7 +183,7 @@ public class ScheduledRemindersTask {
}
private List<LarkCompanyTableInfo> getLarkList() {
List<LarkCompanyTableInfo> larkCompanyTableInfos = larkTableRelationMapper.queryListJoinLarkCompany(FlagStatus.OK.getCode(), AppType.SCHEDULED_REMINDERS.getCode(), TableRelationTypeEnum.SYNC_EMAIL.getCode());
List<LarkCompanyTableInfo> larkCompanyTableInfos = larkTableRelationMapper.queryListJoinLarkCompany(FlagStatus.OK.getCode(), AppType.SCHEDULED_REMINDERS.getCode(), TableRelationTypeEnum.SCHEDULED_REMINDERS.getCode());
return larkCompanyTableInfos.stream()
.filter(r -> {
return r.getId() != null
@ -191,4 +203,16 @@ public class ScheduledRemindersTask {
return request;
}
public static void main(String[] args) {
String str = "[{avatar_url:https://s1-imfile.feishucdn.com/static-resource/v1/v2_5462d76b-2c9c-405f-8753-149c36bf097g~?image_size:72x72&cut_type:default-face&quality:&format:jpeg&sticker_format:.webp, email:, en_name:于相涌, id:ou_cd09481727e91fce1f12b7b0ed1500d2, name:于相涌}]";
str = str.replace("https:","https")
.replaceAll("\\{","{\"")
.replaceAll(":","\":\"")
.replaceAll(",","\",\"")
.replaceAll("}","}")
.replace("https","https:");
JSONArray objects = JSONArray.parseArray(str);
int i = 0;
}
}

Loading…
Cancel
Save