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;
}
}