PushArticleTask.java 16.8 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
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
package com.lyms.talkonlineweb.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

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

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

@Component
@Log4j2
public class PushArticleTask {
@Value("${getAccessToken.on_off}")
public boolean on_off;//配置yml 微信公众号获取access_token(测试环境部署不要开启。会与线上环境冲突)

@Autowired
private LymsArticleService lymsArticleService;
@Autowired
private PatientInfoService patientInfoService;
@Autowired
private LymsPushedartService lymsPushedartService;//推送的历史记录
@Autowired
private PushArticleService pushArticleService;//推送文章新逻辑shiy改
@Autowired
private LymsPatientService lymsPatientService;
@Autowired
private LymsPushMessagesService lymsPushMessagesService;
@Autowired
private LymsPcaseService lymsPcaseService;//病例
@Autowired
private LymsDictService lymsDictService;
@Autowired
public LymsPushAttentionRecordService lymsPushAttentionRecordService;

/**
* 每天18点执行文章推送
*/
@Scheduled(cron = "0 0 18 * * ?")
// @Scheduled(cron = "0 15 17 * * ?")//测试用 下午5点15
public void pushArtcle(){
if(!on_off){
return;
}
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<>();
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.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"));
//公众号跳转小程序需要的登录信息
Map<String,Object> mapInfo =pLoginInfo(lymsPushMessage.getPid());
Integer code= WeiXinUtil.SendWeChatMsg(lymsPushMessage.getGzopenid(), Constant.GZ_TEMPLATE_ID,map,mapInfo);
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);
//子女关注的患者推送短消息
pushPatientAttention(lymsPushMessage,map);
}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"));
//公众号跳转小程序需要的登录信息
Map<String,Object> mapInfo =pLoginInfo(lymsPushMessage.getPid());
Integer code= WeiXinUtil.SendWeChatMsg(lymsPushMessage.getGzopenid(),Constant.GZ_TEMPLATE_ID,map,mapInfo);
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);
//子女关注的患者推送短消息
pushPatientAttention(lymsPushMessage,map);
}
}
//更新到LymsPushMessages记录(推送的是文章的情况IsweixinOne和IsweixinTwo都是0的时候)
lymsPushMessage.setState(1);//推送状态:0待推送 1成功 2失败。(在备注写失败原因)
lymsPushMessage.setPushTime(new Date());
lymsPushMessagesService.updateById(lymsPushMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//用户登录id
public Map<String,Object> pLoginInfo(Integer pid){
//获取登录信息
Map<String,Object> map=new HashMap<>();
String plogin=null;
LymsPatient lymsPatient = lymsPatientService.getById(pid);
if(StringUtil.isNotEmpty(lymsPatient.getEnrolmentPhone())){
plogin=lymsPatient.getEnrolmentPhone();
}else {
List<PatientInfo> pLst=patientInfoService.list(new QueryWrapper<PatientInfo>().eq("id", lymsPatient.getId()));
if(CollectionUtils.isNotEmpty(pLst)){
plogin=pLst.get(0).getMobile();
}
}
map.put("plogin", plogin);
map.put("passwd", lymsPatient.getPpasswd());
return map;
}
//子女关注的患者推送短消息
public void pushPatientAttention(LymsPushMessages lymsPushMessage,Map map) throws Exception{
QueryWrapper<LymsPushAttentionRecord> queryWrapper=new QueryWrapper<>();
queryWrapper.apply("TO_DAYS(created_time) = TO_DAYS(NOW()) and isweixin_state=0 and pa_id={0}",lymsPushMessage.getPid());//今天需要推送的消息
final List<LymsPushAttentionRecord> pushAttentionRecords = lymsPushAttentionRecordService.list(queryWrapper);
for (LymsPushAttentionRecord pushAttentionRecord : pushAttentionRecords) {
Map<String,Object> mapInfo =pLoginInfo(pushAttentionRecord.getPid());
final List<LymsPcase> pcaseList = lymsPcaseService.list(new QueryWrapper<LymsPcase>().lambda().eq(LymsPcase::getPid, pushAttentionRecord.getPid()));
mapInfo.put("type", pcaseList.size()>0 ? 1 : 2);//有病例1,没有2,用于公众号跳转地址
Integer code= WeiXinUtil.SendWeChatMsg(pushAttentionRecord.getPGzopenid(),Constant.GZ_TEMPLATE_ID,map,mapInfo);
if(null==code||code!=0){
//更新到 lyms_push_attention_record 记录
pushAttentionRecord.setIsweixinState(2);//推送状态:0待推送 1成功 2失败。(在备注写失败原因)
pushAttentionRecord.setRemark("推送短消息"+pushAttentionRecord.getPaIsweixinType()+"失败!"+lymsPushMessage.getPname()+"; code:"+code);
pushAttentionRecord.setPushTime(new Date());
lymsPushAttentionRecordService.updateById(pushAttentionRecord);
continue;
}
pushAttentionRecord.setIsweixinState(1);//推送状态:0待推送 1成功 2失败。(在备注写失败原因)
pushAttentionRecord.setPushTime(new Date());
lymsPushAttentionRecordService.updateById(pushAttentionRecord);
}
}

}

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

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