|
@@ -0,0 +1,186 @@
|
|
|
+package com.ssj.service.weixin.push.service.util;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.ssj.bean.weixin.push.domain.TbWxTemplate;
|
|
|
+import com.ssj.framework.core.security.manager.TokenManager;
|
|
|
+import com.ssj.framework.core.util.RedisUtil;
|
|
|
+import com.ssj.framework.core.util.SystemResourceLocator;
|
|
|
+import com.ssj.framework.weixin.util.NewsUtil;
|
|
|
+import com.ssj.framework.weixin.util.WeixinUtil;
|
|
|
+import com.ssj.service.weixin.push.service.WxTemplateService;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 重构模板池
|
|
|
+ * 2019年4月11日15:00:41
|
|
|
+ * @author wuwen
|
|
|
+ */
|
|
|
+public class WxTemplateCacheUtils {
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(WxTemplateCacheUtils.class);
|
|
|
+
|
|
|
+ //初始化模板到redis
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ public static synchronized void init() {
|
|
|
+ WxTemplateService wxTemplateService = (WxTemplateService) SystemResourceLocator.getBean(WxTemplateService.class);
|
|
|
+ RedisUtil redisUtil= (RedisUtil) SystemResourceLocator.getBean(RedisUtil.class);
|
|
|
+ List<TbWxTemplate> tbWxTemplates=wxTemplateService.queryTbWxTemplate();
|
|
|
+
|
|
|
+ redisUtil.del(TaskConstant.TEMP_KEY);
|
|
|
+ redisUtil.del( TaskConstant.LONG_KEY);
|
|
|
+ //使用List集合,有序。
|
|
|
+ for (TbWxTemplate tbWxTemplate : tbWxTemplates) {
|
|
|
+ if(tbWxTemplate.getStartTime()!=null && tbWxTemplate.getEndTime()!=null){
|
|
|
+ redisUtil.lpush(
|
|
|
+
|
|
|
+ TaskConstant.LONG_KEY,
|
|
|
+ JSONObject.toJSONString(tbWxTemplate));
|
|
|
+ }else{
|
|
|
+ redisUtil.lpush(
|
|
|
+
|
|
|
+ TaskConstant.TEMP_KEY,
|
|
|
+ JSONObject.toJSONString(tbWxTemplate));
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询redis中所有的模板
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ public static synchronized List<TbWxTemplate> getTbWxTemplates() {
|
|
|
+ RedisUtil redisUtil= (RedisUtil) SystemResourceLocator.getBean(RedisUtil.class);
|
|
|
+ List<TbWxTemplate> lists=new ArrayList<TbWxTemplate>();
|
|
|
+ List<String> longList = redisUtil.lrange(TaskConstant.LONG_KEY,0,-1);
|
|
|
+ List<String> tempList = redisUtil.lrange(TaskConstant.TEMP_KEY,0,-1);
|
|
|
+ for (String str : longList) {
|
|
|
+ lists.add(JSONObject.toJavaObject(JSONObject.parseObject(str), TbWxTemplate.class));
|
|
|
+ }
|
|
|
+ for (String str : tempList) {
|
|
|
+ lists.add(JSONObject.toJavaObject(JSONObject.parseObject(str), TbWxTemplate.class));
|
|
|
+ }
|
|
|
+ return lists;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //查询redis中id的模板
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ public static synchronized TbWxTemplate getTbWxTemplateById(String templateIdShort) {
|
|
|
+ RedisUtil redisUtil= (RedisUtil) SystemResourceLocator.getBean(RedisUtil.class);
|
|
|
+ //长久的调用次数高,放前面
|
|
|
+ List<String> longList = redisUtil.lrange(TaskConstant.LONG_KEY,0,-1);
|
|
|
+ for (String str : longList) {
|
|
|
+ JSONObject json= JSONObject.parseObject(str);
|
|
|
+ if(templateIdShort.equals(json.getString("templateIdShort"))){
|
|
|
+ return JSONObject.toJavaObject(json, TbWxTemplate.class);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<String> tempList = redisUtil.lrange(TaskConstant.TEMP_KEY,0,-1);
|
|
|
+ for (String str : tempList) {
|
|
|
+ JSONObject json= JSONObject.parseObject(str);
|
|
|
+ if(templateIdShort.equals(json.getString("templateIdShort"))){
|
|
|
+ return JSONObject.toJavaObject(json, TbWxTemplate.class);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ //得到列表的总数量
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ public static synchronized Long getTbWxTemplateSize() {
|
|
|
+ RedisUtil redisUtil= (RedisUtil) SystemResourceLocator.getBean(RedisUtil.class);
|
|
|
+ return redisUtil.llen(TaskConstant.LONG_KEY)+redisUtil.llen(TaskConstant.TEMP_KEY);
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询模板
|
|
|
+ public static synchronized TbWxTemplate getTbWxTemplate(String templateIdShort, Date startTime, Date endTime) {
|
|
|
+ //Redis中存在值,直接返回
|
|
|
+ TbWxTemplate wxTemplate=getTbWxTemplateById(templateIdShort);
|
|
|
+ if(wxTemplate!=null){
|
|
|
+ wxTemplate.setLastTime(new Date());
|
|
|
+ wxTemplate.setNumber(wxTemplate.getNumber()+1);
|
|
|
+
|
|
|
+ //更新模板调用数据库,异步任务-----开始
|
|
|
+ //ThreadPool.getInstance().addTask(new SyncTemplateSaveTask(wxTemplate));
|
|
|
+ SyncTemplateSaveTask syncTemplateSaveTask = (SyncTemplateSaveTask) SystemResourceLocator.getBean(SyncTemplateSaveTask.class);
|
|
|
+ syncTemplateSaveTask.run(wxTemplate);
|
|
|
+ //更新模板调用数据库,异步任务-----结束
|
|
|
+ return wxTemplate;
|
|
|
+ }else{//去添加模板
|
|
|
+ /**
|
|
|
+ * 1:如果当前模板池中数量少于25,直接下载添加到池中
|
|
|
+ * 2:如果当前模板池中数量大于等于25,先把池中,上一次生成时间最长的移出池,然后再下载模板添加到Redis池中。
|
|
|
+ */
|
|
|
+ return addPoolTbWxTemplate(templateIdShort, startTime, endTime);//添加模板到池中
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //在list中最右边删除一个,再从左边添加一个,返回添加值
|
|
|
+ private static synchronized TbWxTemplate addPoolTbWxTemplate(String templateIdShort, Date startTime, Date endTime){
|
|
|
+ TokenManager tokenManager = (TokenManager) SystemResourceLocator.getBean(TokenManager.class);
|
|
|
+ String accessToken=tokenManager.getSSJAccessToken();
|
|
|
+ WxTemplateService wxTemplateService = (WxTemplateService) SystemResourceLocator.getBean(WxTemplateService.class);
|
|
|
+ RedisUtil redisUtil= (RedisUtil) SystemResourceLocator.getBean(RedisUtil.class);
|
|
|
+
|
|
|
+ //如果当前的数据大于25条,先删最右边的
|
|
|
+ Long countNum=getTbWxTemplateSize();
|
|
|
+ logger.info("当前模板数量为"+countNum+"...");
|
|
|
+ if(countNum>=25){
|
|
|
+ logger.info("当前模板数量为25,开始启动删除模板任务");
|
|
|
+ //通过key从list尾部删除一个value,并返回该元素
|
|
|
+ String jsonStr=redisUtil.rpop(TaskConstant.TEMP_KEY);
|
|
|
+ TbWxTemplate temp= JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), TbWxTemplate.class);
|
|
|
+ JSONObject reqObj= NewsUtil.delTemplate(accessToken,temp.getTemplateId());
|
|
|
+ logger.info("当前模板数量为25,开始启动删除模板任务,结束返回值:"+reqObj.toJSONString());
|
|
|
+ if("0".equals(reqObj.get("errcode").toString())){
|
|
|
+ temp.setStatus(2);
|
|
|
+ wxTemplateService.save(temp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ TbWxTemplate tbWxTemplate=null;
|
|
|
+ String templateId= NewsUtil.getTemplate(accessToken, templateIdShort);//添加模板ID
|
|
|
+ if(StringUtils.isNotEmpty(templateId)){
|
|
|
+ tbWxTemplate=new TbWxTemplate();
|
|
|
+ String keyType=TaskConstant.TEMP_KEY;
|
|
|
+ tbWxTemplate.setAppid(WeixinUtil.APPID);
|
|
|
+ tbWxTemplate.setLastTime(new Date());
|
|
|
+ tbWxTemplate.setStatus(1);
|
|
|
+ tbWxTemplate.setTemplateId(templateId);
|
|
|
+ tbWxTemplate.setTemplateIdShort(templateIdShort);
|
|
|
+ tbWxTemplate.setCreateTime(new Date());
|
|
|
+ tbWxTemplate.setNumber(1);
|
|
|
+ if(startTime!=null && endTime!=null){
|
|
|
+ tbWxTemplate.setStartTime(startTime);
|
|
|
+ tbWxTemplate.setEndTime(endTime);
|
|
|
+ keyType=TaskConstant.LONG_KEY;
|
|
|
+ }
|
|
|
+ tbWxTemplate=wxTemplateService.save(tbWxTemplate);//保存到数据,得到id
|
|
|
+
|
|
|
+ //开始操作redis
|
|
|
+ //通过key向list头部添加字符串
|
|
|
+ redisUtil.lpush(
|
|
|
+ keyType,
|
|
|
+ JSONObject.toJSONString(tbWxTemplate));
|
|
|
+
|
|
|
+ //添加数据库明细数据,异步任务-----开始
|
|
|
+ //ThreadPool.getInstance().addTask(new SyncTemplateInfoSaveTask(templateIdShort,tbWxTemplate,accessToken));
|
|
|
+
|
|
|
+ SyncTemplateInfoSaveTask syncTemplateInfoSaveTask = (SyncTemplateInfoSaveTask) SystemResourceLocator.getBean(SyncTemplateInfoSaveTask.class);
|
|
|
+ syncTemplateInfoSaveTask.run(templateIdShort,tbWxTemplate,accessToken);
|
|
|
+
|
|
|
+
|
|
|
+ //添加数据库明细数据,异步任务-----结束
|
|
|
+ }
|
|
|
+ return tbWxTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|