PushArticleTask.java 12.9 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
package com.lyms.talkonlineweb.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyms.talkonlineweb.domain.*;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.service.*;
import com.lyms.talkonlineweb.util.Constant;
import com.lyms.talkonlineweb.util.DateUtil;
import com.lyms.talkonlineweb.util.StringUtil;
import com.lyms.talkonlineweb.util.WeiXinUtil;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.*;

import static com.lyms.talkonlineweb.util.DateUtil.YYYY_MM_DD;

/**
* 推送文章任务
*/

@Component
@Log4j2
public class PushArticleTask {

@Autowired
private LymsArticleService lymsArticleService;
@Autowired
private PatientInfoService patientInfoService;
@Autowired
private LymsPushedartService lymsPushedartService;//推送的历史记录
@Autowired
private PushArticleService pushArticleService;//推送文章新逻辑shiy改

@Autowired
LymsPushMessagesService lymsPushMessagesService;

@Autowired
private LymsDictService lymsDictService;

/**
* 每天18点执行文章推送
*/
@Scheduled(cron = "0 0 18 * * ?")
public void pushArtcle(){
Map<String,Object> param=new HashMap<>();
param.put("vtype",999);
List<LymsDict> dcLst=lymsDictService.listByMap(param);
if (dcLst.size()>0 && dcLst.get(0).getCode()==1){
log.debug("开始给患者推送文章>>>>>>");
//从LymsPushMessages记录表查询今天0点筛选出要推送的文章
QueryWrapper<LymsPushMessages> queryWrapper = new QueryWrapper<>();
queryWrapper.apply("TO_DAYS(created_time) = TO_DAYS(NOW()) and state=0");//防止重复
List<LymsPushMessages> lymsPushMessages=lymsPushMessagesService.list(queryWrapper);
for (LymsPushMessages lymsPushMessage : lymsPushMessages) {
try {
//推送的文章在LymsPushedart做记录。用于患者读取推送的文章
LymsPushedart pushedart=new LymsPushedart();
pushedart.setPid(lymsPushMessage.getPid());
pushedart.setAid(lymsPushMessage.getAid());
pushedart.setCreatedtime(new Date());
pushedart.setIsread((byte) 0);
//推送公众号消息逻辑
if(lymsPushMessage.getIsweixinOne()==1||lymsPushMessage.getIsweixinTwo()==1){
//获取token失败
if(StringUtil.isEmpty(AccessTokenServlet.accessToken)){
//更新到LymsPushMessages记录
lymsPushMessage.setState(2);//推送状态:0待推送 1成功 2失败。(在备注写失败原因)
lymsPushMessage.setRemark("推送短消息"+(lymsPushMessage.getIsweixinOne()==1?"1":"2")+",获取token失败!");
lymsPushMessage.setPushTime(new Date());
lymsPushMessagesService.updateById(lymsPushMessage);
//更新到LymsPushedart记录
if(null==lymsPushMessage.getPushedartId()){
pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败)
}else {
pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败)
}
//LymsPushedart的主键id赋值,表示修改。找到以前的推送记录,修改短文字的推送状态
//这里查询为了去掉推送文章的记录重复存
QueryWrapper<LymsPushedart> queryWrapper2=new QueryWrapper<>();
queryWrapper.eq("pid", lymsPushMessage.getPid());
queryWrapper.eq("aid", lymsPushMessage.getAid());
LymsPushedart lymsPushedart = lymsPushedartService.getOne(queryWrapper2);
if(lymsPushedart!=null){
lymsPushMessage.setPushedartId(lymsPushedart.getId());
}
pushedart.setId(lymsPushMessage.getPushedartId());
lymsPushedartService.saveOrUpdate(pushedart);
log.info("推送短消息"+(lymsPushMessage.getIsweixinOne()==1?"1":"2")+",获取token失败!");
continue;
}
//公众号openId为空
if(StringUtil.isEmpty(lymsPushMessage.getGzopenid())){
//更新到LymsPushMessages记录
lymsPushMessage.setState(2);//推送状态:0待推送 1成功 2失败。(在备注写失败原因)
lymsPushMessage.setRemark(lymsPushMessage.getPname()+":公众号openId为空!");
lymsPushMessage.setPushTime(new Date());
lymsPushMessagesService.updateById(lymsPushMessage);
//更新到LymsPushedart记录
if(null==lymsPushMessage.getPushedartId()){
pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败)
}else {
pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败)
}
//LymsPushedart的主键id赋值,表示修改。找到以前的推送记录,修改短文字的推送状态
//这里查询为了去掉推送文章的记录重复存
if(lymsPushMessage.getPushedartId()==null){
QueryWrapper<LymsPushedart> queryWrapper2=new QueryWrapper<>();
queryWrapper2.eq("pid", lymsPushMessage.getPid());
queryWrapper2.eq("aid", lymsPushMessage.getAid());
LymsPushedart lymsPushedart = lymsPushedartService.getOne(queryWrapper2);
if(lymsPushedart!=null){
lymsPushMessage.setPushedartId(lymsPushedart.getId());
}
}
pushedart.setId(lymsPushMessage.getPushedartId());
lymsPushedartService.saveOrUpdate(pushedart);
log.info(lymsPushMessage.getPname()+":公众号openId为空!");
continue;
}
if(lymsPushMessage.getIsweixinOne()==1&&null==lymsPushMessage.getPushedartId()){
//推送微信消息1
Map<String,Object> map=new HashMap<>();
map.put("first",new DataEntity(lymsPushMessage.getWeixTextOne(),"#173177"));
map.put("keyword1",new DataEntity(lymsPushMessage.getPname(),"#173177"));
map.put("keyword2",new DataEntity(lymsPushMessage.getHname(),"#173177"));
map.put("keyword3",new DataEntity(lymsPushMessage.getDname(),"#173177"));
map.put("remark",new DataEntity("预祝您早日康复","#173177"));
Integer code= WeiXinUtil.SendWeChatMsg(lymsPushMessage.getGzopenid(), Constant.GZ_TEMPLATE_ID,map);
if(null==code||code!=0){
//更新到LymsPushMessages记录
lymsPushMessage.setState(2);//推送状态:0待推送 1成功 2失败。(在备注写失败原因)
lymsPushMessage.setRemark("推送短消息1失败。。。"+lymsPushMessage.getPname()+"; code:"+code);
lymsPushMessage.setPushTime(new Date());
lymsPushMessagesService.updateById(lymsPushMessage);
//更新到LymsPushedart记录
pushedart.setIsweixinone(2);//是否推送短消息1(0否,1是 2推送失败)
//LymsPushedart的主键id赋值,表示修改。找到以前的推送记录,修改短文字的推送状态
pushedart.setId(lymsPushMessage.getPushedartId());
lymsPushedartService.saveOrUpdate(pushedart);
log.info("推送短消息1失败!"+lymsPushMessage.getPname()+"; code:"+code);
continue;
}
//成功标记记录1
pushedart.setIsweixinone(1);//是否推送短消息1(0否,1是 2推送失败)
//插入or更新到LymsPushedart记录
lymsPushedartService.saveOrUpdate(pushedart);
//更新到LymsPushMessages记录
lymsPushMessage.setState(1);//推送状态:0待推送 1成功 2失败。(在备注写失败原因)
lymsPushMessage.setPushTime(new Date());
lymsPushMessagesService.updateById(lymsPushMessage);
}else if(lymsPushMessage.getIsweixinTwo()==1){
//LymsPushedart的主键id赋值,表示修改。找到以前的推送记录,修改短文字的推送状态
pushedart.setId(lymsPushMessage.getPushedartId());
//推送微信消息2
Map<String,Object> map=new HashMap<>();
map.put("first",new DataEntity(lymsPushMessage.getWeixTextTwo(),"#173177"));
map.put("keyword1",new DataEntity(lymsPushMessage.getPname(),"#173177"));
map.put("keyword2",new DataEntity(lymsPushMessage.getHname(),"#173177"));
map.put("keyword3",new DataEntity(lymsPushMessage.getDname(),"#173177"));
map.put("remark",new DataEntity("预祝您早日康复","#173177"));
Integer code= WeiXinUtil.SendWeChatMsg(lymsPushMessage.getGzopenid(),Constant.GZ_TEMPLATE_ID,map);
if(null==code||code!=0){
//更新到LymsPushMessages记录
lymsPushMessage.setState(2);//推送状态:0待推送 1成功 2失败。(在备注写失败原因)
lymsPushMessage.setRemark("推送短消息2失败!"+lymsPushMessage.getPname()+"; code:"+code);
lymsPushMessage.setPushTime(new Date());
lymsPushMessagesService.updateById(lymsPushMessage);
//更新到LymsPushedart记录
pushedart.setIsweixintwo(2);//是否推送短消息2(0否,1是 2推送失败)
lymsPushedartService.saveOrUpdate(pushedart);
log.info("推送短消息2失败!"+lymsPushMessage.getPname()+"; code:"+code);
continue;
}
//成功标记记录2
pushedart.setIsweixintwo(1);//是否推送短消息1(0否,1是 2推送失败)
//插入or更新到LymsPushedart记录
lymsPushedartService.saveOrUpdate(pushedart);
//更新到LymsPushMessages记录
lymsPushMessage.setState(1);//推送状态:0待推送 1成功 2失败。(在备注写失败原因)
lymsPushMessage.setPushTime(new Date());
lymsPushMessagesService.updateById(lymsPushMessage);
}
}
//更新到LymsPushMessages记录(推送的是文章的情况IsweixinOne和IsweixinTwo都是0的时候)
lymsPushMessage.setState(1);//推送状态:0待推送 1成功 2失败。(在备注写失败原因)
lymsPushMessage.setPushTime(new Date());
lymsPushMessagesService.updateById(lymsPushMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


}
@Data
class DataEntity {
//内容
private String value;
//字体颜色
private String color;

public DataEntity(String value ,String color){
this.value = value;
this.color = color;
}
}