飞书机器人

于相涌/Lark
YXY 2 years ago
parent 0f9b8f03f9
commit 76024840b2

@ -0,0 +1,40 @@
package com.ruoyi.common.enums;
/**
*
*
* @author ruoyi
*/
public enum ConfigurationSubTypeEnum {
/**
*
*/
REMINDER_BEFORE("REMINDER_BEFORE", "发生前提醒"),
REMINDER_AFTER("REMINDER_AFTER", "发生后提醒"),
REMINDER_AROUND("REMINDER_AROUND", "发生前后提醒");
private final String code;
private final String info;
ConfigurationSubTypeEnum(String code, String info) {
this.code = code;
this.info = info;
}
public String getCode() {
return code;
}
public String getInfo() {
return info;
}
public static ConfigurationSubTypeEnum getByCode(String code){
for (ConfigurationSubTypeEnum value : ConfigurationSubTypeEnum.values()) {
if (value.getCode().equals(code)){
return value;
}
}
return null;
}
}

@ -0,0 +1,29 @@
package com.ruoyi.common.enums;
/**
*
*
* @author ruoyi
*/
public enum ConfigurationTypeEnum {
/**
*
*/
SCHEDULE_REMINDER("SCHEDULE_REMINDER", "定时提醒配置");
private final String code;
private final String info;
ConfigurationTypeEnum(String code, String info) {
this.code = code;
this.info = info;
}
public String getCode() {
return code;
}
public String getInfo() {
return info;
}
}

@ -14,7 +14,8 @@ public enum EventOperateType {
UPDATE("UPDATE", "更新"),
DELETE("DELETE", "删除"),
STOP("STOP", "终止"),
SYNC_MAIL("SYNC_MAIL", "同步邮件"),;
SYNC_MAIL("SYNC_MAIL", "同步邮件"),
SCHEDULE_REMINDER("SCHEDULE_REMINDER", "定时提醒");
private final String code;
private final String info;

@ -0,0 +1,38 @@
package com.ruoyi.common.enums;
/**
*
*
* @author ruoyi
*/
public enum TemplateSubTypeEnum {
/**
*
*/
TEXT("text", "文本文档");
private final String code;
private final String info;
TemplateSubTypeEnum(String code, String info) {
this.code = code;
this.info = info;
}
public String getCode() {
return code;
}
public String getInfo() {
return info;
}
public static TemplateSubTypeEnum getByCode(String code){
for (TemplateSubTypeEnum value : TemplateSubTypeEnum.values()) {
if (value.getCode().equals(code)){
return value;
}
}
return null;
}
}

@ -0,0 +1,29 @@
package com.ruoyi.common.enums;
/**
*
*
* @author ruoyi
*/
public enum TemplateTypeEnum {
/**
*
*/
SCHEDULED_REMINDER("SCHEDULED_REMINDER", "定时提醒");
private final String code;
private final String info;
TemplateTypeEnum(String code, String info) {
this.code = code;
this.info = info;
}
public String getCode() {
return code;
}
public String getInfo() {
return info;
}
}

@ -0,0 +1,43 @@
package com.ruoyi.common.enums;
/**
*
*
* @author ruoyi
*/
public enum TimeUnitEnum {
/**
*
*/
YEAR("YEAR", "年"),
MONTH("MONTH", "月"),
DAY("DAY", "日"),
HOUR("HOUR", "时"),
MINUTE("MINUTE", "分"),
SECOND("SECOND", "秒");
private final String code;
private final String info;
TimeUnitEnum(String code, String info) {
this.code = code;
this.info = info;
}
public String getCode() {
return code;
}
public String getInfo() {
return info;
}
public static TimeUnitEnum getByCode(String code){
for (TimeUnitEnum value : TimeUnitEnum.values()) {
if (value.getCode().equals(code)){
return value;
}
}
return null;
}
}

@ -1,5 +1,10 @@
package com.ruoyi.flyingbook.LarkHelper.script;
import com.ruoyi.common.enums.ConfigurationSubTypeEnum;
import com.ruoyi.common.enums.TimeUnitEnum;
import com.ruoyi.flyingbook.domain.LarkTableConfiguration;
import org.apache.commons.lang3.StringUtils;
import java.time.LocalDateTime;
/**
@ -8,23 +13,49 @@ import java.time.LocalDateTime;
*/
public class LarkTableFilterScriptHelper {
public static String buildTimeRange(String paramCode, LocalDateTime fromTime) {
/**
* param1
* param2
*/
public static String buildTimeRange(LarkTableConfiguration larkTableConfiguration,ConfigurationSubTypeEnum subType, String paramCode, LocalDateTime fromTime) {
Long minute = getMinute(larkTableConfiguration);
switch (subType) {
case REMINDER_BEFORE:
fromTime = fromTime.minusMinutes(minute);
break;
case REMINDER_AFTER:
fromTime = fromTime.plusMinutes(minute);
break;
default:
break;
}
String range = fromTime.getMinute() >= 30 ? ">=" : "<";
return String.format("AND(YEAR(CurrentValue.[%s]) = %s," +
"MONTH(CurrentValue.[%s]) = %s," +
"DAY(CurrentValue.[%s]) = %s," +
"HOUR(CurrentValue.[%s]) = %s," +
"MINUTE(CurrentValue.[%s]) %s 30)",
paramCode,
fromTime.getYear(),
paramCode,
fromTime.getMonth().getValue(),
paramCode,
fromTime.getDayOfMonth(),
paramCode,
fromTime.getHour(),
paramCode,
range);
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(")");
return sb.toString();
}
private static Long getMinute(LarkTableConfiguration larkTableConfiguration){
TimeUnitEnum timeUnit = TimeUnitEnum.getByCode(larkTableConfiguration.getParam2());
if (timeUnit == null){
throw new RuntimeException(String.format("tableId:%s 配置时间单位:%s不存在",larkTableConfiguration.getLarkTableId(),larkTableConfiguration.getParam2()));
}
Long value = Long.valueOf(larkTableConfiguration.getParam1());
Long minute = 0L;
switch (timeUnit){
case HOUR:
minute = value * 60;
break;
case MINUTE:
minute = value;
}
return minute;
}
}

@ -0,0 +1,37 @@
package com.ruoyi.flyingbook.domain;
import com.ruoyi.common.core.domain.BaseEntity;
import lombok.Data;
/**
* @author ruoyi
* @date 2023-04-20
*/
@Data
public class LarkTableConfiguration extends BaseEntity {
private static final long serialVersionUID = 1L;
private Long id;
/**
* id
*/
private Long larkTableId;
/**
*
* @see com.ruoyi.common.enums.ConfigurationTypeEnum
*/
private String configType;
/**
*
* @see com.ruoyi.common.enums.ConfigurationSubTypeEnum
*/
private String subConfigType;
private String param1;
private String param2;
private String param3;
private String param4;
private Long flag;
}

@ -0,0 +1,39 @@
package com.ruoyi.flyingbook.domain;
import lombok.Data;
import com.ruoyi.common.core.domain.BaseEntity;
/**
* @author ruoyi
* @date 2023-04-20
*/
@Data
public class LarkTemplate extends BaseEntity {
private static final long serialVersionUID = 1L;
private Long id;
/**
* id
*/
private Long larkTableId;
/**
*
* @see com.ruoyi.common.enums.TemplateTypeEnum
*/
private String templateType;
/**
*
* @see com.ruoyi.common.enums.TemplateSubTypeEnum
*/
private String subTemplateType;
/**
*
*/
private String templateContent;
private Long flag;
}

@ -0,0 +1,63 @@
package com.ruoyi.flyingbook.mapper;
import com.ruoyi.flyingbook.domain.LarkTableConfiguration;
import java.util.List;
/**
* Mapper
*
* @author ruoyi
* @date 2023-04-20
*/
public interface LarkTableConfigurationMapper
{
/**
*
*
* @param id ID
* @return
*/
public LarkTableConfiguration selectLarkTableConfigurationById(Long id);
/**
*
*
* @param larkTableConfiguration
* @return
*/
public List<LarkTableConfiguration> selectLarkTableConfigurationList(LarkTableConfiguration larkTableConfiguration);
/**
*
*
* @param larkTableConfiguration
* @return
*/
public int insertLarkTableConfiguration(LarkTableConfiguration larkTableConfiguration);
/**
*
*
* @param larkTableConfiguration
* @return
*/
public int updateLarkTableConfiguration(LarkTableConfiguration larkTableConfiguration);
/**
*
*
* @param id ID
* @return
*/
public int deleteLarkTableConfigurationById(Long id);
/**
*
*
* @param ids ID
* @return
*/
public int deleteLarkTableConfigurationByIds(Long[] ids);
}

@ -0,0 +1,63 @@
package com.ruoyi.flyingbook.mapper;
import com.ruoyi.flyingbook.domain.LarkTemplate;
import java.util.List;
/**
* Mapper
*
* @author ruoyi
* @date 2023-04-20
*/
public interface LarkTemplateMapper
{
/**
*
*
* @param id ID
* @return
*/
public LarkTemplate selectLarkTemplateById(Long id);
/**
*
*
* @param larkTemplate
* @return
*/
public List<LarkTemplate> selectLarkTemplateList(LarkTemplate larkTemplate);
/**
*
*
* @param larkTemplate
* @return
*/
public int insertLarkTemplate(LarkTemplate larkTemplate);
/**
*
*
* @param larkTemplate
* @return
*/
public int updateLarkTemplate(LarkTemplate larkTemplate);
/**
*
*
* @param id ID
* @return
*/
public int deleteLarkTemplateById(Long id);
/**
*
*
* @param ids ID
* @return
*/
public int deleteLarkTemplateByIds(Long[] ids);
}

@ -0,0 +1,63 @@
package com.ruoyi.flyingbook.service;
import com.ruoyi.flyingbook.domain.LarkTableConfiguration;
import java.util.List;
/**
* Service
*
* @author ruoyi
* @date 2023-04-20
*/
public interface ILarkTableConfigurationService
{
/**
*
*
* @param id ID
* @return
*/
public LarkTableConfiguration selectLarkTableConfigurationById(Long id);
/**
*
*
* @param larkTableConfiguration
* @return
*/
public List<LarkTableConfiguration> selectLarkTableConfigurationList(LarkTableConfiguration larkTableConfiguration);
/**
*
*
* @param larkTableConfiguration
* @return
*/
public int insertLarkTableConfiguration(LarkTableConfiguration larkTableConfiguration);
/**
*
*
* @param larkTableConfiguration
* @return
*/
public int updateLarkTableConfiguration(LarkTableConfiguration larkTableConfiguration);
/**
*
*
* @param ids ID
* @return
*/
public int deleteLarkTableConfigurationByIds(Long[] ids);
/**
*
*
* @param id ID
* @return
*/
public int deleteLarkTableConfigurationById(Long id);
}

@ -0,0 +1,62 @@
package com.ruoyi.flyingbook.service;
import com.ruoyi.flyingbook.domain.LarkTemplate;
import java.util.List;
/**
* Service
*
* @author ruoyi
* @date 2023-04-20
*/
public interface ILarkTemplateService {
/**
*
*
* @param id ID
* @return
*/
public LarkTemplate selectLarkTemplateById(Long id);
/**
*
*
* @param larkTemplate
* @return
*/
public List<LarkTemplate> selectLarkTemplateList(LarkTemplate larkTemplate);
/**
*
*
* @param larkTemplate
* @return
*/
public int insertLarkTemplate(LarkTemplate larkTemplate);
/**
*
*
* @param larkTemplate
* @return
*/
public int updateLarkTemplate(LarkTemplate larkTemplate);
/**
*
*
* @param ids ID
* @return
*/
public int deleteLarkTemplateByIds(Long[] ids);
/**
*
*
* @param id ID
* @return
*/
public int deleteLarkTemplateById(Long id);
}

@ -0,0 +1,97 @@
package com.ruoyi.flyingbook.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.flyingbook.domain.LarkTableConfiguration;
import com.ruoyi.flyingbook.mapper.LarkTableConfigurationMapper;
import com.ruoyi.flyingbook.service.ILarkTableConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Service
*
* @author ruoyi
* @date 2023-04-20
*/
@Service
public class LarkTableConfigurationServiceImpl implements ILarkTableConfigurationService
{
@Autowired
private LarkTableConfigurationMapper larkTableConfigurationMapper;
/**
*
*
* @param id ID
* @return
*/
@Override
public LarkTableConfiguration selectLarkTableConfigurationById(Long id)
{
return larkTableConfigurationMapper.selectLarkTableConfigurationById(id);
}
/**
*
*
* @param larkTableConfiguration
* @return
*/
@Override
public List<LarkTableConfiguration> selectLarkTableConfigurationList(LarkTableConfiguration larkTableConfiguration)
{
return larkTableConfigurationMapper.selectLarkTableConfigurationList(larkTableConfiguration);
}
/**
*
*
* @param larkTableConfiguration
* @return
*/
@Override
public int insertLarkTableConfiguration(LarkTableConfiguration larkTableConfiguration)
{
larkTableConfiguration.setCreateTime(DateUtils.getNowDate());
return larkTableConfigurationMapper.insertLarkTableConfiguration(larkTableConfiguration);
}
/**
*
*
* @param larkTableConfiguration
* @return
*/
@Override
public int updateLarkTableConfiguration(LarkTableConfiguration larkTableConfiguration)
{
larkTableConfiguration.setUpdateTime(DateUtils.getNowDate());
return larkTableConfigurationMapper.updateLarkTableConfiguration(larkTableConfiguration);
}
/**
*
*
* @param ids ID
* @return
*/
@Override
public int deleteLarkTableConfigurationByIds(Long[] ids)
{
return larkTableConfigurationMapper.deleteLarkTableConfigurationByIds(ids);
}
/**
*
*
* @param id ID
* @return
*/
@Override
public int deleteLarkTableConfigurationById(Long id)
{
return larkTableConfigurationMapper.deleteLarkTableConfigurationById(id);
}
}

@ -0,0 +1,97 @@
package com.ruoyi.flyingbook.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.flyingbook.domain.LarkTemplate;
import com.ruoyi.flyingbook.mapper.LarkTemplateMapper;
import com.ruoyi.flyingbook.service.ILarkTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Service
*
* @author ruoyi
* @date 2023-04-20
*/
@Service
public class LarkTemplateServiceImpl implements ILarkTemplateService
{
@Autowired
private LarkTemplateMapper larkTemplateMapper;
/**
*
*
* @param id ID
* @return
*/
@Override
public LarkTemplate selectLarkTemplateById(Long id)
{
return larkTemplateMapper.selectLarkTemplateById(id);
}
/**
*
*
* @param larkTemplate
* @return
*/
@Override
public List<LarkTemplate> selectLarkTemplateList(LarkTemplate larkTemplate)
{
return larkTemplateMapper.selectLarkTemplateList(larkTemplate);
}
/**
*
*
* @param larkTemplate
* @return
*/
@Override
public int insertLarkTemplate(LarkTemplate larkTemplate)
{
larkTemplate.setCreateTime(DateUtils.getNowDate());
return larkTemplateMapper.insertLarkTemplate(larkTemplate);
}
/**
*
*
* @param larkTemplate
* @return
*/
@Override
public int updateLarkTemplate(LarkTemplate larkTemplate)
{
larkTemplate.setUpdateTime(DateUtils.getNowDate());
return larkTemplateMapper.updateLarkTemplate(larkTemplate);
}
/**
*
*
* @param ids ID
* @return
*/
@Override
public int deleteLarkTemplateByIds(Long[] ids)
{
return larkTemplateMapper.deleteLarkTemplateByIds(ids);
}
/**
*
*
* @param id ID
* @return
*/
@Override
public int deleteLarkTemplateById(Long id)
{
return larkTemplateMapper.deleteLarkTemplateById(id);
}
}

@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.flyingbook.mapper.LarkTableConfigurationMapper">
<resultMap type="com.ruoyi.flyingbook.domain.LarkTableConfiguration" id="LarkTableConfigurationResult">
<result property="id" column="id" />
<result property="larkTableId" column="lark_table_id" />
<result property="configType" column="config_type" />
<result property="subConfigType" column="sub_config_type" />
<result property="param1" column="param1" />
<result property="param2" column="param2" />
<result property="param3" column="param3" />
<result property="param4" column="param4" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="flag" column="flag" />
<result property="remark" column="remark" />
</resultMap>
<sql id="selectLarkTableConfigurationVo">
select id, lark_table_id, config_type, sub_config_type, param1, param2, param3, param4, create_by, create_time, update_by, update_time, flag, remark from lark_table_configuration
</sql>
<select id="selectLarkTableConfigurationList" parameterType="com.ruoyi.flyingbook.domain.LarkTableConfiguration" resultMap="LarkTableConfigurationResult">
<include refid="selectLarkTableConfigurationVo"/>
<where>
<if test="larkTableId != null "> and lark_table_id = #{larkTableId}</if>
<if test="configType != null and configType != ''"> and config_type = #{configType}</if>
<if test="subConfigType != null and subConfigType != ''"> and sub_config_type = #{subConfigType}</if>
<if test="param1 != null and param1 != ''"> and param1 = #{param1}</if>
<if test="param2 != null and param2 != ''"> and param2 = #{param2}</if>
<if test="param3 != null and param3 != ''"> and param3 = #{param3}</if>
<if test="param4 != null and param4 != ''"> and param4 = #{param4}</if>
<if test="flag != null "> and flag = #{flag}</if>
</where>
</select>
<select id="selectLarkTableConfigurationById" parameterType="Long" resultMap="LarkTableConfigurationResult">
<include refid="selectLarkTableConfigurationVo"/>
where id = #{id}
</select>
<insert id="insertLarkTableConfiguration" parameterType="com.ruoyi.flyingbook.domain.LarkTableConfiguration" useGeneratedKeys="true" keyProperty="id">
insert into lark_table_configuration
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="larkTableId != null">lark_table_id,</if>
<if test="configType != null and configType != ''">config_type,</if>
<if test="subConfigType != null and subConfigType != ''">sub_config_type,</if>
<if test="param1 != null">param1,</if>
<if test="param2 != null">param2,</if>
<if test="param3 != null">param3,</if>
<if test="param4 != null">param4,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="flag != null">flag,</if>
<if test="remark != null">remark,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="larkTableId != null">#{larkTableId},</if>
<if test="configType != null and configType != ''">#{configType},</if>
<if test="subConfigType != null and subConfigType != ''">#{subConfigType},</if>
<if test="param1 != null">#{param1},</if>
<if test="param2 != null">#{param2},</if>
<if test="param3 != null">#{param3},</if>
<if test="param4 != null">#{param4},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="flag != null">#{flag},</if>
<if test="remark != null">#{remark},</if>
</trim>
</insert>
<update id="updateLarkTableConfiguration" parameterType="com.ruoyi.flyingbook.domain.LarkTableConfiguration">
update lark_table_configuration
<trim prefix="SET" suffixOverrides=",">
<if test="larkTableId != null">lark_table_id = #{larkTableId},</if>
<if test="configType != null and configType != ''">config_type = #{configType},</if>
<if test="subConfigType != null and subConfigType != ''">sub_config_type = #{subConfigType},</if>
<if test="param1 != null">param1 = #{param1},</if>
<if test="param2 != null">param2 = #{param2},</if>
<if test="param3 != null">param3 = #{param3},</if>
<if test="param4 != null">param4 = #{param4},</if>
<if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="flag != null">flag = #{flag},</if>
<if test="remark != null">remark = #{remark},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteLarkTableConfigurationById" parameterType="Long">
delete from lark_table_configuration where id = #{id}
</delete>
<delete id="deleteLarkTableConfigurationByIds" parameterType="String">
delete from lark_table_configuration where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.flyingbook.mapper.LarkTemplateMapper">
<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="subTemplateType" column="sub_template_type" />
<result property="templateContent" column="template_content" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="flag" column="flag" />
<result property="remark" column="remark" />
</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
</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="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>
</where>
</select>
<select id="selectLarkTemplateById" parameterType="Long" resultMap="LarkTemplateResult">
<include refid="selectLarkTemplateVo"/>
where id = #{id}
</select>
<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="subTemplateType != null and subTemplateType != ''">sub_template_type,</if>
<if test="templateContent != null">template_content,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="flag != null">flag,</if>
<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="subTemplateType != null and subTemplateType != ''">#{subTemplateType},</if>
<if test="templateContent != null">#{templateContent},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="flag != null">#{flag},</if>
<if test="remark != null">#{remark},</if>
</trim>
</insert>
<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="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>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="flag != null">flag = #{flag},</if>
<if test="remark != null">remark = #{remark},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteLarkTemplateById" parameterType="Long">
delete from lark_template where id = #{id}
</delete>
<delete id="deleteLarkTemplateByIds" parameterType="String">
delete from lark_template where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>

@ -1,15 +1,21 @@
package com.ruoyi.quartz.task;
import cn.hutool.extra.template.Template;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lark.oapi.service.bitable.v1.model.ListAppTableRecordRespBody;
import com.ruoyi.common.enums.AppType;
import com.ruoyi.common.enums.FlagStatus;
import com.ruoyi.common.enums.TableRelationTypeEnum;
import com.ruoyi.common.enums.*;
import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper;
import com.ruoyi.flyingbook.LarkHelper.script.LarkTableFilterScriptHelper;
import com.ruoyi.flyingbook.domain.EventLog;
import com.ruoyi.flyingbook.domain.LarkCompanyTableInfo;
import com.ruoyi.flyingbook.domain.LarkTableConfiguration;
import com.ruoyi.flyingbook.domain.LarkTemplate;
import com.ruoyi.flyingbook.domain.lark.LarkTableRequest;
import com.ruoyi.flyingbook.mapper.EventLogMapper;
import com.ruoyi.flyingbook.mapper.LarkTableConfigurationMapper;
import com.ruoyi.flyingbook.mapper.LarkTableRelationMapper;
import com.ruoyi.flyingbook.mapper.LarkTemplateMapper;
import com.ruoyi.quartz.helper.OkHttpHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
@ -18,8 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -38,6 +44,12 @@ public class ScheduledRemindersTask {
@Autowired
private LarkTableHelper larkTableHelper;
@Autowired
private LarkTemplateMapper larkTemplateMapper;
@Autowired
private LarkTableConfigurationMapper larkTableConfigurationMapper;
@Autowired
private EventLogMapper eventLogMapper;
/**查询分页大小*/
private static final Integer PAGE_SIZE = 20;
@ -51,33 +63,113 @@ public class ScheduledRemindersTask {
if (CollectionUtils.isEmpty(larkList)){
return;
}
String script = LarkTableFilterScriptHelper.buildTimeRange(RANGE_KEY,LocalDateTime.now());
List<EventLog> logList = new ArrayList<>();
for (LarkCompanyTableInfo larkCompanyTableInfo : larkList) {
try {
this.sendMessage(script,larkCompanyTableInfo);
List<LarkTableConfiguration> larkConfiguration = this.getLarkConfiguration(larkCompanyTableInfo);
for (LarkTableConfiguration larkTableConfiguration : larkConfiguration) {
ConfigurationSubTypeEnum subType = ConfigurationSubTypeEnum.getByCode(larkTableConfiguration.getSubConfigType());
if (subType == null){
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);
}else {
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration,subType,RANGE_KEY,LocalDateTime.now());
this.sendMessage(script,larkCompanyTableInfo,logList);
}
}
}catch (Exception e){
//todo yxy 记录日志
log.error("ScheduledRemindersTask#scheduledReminders error request:{}",JSONObject.toJSONString(larkCompanyTableInfo),e);
new EventLog(larkCompanyTableInfo.getId(),EventOperateType.SCHEDULE_REMINDER.getCode(), JSONObject.toJSONString(larkCompanyTableInfo),null,e.getMessage());
}
}
if (CollectionUtils.isNotEmpty(logList)){
eventLogMapper.insertBatchEventLog(logList);
}
log.info("scheduledRemindersTask end");
}
private void sendMessage(String script,LarkCompanyTableInfo larkCompanyTableInfo){
private void sendMessage(String script,LarkCompanyTableInfo larkCompanyTableInfo,List<EventLog> logList){
String pageToken = null;
LarkTableRequest request = this.buildRequest(larkCompanyTableInfo, script);
LarkTemplate template = this.getTemplate(larkCompanyTableInfo);
do {
ListAppTableRecordRespBody respBody = larkTableHelper.listTableRecord(request);
pageToken = respBody.getPageToken();
Arrays.stream(respBody.getItems()).forEach(r -> {
Map<String, Object> fields = r.getFields();
JSONObject msg = new JSONObject();
OkHttpHelper.post(larkCompanyTableInfo.getToAppToken(),msg.toString());
});
JSONObject msg = new JSONObject();
try {
ListAppTableRecordRespBody respBody = larkTableHelper.listTableRecord(request);
pageToken = respBody.getPageToken();
Arrays.stream(respBody.getItems()).forEach(r -> {
Map<String, Object> fields = r.getFields();
JSONObject msg = this.buildMsg(larkCompanyTableInfo,fields, template);
OkHttpHelper.post(larkCompanyTableInfo.getToAppToken(),msg.toString());
});
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());
}
}while (StringUtils.isNotBlank(pageToken));
}
private LarkTemplate getTemplate(LarkCompanyTableInfo larkCompanyTableInfo){
LarkTemplate larkTemplate = new LarkTemplate();
larkTemplate.setTemplateType(TemplateTypeEnum.SCHEDULED_REMINDER.getCode());
larkTemplate.setFlag(FlagStatus.OK.getCode());
larkTemplate.setLarkTableId(larkCompanyTableInfo.getId());
List<LarkTemplate> larkTemplates = larkTemplateMapper.selectLarkTemplateList(larkTemplate);
if (CollectionUtils.isEmpty(larkTemplates)){
throw new RuntimeException(String.format("当前tableId:%s %s类型的模板配置不存在",larkCompanyTableInfo.getId(),larkTemplate.getTemplateType()));
}
if (larkTemplates.size() > 1){
throw new RuntimeException(String.format("当前tableId:%s %s类型存在多个有效的模板配置",larkCompanyTableInfo.getId(),larkTemplate.getTemplateType()));
}
return larkTemplates.get(0);
}
private List<LarkTableConfiguration> getLarkConfiguration(LarkCompanyTableInfo larkCompanyTableInfo){
List<LarkTableConfiguration> configurationList = new ArrayList<>();
LarkTableConfiguration query = new LarkTableConfiguration();
query.setLarkTableId(larkCompanyTableInfo.getId());
query.setFlag(FlagStatus.OK.getCode());
query.setConfigType(ConfigurationTypeEnum.SCHEDULE_REMINDER.getCode());
configurationList = larkTableConfigurationMapper.selectLarkTableConfigurationList(query);
if (CollectionUtils.isEmpty(configurationList)){
throw new RuntimeException(String.format("当前tableId:%s 无定时提醒配置",larkCompanyTableInfo.getId()));
}
return configurationList;
}
private JSONObject buildMsg(LarkCompanyTableInfo larkCompanyTableInfo,Map<String, Object> fields, LarkTemplate template){
JSONObject msg = new JSONObject();
TemplateSubTypeEnum templateSubTypeEnum = TemplateSubTypeEnum.getByCode(template.getSubTemplateType());
if (templateSubTypeEnum == null){
throw new RuntimeException(String.format("当前tableId:%s 模板类型%s对应策略不存在",larkCompanyTableInfo.getId(),template.getSubTemplateType()));
}
switch (templateSubTypeEnum){
case TEXT:
msg.put("msg_type",templateSubTypeEnum.getCode());
String templateContent = template.getTemplateContent();
JSONArray people = JSONObject.parseArray(String.valueOf(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")));
}
templateContent.replaceAll("@name",sb.toString()).replaceAll("#name",String.valueOf(fields.get("学生姓名")));
JSONObject content = new JSONObject();
content.put("text",sb.append(templateContent).toString());
msg.put("content",content);
break;
default:
break;
}
return msg;
}
private List<LarkCompanyTableInfo> getLarkList() {
List<LarkCompanyTableInfo> larkCompanyTableInfos = larkTableRelationMapper.queryListJoinLarkCompany(FlagStatus.OK.getCode(), AppType.SCHEDULED_REMINDERS.getCode(), TableRelationTypeEnum.SYNC_EMAIL.getCode());
return larkCompanyTableInfos.stream()

Loading…
Cancel
Save