PatentTask.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 org.apache.commons.lang3.time.DateFormatUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.scheduling.annotation.Scheduled;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestMethod;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import com.goafanti.admin.service.AdminService;
  19. import com.goafanti.common.bo.EmailBo;
  20. import com.goafanti.common.dao.NoticeMapper;
  21. import com.goafanti.common.enums.NoticeStatus;
  22. import com.goafanti.common.enums.PatentCategoryStatus;
  23. import com.goafanti.common.model.Admin;
  24. import com.goafanti.common.model.Notice;
  25. import com.goafanti.common.model.PatentNew;
  26. import com.goafanti.common.model.PatentNewLog;
  27. import com.goafanti.common.utils.LoggerUtils;
  28. import com.goafanti.common.utils.SendEmailUtil;
  29. import com.goafanti.patent.service.PatentNewService;
  30. @Component
  31. @RestController
  32. public class PatentTask {
  33. @Resource
  34. private PatentNewService patentNewService;
  35. @Resource
  36. private AdminService adminService;
  37. @Autowired
  38. private NoticeMapper noticeMapper;
  39. int pointsDataLimit=50;
  40. private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
  41. private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
  42. private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
  43. private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
  44. @Scheduled(cron = "0 0 0 * * ?")
  45. @RequestMapping(value = "/open/patentRemind", method = RequestMethod.GET)
  46. public void patentRemind() {
  47. LoggerUtils.debug(getClass(), "====================开始检查专利提醒==================");
  48. try {
  49. List<PatentNew> l=patentNewService.AllselectStartPatentNew();
  50. List<Notice> nlist=new ArrayList<>();
  51. for (PatentNew p : l) {
  52. Calendar cst =Calendar.getInstance();
  53. Calendar ced =Calendar.getInstance();
  54. Calendar ted =Calendar.getInstance();
  55. cst.setTime(p.getApplyDate());
  56. ced.setTime(new Date());
  57. //获得累计年限
  58. int d = (int) ((ced.getTimeInMillis()-cst.getTimeInMillis()) / (1000*3600*24));
  59. int y=d/365;
  60. if(d%365>0)y++;
  61. ted.setTime(cst.getTime());
  62. ted.add(Calendar.YEAR,y);
  63. //计算相差日期
  64. int count = (int) ((ted.getTimeInMillis()-ced.getTimeInMillis()) / (1000*3600*24));
  65. Notice n =new Notice();
  66. n.setId(UUID.randomUUID().toString());
  67. n.setCreateTime(new Date());
  68. n.setReaded(0);//未读
  69. n.setAid("1");
  70. n.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode());
  71. if (count>=0&&count<=90) {
  72. Admin a=adminService.selectByPrimaryKey(p.getAid());
  73. //设置如果找不到用户则提醒管理员
  74. String str2="名称="+p.getName()+",email="+p.getEmail();;
  75. n.setContent(str2);
  76. if(a==null) {
  77. n.setContent(str2+",aid="+p.getAid());
  78. nlist.add(n);
  79. continue;
  80. }else if (!SendEmailUtil.isEmail(p.getEmail())) {
  81. n.setContent(str2+",管理员="+a.getName());
  82. nlist.add(n);
  83. //再发一条给管理员
  84. Notice n2 =new Notice();
  85. n2.setId(UUID.randomUUID().toString());
  86. n2.setCreateTime(new Date());
  87. n2.setReaded(0);//未读
  88. n2.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode());
  89. n2.setAid(a.getId());
  90. nlist.add(n2);
  91. continue;
  92. }else {
  93. //计算出年费金额,然后存入数据库
  94. BigDecimal money=countMoney(y,p.getType());
  95. ted.setTime(cst.getTime());
  96. ted.add(Calendar.YEAR, y);
  97. EmailBo emailBo = new EmailBo("专利提醒",p.getEmail(), p.getApplicant(), "科德集团", a.getName(),p.getName(), p.getPatentNo(), money.toString(), DateFormatUtils.format(ted, "yyyy-MM-dd"));
  98. SendEmailUtil.getInstance().patentSend(emailBo);
  99. p.setPatentAmount(money);
  100. p.setStatus(1);//提醒中
  101. p.setYears(y);
  102. patentNewService.updatePatenNew(p);
  103. PatentNewLog pl=new PatentNewLog();
  104. String str=StrToString("专利提醒",p.getEmail(), p.getApplicant(), a.getName(),p.getName(), p.getPatentNo(),
  105. money.toString(), DateFormatUtils.format(ted, "yyyy-MM-dd"));
  106. pl.setContent(str);
  107. pl.setCreateTime(new Date());
  108. pl.setPid(p.getId());
  109. patentNewService.addPatenNewLog(pl);
  110. n.setAid(p.getAid());
  111. n.setContent(str);
  112. n.setNoticeType(NoticeStatus.TASK_PATENT_REMIND.getCode());
  113. nlist.add(n);
  114. }
  115. }
  116. Thread.sleep(2000);
  117. }
  118. if (!nlist.isEmpty()) {
  119. List<Notice> newList=new ArrayList<>();
  120. for(int i=0;i<nlist.size();i++){
  121. newList.add(nlist.get(i));
  122. if(pointsDataLimit == newList.size()||i == nlist.size()-1){
  123. if(newList.size()>0) noticeMapper.insertBatch(newList);
  124. newList.clear();
  125. Thread.sleep(2000);
  126. }
  127. }
  128. }
  129. List<PatentNew> ls=patentNewService.AllselectEndPatentNew();
  130. for (PatentNew p : ls) {
  131. Calendar cst =Calendar.getInstance();
  132. Calendar ced =Calendar.getInstance();
  133. Calendar ted =Calendar.getInstance();
  134. cst.setTime(p.getApplyDate());
  135. ced.setTime(new Date());
  136. int d = (int) ((ced.getTimeInMillis()-cst.getTimeInMillis()) / (1000*3600*24));
  137. int y=(d/365);
  138. if(d%365>0)y++;
  139. ted.setTime(cst.getTime());
  140. ted.add(Calendar.YEAR,y);
  141. //计算相差日期
  142. int count = (int) ((ted.getTimeInMillis()-ced.getTimeInMillis()) / (1000*3600*24));
  143. if (count>200) {
  144. p.setStatus(0);//恢复待提醒
  145. patentNewService.updatePatenNew(p);
  146. }
  147. }
  148. } catch (Exception e) {
  149. LoggerUtils.debug(getClass(), "=====================专利提醒失败======================");
  150. Notice n =new Notice(UUID.randomUUID().toString(),new Date(),0,"1",NoticeStatus.TASK_PATENT_ERROR.getCode(),"==============专利提醒失败================");
  151. noticeMapper.insert(n);
  152. e.printStackTrace();
  153. }
  154. LoggerUtils.debug(getClass(), "=====================专利提醒结束======================");
  155. }
  156. private String StrToString(String name, String email, String applicant, String adminName, String pantentName,
  157. String pantentNo, String money, String format) {
  158. StringBuilder str =new StringBuilder("");
  159. str.append(name).append(":").append("邮箱:").append(email).append(",授权公司:").append(applicant).append(",录入人:").append(adminName)
  160. .append(",专利名称:").append(pantentName).append (",专利编号:").append(pantentNo).append(",金额:").append(money).append(",时间:").append(format);
  161. return str.toString();
  162. }
  163. /**
  164. *
  165. * @param 年份
  166. * @param 分类
  167. * @return 金额
  168. */
  169. private static BigDecimal countMoney(Integer y, Integer type) {
  170. int money=0;
  171. if (type==PatentCategoryStatus.INVENTION.getCode()) {
  172. if (y>0&&y<4) {
  173. money=900;
  174. }else if (y>3&&y<7) {
  175. money=1200;
  176. }else if (y>6&&y<10) {
  177. money=2000;
  178. }else if (y>9&&y<13) {
  179. money=4000;
  180. }else if (y>12&&y<16) {
  181. money=6000;
  182. }else if (y>15&&y<21) {
  183. money=8000;
  184. }
  185. } else {
  186. if (y>0&&y<4) {
  187. money=600;
  188. }else if (y>3&&y<6) {
  189. money=900;
  190. }else if (y>5&&y<9) {
  191. money=1200;
  192. }else if (y>8&&y<11) {
  193. money=2000;
  194. }
  195. }
  196. return new BigDecimal(money);
  197. }
  198. /**
  199. * @param htmlStr
  200. * @return
  201. * 删除Html标签
  202. */
  203. public static String delHTMLTag(String htmlStr) {
  204. Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
  205. Matcher m_script = p_script.matcher(htmlStr);
  206. htmlStr = m_script.replaceAll(""); // 过滤script标签
  207. Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
  208. Matcher m_style = p_style.matcher(htmlStr);
  209. htmlStr = m_style.replaceAll(""); // 过滤style标签
  210. Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
  211. Matcher m_html = p_html.matcher(htmlStr);
  212. htmlStr = m_html.replaceAll(""); // 过滤html标签
  213. Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
  214. Matcher m_space = p_space.matcher(htmlStr);
  215. htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
  216. return htmlStr.trim(); // 返回文本字符串
  217. }
  218. public static String getTextFromHtml(String htmlStr){
  219. htmlStr = delHTMLTag(htmlStr);
  220. htmlStr = htmlStr.replaceAll("&nbsp;", "");
  221. return htmlStr;
  222. }
  223. }