package com.goafanti.common.task; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.Resource; import javax.mail.SendFailedException; import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.goafanti.admin.service.AdminService; import com.goafanti.common.bo.EmailBo; import com.goafanti.common.constant.AFTConstants; import com.goafanti.common.dao.UserMapper; import com.goafanti.common.enums.NoticeStatus; import com.goafanti.common.enums.PatentCategoryStatus; import com.goafanti.common.model.Admin; import com.goafanti.common.model.Notice; import com.goafanti.common.model.PatentNew; 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.service.PatentNewService; @Component @RestController public class PatentTask { @Resource private PatentNewService patentNewService; @Resource private AdminService adminService; @Autowired private UserMapper userMapper; @Autowired 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<>(); for (PatentNew p : l) { //因为改成数据关联字段修改 String userName =""; if (p.getUid()!=null) { userName=userMapper.selectByPrimaryKey(p.getUid()).getNickname(); }else { userName=p.getApplicant(); } // 申请时间 Calendar sq = Calendar.getInstance(); sq.setTime(p.getApplyDate()); Integer status = 0; // 当前时间 Calendar now = Calendar.getInstance(); now.setTime(new Date()); now.set(Calendar.MILLISECOND, 0); now.set(Calendar.HOUR_OF_DAY, 0); now.clear(Calendar.MINUTE); now.clear(Calendar.SECOND); Date date=new Date(); // 提醒 Calendar ted = Calendar.getInstance(); // 先获取到目前多少天数,再计算出年限 int d = (int) ((now.getTimeInMillis() - sq.getTimeInMillis()) / (1000 * 3600 * 24)); int y = d / 365; if (d % 365 > 0) y++; ted.setTime(sq.getTime()); ted.add(Calendar.YEAR, y); // 计算相差日期 int count = (int) ((ted.getTimeInMillis() - now.getTimeInMillis()) / (1000 * 3600 * 24)); Notice n = new Notice(); n.setId(UUID.randomUUID().toString()); n.setCreateTime(date); n.setReaded(0);// 未读 n.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode()); boolean flag=false; if (count > 30 && count <= 90&&p.getStatus()!=1) { flag=true; status = 1; } else if (count > 20 && count <= 30&&p.getStatus()!=2) { flag=true; status = 2; }else if (count > 10 && count <= 20&&p.getStatus()!=3) { flag=true; status = 3; }else if (count > 3 && count <= 10&&p.getStatus()!=4) { flag=true; status = 4; }else if (count == 3&&p.getStatus()!=5) { flag=true; status = 5; }else if (count == 2&&p.getStatus()!=6) { flag=true; status = 6; }else if (count == 1&&p.getStatus()!=7) { flag=true; status = 7; } if (flag) { Admin a = adminService.selectByPrimaryKey(p.getAid()); StringBuffer str2 = new StringBuffer(); str2.append("专利编号=").append(p.getPatentNo()).append("名称=").append(p.getName()).append(",email=") .append(p.getEmail()); n.setContent(str2.toString()); if (a == null) { str2.append(",aid=").append(p.getAid()).append(",找不到管理员。"); n.setContent(str2.toString()); n.setAid("1"); nlist.add(n); continue; } else if (!SendEmailUtil.isEmail(p.getEmail().trim())) { 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); continue; } else { // 计算出年费金额,然后存入数据库 BigDecimal money = countMoney(y, p.getType()); EmailBo emailBo = new EmailBo("专利提醒", p.getEmail().trim(),userName, "科德集团", a.getName(), p.getName(), p.getPatentNo(), money.toString(), DateFormatUtils.format(ted, "yyyy-MM-dd"),status); n.setNoticeType(NoticeStatus.TASK_PATENT_REMIND.getCode()); String title = "专利提醒成功!"; p.setStatus(status);// 提醒中 try { asyncUtils.patentSend(emailBo); } catch (SendFailedException e) { // 如果邮件发送失败提醒 n.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode()); title = "提醒失败!对方邮箱不存在或者拒收!"; p.setStatus(0); } p.setPatentAmount(money); p.setYears(y); patentNewService.updatePatenNew(p); PatentNewLog pl = new PatentNewLog(); String str = StrToString(title, p.getEmail().trim(), userName ,p.getName(),date,status); pl.setContent(str); pl.setCreateTime(new Date()); pl.setPid(p.getId()); logList.add(pl); n.setAid(p.getAid()); n.setContent(str); nlist.add(n); } } } 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(); } } } 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); logList2.clear(); Thread.sleep(2000); } } } } 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 String StrToString(String title, String email, String userName, String pname, Date date, Integer status) { //状态 0 未提醒 1 剩余90天 2剩余30天 3剩余20天 4剩余10天 5剩余3天 6剩余2天 7剩余1天 Integer days=0; StringBuffer sb=new StringBuffer().append(title).append("客户[").append(userName).append("]专利[") .append(pname).append("]"); if (status==1) days=90; else if (status==2) days=30; else if (status==3) days=20; else if (status==4) days=10; else if (status==5) days=3; else if (status==6) days=2; else if (status==7) days=1; if(days>0) { sb.append(",续费时间还剩").append(days).append("天"); } sb.append(",已于").append(DateUtils.formatDate(date, AFTConstants.YYYYMMDDHHMMSS)).append("发送专利提醒至邮箱") .append(email).append("!"); return sb.toString(); } /** * * @param 年份 * @param 分类 * @return 金额 */ private static BigDecimal countMoney(Integer y, Integer type) { int money=0; if (type==PatentCategoryStatus.INVENTION.getCode()) { if (y>0&&y<4) { money=900; }else if (y>3&&y<7) { money=1200; }else if (y>6&&y<10) { money=2000; }else if (y>9&&y<13) { money=4000; }else if (y>12&&y<16) { money=6000; }else if (y>15&&y<21) { money=8000; } } else { if (y>0&&y<4) { money=600; }else if (y>3&&y<6) { money=900; }else if (y>5&&y<9) { money=1200; }else if (y>8&&y<11) { money=2000; } } return new BigDecimal(money); } /** * @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; } public static void main(String[] args) { // 申请时间一月份是0。 Calendar sq = Calendar.getInstance(); sq.set(2020, 7, 29); System.out.println(DateUtils.formatDate(sq.getTime(), AFTConstants.YYYYMMDD)); // 当前时间 Calendar now = Calendar.getInstance(); now.setTime(new Date()); now.set(Calendar.MILLISECOND, 0); // 提醒 Calendar ted = Calendar.getInstance(); // 先获取到目前多少天数,再计算出年限 int d = (int) ((now.getTimeInMillis() - sq.getTimeInMillis()) / (1000 * 3600 * 24)); System.out.println(d); int y = d / 365; if (d % 365 > 0)y++; System.out.println(y); ted.setTime(sq.getTime()); ted.add(Calendar.YEAR, y); System.out.println(DateUtils.formatDate(ted.getTime(), AFTConstants.YYYYMMDD)); // 计算相差日期 int count = (int) ((ted.getTimeInMillis() - now.getTimeInMillis()) / (1000 * 3600 * 24)); System.out.println(count); } }