Commit 658466d7 by 18356073052

群发代码修改

1 parent b0726e86
......@@ -18,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.util.List;
/**
......@@ -50,7 +51,7 @@ public class WeCustomerMessagePushController extends BaseController {
public AjaxResult add(@RequestBody CustomerMessagePushDto customerMessagePushDto) {
try {
weCustomerMessagePushService.addWeCustomerMessagePush(customerMessagePushDto);
} catch (JsonProcessingException e) {
} catch (JsonProcessingException | ParseException e) {
e.printStackTrace();
return AjaxResult.error("群发失败");
}
......@@ -104,5 +105,4 @@ public class WeCustomerMessagePushController extends BaseController {
return AjaxResult.success();
}
}
......@@ -387,4 +387,25 @@ public class WeConstans {
public static final String THIRD_APP_PARAM_TIP = "agentId";
public static final String WECUSTOMERS_KEY="weCustomer";
/**
* 发给客户
*/
public static final String SEND_MESSAGE_CUSTOMER = "0";
/**
* 发给客户群
*/
public static final String SEND_MESSAGE_GROUP="1";
/**
* 消息范围 1 指定客户
*/
public static final String SEND_MESSAGE_CUSTOMER_ALL = "0";
/**
* 消息范围 1 指定客户
*/
public static final String SEND_MESSAGE_CUSTOMER_PART="1";
}
package com.linkwechat.common.enums;
import lombok.Getter;
import java.util.Optional;
import java.util.stream.Stream;
/**
* 群发消息类型 0 文本消息 1 图片消息 2 链接消息 3 小程序消息 4 图文消息
*/
@SuppressWarnings("all")
@Getter
public enum GroupMessageType {
/**
* 文本消息
*/
TEXT("0", "text"),
/**
* 图片消息
*/
IMAGE("1", "image"),
/**
* 链接消息
*/
LINK("2", "link"),
/**
* 小程序消息
*/
MINIPROGRAM("3", "miniprogram"),
/**
* 图文消息
*/
TEXT_IMAGE("4", "text_image"),
;
/**
* 媒体类型
*/
String messageType;
/**
* 数据值
*/
String type;
GroupMessageType(String type, String messageType) {
this.type = type;
this.messageType = messageType;
}
public static Optional<GroupMessageType> of(String type) {
return Stream.of(values()).filter(s -> s.type.equals(type)).findFirst();
}
}
......@@ -189,4 +189,10 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
}
return lDate;
}
public static long getMillionSceondsBydate(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
long millionSeconds = sdf.parse(date).getTime();//毫秒
return millionSeconds;
}
}
package com.linkwechat.wecom.domain;
import com.baomidou.mybatisplus.annotation.TableName;
import com.linkwechat.common.core.domain.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 群发消息定时任务表
* @author Kewen
*/
@Data
@TableName("we_customer_messageTimeTask")
public class WeCustomerMessageTimeTask extends BaseEntity {
public WeCustomerMessageTimeTask(Long messageId, String messageInfo, String customersInfo, String groupsInfo, Long settingTime) {
this.messageId = messageId;
this.messageInfo = messageInfo;
this.customersInfo = customersInfo;
this.groupsInfo = groupsInfo;
this.settingTime = settingTime;
}
public WeCustomerMessageTimeTask() {
}
/**
* 任务id
*/
private Long taskId;
/**
* 消息id
*/
private Long messageId;
/**
* 消息原始信息
*/
private String messageInfo;
/**
* 客户信息列表
*/
private String customersInfo;
/**
* 客户群组信息列表
*/
private String groupsInfo;
/**
* 定时时间的毫秒数
*/
private Long settingTime;
}
......@@ -78,5 +78,10 @@ public class CustomerMessagePushVo {
*/
private Integer timedTask;
/**
* 消息发送状态 0 未发送 1 已发送
*/
private String checkStatus;
}
......@@ -21,4 +21,13 @@ public interface WeCustomerMessageMapper extends BaseMapper<WeCustomerMessage> {
* @return int
*/
int updateWeCustomerMessageActualSend(@Param("messageId") Long messageId,@Param("actualSend") Integer actualSend);
/**
* 更新消息发送状态
* @param messageId id
* @param status 消息发送状态 0 未发送 1 已发送
* @return int
*/
int updateWeCustomerMessageCheckStatusById(@Param("messageId") Long messageId,@Param("status") String status);
}
package com.linkwechat.wecom.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.linkwechat.wecom.domain.WeCustomerMessageTimeTask;
/**
* 群发消息 定时任务表 Mapper接口
*
* @author kewen
* @date 2021-02-09
*/
public interface WeCustomerMessageTimeTaskMapper extends BaseMapper<WeCustomerMessageTimeTask> {
}
......@@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.linkwechat.wecom.domain.dto.message.CustomerMessagePushDto;
import com.linkwechat.wecom.domain.vo.CustomerMessagePushVo;
import java.text.ParseException;
import java.util.List;
/**
......@@ -19,7 +20,7 @@ public interface IWeCustomerMessagePushService {
*
*@param customerMessagePushDto 原始数据信息
*/
public void addWeCustomerMessagePush(CustomerMessagePushDto customerMessagePushDto) throws JsonProcessingException;
public void addWeCustomerMessagePush(CustomerMessagePushDto customerMessagePushDto) throws JsonProcessingException, ParseException;
/**
* 群发消息列表
......
......@@ -8,9 +8,11 @@ import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.utils.SnowFlakeUtil;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeCustomerMessagePushClient;
import com.linkwechat.wecom.domain.WeCustomerMessage;
import com.linkwechat.wecom.domain.WeCustomerMessageOriginal;
import com.linkwechat.wecom.domain.dto.message.*;
import com.linkwechat.wecom.domain.vo.CustomerMessagePushVo;
import com.linkwechat.wecom.mapper.WeCustomerMessageMapper;
import com.linkwechat.wecom.mapper.WeCustomerMessageOriginalMapper;
import com.linkwechat.wecom.mapper.WeCustomerMessgaeResultMapper;
import com.linkwechat.wecom.service.IWeCustomerMessageOriginalService;
......@@ -29,6 +31,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* @author kewen
* @date 2020-12-12
*/
@SuppressWarnings("all")
@Service
public class WeCustomerMessageOriginalServiceImpl extends ServiceImpl<WeCustomerMessageOriginalMapper, WeCustomerMessageOriginal> implements IWeCustomerMessageOriginalService {
......@@ -47,6 +50,9 @@ public class WeCustomerMessageOriginalServiceImpl extends ServiceImpl<WeCustomer
@Autowired
private ObjectMapper objectMapper;
@Autowired
private WeCustomerMessageMapper customerMessageMapper;
@Override
public List<CustomerMessagePushVo> customerMessagePushs(String sender, String content, String pushType, String beginTime, String endTime) {
return weCustomerMessageOriginalMapper.selectCustomerMessagePushs(sender, content, pushType, beginTime, endTime);
......@@ -130,7 +136,8 @@ public class WeCustomerMessageOriginalServiceImpl extends ServiceImpl<WeCustomer
if (d.getStatus().equals(WeConstans.sendMessageStatusEnum.SEND.getStatus())) {
atomicInteger.incrementAndGet();
//更新消息发送状态
customerMessageMapper.updateWeCustomerMessageCheckStatusById(messageId,WeConstans.sendMessageStatusEnum.SEND.getStatus());
}
weCustomerMessgaeResultMapper.updateWeCustomerMessgaeResult(messageId, d.getChat_id(), d.getExternal_userid(), d.getStatus(), d.getSend_time());
......
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
......@@ -16,6 +17,7 @@ import com.linkwechat.wecom.client.WeCustomerMessagePushClient;
import com.linkwechat.wecom.domain.*;
import com.linkwechat.wecom.domain.dto.message.*;
import com.linkwechat.wecom.domain.vo.CustomerMessagePushVo;
import com.linkwechat.wecom.mapper.WeCustomerMessageTimeTaskMapper;
import com.linkwechat.wecom.service.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
......@@ -24,6 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
......@@ -65,9 +68,12 @@ public class WeCustomerMessagePushServiceImpl implements IWeCustomerMessagePushS
@Autowired
private RedisCache redisCache;
@Autowired
private WeCustomerMessageTimeTaskMapper customerMessageTimeTaskMapper;
@Override
@Transactional
public void addWeCustomerMessagePush(CustomerMessagePushDto customerMessagePushDto) throws JsonProcessingException {
public void addWeCustomerMessagePush(CustomerMessagePushDto customerMessagePushDto) throws JsonProcessingException, ParseException {
if ((null != customerMessagePushDto.getSettingTime() && !"".equals(customerMessagePushDto.getSettingTime()))
&& DateUtils.diffTime(new Date(), DateUtil.parse(customerMessagePushDto.getSettingTime(), "yyyy-MM-dd HH:mm:ss")) > 0) {
......@@ -77,7 +83,7 @@ public class WeCustomerMessagePushServiceImpl implements IWeCustomerMessagePushS
List<WeCustomer> customers = Lists.newArrayList();
List<WeGroup> groups = new ArrayList<>();
// 0 发给客户
if (customerMessagePushDto.getPushType().equals("0")) {
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_CUSTOMER)) {
//查询客户信息列表
customers = externalUserIds(customerMessagePushDto.getPushRange(), customerMessagePushDto.getStaffId()
, customerMessagePushDto.getDepartment(), customerMessagePushDto.getTag());
......@@ -87,7 +93,7 @@ public class WeCustomerMessagePushServiceImpl implements IWeCustomerMessagePushS
}
// 0 发给客户群
if (customerMessagePushDto.getPushType().equals("1")) {
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_GROUP)) {
if (customerMessagePushDto.getStaffId() == null || customerMessagePushDto.getStaffId().equals("")) {
throw new WeComException("请选择人员!");
......@@ -118,29 +124,21 @@ public class WeCustomerMessagePushServiceImpl implements IWeCustomerMessagePushS
//保存分类消息信息
weCustomerSeedMessageService.saveSeedMessage(customerMessagePushDto, messageId);
log.info("消息发送信息:{}", JSONUtil.toJsonStr(customerMessagePushDto));
CustomerMessagePushDto customerMessagePushDto1 = JSONUtil.toBean(JSONUtil.parseObj(JSONUtil.toJsonStr(customerMessagePushDto)), CustomerMessagePushDto.class);
//发送群发消息
//调用微信api发送消息
if (null == customerMessagePushDto.getSettingTime() || customerMessagePushDto.getSettingTime().equals("")) {
weCustomerMessageService.sendMessgae(customerMessagePushDto, messageId,customers,groups);
} else {
//发送时间不能小于当前时间
//定时发送消息(异步执行)
//保存消息体到数据库,增加一个发送状态(0 已发送 1 未发送)
//定义一个任务执行队列
//把任务放入执行队列
//任务执行 sendMessgae(WeMessagePush weMessagePush) 方法
Timer timer = new Timer();
List<WeCustomer> finalCustomers = customers;
List<WeGroup> finalGroups = groups;
timer.schedule(new TimerTask() {
@SneakyThrows
@Override
public void run() {
weCustomerMessageService.sendMessgae(customerMessagePushDto, messageId, finalCustomers, finalGroups);
}
}, DateUtil.parse(customerMessagePushDto.getSettingTime(), "yyyy-MM-dd HH:mm:ss"));
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]", "")
,DateUtils.getMillionSceondsBydate(customerMessagePushDto.getSettingTime()));
customerMessageTimeTaskMapper.insert(timeTask);
}
......@@ -161,7 +159,7 @@ public class WeCustomerMessagePushServiceImpl implements IWeCustomerMessagePushS
* @return {@link List}s 客户的外部联系人id列表
*/
public List<WeCustomer> externalUserIds(String pushRange, String staffId, String department, String tag) {
if (pushRange.equals("0")) {
if (pushRange.equals(WeConstans.SEND_MESSAGE_CUSTOMER_ALL)) {
//从redis中读取数据
return redisCache.getCacheList(WeConstans.WECUSTOMERS_KEY);
} else {
......
......@@ -5,7 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.enums.ChatType;
import com.linkwechat.common.exception.wecom.WeComException;
import com.linkwechat.common.enums.GroupMessageType;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeCustomerMessagePushClient;
......@@ -31,6 +31,7 @@ import java.util.stream.Collectors;
* @author kewen
* @date 2020-12-12
*/
@SuppressWarnings("all")
@Service
public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageMapper, WeCustomerMessage> implements IWeCustomerMessageService {
......@@ -90,7 +91,7 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM
//发送群发消息
//发送类类型: 给单个客户发,群发
//发给客户
if (customerMessagePushDto.getPushType().equals("0")) {
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_CUSTOMER)) {
WeCustomerMessagePushDto messagePushDto = new WeCustomerMessagePushDto();
messagePushDto.setChat_type(ChatType.of(customerMessagePushDto.getPushType()).getName());
......@@ -106,7 +107,7 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM
}
//发给客户群
if (customerMessagePushDto.getPushType().equals("1")) {
if (customerMessagePushDto.getPushType().equals(WeConstans.SEND_MESSAGE_GROUP)) {
if (CollectionUtils.isNotEmpty(groups)) {
List<String> owners = groups.stream().map(WeGroup::getOwner).collect(Collectors.toList());
......@@ -140,21 +141,21 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM
public void childMessage(WeCustomerMessagePushDto weCustomerMessagePushDto, CustomerMessagePushDto customerMessagePushDto) {
// 消息类型 0 文本消息 1 图片消息 2 链接消息 3 小程序消息
if (customerMessagePushDto.getMessageType().equals("0")) {
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.TEXT.getType())) {
weCustomerMessagePushDto.setImage(null);
weCustomerMessagePushDto.setLink(null);
weCustomerMessagePushDto.setMiniprogram(null);
weCustomerMessagePushDto.setText(customerMessagePushDto.getTextMessage());
}
if (customerMessagePushDto.getMessageType().equals("1")) {
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.IMAGE.getMessageType())) {
weCustomerMessagePushDto.setImage(customerMessagePushDto.getImageMessage());
weCustomerMessagePushDto.setLink(null);
weCustomerMessagePushDto.setMiniprogram(null);
weCustomerMessagePushDto.setText(null);
}
if (customerMessagePushDto.getMessageType().equals("2")) {
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.LINK.getType())) {
weCustomerMessagePushDto.setImage(null);
weCustomerMessagePushDto.setLink(customerMessagePushDto.getLinkMessage());
weCustomerMessagePushDto.setMiniprogram(null);
......@@ -162,7 +163,7 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM
}
if (customerMessagePushDto.getMessageType().equals("3")) {
if (customerMessagePushDto.getMessageType().equals(GroupMessageType.MINIPROGRAM.getType())) {
weCustomerMessagePushDto.setImage(null);
weCustomerMessagePushDto.setLink(null);
weCustomerMessagePushDto.setMiniprogram(customerMessagePushDto.getMiniprogramMessage());
......@@ -170,7 +171,7 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM
}
if(customerMessagePushDto.getMessageType().equals("4")){
if(customerMessagePushDto.getMessageType().equals(GroupMessageType.TEXT_IMAGE.getType())){
weCustomerMessagePushDto.setImage(customerMessagePushDto.getImageMessage());
weCustomerMessagePushDto.setLink(null);
weCustomerMessagePushDto.setMiniprogram(null);
......@@ -179,5 +180,4 @@ public class WeCustomerMessageServiceImpl extends ServiceImpl<WeCustomerMessageM
}
}
......@@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
......
......@@ -21,5 +21,14 @@
message_id=#{messageId}
</where>
</update>
<update id="updateWeCustomerMessageCheckStatusById">
UPDATE we_customer_message
<set>
check_status=#{status}
</set>
<where>
message_id=#{messageId}
</where>
</update>
</mapper>
......@@ -17,7 +17,8 @@
wcm.expect_send,
wcm.actual_send,
wcm.timed_task,
wcm.msgid
wcm.msgid,
wcm.check_status
FROM
we_customer_messageOriginal wcmo
LEFT JOIN we_customer_message wcm ON wcmo.message_original_Id = wcm.original_id
......
<?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.linkwechat.wecom.mapper.WeCustomerMessageTimeTaskMapper">
</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!