Commit ee09693a by 18356073052

群发代码修改

1 parent 1c47634c
......@@ -14,6 +14,8 @@
<typeHandlers>
<typeHandler handler="com.linkwechat.framework.handler.StringArrayJoinTypeHandler"/>
<typeHandler handler="com.linkwechat.framework.handler.GenericTypeHandler"/>
<typeHandler handler="com.linkwechat.framework.handler.ListTypeHandler"/>
</typeHandlers>
</configuration>
......@@ -69,6 +69,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<!-- io常用工具类 -->
......
package com.linkwechat.framework.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.linkwechat.wecom.domain.dto.message.CustomerMessagePushDto;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 用以mysql中json格式的字段,进行转换的自定义转换器,转换为实体类的T类型 属性
* @param <T>
*/
@SuppressWarnings("all")
@MappedTypes(value = {JSONObject.class, CustomerMessagePushDto.class})
@MappedJdbcTypes(value = {JdbcType.VARCHAR}, includeNullJdbcType = true)
public class GenericTypeHandler<T extends Object> extends BaseTypeHandler<T> {
private Class<T> clazz;
public GenericTypeHandler(Class<T> clazz) {
if (clazz == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.clazz = clazz;
}
/**
* 设置非空参数
* @param ps
* @param i
* @param parameter
* @param jdbcType
* @throws SQLException
*/
@Override
public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, JSON.toJSONString(parameter));
}
/**
* 根据列名,获取可以为空的结果
* @param rs
* @param columnName
* @return
* @throws SQLException
*/
@Override
public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
String sqlJson = rs.getString(columnName);
if (null != sqlJson) {
return JSONObject.parseObject(sqlJson, clazz);
}
return null;
}
/**
* 根据列索引,获取可以为空的结果
* @param rs
* @param columnIndex
* @return
* @throws SQLException
*/
@Override
public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String sqlJson = rs.getString(columnIndex);
if (null != sqlJson) {
return JSONObject.parseObject(sqlJson, clazz);
}
return null;
}
@Override
public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String sqlJson = cs.getString(columnIndex);
if (null != sqlJson) {
return JSONObject.parseObject(sqlJson, clazz);
}
return null;
}
}
\ No newline at end of file
package com.linkwechat.framework.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeGroup;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.type.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* 处理jsonArray字符串为pojoList
* @param <T>
*/
//支持的java对象
@SuppressWarnings("all")
@MappedTypes(value = {WeCustomer.class,WeGroup.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class ListTypeHandler<T extends Object> implements TypeHandler<List<T>> {
private List<T> getListByJsonArrayString(String content) {
if (StringUtils.isEmpty(content)) {
return new ArrayList<>();
}
return JSON.parseObject(content,new TypeReference<ArrayList>(){});
}
/**
* 用于定义在Mybatis设置参数时该如何把Java类型的参数转换为对应的数据库类型
*
* <PRE>
* PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
* SET SALARY = ? WHERE ID = ?");
* pstmt.setBigDecimal(1, 153833.00)
* pstmt.setInt(2, 110592)
* </PRE>
*
* @param preparedStatement An object that represents a precompiled SQL statement
* @param i 当前参数的位置
* @param t 当前参数的Java对象
* @param jdbcType 当前参数的数据库类型
* @throws SQLException
*/
@Override
public void setParameter(PreparedStatement preparedStatement, int i, List<T> t, JdbcType jdbcType) throws SQLException {
if (CollectionUtils.isEmpty(t)) {
preparedStatement.setString(i, null);
} else {
preparedStatement.setString(i, JSON.toJSONString(t).replaceAll("[\ud800\udc00-\udbff\udfff\ud800-\udfff]", ""));
}
}
@Override
public List<T> getResult(ResultSet resultSet, String s) throws SQLException {
return getListByJsonArrayString(resultSet.getString(s));
}
@Override
public List<T> getResult(ResultSet resultSet, int i) throws SQLException {
return getListByJsonArrayString(resultSet.getString(i));
}
@Override
public List<T> getResult(CallableStatement callableStatement, int i) throws SQLException {
return getListByJsonArrayString(callableStatement.getString(i));
}
}
\ No newline at end of file
......@@ -2,16 +2,16 @@ package com.linkwechat.quartz.task;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Lists;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.elasticsearch.ElasticSearch;
import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.service.IWeChatContactMappingService;
import com.linkwechat.wecom.service.IWeCustomerService;
import com.linkwechat.wecom.service.IWeSensitiveActHitService;
import com.linkwechat.wecom.service.IWeSensitiveService;
import com.linkwechat.wecom.domain.WeCustomerMessageTimeTask;
import com.linkwechat.wecom.mapper.WeCustomerMessageTimeTaskMapper;
import com.linkwechat.wecom.service.*;
import com.tencent.wework.FinanceUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
......@@ -48,6 +48,11 @@ public class RyTask {
private IWeSensitiveActHitService weSensitiveActHitService;
@Autowired
private IWeCustomerService weCustomerService;
@Autowired
private WeCustomerMessageTimeTaskMapper customerMessageTimeTaskMapper;
@Autowired
private IWeCustomerMessageService weCustomerMessageService;
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
......@@ -130,4 +135,24 @@ public class RyTask {
log.info("执行有参方法: params:{},{}", corpId, secret);
}
public void messageTask(){
//获的当前时间的毫秒数
long currentTime = System.currentTimeMillis();
//customerMessageTimeTaskMapper
List<WeCustomerMessageTimeTask> weCustomerMessageTimeTasks = customerMessageTimeTaskMapper.selectWeCustomerMessageTimeTaskGteSettingTime(currentTime);
if(CollectionUtils.isNotEmpty(weCustomerMessageTimeTasks)){
weCustomerMessageTimeTasks.forEach(s->{
try {
weCustomerMessageService.sendMessgae(s.getMessageInfo(), s.getMessageId(),s.getCustomersInfo(),s.getGroupsInfo());
} catch (JsonProcessingException e) {
log.error("定时群发消息处理异常:ex:{}", e);
e.printStackTrace();
}
});
}
}
}
......@@ -2,10 +2,13 @@ package com.linkwechat.wecom.domain;
import com.baomidou.mybatisplus.annotation.TableName;
import com.linkwechat.common.core.domain.BaseEntity;
import com.linkwechat.wecom.domain.dto.message.CustomerMessagePushDto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* 群发消息定时任务表
* @author Kewen
......@@ -14,7 +17,7 @@ import lombok.NoArgsConstructor;
@TableName("we_customer_messageTimeTask")
public class WeCustomerMessageTimeTask extends BaseEntity {
public WeCustomerMessageTimeTask(Long messageId, String messageInfo, String customersInfo, String groupsInfo, Long settingTime) {
public WeCustomerMessageTimeTask(Long messageId, CustomerMessagePushDto messageInfo, List<WeCustomer> customersInfo, List<WeGroup> groupsInfo, Long settingTime) {
this.messageId = messageId;
this.messageInfo = messageInfo;
this.customersInfo = customersInfo;
......@@ -38,17 +41,17 @@ public class WeCustomerMessageTimeTask extends BaseEntity {
/**
* 消息原始信息
*/
private String messageInfo;
private CustomerMessagePushDto messageInfo;
/**
* 客户信息列表
*/
private String customersInfo;
private List<WeCustomer> customersInfo;
/**
* 客户群组信息列表
*/
private String groupsInfo;
private List<WeGroup> groupsInfo;
/**
* 定时时间的毫秒数
......
......@@ -2,6 +2,9 @@ package com.linkwechat.wecom.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.linkwechat.wecom.domain.WeCustomerMessageTimeTask;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 群发消息 定时任务表 Mapper接口
......@@ -10,4 +13,21 @@ import com.linkwechat.wecom.domain.WeCustomerMessageTimeTask;
* @date 2021-02-09
*/
public interface WeCustomerMessageTimeTaskMapper extends BaseMapper<WeCustomerMessageTimeTask> {
/**
* 查询群发任务列表
*
* @param timeMillis 当前时间毫秒数
* @return {@link WeCustomerMessageTimeTask}s
*/
List<WeCustomerMessageTimeTask> selectWeCustomerMessageTimeTaskGteSettingTime(@Param("timeMillis") long timeMillis);
/**
* 保存群发任务
*
* @param customerMessageTimeTask 群发任务信息
* @return int
*/
int saveWeCustomerMessageTimeTask(WeCustomerMessageTimeTask customerMessageTimeTask);
}
......@@ -133,12 +133,10 @@ public class WeCustomerMessagePushServiceImpl implements IWeCustomerMessagePushS
weCustomerMessageService.sendMessgae(customerMessagePushDto, messageId,customers,groups);
} else {
WeCustomerMessageTimeTask timeTask=new WeCustomerMessageTimeTask(messageId, JSONUtil.toJsonStr(customerMessagePushDto),
JSONUtil.toJsonStr(customers).replaceAll("[\ud800\udc00-\udbff\udfff\ud800-\udfff]", "")
,JSONUtil.toJsonStr(groups).replaceAll("[\ud800\udc00-\udbff\udfff\ud800-\udfff]", "")
WeCustomerMessageTimeTask timeTask=new WeCustomerMessageTimeTask(messageId, customerMessagePushDto, customers,groups
,DateUtils.getMillionSceondsBydate(customerMessagePushDto.getSettingTime()));
customerMessageTimeTaskMapper.insert(timeTask);
customerMessageTimeTaskMapper.saveWeCustomerMessageTimeTask(timeTask);
}
......
......@@ -199,7 +199,7 @@ public class WeTaskFissionServiceImpl implements IWeTaskFissionService {
}
try {
weCustomerMessagePushService.addWeCustomerMessagePush(customerMessagePushDto);
} catch (JsonProcessingException e) {
} catch (JsonProcessingException | ParseException e) {
e.printStackTrace();
log.error("发送任务失败》》》》》》》》》》》params:{},ex:{}", JSONObject.toJSONString(customerMessagePushDto), e);
}
......
......@@ -3,4 +3,53 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.linkwechat.wecom.mapper.WeCustomerMessageTimeTaskMapper">
<resultMap id="tasks" type="com.linkwechat.wecom.domain.WeCustomerMessageTimeTask">
<id column="task_id" property="taskId"/>
<result column="message_id" property="messageId"/>
<result column="message_info" property="messageInfo" typeHandler="com.linkwechat.framework.handler.GenericTypeHandler"/>
<result column="customers_info" property="customersInfo" typeHandler="com.linkwechat.framework.handler.ListTypeHandler"/>
<result column="groups_info" property="groupsInfo" typeHandler="com.linkwechat.framework.handler.ListTypeHandler"/>
<result column="setting_time" property="settingTime"/>
</resultMap>
<insert id="saveWeCustomerMessageTimeTask">
INSERT INTO we_customer_messageTimeTask (
message_id,
message_info,
customers_info,
groups_info,
setting_time,
create_by,
create_time,
update_by,
update_time
)
VALUES
(
#{messageId},
#{messageInfo,jdbcType=OTHER,typeHandler=com.linkwechat.framework.handler.GenericTypeHandler},
#{customersInfo,jdbcType=OTHER,typeHandler=com.linkwechat.framework.handler.ListTypeHandler},
#{groupsInfo,jdbcType=OTHER,typeHandler=com.linkwechat.framework.handler.ListTypeHandler},
#{settingTime},
#{createBy},
#{createTime},
#{updateBy},
#{updateTime}
)
</insert>
<select id="selectWeCustomerMessageTimeTaskGteSettingTime" resultMap="tasks">
SELECT
message_id,
message_info,
customers_info,
groups_info,
setting_time
FROM
we_customer_messageTimeTask
<where>
setting_time lte #{timeMillis}
</where>
</select>
</mapper>
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!