From 4fa74834d33454c8fbb4e1a5d0c9fc1ad2e60f6f Mon Sep 17 00:00:00 2001 From: shiyang <316555390@qq.com> Date: Mon, 20 Dec 2021 13:54:41 +0800 Subject: [PATCH] =?UTF-8?q?0=E7=82=B9=E6=8E=A8=E9=80=81=E7=9A=84=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E5=BC=80=E5=A7=8B=E7=AD=9B=E9=80=89=E5=AD=98=E5=85=A5?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E3=80=829=E7=82=B9=E7=AD=9B=E9=80=89?= =?UTF-8?q?=E7=9A=84=E6=96=87=E7=AB=A0=E8=AE=B0=E5=BD=95=E5=BC=80=E5=A7=8B?= =?UTF-8?q?=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../talkonlineweb/controller/TestController.java | 19 +- .../talkonlineweb/domain/LymsPushMessages.java | 256 +++++++++++++++++++++ .../lyms/talkonlineweb/domain/LymsPushedart.java | 4 +- .../com/lyms/talkonlineweb/domain/PushArticle.java | 34 ++- .../mapper/LymsPushMessagesMapper.java | 15 ++ .../service/LymsPushMessagesService.java | 11 + .../service/impl/LymsPushMessagesServiceImpl.java | 20 ++ .../talkonlineweb/task/AccessTokenServlet.java | 66 +++++- .../lyms/talkonlineweb/task/PushArticleTask.java | 107 ++++++--- .../talkonlineweb/task/PushArticleTaskData.java | 86 +++++++ .../com/lyms/talkonlineweb/task/TokenThread.java | 54 ----- .../java/com/lyms/talkonlineweb/util/DateUtil.java | 2 + .../com/lyms/talkonlineweb/util/WeiXinUtil.java | 33 ++- .../resources/mapper/LymsPushMessagesMapper.xml | 42 ++++ 14 files changed, 646 insertions(+), 103 deletions(-) create mode 100644 talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPushMessages.java create mode 100644 talkonlineweb/src/main/java/com/lyms/talkonlineweb/mapper/LymsPushMessagesMapper.java create mode 100644 talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/LymsPushMessagesService.java create mode 100644 talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsPushMessagesServiceImpl.java create mode 100644 talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTaskData.java delete mode 100644 talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/TokenThread.java create mode 100644 talkonlineweb/src/main/resources/mapper/LymsPushMessagesMapper.xml diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/TestController.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/TestController.java index 8a29240..d3c4b10 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/TestController.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/TestController.java @@ -1,19 +1,28 @@ package com.lyms.talkonlineweb.controller; -import com.lyms.talkonlineweb.annotation.TokenRequired; -import com.lyms.talkonlineweb.domain.LymsUser; -import com.lyms.talkonlineweb.service.LymsUserService; +import com.lyms.talkonlineweb.task.PushArticleTask; +import com.lyms.talkonlineweb.task.PushArticleTaskData; import lombok.extern.java.Log; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.List; - @RestController @Log public class TestController { + @Autowired + public PushArticleTask pushArticleTask; + @Autowired + public PushArticleTaskData pushArticleTaskMySql; + @GetMapping("test0") + public void test0() { + pushArticleTask.pushArtcle(); + } + @GetMapping("test9") + public void test9() { + pushArticleTaskMySql.pushArtcleData(); + } // @Autowired diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPushMessages.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPushMessages.java new file mode 100644 index 0000000..6c5e655 --- /dev/null +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPushMessages.java @@ -0,0 +1,256 @@ +package com.lyms.talkonlineweb.domain; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import java.io.Serializable; +import java.util.Date; +import lombok.Data; + +/** + * 每天0点存入9点要推送的信息 + * @TableName lyms_push_messages + */ +@TableName(value ="lyms_push_messages") +@Data +public class LymsPushMessages implements Serializable { + /** + * + */ + @TableId(value = "id", type = IdType.AUTO) + private Integer id; + + /** + * 患者id + */ + @TableField(value = "pid") + private Integer pid; + + /** + * 患者名称 + */ + @TableField(value = "pname") + private String pname; + + /** + * 医院id + */ + @TableField(value = "hid") + private Integer hid; + + /** + * 医院名称 + */ + @TableField(value = "hname") + private String hname; + + /** + * 科室id + */ + @TableField(value = "did") + private Integer did; + + /** + * 科室名称 + */ + @TableField(value = "dname") + private String dname; + + /** + * 医生id + */ + @TableField(value = "dtid") + private Integer dtid; + + /** + * 医生名称 + */ + @TableField(value = "dtname") + private String dtname; + + /** + * 疾病id + */ + @TableField(value = "iid") + private Integer iid; + + /** + * 疾病名称 + */ + @TableField(value = "iname") + private String iname; + + /** + * 病例id + */ + @TableField(value = "ilid") + private Integer ilid; + + /** + * 患者关注公众号openid + */ + @TableField(value = "gzopenid") + private String gzopenid; + + /** + * 推送文章id + */ + @TableField(value = "aid") + private Integer aid; + + /** + * 推送文章标题 + */ + @TableField(value = "atitle") + private String atitle; + + /** + * 推送文章内容 + */ + @TableField(value = "acontent") + private String acontent; + + /** + * 推送文章的序号 + */ + @TableField(value = "serialNumber") + private Integer serialnumber; + + /** + * 推送短文字1的内容 + */ + @TableField(value = "weix_text_one") + private String weixTextOne; + + /** + * 推送短文字2的内容 + */ + @TableField(value = "weix_text_two") + private String weixTextTwo; + + /** + * 是否推送短文字1内容:0不推送 1推送 + */ + @TableField(value = "isweixin_one") + private Integer isweixinOne; + + /** + * 是否推送短文字2内容:0不推送 1推送 + */ + @TableField(value = "isweixin_two") + private Integer isweixinTwo; + + /** + * 已推送列表id(lyms_pushedart表) + */ + @TableField(value = "pushedart_id") + private Integer pushedartId; + + /** + * 计划推送日期 + */ + @TableField(value = "plan_time") + private Date planTime; + + @TableField(exist = false) + private static final long serialVersionUID = 1L; + + @Override + public boolean equals(Object that) { + if (this == that) { + return true; + } + if (that == null) { + return false; + } + if (getClass() != that.getClass()) { + return false; + } + LymsPushMessages other = (LymsPushMessages) that; + return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) + && (this.getPid() == null ? other.getPid() == null : this.getPid().equals(other.getPid())) + && (this.getPname() == null ? other.getPname() == null : this.getPname().equals(other.getPname())) + && (this.getHid() == null ? other.getHid() == null : this.getHid().equals(other.getHid())) + && (this.getHname() == null ? other.getHname() == null : this.getHname().equals(other.getHname())) + && (this.getDid() == null ? other.getDid() == null : this.getDid().equals(other.getDid())) + && (this.getDname() == null ? other.getDname() == null : this.getDname().equals(other.getDname())) + && (this.getDtid() == null ? other.getDtid() == null : this.getDtid().equals(other.getDtid())) + && (this.getDtname() == null ? other.getDtname() == null : this.getDtname().equals(other.getDtname())) + && (this.getIid() == null ? other.getIid() == null : this.getIid().equals(other.getIid())) + && (this.getIname() == null ? other.getIname() == null : this.getIname().equals(other.getIname())) + && (this.getIlid() == null ? other.getIlid() == null : this.getIlid().equals(other.getIlid())) + && (this.getGzopenid() == null ? other.getGzopenid() == null : this.getGzopenid().equals(other.getGzopenid())) + && (this.getAid() == null ? other.getAid() == null : this.getAid().equals(other.getAid())) + && (this.getAtitle() == null ? other.getAtitle() == null : this.getAtitle().equals(other.getAtitle())) + && (this.getAcontent() == null ? other.getAcontent() == null : this.getAcontent().equals(other.getAcontent())) + && (this.getSerialnumber() == null ? other.getSerialnumber() == null : this.getSerialnumber().equals(other.getSerialnumber())) + && (this.getWeixTextOne() == null ? other.getWeixTextOne() == null : this.getWeixTextOne().equals(other.getWeixTextOne())) + && (this.getWeixTextTwo() == null ? other.getWeixTextTwo() == null : this.getWeixTextTwo().equals(other.getWeixTextTwo())) + && (this.getIsweixinOne() == null ? other.getIsweixinOne() == null : this.getIsweixinOne().equals(other.getIsweixinOne())) + && (this.getIsweixinTwo() == null ? other.getIsweixinTwo() == null : this.getIsweixinTwo().equals(other.getIsweixinTwo())) + && (this.getPlanTime() == null ? other.getPlanTime() == null : this.getPlanTime().equals(other.getPlanTime())); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); + result = prime * result + ((getPid() == null) ? 0 : getPid().hashCode()); + result = prime * result + ((getPname() == null) ? 0 : getPname().hashCode()); + result = prime * result + ((getHid() == null) ? 0 : getHid().hashCode()); + result = prime * result + ((getHname() == null) ? 0 : getHname().hashCode()); + result = prime * result + ((getDid() == null) ? 0 : getDid().hashCode()); + result = prime * result + ((getDname() == null) ? 0 : getDname().hashCode()); + result = prime * result + ((getDtid() == null) ? 0 : getDtid().hashCode()); + result = prime * result + ((getDtname() == null) ? 0 : getDtname().hashCode()); + result = prime * result + ((getIid() == null) ? 0 : getIid().hashCode()); + result = prime * result + ((getIname() == null) ? 0 : getIname().hashCode()); + result = prime * result + ((getIlid() == null) ? 0 : getIlid().hashCode()); + result = prime * result + ((getGzopenid() == null) ? 0 : getGzopenid().hashCode()); + result = prime * result + ((getAid() == null) ? 0 : getAid().hashCode()); + result = prime * result + ((getAtitle() == null) ? 0 : getAtitle().hashCode()); + result = prime * result + ((getAcontent() == null) ? 0 : getAcontent().hashCode()); + result = prime * result + ((getSerialnumber() == null) ? 0 : getSerialnumber().hashCode()); + result = prime * result + ((getWeixTextOne() == null) ? 0 : getWeixTextOne().hashCode()); + result = prime * result + ((getWeixTextTwo() == null) ? 0 : getWeixTextTwo().hashCode()); + result = prime * result + ((getIsweixinOne() == null) ? 0 : getIsweixinOne().hashCode()); + result = prime * result + ((getIsweixinTwo() == null) ? 0 : getIsweixinTwo().hashCode()); + result = prime * result + ((getPlanTime() == null) ? 0 : getPlanTime().hashCode()); + return result; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ["); + sb.append("Hash = ").append(hashCode()); + sb.append(", id=").append(id); + sb.append(", pid=").append(pid); + sb.append(", pname=").append(pname); + sb.append(", hid=").append(hid); + sb.append(", hname=").append(hname); + sb.append(", did=").append(did); + sb.append(", dname=").append(dname); + sb.append(", dtid=").append(dtid); + sb.append(", dtname=").append(dtname); + sb.append(", iid=").append(iid); + sb.append(", iname=").append(iname); + sb.append(", ilid=").append(ilid); + sb.append(", gzopenid=").append(gzopenid); + sb.append(", aid=").append(aid); + sb.append(", atitle=").append(atitle); + sb.append(", acontent=").append(acontent); + sb.append(", serialnumber=").append(serialnumber); + sb.append(", weixTextOne=").append(weixTextOne); + sb.append(", weixTextTwo=").append(weixTextTwo); + sb.append(", isweixinOne=").append(isweixinOne); + sb.append(", isweixinTwo=").append(isweixinTwo); + sb.append(", planTime=").append(planTime); + sb.append(", serialVersionUID=").append(serialVersionUID); + sb.append("]"); + return sb.toString(); + } +} \ No newline at end of file diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPushedart.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPushedart.java index 764dcf9..b9b5ab9 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPushedart.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPushedart.java @@ -54,13 +54,13 @@ public class LymsPushedart implements Serializable { /** * 是否推送短消息1(0否,1是) */ - @TableField(value = "isweixinone") + @TableField(value = "isweixin_one") private Integer isweixinone; /** * 是否推送短消息2(0否,1是) */ - @TableField(value = "isweixintwo") + @TableField(value = "isweixin_two") private Integer isweixintwo; @TableField(exist = false) diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/PushArticle.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/PushArticle.java index 29d5aca..90e99d2 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/PushArticle.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/PushArticle.java @@ -18,8 +18,8 @@ public class PushArticle implements Serializable { /** * 患者id */ - @TableField(value = "id") - private Integer id; + @TableField(value = "pid") + private Integer pid; /** * 患者姓名 @@ -165,6 +165,30 @@ public class PushArticle implements Serializable { @TableField(value = "weixTextTwo") private String weixTextTwo; + /** + * 患者关注公众号openid + */ + @TableField(value = "gzopenid") + private String gzopenid; + + /** + * 推送文章标题 + */ + @TableField(value = "atitle") + private String atitle; + + /** + * 推送文章内容 + */ + @TableField(value = "acontent") + private String acontent; + + /** + * 推送文章的序号 + */ + @TableField(value = "serialNumber") + private Integer serialnumber; + @TableField(exist = false) private static final long serialVersionUID = 1L; @@ -180,7 +204,7 @@ public class PushArticle implements Serializable { return false; } PushArticle other = (PushArticle) that; - return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) + return (this.getPid() == null ? other.getPid() == null : this.getPid().equals(other.getPid())) && (this.getPname() == null ? other.getPname() == null : this.getPname().equals(other.getPname())) && (this.getIdno() == null ? other.getIdno() == null : this.getIdno().equals(other.getIdno())) && (this.getPpasswd() == null ? other.getPpasswd() == null : this.getPpasswd().equals(other.getPpasswd())) @@ -209,7 +233,7 @@ public class PushArticle implements Serializable { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); + result = prime * result + ((getPid() == null) ? 0 : getPid().hashCode()); result = prime * result + ((getPname() == null) ? 0 : getPname().hashCode()); result = prime * result + ((getIdno() == null) ? 0 : getIdno().hashCode()); result = prime * result + ((getPpasswd() == null) ? 0 : getPpasswd().hashCode()); @@ -241,7 +265,7 @@ public class PushArticle implements Serializable { sb.append(getClass().getSimpleName()); sb.append(" ["); sb.append("Hash = ").append(hashCode()); - sb.append(", id=").append(id); + sb.append(", pid=").append(pid); sb.append(", pname=").append(pname); sb.append(", idno=").append(idno); sb.append(", ppasswd=").append(ppasswd); diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/mapper/LymsPushMessagesMapper.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/mapper/LymsPushMessagesMapper.java new file mode 100644 index 0000000..378a807 --- /dev/null +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/mapper/LymsPushMessagesMapper.java @@ -0,0 +1,15 @@ +package com.lyms.talkonlineweb.mapper; + +import com.lyms.talkonlineweb.domain.LymsPushMessages; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** + * @Entity com.lyms.talkonlineweb.domain.LymsPushMessages + */ +public interface LymsPushMessagesMapper extends BaseMapper { + +} + + + + diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/LymsPushMessagesService.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/LymsPushMessagesService.java new file mode 100644 index 0000000..a4435c9 --- /dev/null +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/LymsPushMessagesService.java @@ -0,0 +1,11 @@ +package com.lyms.talkonlineweb.service; + +import com.lyms.talkonlineweb.domain.LymsPushMessages; +import com.baomidou.mybatisplus.extension.service.IService; + +/** + * + */ +public interface LymsPushMessagesService extends IService { + +} diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsPushMessagesServiceImpl.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsPushMessagesServiceImpl.java new file mode 100644 index 0000000..8bad6e8 --- /dev/null +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/service/impl/LymsPushMessagesServiceImpl.java @@ -0,0 +1,20 @@ +package com.lyms.talkonlineweb.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.lyms.talkonlineweb.domain.LymsPushMessages; +import com.lyms.talkonlineweb.service.LymsPushMessagesService; +import com.lyms.talkonlineweb.mapper.LymsPushMessagesMapper; +import org.springframework.stereotype.Service; + +/** + * + */ +@Service +public class LymsPushMessagesServiceImpl extends ServiceImpl + implements LymsPushMessagesService{ + +} + + + + diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java index b26b686..bb8b830 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java @@ -1,24 +1,74 @@ package com.lyms.talkonlineweb.task; +import com.alibaba.fastjson.JSONObject; +import com.lyms.talkonlineweb.util.HttpUtil; +import com.lyms.talkonlineweb.util.StringUtil; +import com.lyms.talkonlineweb.util.WeiXinUtil; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; +/** + * 获取到access_token + * 每1小时59获取一次token,保证长期有效。 + * (微信公众号规定2小时token失效,每天只能获取2000次。) + */ @Component -public class AccessTokenServlet extends HttpServlet { - final String appId ="wxd3c36244d006cb90";//公众号appid - final String appSecret ="fc80b5dd03a581a088adcd2c65a7e10a";//公众号AppSecret +public class AccessTokenServlet extends HttpServlet implements Runnable { + private final String appId ="wxd3c36244d006cb90";//公众号appid + private final String appSecret ="fc80b5dd03a581a088adcd2c65a7e10a";//公众号AppSecret + public static String accessToken = null; /** - * 启动后开启线程每1小时59获取一次token,保证长期有效。(微信公众号规定2小时token失效,每天只能获取2000次。) + * 启动后开启线程每1小时59获取一次token * @throws ServletException */ - @PostConstruct//部署后启动方法 + @PostConstruct//部署时开启注释,启动线程 public void init() throws ServletException { - TokenThread.appId = appId; - TokenThread.appSecret = appSecret; - new Thread(new TokenThread()).start(); //启动进程 + new Thread(new AccessTokenServlet()).start(); //启动线程 + } + public void run(){ + while (true){ + try{ + accessToken = this.getToken(); + if(null!=accessToken){ + Thread.sleep(1000 * 7000); //获取到access_token 休眠7000秒 + }else{ + Thread.sleep(1000 * 3); //获取的access_token为空 休眠3秒 + } + }catch(Exception e){ + System.out.println("发生异常:"+e.getMessage()); + e.printStackTrace(); + try{ + Thread.sleep(1000 * 10); //发生异常休眠1秒 + }catch (Exception e1){ + + } + } + } + } + + /** + * 获取token + * 微信公众号 + * @return token + */ + public String getToken() { + // 授予形式 + String grant_type = "client_credential"; + // 接口地址拼接参数 + String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + appId + + "&secret=" + appSecret; + String tokenJsonStr = WeiXinUtil.repeatDoGetPost(getTokenApi, "GET", null); + if(StringUtil.isEmpty(tokenJsonStr)){ + System.out.println("获取TOKEN :失败,repeatDoGetPost返回值为空>>>>>>>"); + return null; + } + JSONObject tokenJson = JSONObject.parseObject(tokenJsonStr); + String token = tokenJson.get("access_token").toString(); + System.out.println("获取到的TOKEN : " + token); + return token; } } diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java index af68356..cc507bc 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTask.java @@ -2,14 +2,20 @@ 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.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; + /** * 推送文章任务 */ @@ -28,57 +34,104 @@ public class PushArticleTask { private PushArticleService pushArticleService;//推送文章新逻辑shiy改 @Autowired + LymsPushMessagesService lymsPushMessagesService; + + @Autowired private LymsDictService lymsDictService; - final String gzopenid ="oQEmP6uq2pUQ80NctiX3GnFxDMn8";//测试:公众号关注的微信openId - final String template_id ="";//测试:公众号模板id -// @Scheduled(initialDelay=10000, fixedRate=30000) + final String gzopenid ="oQEmP6rFmf0lOb--mUf4_MAZPXEQ";//测试:公众号关注的微信openId + final String template_id ="ZDxcRDJ3okC9Lbzpfhr_v4e8W1VWrho-f5uHW_VZHTg";//公众号模板:诊后注意事项id + + @Scheduled(cron = "0 0 9 * * ?")//每天上午9点执行文章推送 public void pushArtcle(){ + String token= AccessTokenServlet.accessToken; Map param=new HashMap<>(); param.put("vtype",999); List dcLst=lymsDictService.listByMap(param); if (dcLst.size()>0 && dcLst.get(0).getCode()==1){ log.debug("开始给患者推送文章>>>>>>"); - List pushArticle=pushArticleService.list(); - for (PushArticle article : pushArticle) { + //从LymsPushMessages记录表查询今天0点筛选出要推送的文章 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.apply("TO_DAYS(plan_time) = TO_DAYS(NOW())"); + List lymsPushMessages=lymsPushMessagesService.list(queryWrapper); + for (LymsPushMessages lymsPushMessage : lymsPushMessages) { + //推送的文章在LymsPushedart做记录。用于患者读取推送的文章 LymsPushedart pushedart=new LymsPushedart(); - pushedart.setPid(article.getId()); - pushedart.setAid(article.getAid()); + pushedart.setPid(lymsPushMessage.getPid()); + pushedart.setAid(lymsPushMessage.getAid()); pushedart.setCreatedtime(new Date()); pushedart.setIsread((byte) 0); - //查询推送文章后短文字推送状态 - QueryWrapper queryWrapper=new QueryWrapper<>(); - queryWrapper.eq("pid", article.getId()); - queryWrapper.eq("aid", article.getAid()); - LymsPushedart lymsPushedart = lymsPushedartService.getOne(queryWrapper); - if(null!=lymsPushedart){ - pushedart.setId(lymsPushedart.getId()); - if(lymsPushedart.getIsweixinone()==0){ - if(StringUtil.isEmpty(TokenThread.accessToken)){ - log.info("获取token失败。。。"+article.getDtname()+":短消息1推送失败"); + //PushedartId(如果null表示需要推送文章,否则需要推送短消息) + if(null!=lymsPushMessage.getPushedartId()){ + pushedart.setId(lymsPushMessage.getPushedartId()); + if(lymsPushMessage.getIsweixinOne()==1){ + if(StringUtil.isEmpty(token)){ + log.info("推送短消息1,获取token失败。。。"); continue; } //推送微信消息1 Map map=new HashMap<>(); - map.put("data", article.getWeixTextOne()); - WeiXinUtil.SendWeChatMsg(TokenThread.accessToken, gzopenid, template_id,map); + 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(token,gzopenid,template_id,map); + if(null==code||code!=0){ + log.info("推送短消息1失败。。。"+lymsPushMessage.getPname()+"; code:"+code); + continue; + } //成功标记记录1 - pushedart.setIsweixinone(1); - }else if (lymsPushedart.getIsweixintwo()==0){ - if(StringUtil.isEmpty(TokenThread.accessToken)){ - log.info("获取token失败。。。"+article.getDtname()+":短消息2推送失败"); + pushedart.setIsweixinone(1);//0未推送1已推送 + }else if(lymsPushMessage.getIsweixinTwo()==1){ + if(StringUtil.isEmpty(token)){ + log.info("推送短消息2,获取token失败。。。"); continue; } //推送微信消息2 Map map=new HashMap<>(); - map.put("data", article.getWeixTextTwo()); - WeiXinUtil.SendWeChatMsg(TokenThread.accessToken, gzopenid, template_id,map); + 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(token,gzopenid,template_id,map); + if(null==code||code!=0){ + log.info("推送短消息2失败。。。"+lymsPushMessage.getPname()+"; code:"+code); + continue; + } //成功标记记录2 - pushedart.setIsweixintwo(1); + pushedart.setIsweixintwo(1);//0未推送1已推送 } } - lymsPushedartService.saveOrUpdate(pushedart);//插入到提送记录 + //插入or更新到LymsPushedart记录 + boolean b = lymsPushedartService.saveOrUpdate(pushedart); + if (b) { + if(null!=pushedart.getIsweixinone()&&pushedart.getIsweixinone()==1){ + log.info("推送短文字1给:" + lymsPushMessage.getPname() + "成功!"); + }else if(null!=pushedart.getIsweixintwo()&&pushedart.getIsweixintwo()==1){ + log.info("推送短文字2给:" + lymsPushMessage.getPname() + "成功!"); + } + log.info("推送文章给:" + lymsPushMessage.getPname() + "成功!"); + } else { + log.info("推送文章给:" + lymsPushMessage.getPname() + "失败:插入or更新到LymsPushedart记录返回false>>>>>>>>>"); + } } } } + + +} +@Data +class DataEntity { + //内容 + private String value; + //字体颜色 + private String color; + + public DataEntity(String value ,String color){ + this.value = value; + this.color = color; + } } + diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTaskData.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTaskData.java new file mode 100644 index 0000000..8f3b008 --- /dev/null +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/PushArticleTaskData.java @@ -0,0 +1,86 @@ +package com.lyms.talkonlineweb.task; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.lyms.talkonlineweb.domain.LymsDict; +import com.lyms.talkonlineweb.domain.LymsPushMessages; +import com.lyms.talkonlineweb.domain.LymsPushedart; +import com.lyms.talkonlineweb.domain.PushArticle; +import com.lyms.talkonlineweb.service.*; +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.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import org.springframework.util.unit.DataUnit; + +import java.util.*; + +import static com.lyms.talkonlineweb.util.DateUtil.YYYY_MM_DD; + +/** + * 推送文章任务 + */ + +@Component +@Log4j2 +public class PushArticleTaskData { + + @Autowired + private LymsArticleService lymsArticleService; + @Autowired + private PatientInfoService patientInfoService; + @Autowired + private LymsPushedartService lymsPushedartService;//推送的历史记录 + @Autowired + private PushArticleService pushArticleService;//推送文章新逻辑shiy改 + @Autowired + LymsPushMessagesService lymsPushMessagesService; + @Autowired + private LymsDictService lymsDictService; + + + @Scheduled(cron = "0 0 0 * * ?")//每天0点执行-要推送的文章存到数据库 + public void pushArtcleData(){ + Map param=new HashMap<>(); + param.put("vtype",999); + List dcLst=lymsDictService.listByMap(param); + if (dcLst.size()>0 && dcLst.get(0).getCode()==1){ + log.info("开始筛选"+(DateUtil.getDateTime(new Date(), YYYY_MM_DD))+"要推送的文章存入mysql>>>>>>"); + //筛选出需要推送的文章信息-(逻辑详见视图) + List pushArticleList=pushArticleService.list(); + for (PushArticle article : pushArticleList) { + //需要推送文章的对象 + LymsPushMessages lymsPushMessages=new LymsPushMessages(); + BeanUtils.copyProperties(article,lymsPushMessages); + //查询推已推送的文章(如果null表示需要推送文章,否则需要推送短消息) + QueryWrapper queryWrapper=new QueryWrapper<>(); + queryWrapper.eq("pid", lymsPushMessages.getPid()); + queryWrapper.eq("aid", lymsPushMessages.getAid()); + LymsPushedart lymsPushedart = lymsPushedartService.getOne(queryWrapper); + if(null!=lymsPushedart){ + //每次只推送一条短消息,判断推送短消息1还是2 + if(lymsPushedart.getIsweixinone()==0){//0未推送1已推送 + //标记要推送的记录需要推送短消息1 + lymsPushMessages.setIsweixinOne(1); + //存入已推送文章列表id + lymsPushMessages.setPushedartId(lymsPushedart.getId()); + }else if (lymsPushedart.getIsweixintwo()==0){//0未推送1已推送 + //标记要推送的记录需要推送短消息2 + lymsPushMessages.setIsweixinTwo(1); + //存入已推送文章列表id + lymsPushMessages.setPushedartId(lymsPushedart.getId()); + } + } + //保存到要推送的表lyms_push_messages + lymsPushMessagesService.save(lymsPushMessages); + } + } + } + +} + + diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/TokenThread.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/TokenThread.java deleted file mode 100644 index bad1c21..0000000 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/TokenThread.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.lyms.talkonlineweb.task; - -import com.alibaba.fastjson.JSONObject; -import com.lyms.talkonlineweb.util.HttpUtil; - -public class TokenThread implements Runnable{ - public static String appId = "wxd3c36244d006cb90"; - - public static String appSecret= "fc80b5dd03a581a088adcd2c65a7e10a"; - - public static String accessToken = null; - - public void run(){ - while (true){ - try{ - accessToken = this.getToken(); - if(null!=accessToken){ - System.out.println(accessToken); - Thread.sleep(7000 * 1000); //获取到access_token 休眠7000秒 - - }else{ - Thread.sleep(1000*3); //获取的access_token为空 休眠3秒 - } - }catch(Exception e){ - System.out.println("发生异常:"+e.getMessage()); - e.printStackTrace(); - try{ - Thread.sleep(1000*10); //发生异常休眠1秒 - }catch (Exception e1){ - - } - } - } - } - - - /** - * 获取token - * 微信公众号 - * @return token - */ - public String getToken() { - // 授予形式 - String grant_type = "client_credential"; - // 接口地址拼接参数 - String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + this.appId - + "&secret=" + this.appSecret; - String tokenJsonStr = HttpUtil.doGetPost(getTokenApi, "GET", null); - JSONObject tokenJson = JSONObject.parseObject(tokenJsonStr); - String token = tokenJson.get("access_token").toString(); - System.out.println("获取到的TOKEN : " + token); - return token; - } -} diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/DateUtil.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/DateUtil.java index 646c126..9316046 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/DateUtil.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/DateUtil.java @@ -3,6 +3,7 @@ package com.lyms.talkonlineweb.util; import lombok.extern.slf4j.Slf4j; import java.text.DateFormat; +import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ConcurrentHashMap; @@ -75,4 +76,5 @@ public class DateUtil { SimpleDateFormat fm = new SimpleDateFormat("yyMMddHHmmss"); return fm.format(new Date()); } + } diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java index cc7a050..36d4dd1 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java @@ -1,7 +1,10 @@ package com.lyms.talkonlineweb.util; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.lyms.talkonlineweb.result.BaseResponse; import lombok.extern.log4j.Log4j2; +import org.springframework.util.CollectionUtils; import java.util.HashMap; import java.util.Map; @@ -60,7 +63,7 @@ public class WeiXinUtil { * @param template_id 公众号消息模板id * @param dataMap 推送内容消息主题显示相关map */ - public static void SendWeChatMsg(String token,String openid,String template_id,Map dataMap) { + public static Integer SendWeChatMsg(String token, String openid, String template_id, Map dataMap) { // 接口地址 String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token; //整体参数map @@ -68,7 +71,33 @@ public class WeiXinUtil { paramMap.put("touser", openid); paramMap.put("template_id", template_id); paramMap.put("data", dataMap); - System.out.println(HttpUtil.doGetPost(sendMsgApi,"POST",paramMap)); + String result= repeatDoGetPost(sendMsgApi,"POST",paramMap); + if(StringUtil.isEmpty(result)){ + return null; + } + Map resultMap = JSON.parseObject(result, HashMap.class); + return Integer.parseInt(resultMap.get("errcode").toString()); + + } + + /** + * 网络超时-重新发送3次 + */ + public static String repeatDoGetPost(String sendMsgApi,String type,Map paramMap) { + for (int i = 1; i <= 3; i++) { + String result=HttpUtil.doGetPost(sendMsgApi,type,paramMap); + if (StringUtil.isEmpty(result)) { + try{ + Thread.sleep(1000); + } catch (Exception e){ + e.printStackTrace(); + } + continue; + } else { + return result; + } + } + return null; } } diff --git a/talkonlineweb/src/main/resources/mapper/LymsPushMessagesMapper.xml b/talkonlineweb/src/main/resources/mapper/LymsPushMessagesMapper.xml new file mode 100644 index 0000000..741c079 --- /dev/null +++ b/talkonlineweb/src/main/resources/mapper/LymsPushMessagesMapper.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + id,pid,pname, + hid,hname,did, + dname,dtid,dtname, + iid,iname,ilid, + gzopenid,aid,atitle, + acontent,serialNumber,weix_text_one, + weix_text_two,isweixin_one,isweixin_two, + plan_time + + -- 1.8.3.1