PatentTask.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package com.goafanti.common.task;
  2. import java.math.BigDecimal;
  3. import java.util.ArrayList;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.List;
  7. import java.util.UUID;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import javax.annotation.Resource;
  11. import javax.mail.SendFailedException;
  12. import org.apache.commons.lang3.time.DateFormatUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.scheduling.annotation.Scheduled;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import com.goafanti.admin.service.AdminService;
  20. import com.goafanti.common.bo.EmailBo;
  21. import com.goafanti.common.constant.AFTConstants;
  22. import com.goafanti.common.dao.UserMapper;
  23. import com.goafanti.common.enums.NoticeStatus;
  24. import com.goafanti.common.enums.PatentCategoryStatus;
  25. import com.goafanti.common.model.Admin;
  26. import com.goafanti.common.model.Notice;
  27. import com.goafanti.common.model.PatentNew;
  28. import com.goafanti.common.model.PatentNewLog;
  29. import com.goafanti.common.utils.AsyncUtils;
  30. import com.goafanti.common.utils.DateUtils;
  31. import com.goafanti.common.utils.LoggerUtils;
  32. import com.goafanti.common.utils.SendEmailUtil;
  33. import com.goafanti.patent.service.PatentNewService;
  34. @Component
  35. @RestController
  36. public class PatentTask {
  37. @Resource
  38. private PatentNewService patentNewService;
  39. @Resource
  40. private AdminService adminService;
  41. @Autowired
  42. private UserMapper userMapper;
  43. @Autowired
  44. private AsyncUtils asyncUtils;
  45. int pointsDataLimit=50;
  46. private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
  47. private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
  48. private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
  49. private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
  50. /**
  51. * 专利提醒定时任务
  52. * 每天0点触发任务
  53. * 1、先计算出申请时间与当前时间相隔天数 2、然后用相隔天使除以365取余计算年份差 3、在用申请时间加上年限获取最后日期
  54. * 4、在相隔30天、20天、10天、3天、2天、1天分别一次提醒。原是在距离90天提醒。
  55. */
  56. @Scheduled(cron = "0 0 0 * * ?")
  57. @RequestMapping(value = "/open/patentRemind", method = RequestMethod.GET)
  58. public void patentRemind() {
  59. LoggerUtils.debug(getClass(), "====================开始检查专利提醒==================");
  60. try {
  61. List<PatentNew> l = patentNewService.AllselectStartPatentNew();
  62. List<Notice> nlist = new ArrayList<>();
  63. List<PatentNewLog> logList = new ArrayList<>();
  64. for (PatentNew p : l) {
  65. //因为改成数据关联字段修改
  66. String userName ="";
  67. if (p.getUid()!=null) {
  68. userName=userMapper.selectByPrimaryKey(p.getUid()).getNickname();
  69. }else {
  70. userName=p.getApplicant();
  71. }
  72. // 申请时间
  73. Calendar sq = Calendar.getInstance();
  74. sq.setTime(p.getApplyDate());
  75. Integer status = 0;
  76. // 当前时间
  77. Calendar now = Calendar.getInstance();
  78. now.setTime(new Date());
  79. now.set(Calendar.MILLISECOND, 0);
  80. now.set(Calendar.HOUR_OF_DAY, 0);
  81. now.clear(Calendar.MINUTE);
  82. now.clear(Calendar.SECOND);
  83. Date date=new Date();
  84. // 提醒
  85. Calendar ted = Calendar.getInstance();
  86. // 先获取到目前多少天数,再计算出年限
  87. int d = (int) ((now.getTimeInMillis() - sq.getTimeInMillis()) / (1000 * 3600 * 24));
  88. int y = d / 365;
  89. if (d % 365 > 0)
  90. y++;
  91. ted.setTime(sq.getTime());
  92. ted.add(Calendar.YEAR, y);
  93. // 计算相差日期
  94. int count = (int) ((ted.getTimeInMillis() - now.getTimeInMillis()) / (1000 * 3600 * 24));
  95. Notice n = new Notice();
  96. n.setId(UUID.randomUUID().toString());
  97. n.setCreateTime(date);
  98. n.setReaded(0);// 未读
  99. n.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode());
  100. boolean flag=false;
  101. if (count > 30 && count <= 90&&p.getStatus()!=1) {
  102. flag=true;
  103. status = 1;
  104. } else if (count > 20 && count <= 30&&p.getStatus()!=2) {
  105. flag=true;
  106. status = 2;
  107. }else if (count > 10 && count <= 20&&p.getStatus()!=3) {
  108. flag=true;
  109. status = 3;
  110. }else if (count > 3 && count <= 10&&p.getStatus()!=4) {
  111. flag=true;
  112. status = 4;
  113. }else if (count == 3&&p.getStatus()!=5) {
  114. flag=true;
  115. status = 5;
  116. }else if (count == 2&&p.getStatus()!=6) {
  117. flag=true;
  118. status = 6;
  119. }else if (count == 1&&p.getStatus()!=7) {
  120. flag=true;
  121. status = 7;
  122. }
  123. if (flag) {
  124. Admin a = adminService.selectByPrimaryKey(p.getAid());
  125. StringBuffer str2 = new StringBuffer();
  126. str2.append("专利编号=").append(p.getPatentNo()).append("名称=").append(p.getName()).append(",email=")
  127. .append(p.getEmail());
  128. n.setContent(str2.toString());
  129. if (a == null) {
  130. str2.append(",aid=").append(p.getAid()).append(",找不到管理员。");
  131. n.setContent(str2.toString());
  132. n.setAid("1");
  133. nlist.add(n);
  134. continue;
  135. } else if (!SendEmailUtil.isEmail(p.getEmail().trim())) {
  136. str2.append(",管理员=").append(a.getName()).append(",邮箱不正确。");
  137. n.setAid("1");
  138. n.setContent(str2.toString());
  139. nlist.add(n);
  140. // 再发一条给当事人
  141. Notice n2 = new Notice();
  142. n2.setId(UUID.randomUUID().toString());
  143. n2.setCreateTime(date);
  144. n2.setReaded(0);// 未读
  145. n2.setContent(str2.toString());
  146. n2.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode());
  147. n2.setAid(a.getId());
  148. nlist.add(n2);
  149. continue;
  150. } else {
  151. // 计算出年费金额,然后存入数据库
  152. BigDecimal money = countMoney(y, p.getType());
  153. EmailBo emailBo = new EmailBo("专利提醒", p.getEmail().trim(),userName, "科德集团", a.getName(),
  154. p.getName(), p.getPatentNo(), money.toString(), DateFormatUtils.format(ted, "yyyy-MM-dd"),status);
  155. n.setNoticeType(NoticeStatus.TASK_PATENT_REMIND.getCode());
  156. String title = "专利提醒成功!";
  157. p.setStatus(status);// 提醒中
  158. try {
  159. asyncUtils.patentSend(emailBo);
  160. } catch (SendFailedException e) {
  161. // 如果邮件发送失败提醒
  162. n.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode());
  163. title = "提醒失败!对方邮箱不存在或者拒收!";
  164. p.setStatus(0);
  165. }
  166. p.setPatentAmount(money);
  167. p.setYears(y);
  168. patentNewService.updatePatenNew(p);
  169. PatentNewLog pl = new PatentNewLog();
  170. String str = StrToString(title, p.getEmail().trim(), userName ,p.getName(),date,status);
  171. pl.setContent(str);
  172. pl.setCreateTime(new Date());
  173. pl.setPid(p.getId());
  174. logList.add(pl);
  175. n.setAid(p.getAid());
  176. n.setContent(str);
  177. nlist.add(n);
  178. }
  179. }
  180. }
  181. if (!nlist.isEmpty()) {
  182. List<Notice> newList = new ArrayList<>();
  183. for (int i = 0; i < nlist.size(); i++) {
  184. newList.add(nlist.get(i));
  185. if (pointsDataLimit == newList.size() || i == nlist.size() - 1) {
  186. if (newList.size() > 0) asyncUtils.addNoticeBatch(newList);
  187. Thread.sleep(2000);
  188. newList.clear();
  189. }
  190. }
  191. }
  192. if (!logList.isEmpty()) {
  193. List<PatentNewLog> logList2 = new ArrayList<>();
  194. for (int i = 0; i < logList.size(); i++) {
  195. logList2.add(logList.get(i));
  196. if (pointsDataLimit == logList2.size() || i == logList.size() - 1) {
  197. if (logList2.size() > 0) patentNewService.insertLogBatch(logList2);
  198. logList2.clear();
  199. Thread.sleep(2000);
  200. }
  201. }
  202. }
  203. } catch (Exception e) {
  204. LoggerUtils.debug(getClass(), "=====================专利提醒失败======================");
  205. Notice n = new Notice(UUID.randomUUID().toString(), new Date(), 0, "1",
  206. NoticeStatus.TASK_PATENT_ERROR.getCode(), "==============专利提醒失败================");
  207. asyncUtils.addNotice(n);
  208. e.printStackTrace();
  209. }
  210. LoggerUtils.debug(getClass(), "=====================专利提醒结束======================");
  211. }
  212. private String StrToString(String title, String email, String userName, String pname, Date date, Integer status) {
  213. //状态 0 未提醒 1 剩余90天 2剩余30天 3剩余20天 4剩余10天 5剩余3天 6剩余2天 7剩余1天
  214. Integer days=0;
  215. StringBuffer sb=new StringBuffer().append(title).append("客户[").append(userName).append("]专利[")
  216. .append(pname).append("]");
  217. if (status==1) days=90;
  218. else if (status==2) days=30;
  219. else if (status==3) days=20;
  220. else if (status==4) days=10;
  221. else if (status==5) days=3;
  222. else if (status==6) days=2;
  223. else if (status==7) days=1;
  224. if(days>0) {
  225. sb.append(",续费时间还剩").append(days).append("天");
  226. }
  227. sb.append(",已于").append(DateUtils.formatDate(date, AFTConstants.YYYYMMDDHHMMSS)).append("发送专利提醒至邮箱")
  228. .append(email).append("!");
  229. return sb.toString();
  230. }
  231. /**
  232. *
  233. * @param 年份
  234. * @param 分类
  235. * @return 金额
  236. */
  237. private static BigDecimal countMoney(Integer y, Integer type) {
  238. int money=0;
  239. if (type==PatentCategoryStatus.INVENTION.getCode()) {
  240. if (y>0&&y<4) {
  241. money=900;
  242. }else if (y>3&&y<7) {
  243. money=1200;
  244. }else if (y>6&&y<10) {
  245. money=2000;
  246. }else if (y>9&&y<13) {
  247. money=4000;
  248. }else if (y>12&&y<16) {
  249. money=6000;
  250. }else if (y>15&&y<21) {
  251. money=8000;
  252. }
  253. } else {
  254. if (y>0&&y<4) {
  255. money=600;
  256. }else if (y>3&&y<6) {
  257. money=900;
  258. }else if (y>5&&y<9) {
  259. money=1200;
  260. }else if (y>8&&y<11) {
  261. money=2000;
  262. }
  263. }
  264. return new BigDecimal(money);
  265. }
  266. /**
  267. * @param htmlStr
  268. * @return
  269. * 删除Html标签
  270. */
  271. public static String delHTMLTag(String htmlStr) {
  272. Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
  273. Matcher m_script = p_script.matcher(htmlStr);
  274. htmlStr = m_script.replaceAll(""); // 过滤script标签
  275. Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
  276. Matcher m_style = p_style.matcher(htmlStr);
  277. htmlStr = m_style.replaceAll(""); // 过滤style标签
  278. Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
  279. Matcher m_html = p_html.matcher(htmlStr);
  280. htmlStr = m_html.replaceAll(""); // 过滤html标签
  281. Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
  282. Matcher m_space = p_space.matcher(htmlStr);
  283. htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
  284. return htmlStr.trim(); // 返回文本字符串
  285. }
  286. public static String getTextFromHtml(String htmlStr){
  287. htmlStr = delHTMLTag(htmlStr);
  288. htmlStr = htmlStr.replaceAll("&nbsp;", "");
  289. return htmlStr;
  290. }
  291. public static void main(String[] args) {
  292. // 申请时间一月份是0。
  293. Calendar sq = Calendar.getInstance();
  294. sq.set(2020, 7, 29);
  295. System.out.println(DateUtils.formatDate(sq.getTime(), AFTConstants.YYYYMMDD));
  296. // 当前时间
  297. Calendar now = Calendar.getInstance();
  298. now.setTime(new Date());
  299. now.set(Calendar.MILLISECOND, 0);
  300. // 提醒
  301. Calendar ted = Calendar.getInstance();
  302. // 先获取到目前多少天数,再计算出年限
  303. int d = (int) ((now.getTimeInMillis() - sq.getTimeInMillis()) / (1000 * 3600 * 24));
  304. System.out.println(d);
  305. int y = d / 365;
  306. if (d % 365 > 0)y++;
  307. System.out.println(y);
  308. ted.setTime(sq.getTime());
  309. ted.add(Calendar.YEAR, y);
  310. System.out.println(DateUtils.formatDate(ted.getTime(), AFTConstants.YYYYMMDD));
  311. // 计算相差日期
  312. int count = (int) ((ted.getTimeInMillis() - now.getTimeInMillis()) / (1000 * 3600 * 24));
  313. System.out.println(count);
  314. }
  315. }