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