PatentTask.java 9.2 KB

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