Commit 9b4e10d9 by 阮思源

Merge branch 'zyj-1002083' into 'master'

个人号定时群发

See merge request rays/pcloud-book!275
parents aa2fb0ff af0a9b4c
package com.pcloud.book.push.constant;
/**
* @描述:常量
* @作者:zhuyajie
* @创建时间:10:43 2019/12/10
* @版本:1.0
*/
public class PushConstant {
/**
* 个人号定时群发-定时器类型名称
*/
public static final String JOB_GROUP_SELF_PUSH = "selfPush";
/**
* 个人号定时群发-定时器方法名
*/
public static final String JOB_NAME_SELF_PUSH = "judgeSelfPushTime";
}
package com.pcloud.book.push.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
* @描述:个人号群发
* @作者:zhuyajie
* @创建时间:10:44 2019/12/10
* @版本:1.0
*/
@FeignClient(value = "pcloud-service-book", qualifier = "selfPushServiceCloud", path = "book/v1.0/selfPushService")
@Api(description = "个人号群发消息内部服务")
public interface SelfPushService {
@ApiOperation(value = "定时器调用个人号群发接口", httpMethod = "POST")
@RequestMapping(value = "/sendSelfMsgQuartz", method = RequestMethod.POST)
public void sendSelfMsgQuartz(@RequestBody Map<String, Object> map);
}
......@@ -35,4 +35,10 @@ public interface SelfPushBiz {
* @return
*/
PageBeanNew<SelfPushRecordDTO> listSelfPushRecord(Integer currentPage, Integer numPerPage, String startTime, String endTime, Integer status);
/**
* 定时任务触发群发
* @param pushId
*/
void sendSelfMsgQuartz(Long pushId);
}
......@@ -6,6 +6,7 @@ import com.pcloud.book.group.entity.WeixinQrcodeGeneration;
import com.pcloud.book.group.set.GroupSet;
import com.pcloud.book.group.vo.UserBookInfoVO;
import com.pcloud.book.push.biz.SelfPushBiz;
import com.pcloud.book.push.constant.PushConstant;
import com.pcloud.book.push.dao.SelfPushDao;
import com.pcloud.book.push.dao.SelfPushItemDao;
import com.pcloud.book.push.dao.SelfPushUserDao;
......@@ -19,7 +20,11 @@ import com.pcloud.book.push.enums.PushStatusEnum;
import com.pcloud.book.util.common.ThreadPoolUtils;
import com.pcloud.common.page.PageBeanNew;
import com.pcloud.common.page.PageParam;
import com.pcloud.common.utils.DateUtils;
import com.pcloud.common.utils.ListUtils;
import com.pcloud.facade.quartz.entity.CallBackParam;
import com.pcloud.facade.quartz.entity.ScheduleJob;
import com.pcloud.facade.quartz.service.ScheduleService;
import com.pcloud.wechatgroup.group.dto.GroupRobotDTO;
import com.pcloud.wechatgroup.group.dto.GroupUserDTO;
import com.sdk.wxgroup.SendArticleMessageVO;
......@@ -33,19 +38,24 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
/**
* @描述:个人号群发
* @作者:zhuyajie
* @创建时间:11:00 2019/11/27
* @版本:1.0
*/
@Slf4j
@Component("selfPushBiz")
public class SelfPushBizImpl implements SelfPushBiz {
@Autowired
private SelfPushDao selfPushDao;
@Autowired
......@@ -58,28 +68,110 @@ public class SelfPushBizImpl implements SelfPushBiz {
private WechatGroupConsr wechatGroupConsr;
@Autowired
private WeixinQrcodeGenerationDao weixinQrcodeGenerationDao;
@Autowired
private ScheduleService scheduleService;
@Override
public void createSelfPush(Long partyId, SelfPushAddParamDTO addParamDTO) {
Boolean sendNow = addParamDTO.getSendNow();
//群发
SelfPush selfPush = new SelfPush();
selfPush.setCreateUser(partyId);
selfPush.setUpdateUser(partyId);
selfPush.setAltId(addParamDTO.altId);
selfPush.setAltId(addParamDTO.getAltId());
selfPush.setSendNow(sendNow);
if (sendNow) {
selfPush.setSendTime(new Date());
} else {
selfPush.setSendTime(DateUtils.getDateByStr(addParamDTO.getSendTime()));
}
selfPushDao.insert(selfPush);
Long pushId = selfPush.getId();
//群发项
List<SelfPushItem> itemList = addParamDTO.getPushItemList();
setSelfPushItem(pushId, sendNow, partyId, itemList, selfPush.getSendTime());
//群发用户
List<String> userIdList = addParamDTO.getUserIdList();
setSelfPushUser(userIdList, partyId, pushId);
if (sendNow) {
//微信立即发送
msgPush(userIdList, itemList, selfPush.getAltId(), pushId);
} else {
//定时任务
addSendQuartzJob(pushId, addParamDTO.getSendTime());
}
}
/**
* 添加定时任务
*/
private void addSendQuartzJob(Long pushId, String sendTime) {
ScheduleJob scheduleJob = new ScheduleJob();
CallBackParam callBackParam = new CallBackParam();
Map<String, Object> map = new HashMap<>();
map.put("pushId", pushId);
scheduleJob.setJobGroup(PushConstant.JOB_GROUP_SELF_PUSH);
scheduleJob.setJobName(PushConstant.JOB_NAME_SELF_PUSH + pushId);
Long second = (DateUtils.StringToDateTime(sendTime).getTime() - System.currentTimeMillis()) / 1000;
scheduleJob.setStartTime(second.intValue());
scheduleJob.setStartTimeFormat("ss");
scheduleJob.setRepeatCount(0);
scheduleJob.setIntervalTime(0);
scheduleJob.setIntervalTimeFormat("dd");
callBackParam.setBeanName("selfPushService");
callBackParam.setMethodName("sendSelfMsgQuartz");
callBackParam.setParamMap(map);
Map<String, Object> scheduleMap = new HashMap<>();
scheduleMap.put("scheduleJob", scheduleJob);
scheduleMap.put("callBackParam", callBackParam);
try {
scheduleService.addSimpleJob(scheduleMap);
} catch (Exception e) {
log.error("【个人号群发】添加定时任务失败" + e.getMessage(), e);
}
}
/**
* 微信群消息推送
*/
private void msgPush(List<String> userIdList, List<SelfPushItem> itemList, String altId, Long pushId) {
ThreadPoolUtils.SEND_MESSAGE_THREAD_POOL.execute(() -> {
for (String userId : userIdList) {
for (SelfPushItem pushItem : itemList) {
sendSelfMsg(userId, altId, pushItem);
}
}
selfPushItemDao.updatePushStatusByPushId(PushStatusEnum.SUCCESS.value, pushId);
});
}
/**
* 群发项设置
*/
private void setSelfPushItem(Long pushId, Boolean sendNow, Long partyId, List<SelfPushItem> itemList, Date sendTime) {
Integer status;
if (sendNow) {
status = PushStatusEnum.PUSHING.value;
} else {
status = PushStatusEnum.WAIT.value;
}
itemList.forEach(selfPushItem -> {
selfPushItem.setCreateUser(partyId);
selfPushItem.setUpdateUser(partyId);
selfPushItem.setPushId(pushId);
selfPushItem.setPushStatus(PushStatusEnum.PUSHING.value);
selfPushItem.setPushStatus(status);
selfPushItem.setSendTime(sendTime);
});
selfPushItemDao.insert(itemList);
//群发用户
}
/**
* 群发用户设置
*/
private void setSelfPushUser(List<String> userIdList, Long partyId, Long pushId) {
List<SelfPushUser> userList = new ArrayList<>();
for (String userId : addParamDTO.getUserIdList()) {
for (String userId : userIdList) {
SelfPushUser user = new SelfPushUser();
user.setWxUserId(userId);
userList.add(user);
......@@ -89,15 +181,6 @@ public class SelfPushBizImpl implements SelfPushBiz {
selfPushUser.setPushId(pushId);
});
selfPushUserDao.insert(userList);
//微信发送
ThreadPoolUtils.SEND_MESSAGE_THREAD_POOL.execute(() -> {
userList.forEach(selfPushUser -> {
for (SelfPushItem pushItem : itemList) {
sendSelfMsg(selfPushUser.getWxUserId(), selfPush.getAltId(), pushItem);
}
});
selfPushItemDao.updatePushStatusByPushId(PushStatusEnum.SUCCESS.value, pushId);
});
}
/**
......@@ -197,4 +280,19 @@ public class SelfPushBizImpl implements SelfPushBiz {
}
return pageBeanNew;
}
@Override
public void sendSelfMsgQuartz(Long pushId) {
SelfPush selfPush = selfPushDao.getById(pushId);
if (null == selfPush) {
return;
}
List<String> userIdList = selfPushUserDao.getUserIdListByPushId(pushId);
List<SelfPushItem> itemList = selfPushItemDao.getByPushId(pushId);
if (ListUtils.isEmpty(userIdList) || ListUtils.isEmpty(itemList)) {
return;
}
selfPushItemDao.updatePushStatusByPushId(PushStatusEnum.PUSHING.value, pushId);
msgPush(userIdList, itemList, selfPush.getAltId(), pushId);
}
}
......@@ -289,6 +289,14 @@ public class PushCheck {
if (null == addParamDTO) {
throw new BookBizException(BookBizException.PARAM_IS_NULL, "参数为空");
}
if (null == addParamDTO.getSendNow()) {
throw new BookBizException(BookBizException.PARAM_IS_NULL, "缺少立即发送字段");
}
if (!addParamDTO.getSendNow()) {
if (StringUtil.isEmpty(addParamDTO.getSendTime()) || DateUtils.getDateByStr(addParamDTO.getSendTime()).before(new Date())) {
throw new BookBizException(BookBizException.PARAM_IS_NULL, "自定义发送时间设置有误");
}
}
if (StringUtil.isEmpty(addParamDTO.getAltId()) || ListUtils.isEmpty(addParamDTO.getUserIdList())) {
throw new BookBizException(BookBizException.PARAM_IS_NULL, "个人号或群发好友为空");
}
......
......@@ -3,6 +3,8 @@ package com.pcloud.book.push.dao;
import com.pcloud.book.push.entity.SelfPushItem;
import com.pcloud.common.core.dao.BaseDao;
import java.util.List;
/**
* @描述:个人号群发项
* @作者:zhuyajie
......@@ -17,4 +19,11 @@ public interface SelfPushItemDao extends BaseDao<SelfPushItem>{
* @param pushId
*/
public void updatePushStatusByPushId(Integer pushStatus, Long pushId);
/**
* 查群发项列表
* @param pushId
* @return
*/
List<SelfPushItem> getByPushId(Long pushId);
}
......@@ -3,6 +3,8 @@ package com.pcloud.book.push.dao;
import com.pcloud.book.push.entity.SelfPushUser;
import com.pcloud.common.core.dao.BaseDao;
import java.util.List;
/**
* @描述:
* @作者:zhuyajie
......@@ -17,4 +19,11 @@ public interface SelfPushUserDao extends BaseDao<SelfPushUser>{
* @return
*/
Integer getCountByPushId(Long pushId);
/**
* 查用户id列表
* @param pushId
* @return
*/
List<String> getUserIdListByPushId(Long pushId);
}
......@@ -7,6 +7,7 @@ import com.pcloud.common.core.dao.BaseDaoImpl;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
......@@ -25,4 +26,9 @@ public class SelfPushItemDaoImpl extends BaseDaoImpl<SelfPushItem> implements Se
map.put("pushId", pushId);
getSessionTemplate().update(getStatement("updatePushStatusByPushId"), map);
}
@Override
public List<SelfPushItem> getByPushId(Long pushId) {
return getSessionTemplate().selectList(getStatement("getByPushId"), pushId);
}
}
......@@ -6,6 +6,8 @@ import com.pcloud.common.core.dao.BaseDaoImpl;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @描述:
* @作者:zhuyajie
......@@ -19,4 +21,9 @@ public class SelfPushUserDaoImpl extends BaseDaoImpl<SelfPushUser>implements Sel
public Integer getCountByPushId(Long pushId) {
return getSessionTemplate().selectOne(getStatement("getCountByPushId"), pushId);
}
@Override
public List<String> getUserIdListByPushId(Long pushId) {
return getSessionTemplate().selectList(getStatement("getUserIdListByPushId"), pushId);
}
}
......@@ -30,4 +30,12 @@ public class SelfPushAddParamDTO {
* 推送信息
*/
public List<SelfPushItem> pushItemList;
/**
* 立即发送
*/
private Boolean sendNow;
/**
* 发送时间
*/
private String sendTime;
}
......@@ -72,4 +72,8 @@ public class SelfPushRecordDTO extends BaseDto{
@ApiModelProperty("发送好友数量")
private Integer userCount;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("发送时间")
private Date sendTime;
}
......@@ -30,5 +30,10 @@ public class SelfPush extends BaseEntity{
@ApiModelProperty("修改时间")
private Date updateTime;
@ApiModelProperty("立即发送")
private Boolean sendNow;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("发送时间")
private Date sendTime;
}
\ No newline at end of file
......@@ -66,5 +66,7 @@ public class SelfPushItem extends BaseEntity{
@ApiModelProperty("修改时间")
private Date updateTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@ApiModelProperty("发送时间")
private Date sendTime;
}
\ No newline at end of file
......@@ -6,7 +6,10 @@ package com.pcloud.book.push.enums;
* @Date 2019/4/17 15:41
*/
public enum PushStatusEnum {
/**
* 待发送
*/
WAIT(0),
/**
* 发送中
*/
......
package com.pcloud.book.push.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.pcloud.book.push.biz.SelfPushBiz;
import com.pcloud.book.push.service.SelfPushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @描述:个人号群发内部接口
* @作者:zhuyajie
* @创建时间:10:48 2019/12/10
* @版本:1.0
*/
@RequestMapping("selfPushService")
@RestController("selfPushService")
public class SelfPushServiceImpl implements SelfPushService {
@Autowired
private SelfPushBiz selfPushBiz;
@Override
@RequestMapping(value = "/sendSelfMsgQuartz", method = RequestMethod.POST)
public void sendSelfMsgQuartz(@RequestBody Map<String, Object> map) {
Long pushId = JSON.parseObject(JSON.toJSONString(map.get("pushId")), new TypeReference<Long>() {});
if (null ==pushId){
return;
}
selfPushBiz.sendSelfMsgQuartz(pushId);
}
}
......@@ -20,12 +20,13 @@
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="update_user" property="updateUser" jdbcType="BIGINT" />
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
<result column="send_time" property="sendTime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
id, push_id, item_type, text_content, link_url, link_title, link_description,
link_image_url, image_url, app_id, app_url, product_id, product_url, push_status,
create_user, create_time, update_user, update_time
create_user, create_time, update_user, update_time, send_time
</sql>
<insert id="insert" parameterType="com.pcloud.book.push.entity.SelfPushItem" useGeneratedKeys="true" keyProperty="id">
......@@ -34,14 +35,14 @@
link_description, link_image_url, image_url,
app_id, app_url, product_id,
product_url, push_status, create_user,
create_time, update_user, update_time
create_time, update_user, update_time, send_time
)
values (#{pushId,jdbcType=BIGINT}, #{itemType,jdbcType=INTEGER},
#{textContent,jdbcType=VARCHAR}, #{linkUrl,jdbcType=VARCHAR}, #{linkTitle,jdbcType=VARCHAR},
#{linkDescription,jdbcType=VARCHAR}, #{linkImageUrl,jdbcType=VARCHAR}, #{imageUrl,jdbcType=VARCHAR},
#{appId,jdbcType=BIGINT}, #{appUrl,jdbcType=VARCHAR}, #{productId,jdbcType=BIGINT},
#{productUrl,jdbcType=VARCHAR}, #{pushStatus,jdbcType=INTEGER}, #{createUser,jdbcType=BIGINT},
now(), #{updateUser,jdbcType=BIGINT}, now()
now(), #{updateUser,jdbcType=BIGINT}, now(), #{sendTime}
)
</insert>
......@@ -64,7 +65,8 @@
create_user,
create_time,
update_user,
update_time
update_time,
send_time
) values
<foreach collection="list" item="item" index="index" separator=",">
(
......@@ -84,7 +86,8 @@
#{item.createUser,jdbcType=BIGINT},
NOW(),
#{item.updateUser,jdbcType=BIGINT},
NOW()
NOW(),
#{item.sendTime}
)
</foreach>
</insert>
......@@ -107,14 +110,15 @@
i.product_id productId,
i.product_url productUrl,
i.push_status pushStatus,
i.create_time createTime
i.create_time createTime,
i.send_time sendTime
FROM
self_push_item i
LEFT JOIN self_push p ON i.push_id = p.id
WHERE
1 = 1
<if test="startTime != null and endTime != null">
AND i.create_time BETWEEN #{startTime} AND #{endTime}
AND i.send_time BETWEEN #{startTime} AND #{endTime}
</if>
<if test="pushStatus != null">
AND i.push_status = #{pushStatus}
......@@ -129,4 +133,10 @@
WHERE push_id = #{pushId}
</update>
<select id="getByPushId" parameterType="long" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM self_push_item
WHERE push_id = #{pushId}
</select>
</mapper>
\ No newline at end of file
......@@ -8,21 +8,29 @@
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="update_user" property="updateUser" jdbcType="BIGINT" />
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
<result column="send_now" property="sendNow" jdbcType="BIT" />
<result column="send_time" property="sendTime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
id, alt_id, create_user, create_time, update_user, update_time
id, alt_id, create_user, create_time, update_user, update_time, send_now, send_time
</sql>
<insert id="insert" parameterType="com.pcloud.book.push.entity.SelfPush" useGeneratedKeys="true" keyProperty="id">
insert into self_push (id, alt_id, create_user,
create_time, update_user, update_time
create_time, update_user, update_time, send_now, send_time
)
values (#{id,jdbcType=BIGINT}, #{altId,jdbcType=VARCHAR}, #{createUser,jdbcType=BIGINT},
now(), #{updateUser,jdbcType=BIGINT}, now()
now(), #{updateUser,jdbcType=BIGINT}, now(), #{sendNow}, #{sendTime}
)
</insert>
<select id="getById" parameterType="long" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM self_push
WHERE id= #{id}
</select>
</mapper>
\ No newline at end of file
......@@ -57,5 +57,13 @@
push_id = #{pushId}
</select>
<select id="getUserIdListByPushId" parameterType="long" resultType="string">
SELECT
DISTINCT wx_user_id
FROM
self_push_user
WHERE
push_id = #{pushId}
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment