PatentTask.java 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package com.goafanti.common.task;
  2. import com.goafanti.admin.service.AdminService;
  3. import com.goafanti.common.constant.AFTConstants;
  4. import com.goafanti.common.enums.NoticeStatus;
  5. import com.goafanti.common.model.Admin;
  6. import com.goafanti.common.model.Notice;
  7. import com.goafanti.common.model.PatentNewLog;
  8. import com.goafanti.common.utils.AsyncUtils;
  9. import com.goafanti.common.utils.DateUtils;
  10. import com.goafanti.common.utils.LoggerUtils;
  11. import com.goafanti.common.utils.SendEmailUtil;
  12. import com.goafanti.patent.bo.PatentNewBo;
  13. import com.goafanti.patent.service.PatentNewService;
  14. import org.springframework.scheduling.annotation.Scheduled;
  15. import org.springframework.stereotype.Component;
  16. import javax.annotation.Resource;
  17. import java.util.ArrayList;
  18. import java.util.Date;
  19. import java.util.List;
  20. import java.util.UUID;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. @Component
  24. //@RestController
  25. public class PatentTask {
  26. @Resource
  27. private PatentNewService patentNewService;
  28. @Resource
  29. private AdminService adminService;
  30. @Resource
  31. private AsyncUtils asyncUtils;
  32. int pointsDataLimit=50;
  33. private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
  34. private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
  35. private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
  36. private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
  37. /**
  38. * 专利提醒定时任务
  39. * 每天0点触发任务
  40. * 1、先计算出申请时间与当前时间相隔天数 2、然后用相隔天使除以365取余计算年份差 3、在用申请时间加上年限获取最后日期
  41. * 4、在相隔30天、20天、10天、3天、2天、1天分别一次提醒。原是在距离90天提醒。
  42. */
  43. @Scheduled(cron = "0 0 0 * * ?")
  44. // @RequestMapping(value = "/open/patentRemind", method = RequestMethod.GET)
  45. public void patentRemind() {
  46. LoggerUtils.debug(getClass(), "====================开始检查专利提醒==================");
  47. try {
  48. List<PatentNewBo> l = patentNewService.AllselectStartPatentNew();
  49. List<Notice> nlist = new ArrayList<>();
  50. List<PatentNewLog> logList = new ArrayList<>();
  51. List<String> s1=new ArrayList<String>();
  52. List<String> s2=new ArrayList<String>();
  53. List<String> s3=new ArrayList<String>();
  54. List<String> s4=new ArrayList<String>();
  55. List<String> s5=new ArrayList<String>();
  56. List<String> s6=new ArrayList<String>();
  57. Date date=new Date();
  58. for (PatentNewBo p : l) {
  59. Integer status=0;
  60. // 计算相差日期
  61. int count = patentNewService.countDays(p.getEndDate());
  62. boolean flag=false;
  63. if (count > 60 && count <= 90&&p.getStatus()!=1) {
  64. flag=true;
  65. status = 1;
  66. s1.add(p.getId().toString());
  67. } else if (count > 30 && count <= 60&&p.getStatus()!=2) {
  68. flag=true;
  69. status = 2;
  70. s2.add(p.getId().toString());
  71. }else if (count > 15 && count <= 30&&p.getStatus()!=3) {
  72. flag=true;
  73. status = 3;
  74. s3.add(p.getId().toString());
  75. }else if (count > 7 && count <= 15&&p.getStatus()!=4) {
  76. flag=true;
  77. status = 4;
  78. s4.add(p.getId().toString());
  79. }else if (count>0&&count <= 7&&p.getStatus()!=5) {
  80. flag=true;
  81. status = 5;
  82. s5.add(p.getId().toString());
  83. }else if(count<0) {
  84. flag=true;
  85. s6.add(p.getId().toString());
  86. }
  87. if (flag) {
  88. Notice n = new Notice();
  89. n.setId(UUID.randomUUID().toString());
  90. n.setCreateTime(date);
  91. n.setReaded(0);// 未读
  92. n.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode());
  93. Admin a = adminService.selectByPrimaryKey(p.getAid());
  94. StringBuffer str2 = new StringBuffer();
  95. str2.append("专利编号=").append(p.getPatentNo()).append("名称=").append(p.getName());
  96. if (a == null) {
  97. str2.append(",aid=").append(p.getAid()).append(",找不到管理员。");
  98. n.setAid("1");
  99. n.setContent(str2.toString());
  100. nlist.add(n);
  101. } else if (!SendEmailUtil.isEmail(p.getEmail())) {
  102. str2.append(",管理员=").append(a.getName()).append(",邮箱不正确。");
  103. n.setAid("1");
  104. n.setContent(str2.toString());
  105. nlist.add(n);
  106. // 再发一条给当事人
  107. Notice n2 = new Notice();
  108. n2.setId(UUID.randomUUID().toString());
  109. n2.setCreateTime(date);
  110. n2.setReaded(0);// 未读
  111. n2.setContent(str2.toString());
  112. n2.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode());
  113. n2.setAid(a.getId());
  114. nlist.add(n2);
  115. } else {
  116. if (p.getSalesmanRemind()==0){
  117. Notice nt = new Notice();
  118. nt.setId(UUID.randomUUID().toString());
  119. nt.setCreateTime(date);
  120. nt.setReaded(0);// 未读
  121. nt.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode());
  122. PatentNewLog pl = new PatentNewLog();
  123. StringBuffer str =new StringBuffer();
  124. str.append("专利编号[").append(p.getPatentNo()).append("],名称[").append(p.getName()).append("],email[")
  125. .append(p.getEmail().trim()).append("]").append("已超过结束时间,但未做提醒,请悉知!!!");
  126. pl.setContent(str.toString());
  127. pl.setCreateTime(new Date());
  128. pl.setPid(p.getId());
  129. logList.add(pl);
  130. n.setAid(p.getAid());
  131. n.setNoticeType(NoticeStatus.TASK_PATENT_REMIND.getCode());
  132. n.setContent(str.toString());
  133. nlist.add(n);
  134. }else {
  135. PatentNewLog pl = new PatentNewLog();
  136. str2.append("专利编号[").append(p.getPatentNo()).append("],名称[").append(p.getName()).append("],email[")
  137. .append(p.getEmail().trim()).append("]");
  138. String str = StrToString(str2,p.getEndDate(),status);
  139. pl.setContent(str);
  140. pl.setCreateTime(new Date());
  141. pl.setPid(p.getId());
  142. logList.add(pl);
  143. n.setAid(p.getAid());
  144. n.setNoticeType(NoticeStatus.TASK_PATENT_REMIND.getCode());
  145. n.setContent(str);
  146. nlist.add(n);
  147. }
  148. }
  149. }
  150. }
  151. pushNotice(nlist);
  152. pushPatentLog(logList);
  153. //将不同状态的修改成不同状态
  154. updatePatentNew(s1,1);
  155. updatePatentNew(s2,2);
  156. updatePatentNew(s3,3);
  157. updatePatentNew(s4,4);
  158. updatePatentNew(s5,5);
  159. patentNewService.pushResetPatentNew(s6);
  160. } catch (Exception e) {
  161. LoggerUtils.debug(getClass(), "=====================专利提醒失败======================");
  162. Notice n = new Notice(UUID.randomUUID().toString(), new Date(), 0, "1",
  163. NoticeStatus.TASK_PATENT_ERROR.getCode(), "==============专利提醒失败================");
  164. asyncUtils.addNotice(n);
  165. e.printStackTrace();
  166. }
  167. LoggerUtils.debug(getClass(), "=====================专利提醒结束======================");
  168. }
  169. private void pushNotice(List<Notice> nlist) throws InterruptedException {
  170. if (!nlist.isEmpty()) {
  171. List<Notice> newList = new ArrayList<>();
  172. for (int i = 0; i < nlist.size(); i++) {
  173. newList.add(nlist.get(i));
  174. if (pointsDataLimit == newList.size() || i == nlist.size() - 1) {
  175. if (newList.size() > 0) asyncUtils.addNoticeBatch(newList);
  176. Thread.sleep(2000);
  177. newList.clear();
  178. }
  179. }
  180. }
  181. }
  182. private void pushPatentLog(List<PatentNewLog> logList) throws InterruptedException {
  183. if (!logList.isEmpty()) {
  184. List<PatentNewLog> logList2 = new ArrayList<>();
  185. for (int i = 0; i < logList.size(); i++) {
  186. logList2.add(logList.get(i));
  187. if (pointsDataLimit == logList2.size() || i == logList.size() - 1) {
  188. if (logList2.size() > 0) patentNewService.insertLogBatch(logList2);
  189. Thread.sleep(2000);
  190. logList2.clear();
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * 修改专利状态
  197. * @param status
  198. * @throws InterruptedException
  199. */
  200. private void updatePatentNew(List<String> list, Integer status) throws InterruptedException {
  201. if (!list.isEmpty()) {
  202. List<String> l2 = new ArrayList<>();
  203. for (int i = 0; i < list.size(); i++) {
  204. l2.add(list.get(i));
  205. if (pointsDataLimit == l2.size() || i == list.size() - 1) {
  206. if (l2.size() > 0) patentNewService.updateBatchStatusByid(l2, status);
  207. l2.clear();
  208. Thread.sleep(2000);
  209. }
  210. }
  211. }
  212. }
  213. private String StrToString(StringBuffer str, Date date, Integer status) {
  214. //状态 0 未提醒 1 剩余90天 2剩余30天 3剩余20天 4剩余10天 5剩余3天 6剩余2天 7剩余1天
  215. Integer days=0;
  216. if (status==1) days=90;
  217. else if (status==2) days=60;
  218. else if (status==3) days=30;
  219. else if (status==4) days=15;
  220. else if (status==5) days=7;
  221. if(days>0) {
  222. str.append(",续费时间还剩").append(days).append("天");
  223. }
  224. str.append(",截止时间[").append(DateUtils.formatDate(date, AFTConstants.YYYYMMDD)).append("]请在截止时间前通知客户!");
  225. return str.toString();
  226. }
  227. /**
  228. * @param htmlStr
  229. * @return
  230. * 删除Html标签
  231. */
  232. public static String delHTMLTag(String htmlStr) {
  233. Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
  234. Matcher m_script = p_script.matcher(htmlStr);
  235. htmlStr = m_script.replaceAll(""); // 过滤script标签
  236. Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
  237. Matcher m_style = p_style.matcher(htmlStr);
  238. htmlStr = m_style.replaceAll(""); // 过滤style标签
  239. Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
  240. Matcher m_html = p_html.matcher(htmlStr);
  241. htmlStr = m_html.replaceAll(""); // 过滤html标签
  242. Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
  243. Matcher m_space = p_space.matcher(htmlStr);
  244. htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
  245. return htmlStr.trim(); // 返回文本字符串
  246. }
  247. public static String getTextFromHtml(String htmlStr){
  248. htmlStr = delHTMLTag(htmlStr);
  249. htmlStr = htmlStr.replaceAll("&nbsp;", "");
  250. return htmlStr;
  251. }
  252. }