PatentTask.java 8.3 KB

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