package com.goafanti.common.task; import com.goafanti.admin.service.AdminService; import com.goafanti.common.constant.AFTConstants; import com.goafanti.common.enums.NoticeStatus; import com.goafanti.common.model.Admin; import com.goafanti.common.model.Notice; import com.goafanti.common.model.PatentNewLog; import com.goafanti.common.utils.AsyncUtils; import com.goafanti.common.utils.DateUtils; import com.goafanti.common.utils.LoggerUtils; import com.goafanti.common.utils.SendEmailUtil; import com.goafanti.patent.bo.PatentNewBo; import com.goafanti.patent.service.PatentNewService; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @Component //@RestController public class PatentTask { @Resource private PatentNewService patentNewService; @Resource private AdminService adminService; @Resource private AsyncUtils asyncUtils; int pointsDataLimit=50; private static final String regEx_script = "]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式 private static final String regEx_style = "]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式 private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式 private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符 /** * 专利提醒定时任务 * 每天0点触发任务 * 1、先计算出申请时间与当前时间相隔天数 2、然后用相隔天使除以365取余计算年份差 3、在用申请时间加上年限获取最后日期 * 4、在相隔30天、20天、10天、3天、2天、1天分别一次提醒。原是在距离90天提醒。 */ @Scheduled(cron = "0 0 0 * * ?") // @RequestMapping(value = "/open/patentRemind", method = RequestMethod.GET) public void patentRemind() { LoggerUtils.debug(getClass(), "====================开始检查专利提醒=================="); try { List l = patentNewService.AllselectStartPatentNew(); List nlist = new ArrayList<>(); List logList = new ArrayList<>(); List s1=new ArrayList(); List s2=new ArrayList(); List s3=new ArrayList(); List s4=new ArrayList(); List s5=new ArrayList(); List s6=new ArrayList(); Date date=new Date(); for (PatentNewBo p : l) { Integer status=0; // 计算相差日期 int count = patentNewService.countDays(p.getEndDate()); boolean flag=false; if (count > 60 && count <= 90&&p.getStatus()!=1) { flag=true; status = 1; s1.add(p.getId().toString()); } else if (count > 30 && count <= 60&&p.getStatus()!=2) { flag=true; status = 2; s2.add(p.getId().toString()); }else if (count > 15 && count <= 30&&p.getStatus()!=3) { flag=true; status = 3; s3.add(p.getId().toString()); }else if (count > 7 && count <= 15&&p.getStatus()!=4) { flag=true; status = 4; s4.add(p.getId().toString()); }else if (count>0&&count <= 7&&p.getStatus()!=5) { flag=true; status = 5; s5.add(p.getId().toString()); }else if(count<0) { flag=true; s6.add(p.getId().toString()); } if (flag) { Notice n = new Notice(); n.setId(UUID.randomUUID().toString()); n.setCreateTime(date); n.setReaded(0);// 未读 n.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode()); Admin a = adminService.selectByPrimaryKey(p.getAid()); StringBuffer str2 = new StringBuffer(); str2.append("专利编号=").append(p.getPatentNo()).append("名称=").append(p.getName()); if (a == null) { str2.append(",aid=").append(p.getAid()).append(",找不到管理员。"); n.setAid("1"); n.setContent(str2.toString()); nlist.add(n); } else if (!SendEmailUtil.isEmail(p.getEmail())) { str2.append(",管理员=").append(a.getName()).append(",邮箱不正确。"); n.setAid("1"); n.setContent(str2.toString()); nlist.add(n); // 再发一条给当事人 Notice n2 = new Notice(); n2.setId(UUID.randomUUID().toString()); n2.setCreateTime(date); n2.setReaded(0);// 未读 n2.setContent(str2.toString()); n2.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode()); n2.setAid(a.getId()); nlist.add(n2); } else { if (p.getSalesmanRemind()==0){ Notice nt = new Notice(); nt.setId(UUID.randomUUID().toString()); nt.setCreateTime(date); nt.setReaded(0);// 未读 nt.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode()); PatentNewLog pl = new PatentNewLog(); StringBuffer str =new StringBuffer(); str.append("专利编号[").append(p.getPatentNo()).append("],名称[").append(p.getName()).append("],email[") .append(p.getEmail().trim()).append("]").append("已超过结束时间,但未做提醒,请悉知!!!"); pl.setContent(str.toString()); pl.setCreateTime(new Date()); pl.setPid(p.getId()); logList.add(pl); n.setAid(p.getAid()); n.setNoticeType(NoticeStatus.TASK_PATENT_REMIND.getCode()); n.setContent(str.toString()); nlist.add(n); }else { PatentNewLog pl = new PatentNewLog(); str2.append("专利编号[").append(p.getPatentNo()).append("],名称[").append(p.getName()).append("],email[") .append(p.getEmail().trim()).append("]"); String str = StrToString(str2,p.getEndDate(),status); pl.setContent(str); pl.setCreateTime(new Date()); pl.setPid(p.getId()); logList.add(pl); n.setAid(p.getAid()); n.setNoticeType(NoticeStatus.TASK_PATENT_REMIND.getCode()); n.setContent(str); nlist.add(n); } } } } pushNotice(nlist); pushPatentLog(logList); //将不同状态的修改成不同状态 updatePatentNew(s1,1); updatePatentNew(s2,2); updatePatentNew(s3,3); updatePatentNew(s4,4); updatePatentNew(s5,5); patentNewService.pushResetPatentNew(s6); } catch (Exception e) { LoggerUtils.debug(getClass(), "=====================专利提醒失败======================"); Notice n = new Notice(UUID.randomUUID().toString(), new Date(), 0, "1", NoticeStatus.TASK_PATENT_ERROR.getCode(), "==============专利提醒失败================"); asyncUtils.addNotice(n); e.printStackTrace(); } LoggerUtils.debug(getClass(), "=====================专利提醒结束======================"); } private void pushNotice(List nlist) throws InterruptedException { if (!nlist.isEmpty()) { List newList = new ArrayList<>(); for (int i = 0; i < nlist.size(); i++) { newList.add(nlist.get(i)); if (pointsDataLimit == newList.size() || i == nlist.size() - 1) { if (newList.size() > 0) asyncUtils.addNoticeBatch(newList); Thread.sleep(2000); newList.clear(); } } } } private void pushPatentLog(List logList) throws InterruptedException { if (!logList.isEmpty()) { List logList2 = new ArrayList<>(); for (int i = 0; i < logList.size(); i++) { logList2.add(logList.get(i)); if (pointsDataLimit == logList2.size() || i == logList.size() - 1) { if (logList2.size() > 0) patentNewService.insertLogBatch(logList2); Thread.sleep(2000); logList2.clear(); } } } } /** * 修改专利状态 * @param status * @throws InterruptedException */ private void updatePatentNew(List list, Integer status) throws InterruptedException { if (!list.isEmpty()) { List l2 = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { l2.add(list.get(i)); if (pointsDataLimit == l2.size() || i == list.size() - 1) { if (l2.size() > 0) patentNewService.updateBatchStatusByid(l2, status); l2.clear(); Thread.sleep(2000); } } } } private String StrToString(StringBuffer str, Date date, Integer status) { //状态 0 未提醒 1 剩余90天 2剩余30天 3剩余20天 4剩余10天 5剩余3天 6剩余2天 7剩余1天 Integer days=0; if (status==1) days=90; else if (status==2) days=60; else if (status==3) days=30; else if (status==4) days=15; else if (status==5) days=7; if(days>0) { str.append(",续费时间还剩").append(days).append("天"); } str.append(",截止时间[").append(DateUtils.formatDate(date, AFTConstants.YYYYMMDD)).append("]请在截止时间前通知客户!"); return str.toString(); } /** * @param htmlStr * @return * 删除Html标签 */ public static String delHTMLTag(String htmlStr) { Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); Matcher m_script = p_script.matcher(htmlStr); htmlStr = m_script.replaceAll(""); // 过滤script标签 Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); Matcher m_style = p_style.matcher(htmlStr); htmlStr = m_style.replaceAll(""); // 过滤style标签 Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); Matcher m_html = p_html.matcher(htmlStr); htmlStr = m_html.replaceAll(""); // 过滤html标签 Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE); Matcher m_space = p_space.matcher(htmlStr); htmlStr = m_space.replaceAll(""); // 过滤空格回车标签 return htmlStr.trim(); // 返回文本字符串 } public static String getTextFromHtml(String htmlStr){ htmlStr = delHTMLTag(htmlStr); htmlStr = htmlStr.replaceAll(" ", ""); return htmlStr; } }