Commit b9300210 by 郑永强

增加早晚报推送规则逻辑

parent c5e514b4
...@@ -14,9 +14,11 @@ import com.pcloud.book.base.exception.BookBizException; ...@@ -14,9 +14,11 @@ import com.pcloud.book.base.exception.BookBizException;
import com.pcloud.book.consumer.wechatgroup.WechatGroupConsr; import com.pcloud.book.consumer.wechatgroup.WechatGroupConsr;
import com.pcloud.book.group.biz.WeixinQrcodeBiz; import com.pcloud.book.group.biz.WeixinQrcodeBiz;
import com.pcloud.book.group.dao.GroupQrcodeDao; import com.pcloud.book.group.dao.GroupQrcodeDao;
import com.pcloud.book.group.dto.BookGroupDTO;
import com.pcloud.book.group.dto.BookWxQrcodeDTO; import com.pcloud.book.group.dto.BookWxQrcodeDTO;
import com.pcloud.book.group.dto.GroupQrcodeFoAdDTO; import com.pcloud.book.group.dto.GroupQrcodeFoAdDTO;
import com.pcloud.common.core.aspect.ParamLog; import com.pcloud.common.core.aspect.ParamLog;
import com.pcloud.common.exceptions.BizException;
import com.pcloud.common.page.PageBeanNew; import com.pcloud.common.page.PageBeanNew;
import com.pcloud.common.page.PageParam; import com.pcloud.common.page.PageParam;
import com.pcloud.common.utils.DateUtils; import com.pcloud.common.utils.DateUtils;
...@@ -237,30 +239,16 @@ public class AdNewsBizImpl implements AdNewsBiz { ...@@ -237,30 +239,16 @@ public class AdNewsBizImpl implements AdNewsBiz {
AdNewsMessageStatistic adNewsMessageStatistic; AdNewsMessageStatistic adNewsMessageStatistic;
if (adNewsSet.getContentFrom() == AdContentFromEnum.WECHAT.key) { if (adNewsSet.getContentFrom() == AdContentFromEnum.WECHAT.key) {
// 填充要发送的早晚报
setPushAdNewsList(adNewsSet,groupQrcodeFoAdDTOS);
// 查询要发的记录 // 构建早晚报发送记录对象
List<AdNews> adNewsList = null;
adNewsList = adNewsDao.getNewsToSendBySetIdAndAdviser4Own(adNewsSetId, adNewsSet.getCreateUser(), adNewsSet.getSendCount());
// 如果自有公众号的数据为空,则查询系统推荐公众号数据
if (ListUtils.isEmpty(adNewsList)) {
adNewsList = adNewsDao.getNewsToSendBySetIdAndAdviser(adNewsSetId, adNewsSet.getCreateUser(), adNewsSet.getSendCount());
}else if (adNewsList.size() < adNewsSet.getSendCount()) {
// 如果自有公众号的数据条数少于要发送的条数,则用系统推荐公众号的补齐剩余的条数
List<AdNews> _adNewsList = adNewsDao.getNewsToSendBySetIdAndAdviser(adNewsSetId, adNewsSet.getCreateUser(), adNewsSet.getSendCount()-adNewsList.size());
if(!ListUtils.isEmpty(_adNewsList)){
adNewsList.addAll(_adNewsList);
}
}
if (ListUtils.isEmpty(adNewsList)) {
LOGGER.info("记录为空,没有可供发送的早晚报!");
return;
}
// 构建记录对象
for (GroupQrcodeFoAdDTO groupQrcodeFoAdDTO : groupQrcodeFoAdDTOS) { for (GroupQrcodeFoAdDTO groupQrcodeFoAdDTO : groupQrcodeFoAdDTOS) {
for (AdNews adNews : adNewsList) { if (ListUtils.isEmpty(groupQrcodeFoAdDTO.getAdNewsList())) {
LOGGER.info("BookGroupId:"+ groupQrcodeFoAdDTO.getBookGroupId() + " 没有可供发送的早晚报!");
continue;
}
for (AdNews adNews : groupQrcodeFoAdDTO.getAdNewsList()) {
adNewsGroupRecord = new AdNewsGroupRecord(); adNewsGroupRecord = new AdNewsGroupRecord();
adNewsGroupRecord.setAdNewsId(adNews.getId()); adNewsGroupRecord.setAdNewsId(adNews.getId());
adNewsGroupRecord.setAdNewsSetId(adNewsSetId); adNewsGroupRecord.setAdNewsSetId(adNewsSetId);
...@@ -280,14 +268,15 @@ public class AdNewsBizImpl implements AdNewsBiz { ...@@ -280,14 +268,15 @@ public class AdNewsBizImpl implements AdNewsBiz {
adNewsMessageStatistic.setClassifyId(groupQrcodeFoAdDTO.getClassifyId()); adNewsMessageStatistic.setClassifyId(groupQrcodeFoAdDTO.getClassifyId());
adNewsMessageStatistic.setQrcodeId(groupQrcodeFoAdDTO.getQrcodeId()); adNewsMessageStatistic.setQrcodeId(groupQrcodeFoAdDTO.getQrcodeId());
adNewsMessageStatistic.setCreateUser(adNewsSet.getCreateUser()); adNewsMessageStatistic.setCreateUser(adNewsSet.getCreateUser());
adNewsMessageStatistic.setMessageCount(adNewsList.size()); adNewsMessageStatistic.setMessageCount(groupQrcodeFoAdDTO.getAdNewsList().size());
adNewsMessageStatistic.setInsertMark(groupQrcodeFoAdDTOS.indexOf(groupQrcodeFoAdDTO)); adNewsMessageStatistic.setInsertMark(groupQrcodeFoAdDTOS.indexOf(groupQrcodeFoAdDTO));
adNewsMessageStatistics.add(adNewsMessageStatistic); adNewsMessageStatistics.add(adNewsMessageStatistic);
} }
// 因为拼短链接需要主键,所以需要先入库拿到主键后然后拼接Url,最后再发送微信消息; // 因为拼短链接需要主键,所以需要先入库拿到主键后然后拼接Url,最后再发送微信消息;
// 记录入库 // 记录入库
this.insertRecord(adNewsGroupRecords, adNewsMessageStatistics); int flag = this.insertRecord(adNewsGroupRecords, adNewsMessageStatistics);
if(flag == 0){ return; }
// 拼接中转链接 // 拼接中转链接
String transferUrl; String transferUrl;
...@@ -299,7 +288,7 @@ public class AdNewsBizImpl implements AdNewsBiz { ...@@ -299,7 +288,7 @@ public class AdNewsBizImpl implements AdNewsBiz {
// 获取对应当前群的记录详情 // 获取对应当前群的记录详情
if (groupQrcodeFoAdDTO.getQrcodeId() == item.getQrcodeId()) { if (groupQrcodeFoAdDTO.getQrcodeId() == item.getQrcodeId()) {
// 将对应当前群里的需要发送的新闻链接拼接完成 // 将对应当前群里的需要发送的新闻链接拼接完成
adNews = adNewsList.stream().filter(x -> x.getId() == item.getAdNewsId()).findFirst().get(); adNews = groupQrcodeFoAdDTO.getAdNewsList().stream().filter(x -> x.getId() == item.getAdNewsId()).findFirst().get();
transferUrl = wechatGroupLinkPrefix transferUrl = wechatGroupLinkPrefix
.concat("/link") .concat("/link")
.concat("?message_statistic_id=").concat(item.getMessageStatisticId().toString()) .concat("?message_statistic_id=").concat(item.getMessageStatisticId().toString())
...@@ -309,7 +298,7 @@ public class AdNewsBizImpl implements AdNewsBiz { ...@@ -309,7 +298,7 @@ public class AdNewsBizImpl implements AdNewsBiz {
} }
} }
// 将多条需要发送的早晚报拼接成300字符一组的消息 // 将多条需要发送的早晚报拼接成300字符一组的消息
contents = this.concatContent(adNewsList); contents = this.concatContent(groupQrcodeFoAdDTO.getAdNewsList());
// 将对应群的消息发送出去 // 将对应群的消息发送出去
this.sendAdNewsToWechatGroup(adNewsSet, new ArrayList<GroupQrcodeFoAdDTO>() {{ this.sendAdNewsToWechatGroup(adNewsSet, new ArrayList<GroupQrcodeFoAdDTO>() {{
add(groupQrcodeFoAdDTO); add(groupQrcodeFoAdDTO);
...@@ -359,19 +348,117 @@ public class AdNewsBizImpl implements AdNewsBiz { ...@@ -359,19 +348,117 @@ public class AdNewsBizImpl implements AdNewsBiz {
} }
} }
// 设置群标签名称
private void setGroupLabelName(List<GroupQrcodeFoAdDTO> groupQrcodeFoAdDTOS) {
List<Long> labelIds = new ArrayList<>();
for (GroupQrcodeFoAdDTO item : groupQrcodeFoAdDTOS) {
if (item.getProLabelId() != null) {
labelIds.add(item.getProLabelId());
}
if (item.getDepLabelId() != null) {
labelIds.add(item.getDepLabelId());
}
if (item.getPurLabelId() != null) {
labelIds.add(item.getPurLabelId());
}
}
Map<Long, String> labelMap = new HashMap<>();
if (!ListUtils.isEmpty(labelIds)) {
labelMap = ResponseHandleUtil.parseMap(labelService.getLabelName(labelIds), Long.class, String.class);
}
if (!MapUtils.isEmpty(labelMap)) {
for (GroupQrcodeFoAdDTO item : groupQrcodeFoAdDTOS) {
if (item.getProLabelId() != null) {
item.setProLabelName(labelMap.get(item.getProLabelId()));
}
if (item.getDepLabelId() != null) {
item.setDepLabelName(labelMap.get(item.getDepLabelId()));
}
if (item.getPurLabelId() != null) {
item.setPurLabelName(labelMap.get(item.getPurLabelId()));
}
}
}
}
/**
* 根据规则获取推送的新闻
*
* 规则:优先推送自有公众号的内容,如果自有公众号的内容不够,再从公共库里进行挑选;
* 公共库需要用群的标签匹配新闻的标题,如果匹配不到再进行公众号的名称匹配,如果还是没有,最后则直接推送最新入库的数据
*
* @param adNewsSet
* @param groupQrcodeFoAdDTOS
* @return
*/
private void setPushAdNewsList(AdNewsSet adNewsSet,List<GroupQrcodeFoAdDTO> groupQrcodeFoAdDTOS) {
// 获取自有公众号的新闻
List<AdNews> adNewsList = adNewsDao.getNewsToSendBySetIdAndAdviser4Own(adNewsSet.getId(), adNewsSet.getCreateUser(), adNewsSet.getSendCount());
if(ListUtils.isEmpty(adNewsList)){
adNewsList = new ArrayList<>();
}
// 初始化 groupQrcodeFoAdDTOS.adNewsList
for (GroupQrcodeFoAdDTO groupQrcodeFoAdDTO : groupQrcodeFoAdDTOS)
{
groupQrcodeFoAdDTO.setAdNewsList(adNewsList);
}
List<AdNews> _adNewsList = new ArrayList<>();
// 如果自有公众号的数据条数少于要发送的条数,则用系统推荐公众号的补齐剩余的条数
if(adNewsList.size() < adNewsSet.getSendCount()){
// 根据群批量查找社群码的标签
setGroupLabelName(groupQrcodeFoAdDTOS);
// 标签去重进行匹配新闻
List<Long> bookGroupIds = groupQrcodeFoAdDTOS.stream().map(x -> x.getBookGroupId()).distinct().collect(Collectors.toList());
// 三天前
String beginTime = DateUtils.formatDate(DateUtils.subDays(3), DateUtils.DATE_FORMAT_DATEONLY);
for (GroupQrcodeFoAdDTO groupQrcodeFoAdDTO : groupQrcodeFoAdDTOS){
_adNewsList = adNewsDao.getNewsByLabelName(adNewsSet.getId(), adNewsSet.getCreateUser(), beginTime, groupQrcodeFoAdDTO.getProLabelName(),groupQrcodeFoAdDTO.getDepLabelName(),groupQrcodeFoAdDTO.getPurLabelName(),adNewsSet.getSendCount() - groupQrcodeFoAdDTO.getAdNewsList().size());
if(!ListUtils.isEmpty(_adNewsList)){
groupQrcodeFoAdDTO.getAdNewsList().addAll(_adNewsList);
}
// 如果筛选新闻条数还不够,继续再公众号匹配
if(groupQrcodeFoAdDTO.getAdNewsList().size() < adNewsSet.getSendCount()) {
_adNewsList = adNewsDao.getNews4WechatByLabelName(adNewsSet.getId(), adNewsSet.getCreateUser(), beginTime, groupQrcodeFoAdDTO.getProLabelName(),groupQrcodeFoAdDTO.getDepLabelName(),groupQrcodeFoAdDTO.getPurLabelName(),adNewsSet.getSendCount() - groupQrcodeFoAdDTO.getAdNewsList().size());
if(!ListUtils.isEmpty(_adNewsList)){
groupQrcodeFoAdDTO.getAdNewsList().addAll(_adNewsList);
}
}
// 继续筛选新闻条数不够的群,根据时间排序补齐剩余
if(groupQrcodeFoAdDTO.getAdNewsList().size() < adNewsSet.getSendCount()) {
_adNewsList = adNewsDao.getNewsToSendBySetIdAndAdviser(adNewsSet.getId(), adNewsSet.getCreateUser(), adNewsSet.getSendCount() - groupQrcodeFoAdDTO.getAdNewsList().size());
if (!ListUtils.isEmpty(_adNewsList)) {
groupQrcodeFoAdDTO.getAdNewsList().addAll(_adNewsList);
}
}
}
}
}
/** /**
* 将发送记录入库 * 将发送记录入库
* *
* @param adNewsGroupRecords * @param adNewsGroupRecords
* @param adNewsMessageStatistics * @param adNewsMessageStatistics
*/ */
private void insertRecord(List<AdNewsGroupRecord> adNewsGroupRecords, List<AdNewsMessageStatistic> adNewsMessageStatistics) { private int insertRecord(List<AdNewsGroupRecord> adNewsGroupRecords, List<AdNewsMessageStatistic> adNewsMessageStatistics) {
if(ListUtils.isEmpty(adNewsGroupRecords) || ListUtils.isEmpty(adNewsMessageStatistics)){
LOGGER.info("没有可入库的数据,没有可供发送的早晚报内容");
return 0;
}
adNewsMessageStatisticDao.batchInsert(adNewsMessageStatistics); adNewsMessageStatisticDao.batchInsert(adNewsMessageStatistics);
// 将主键赋值给从表外键;(不会mybatis的主从表批量插入,所以只能这么做) // 将主键赋值给从表外键;(不会mybatis的主从表批量插入,所以只能这么做)
for (AdNewsMessageStatistic item : adNewsMessageStatistics) { for (AdNewsMessageStatistic item : adNewsMessageStatistics) {
adNewsGroupRecords.stream().filter(x -> x.getInsertMark() == item.getInsertMark()).forEach(x -> x.setMessageStatisticId(item.getId())); adNewsGroupRecords.stream().filter(x -> x.getInsertMark() == item.getInsertMark()).forEach(x -> x.setMessageStatisticId(item.getId()));
} }
adNewsGroupRecordDao.batchInsert(adNewsGroupRecords); adNewsGroupRecordDao.batchInsert(adNewsGroupRecords);
return 1;
} }
/** /**
......
...@@ -17,4 +17,30 @@ public interface AdNewsDao extends BaseDao<AdNews> { ...@@ -17,4 +17,30 @@ public interface AdNewsDao extends BaseDao<AdNews> {
* @return * @return
*/ */
List<AdNews> getNewsToSendBySetIdAndAdviser4Own(Long adNewsSetId, Long adviserId, Integer top); List<AdNews> getNewsToSendBySetIdAndAdviser4Own(Long adNewsSetId, Long adviserId, Integer top);
/**
* 根据标签匹配新闻————新闻标题
* @param adNewsSetId
* @param adviserId
* @param beginTime
* @param proLabelName
* @param depLabelName
* @param purLabelName
* @param top
* @return
*/
List<AdNews> getNewsByLabelName(Long adNewsSetId, Long adviserId, String beginTime, String proLabelName, String depLabelName, String purLabelName, Integer top);
/**
* 根据标签匹配新闻————公众号名称
* @param adNewsSetId
* @param adviserId
* @param beginTime
* @param proLabelName
* @param depLabelName
* @param purLabelName
* @param top
* @return
*/
List<AdNews> getNews4WechatByLabelName(Long adNewsSetId, Long adviserId, String beginTime, String proLabelName, String depLabelName, String purLabelName, Integer top);
} }
...@@ -33,4 +33,30 @@ public class AdNewsDaoImpl extends BaseDaoImpl<AdNews> implements AdNewsDao { ...@@ -33,4 +33,30 @@ public class AdNewsDaoImpl extends BaseDaoImpl<AdNews> implements AdNewsDao {
map.put("top", top); map.put("top", top);
return super.getSqlSession().selectList(getStatement("getNewsToSendBySetIdAndAdviser4Own"), map); return super.getSqlSession().selectList(getStatement("getNewsToSendBySetIdAndAdviser4Own"), map);
} }
@Override
public List<AdNews> getNewsByLabelName(Long adNewsSetId, Long adviserId, String beginTime, String proLabelName, String depLabelName, String purLabelName, Integer top) {
Map<String, Object> map = new HashMap<>();
map.put("adNewsSetId", adNewsSetId);
map.put("adviserId", adviserId);
map.put("beginTime", beginTime);
map.put("proLabelName", proLabelName);
map.put("depLabelName", depLabelName);
map.put("purLabelName", purLabelName);
map.put("top", top);
return super.getSqlSession().selectList(getStatement("getNewsByLabelName"), map);
}
@Override
public List<AdNews> getNews4WechatByLabelName(Long adNewsSetId, Long adviserId,String beginTime, String proLabelName, String depLabelName, String purLabelName, Integer top) {
Map<String, Object> map = new HashMap<>();
map.put("adNewsSetId", adNewsSetId);
map.put("adviserId", adviserId);
map.put("beginTime", beginTime);
map.put("proLabelName", proLabelName);
map.put("depLabelName", depLabelName);
map.put("purLabelName", purLabelName);
map.put("top", top);
return super.getSqlSession().selectList(getStatement("getNews4WechatByLabelName"), map);
}
} }
package com.pcloud.book.group.dto; package com.pcloud.book.group.dto;
import com.pcloud.book.adnews.entity.AdNews;
import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
/** /**
* @Description * @Description
* @Author ruansiyuan * @Author ruansiyuan
* @Date 2019/7/18 15:09 * @Date 2019/7/18 15:09
**/ **/
@Data
public class GroupQrcodeFoAdDTO implements Serializable { public class GroupQrcodeFoAdDTO implements Serializable {
private static final long serialVersionUID = 7018070035247234809L; private static final long serialVersionUID = 7018070035247234809L;
...@@ -18,45 +23,25 @@ public class GroupQrcodeFoAdDTO implements Serializable { ...@@ -18,45 +23,25 @@ public class GroupQrcodeFoAdDTO implements Serializable {
private String weixinGroupId; private String weixinGroupId;
public Long getQrcodeId() { // 专业id
return qrcodeId; private Long proLabelId;
}
// 深度id
public void setQrcodeId(Long qrcodeId) { private Long depLabelId;
this.qrcodeId = qrcodeId;
} // 目的id
private Long purLabelId;
public Long getClassifyId() {
return classifyId; // 专业
} private String proLabelName;
public void setClassifyId(Long classifyId) { // 深度
this.classifyId = classifyId; private String depLabelName;
}
// 目的
public Long getBookGroupId() { private String purLabelName;
return bookGroupId;
} // 需要发送的早晚报
private List<AdNews> adNewsList;
public void setBookGroupId(Long bookGroupId) {
this.bookGroupId = bookGroupId;
}
public String getWeixinGroupId() {
return weixinGroupId;
}
public void setWeixinGroupId(String weixinGroupId) {
this.weixinGroupId = weixinGroupId;
}
@Override
public String toString() {
return "GroupQrcodeFoAdDTO{" +
"qrcodeId=" + qrcodeId +
", classifyId=" + classifyId +
", bookGroupId=" + bookGroupId +
", weixinGroupId='" + weixinGroupId + '\'' +
'}';
}
} }
...@@ -143,6 +143,54 @@ ...@@ -143,6 +143,54 @@
LIMIT #{top} LIMIT #{top}
</select> </select>
<!--查找要发送的新闻(新闻标题匹配标签)-->
<!--
1、ad_news_ban 排除掉禁止发送的
2、ad_news_group_record 排除掉已发送的
-->
<select id="getNewsByLabelName" parameterType="map" resultMap="BaseResultMap">
SELECT
a.id, a.type, a.news_from, a.title, a.publisher, a.url, a.short_url, a.news_date, a.create_time,0 content_from
FROM
ad_news a
INNER JOIN ad_news_wechat b ON a.news_from = b.news_from AND b.is_delete = 0
INNER JOIN ad_news_wechat_choose c ON c.ad_news_wechat_id = b.id AND c.is_delete = 0 AND c.adviser_id = #{adviserId}
WHERE
NOT EXISTS ( SELECT 1 FROM ad_news_ban e WHERE a.id = e.ad_news_id AND e.adviser_id = c.adviser_id )
AND NOT EXISTS (SELECT 1 from ad_news_group_record f WHERE f.ad_news_id=a.id and f.ad_news_set_id=#{adNewsSetId})
AND a.create_time >= #{beginTime}
AND (a.title LIKE CONCAT('%', #{proLabelName},'%')
OR a.title LIKE CONCAT('%', #{depLabelName},'%')
OR a.title LIKE CONCAT('%', #{purLabelName},'%'))
ORDER BY
a.id DESC
LIMIT #{top}
</select>
<!--查找要发送的新闻(公众号名称匹配标签)-->
<!--
1、ad_news_ban 排除掉禁止发送的
2、ad_news_group_record 排除掉已发送的
-->
<select id="getNews4WechatByLabelName" parameterType="map" resultMap="BaseResultMap">
SELECT
a.id, a.type, a.news_from, a.title, a.publisher, a.url, a.short_url, a.news_date, a.create_time,0 content_from
FROM
ad_news a
INNER JOIN ad_news_wechat b ON a.news_from = b.news_from AND b.is_delete = 0
INNER JOIN ad_news_wechat_choose c ON c.ad_news_wechat_id = b.id AND c.is_delete = 0 AND c.adviser_id = #{adviserId}
WHERE
NOT EXISTS ( SELECT 1 FROM ad_news_ban e WHERE a.id = e.ad_news_id AND e.adviser_id = c.adviser_id )
AND NOT EXISTS (SELECT 1 from ad_news_group_record f WHERE f.ad_news_id=a.id and f.ad_news_set_id=#{adNewsSetId})
AND a.create_time >= #{beginTime}
AND (b.wechat_name LIKE CONCAT('%', #{proLabelName},'%')
OR b.wechat_name LIKE CONCAT('%', #{depLabelName},'%')
OR b.wechat_name LIKE CONCAT('%', #{purLabelName},'%'))
ORDER BY
a.id DESC
LIMIT #{top}
</select>
<!--获取素材库列表--> <!--获取素材库列表-->
<select id="getAdNewsList" parameterType="map" resultType="com.pcloud.book.adnews.entity.AdNews"> <select id="getAdNewsList" parameterType="map" resultType="com.pcloud.book.adnews.entity.AdNews">
SELECT SELECT
......
...@@ -537,13 +537,18 @@ ...@@ -537,13 +537,18 @@
t.id qrcodeId, t.id qrcodeId,
t.weixin_group_id weixinGroupId, t.weixin_group_id weixinGroupId,
t1.id classifyId, t1.id classifyId,
t1.book_group_id bookGroupId t1.book_group_id bookGroupId,
t2.pro_label_id proLabelId,
t2.dep_label_id depLabelId,
t2.pur_label_id purLabelId
FROM FROM
book_group_qrcode t book_group_qrcode t
INNER JOIN book_group_classify t1 ON t.classify_id = t1.id INNER JOIN book_group_classify t1 ON t.classify_id = t1.id
INNER JOIN book_group t2 ON t2.id = t1.book_group_id
WHERE WHERE
t.is_delete = 0 t.is_delete = 0
AND t1.is_delete = 0 AND t1.is_delete = 0
AND t2.is_delete = 0
AND t1.id IN AND t1.id IN
<foreach collection = "list" index="index" item = "item" open = "(" separator= "," close = ")"> <foreach collection = "list" index="index" item = "item" open = "(" separator= "," close = ")">
#{item} #{item}
......
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