PatentNewServiceImpl.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. package com.goafanti.patent.service.impl;
  2. import java.io.UnsupportedEncodingException;
  3. import java.math.BigDecimal;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.UUID;
  12. import org.apache.poi.ss.usermodel.Cell;
  13. import org.apache.poi.ss.usermodel.CellType;
  14. import org.apache.poi.ss.usermodel.Row;
  15. import org.apache.poi.ss.usermodel.Sheet;
  16. import org.apache.poi.ss.usermodel.Workbook;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import com.goafanti.common.bo.EmailBo;
  22. import com.goafanti.common.bo.Error;
  23. import com.goafanti.common.constant.AFTConstants;
  24. import com.goafanti.common.dao.AdminMapper;
  25. import com.goafanti.common.dao.PatentNewLogMapper;
  26. import com.goafanti.common.dao.PatentNewMapper;
  27. import com.goafanti.common.dao.PatentPaymentDetailsMapper;
  28. import com.goafanti.common.dao.PatentYearPaymentMapper;
  29. import com.goafanti.common.dao.TOrderTaskMapper;
  30. import com.goafanti.common.enums.NoticeStatus;
  31. import com.goafanti.common.error.BusinessException;
  32. import com.goafanti.common.model.Admin;
  33. import com.goafanti.common.model.Notice;
  34. import com.goafanti.common.model.PatentNew;
  35. import com.goafanti.common.model.PatentNewLog;
  36. import com.goafanti.common.model.PatentPaymentDetails;
  37. import com.goafanti.common.model.PatentYearPayment;
  38. import com.goafanti.common.model.TOrderTask;
  39. import com.goafanti.common.utils.AsyncUtils;
  40. import com.goafanti.common.utils.DateUtils;
  41. import com.goafanti.common.utils.ExcelUtils;
  42. import com.goafanti.common.utils.SendEmailUtil;
  43. import com.goafanti.common.utils.excel.NewExcelUtil;
  44. import com.goafanti.core.mybatis.BaseMybatisDao;
  45. import com.goafanti.core.mybatis.page.Pagination;
  46. import com.goafanti.core.shiro.token.TokenManager;
  47. import com.goafanti.patent.bo.PatentNewBo;
  48. import com.goafanti.patent.enums.PatentType;
  49. import com.goafanti.patent.service.PatentNewService;
  50. import cn.jiguang.common.utils.StringUtils;
  51. import javax.mail.MessagingException;
  52. @Service
  53. public class PatentNewServiceImpl extends BaseMybatisDao<PatentNewMapper> implements PatentNewService {
  54. @Autowired
  55. private PatentNewMapper patentNewMapper;
  56. @Autowired
  57. private PatentNewLogMapper patentNewLogMapper;
  58. @Autowired
  59. private TOrderTaskMapper tOrderTaskMapper;
  60. @Autowired
  61. private PatentPaymentDetailsMapper patentPaymentDetailsMapper;
  62. @Autowired
  63. private PatentYearPaymentMapper patentYearPaymentMapper;
  64. @Autowired
  65. private AsyncUtils asyncUtils;
  66. @Autowired
  67. private AdminMapper adminMapper;
  68. private List<PatentYearPayment> patentYear=new ArrayList<PatentYearPayment>();
  69. @Value(value = "${upload.path}")
  70. private String uploadPath=null;
  71. @Override
  72. public int addPatenNew(PatentNew p) {
  73. p.setStatus(0);//
  74. p.setAid(TokenManager.getAdminId());
  75. return patentNewMapper.insertSelective(p);
  76. }
  77. @Override
  78. public int updatePatenNew(PatentNewBo p) {
  79. Admin a = adminMapper.selectByPrimaryKey(TokenManager.getAdminId());
  80. PatentNew pn=patentNewMapper.selectByPrimaryKey(p.getId());
  81. StringBuffer str=new StringBuffer(a.getName());
  82. if (!p.getApplyDate().equals(pn.getApplyDate())
  83. || p.getType() != pn.getType()
  84. || ! p.getTid().equals(pn.getTid())) {
  85. p.setYears(getyear(p));
  86. p.setPatentAmount(sumAmout(p.getType(),p.getTid(),p.getYears()));
  87. if(p.getPatentAmount()==null) {
  88. throw new BusinessException(new Error("年份超过最大可提醒日期。"));
  89. }
  90. p.setStatus(getSatus(p.getEndDate()));
  91. sumYears(p);
  92. }
  93. addPatentNewLog(p,pn,str);
  94. return patentNewMapper.updateByPrimaryKeySelective(p);
  95. }
  96. private void addPatentNewLog(PatentNewBo p, PatentNew pn, StringBuffer str) {
  97. PatentNewLog pl = new PatentNewLog();
  98. pl.setPid(p.getId());
  99. boolean flag=false;
  100. StringBuffer s=new StringBuffer();
  101. if (p.getPatentNo()!=null&&!p.getPatentNo().equals(pn.getPatentNo())) {
  102. s.append("[专利号:").append(pn.getPatentNo()).append(" => ").append(p.getPatentNo()).append("],");
  103. flag=true;
  104. }
  105. if (p.getName()!=null&&!p.getName().equals(pn.getName())) {
  106. s.append("[专利名称:").append(pn.getName()).append(" => ").append(p.getName()).append("],");
  107. flag=true;
  108. }
  109. if (p.getType()!=null&&!p.getType().equals(pn.getType())) {
  110. // 0=发明专利,1=使用新型,2=外观设计
  111. String type = getPatentType(p.getType());
  112. String type1 = getPatentType(pn.getType());
  113. s.append("[专利类型:").append(type1).append(" => ").append(type).append("],");
  114. flag=true;
  115. }
  116. if (p.getTid()!=null&&!p.getTid().equals(pn.getTid())) {
  117. s.append("[项目编号:").append(pn.getTid()).append(" => ").append(p.getTid()).append("],");
  118. flag=true;
  119. }
  120. if (p.getApplyDate()!=null&&!p.getApplyDate().equals(pn.getApplyDate())) {
  121. String date = DateUtils.formatDate(p.getApplyDate(), AFTConstants.YYYYMMDD);
  122. String date1="";
  123. if(pn.getApplyDate()!=null) {
  124. date1 = DateUtils.formatDate(pn.getApplyDate(), AFTConstants.YYYYMMDD);
  125. }
  126. s.append("[申请日期:").append(date1).append(" => ").append(date).append("],");
  127. flag=true;
  128. }
  129. if (p.getAuthorizationDate()!=null&&!p.getAuthorizationDate().equals(pn.getAuthorizationDate())) {
  130. String date = DateUtils.formatDate(p.getAuthorizationDate(), AFTConstants.YYYYMMDD);
  131. String date1="";
  132. if(pn.getAuthorizationDate()!=null) {
  133. date1 = DateUtils.formatDate(pn.getAuthorizationDate(), AFTConstants.YYYYMMDD);
  134. }
  135. s.append("[授权日期:").append(date1).append(" => ").append(date).append("],");
  136. flag=true;
  137. }
  138. if (p.getEmail()!=null&&!p.getEmail().equals(pn.getEmail())) {
  139. s.append("[Email:").append(pn.getEmail()).append(" => ").append(p.getEmail()).append("],");
  140. flag=true;
  141. }
  142. if (p.getDelayingAmount()!=null&&p.getDelayingAmount().compareTo(pn.getDelayingAmount()) != 0) {
  143. s.append("[滞纳金:").append(pn.getDelayingAmount().stripTrailingZeros().toPlainString()).append(" => ")
  144. .append(p.getDelayingAmount().stripTrailingZeros().toPlainString()).append("],");
  145. flag=true;
  146. }
  147. if (p.getRecoveryAmount()!=null&&p.getRecoveryAmount().compareTo(pn.getRecoveryAmount()) != 0) {
  148. s.append("[权利恢复费:").append(pn.getRecoveryAmount().stripTrailingZeros().toPlainString()).append(" => ")
  149. .append(p.getRecoveryAmount().stripTrailingZeros().toPlainString()).append("],");
  150. flag=true;
  151. }
  152. if (p.getAnnualFeeStatus()!=null&&!p.getAnnualFeeStatus().equals(pn.getAnnualFeeStatus())) {
  153. s.append("[年费状态:").append(pn.getAnnualFeeStatus() == 1 ? "已缴费" : "未缴费").append(" => ")
  154. .append(p.getAnnualFeeStatus() == 1 ? "已缴费" : "未缴费").append("],");
  155. flag=true;
  156. }
  157. if (p.getHolders()!=null&&!p.getHolders().equals(pn.getHolders())) {
  158. s.append("[权利人:").append(pn.getHolders()).append(" => ").append(p.getHolders()).append("],");
  159. flag=true;
  160. }
  161. if (p.getInventor()!=null&&!p.getInventor().equals(pn.getInventor())) {
  162. s.append("[发明人:").append(pn.getInventor()).append(" => ").append(p.getInventor()).append("],");
  163. flag=true;
  164. }
  165. if (flag) {
  166. str.append("修改了:").append(s);
  167. pl.setContent(str.toString().substring(0, str.toString().length()-1));
  168. patentNewLogMapper.insertSelective(pl);
  169. }
  170. }
  171. private String getPatentType(Integer type) {
  172. if (type==0) return "发明专利";
  173. else if (type==1) return "实用新型";
  174. else if (type==2) return "外观设计";
  175. return null;
  176. }
  177. @SuppressWarnings("unchecked")
  178. @Override
  179. public Pagination<PatentNewBo> selectPatentNew(PatentNewBo p,Integer pageSize, Integer pageNo) {
  180. Map<String, Object> params = new HashMap<>();
  181. if (pageNo == null || pageNo < 0) pageNo = 1;
  182. if (pageSize == null || pageSize < 0) pageSize = 10;
  183. pushParams(p, params);
  184. Pagination<PatentNewBo> pa=(Pagination<PatentNewBo>) findPage("selectPatentNewList", "selectPatentNewCount", params, pageNo,
  185. pageSize);
  186. List<PatentNewBo> list=(List<PatentNewBo>) pa.getList();
  187. String aid=TokenManager.getAdminId();
  188. for (PatentNewBo pn : list) {
  189. if (aid.equals(pn.getAid())) {
  190. pn.setIsFollow(1);
  191. }else {
  192. pn.setIsFollow(0);
  193. }
  194. }
  195. return pa;
  196. }
  197. private void pushParams(PatentNewBo p, Map<String, Object> params) {
  198. if(p.getStatus()!=null)params.put("status", p.getStatus());
  199. if(p.getName()!=null)params.put("name", p.getName());
  200. if(p.getAname()!=null)params.put("aname", p.getAname());
  201. if(p.getPatentNo()!=null)params.put("patentNo", p.getPatentNo());
  202. if(p.getDepartmentId()!=null)params.put("departmentId", p.getDepartmentId());
  203. if(p.getStartDates()!=null)params.put("startDates", p.getStartDates());
  204. if(p.getEndDates()!=null)params.put("endDates", p.getEndDates()+" 23:59:59");
  205. if(p.getCreateStarts()!=null)params.put("createStarts", p.getCreateStarts());
  206. if(p.getCreateEnds()!=null)params.put("createEnds", p.getCreateEnds()+" 23:59:59");
  207. if(p.getType()!=null)params.put("type", p.getType());
  208. if(p.getDepName()!=null)params.put("depName", p.getDepName());
  209. if(p.getAnnualFeeStatus()!=null)params.put("annualFeeStatus", p.getAnnualFeeStatus());
  210. if(p.getUserName()!=null)params.put("userName", p.getUserName());
  211. if(p.getSalesmanRemind()!=null)params.put("salesmanRemind", p.getSalesmanRemind());
  212. if(p.getInputDep()!=null)params.put("inputDep", p.getInputDep());
  213. if(p.getFollowDep()!=null)params.put("followDep", p.getFollowDep());
  214. if(p.getIsFollow()!=null) {
  215. params.put("isFollow", p.getIsFollow());
  216. }else {
  217. params.put("isFollow", 0);
  218. }
  219. params.put("aid", TokenManager.getAdminId());
  220. if (TokenManager.hasRole(AFTConstants.SUPERADMIN) || TokenManager.hasRole(AFTConstants.PATENT_AUDITOR)) {
  221. params.put("shiro", 2);
  222. } else if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)) {
  223. params.put("shiro", 1);
  224. } else {
  225. params.put("shiro", 0);
  226. }
  227. }
  228. @Override
  229. public List<PatentNewBo> AllselectStartPatentNew() {
  230. return patentNewMapper.AllselectStartPatentNew();
  231. }
  232. @Override
  233. public int addPatenNewLog(PatentNewLog pl) {
  234. return patentNewLogMapper.insertSelective(pl);
  235. }
  236. @Override
  237. public List<PatentNew> AllselectEndPatentNew() {
  238. return patentNewMapper.AllselectEndPatentNew();
  239. }
  240. @Override
  241. public void insertLogBatch(List<PatentNewLog> list) {
  242. patentNewLogMapper.insertLogBatch(list);
  243. }
  244. @Override
  245. public List<PatentNewLog> selectPatentNewLog(Integer id) {
  246. return patentNewLogMapper.selectPatentNewLog(id);
  247. }
  248. @Override
  249. public void pushImportTemplate(MultipartFile file) {
  250. //获得Workbook工作薄对象
  251. Workbook workbook = ExcelUtils.getWorkBook(file);
  252. //创建一个对象,用来存储数据
  253. if(workbook != null){
  254. //获得当前sheet工作表
  255. Sheet sheet = workbook.getSheetAt(0);
  256. if(sheet == null){
  257. return;
  258. }
  259. //获得当前sheet的开始行
  260. int firstRowNum = sheet.getFirstRowNum();
  261. //获得当前sheet的结束行
  262. int lastRowNum = sheet.getLastRowNum();
  263. //处理空白行
  264. for (int i = firstRowNum; i <= sheet.getLastRowNum();i++) {
  265. Row r = sheet.getRow(i);
  266. if(r == null){
  267. // 如果是空行(即没有任何数据、格式),直接把它以下的数据往上移动
  268. sheet.shiftRows(i+1, sheet.getLastRowNum(),-1);
  269. continue;
  270. }
  271. boolean flag = false;
  272. for(Cell c:r){
  273. if(c.getCellTypeEnum() != CellType._NONE){
  274. flag = true;
  275. break;
  276. }
  277. }
  278. if(flag){
  279. i++;
  280. continue;
  281. } else{//如果是空白行(即可能没有数据,但是有一定格式)
  282. if(i == sheet.getLastRowNum())//如果到了最后一行,直接将那一行remove掉
  283. sheet.removeRow(r);
  284. else//如果还没到最后一行,则数据往上移一行
  285. sheet.shiftRows(i+1, sheet.getLastRowNum(),-1);
  286. }
  287. }
  288. lastRowNum=sheet.getLastRowNum();
  289. if (lastRowNum<2) {
  290. throw new BusinessException(new Error("未找到正确的参数。"));
  291. }
  292. List<PatentNew> list=new ArrayList<>();
  293. List<Notice> nl=new ArrayList<Notice>();
  294. String str= new String();
  295. //循环除了第2行的所有行
  296. for(int rowNum = firstRowNum+2;rowNum <= lastRowNum;rowNum++){
  297. //获得当前行
  298. Row row = sheet.getRow(rowNum);
  299. if(row == null){
  300. continue;
  301. }
  302. //获得当前行的开始列
  303. int firstCellNum = row.getFirstCellNum();
  304. //获得当前行的列数
  305. int lastCellNum = row.getLastCellNum();
  306. //循环当前行 cellNum = (firstCellNum+1) 则是除开第一例
  307. PatentNew trb=new PatentNew();
  308. for(int cellNum = (firstCellNum); cellNum < lastCellNum;cellNum++){
  309. Cell cell = row.getCell(cellNum);
  310. try {
  311. switch (cellNum) {
  312. case 0:
  313. trb.setPatentNo(ExcelUtils.getCellValue(cell));
  314. break;
  315. case 1:
  316. trb.setName(ExcelUtils.getCellValue(cell).trim());
  317. break;
  318. case 2:
  319. trb.setType(PatentType.getCodeByDesc(ExcelUtils.getCellValue(cell)));
  320. break;
  321. case 3:
  322. trb.setApplyDate(ExcelUtils.getCellDate(cell));
  323. break;
  324. case 4:
  325. trb.setHolders(ExcelUtils.getCellValue(cell));
  326. break;
  327. case 5:
  328. trb.setAuthorizationDate(ExcelUtils.getCellDate(cell));
  329. break;
  330. case 6:
  331. trb.setInventor(ExcelUtils.getCellValue(cell));
  332. break;
  333. case 7:
  334. trb.setEmail(ExcelUtils.getCellValue(cell));
  335. break;
  336. case 8:
  337. str=ExcelUtils.getCellValue(cell);
  338. if (StringUtils.isNotEmpty(str)) {
  339. trb.setTid(Integer.parseInt(str));
  340. }
  341. break;
  342. }
  343. } catch (Exception e) {
  344. e.printStackTrace();
  345. throw new BusinessException(new Error("表格第"+(rowNum+1)+"行"+(cellNum+1)+"列输入内容不正确。"));
  346. }
  347. if (cellNum==0&&trb.getPatentNo()!=null) {
  348. Integer i=patentNewMapper.selectByPatenNo(trb.getPatentNo());
  349. if (i>0) {
  350. throw new BusinessException(new Error("表格第"+(rowNum+1)+"行[专利编号]系统已存在。"));
  351. }
  352. }
  353. }
  354. for (PatentNew pt : list) {
  355. if (pt.getPatentNo().equals(trb.getPatentNo())) {
  356. throw new BusinessException(new Error("表格第"+(rowNum+1)+"行[专利编号]重复导入。"));
  357. }
  358. }
  359. if (trb.getType()==10) {
  360. throw new BusinessException(new Error("表格第"+(rowNum+1)+"专利类型错误。"));
  361. }
  362. Date date=new Date();
  363. trb.setAid(TokenManager.getAdminId());
  364. trb.setInputId(trb.getAid());
  365. trb.setCreateTime(date);
  366. trb.setNewType(1);
  367. trb.setYears(getyear(trb));
  368. trb.setStatus(getSatus(trb.getEndDate()));
  369. trb.setPatentAmount(sumAmout(trb.getType(),trb.getTid(),trb.getYears()));
  370. if(trb.getPatentAmount()==null) {
  371. throw new BusinessException(new Error("表格第"+(rowNum+1)+"行年份超过最大可提醒日期。"));
  372. }
  373. list.add(trb);
  374. if (trb.getStatus()!=0) {
  375. Notice n = addNotice(trb);
  376. n.setId(UUID.randomUUID().toString());
  377. n.setCreateTime(date);
  378. n.setReaded(0);// 未读
  379. n.setNoticeType(NoticeStatus.TASK_PATENT_REMIND.getCode());
  380. nl.add(n);
  381. }
  382. }
  383. List<PatentNew> list2=new ArrayList<>();
  384. for (int i = 0; i < list.size(); i++) {
  385. list2.add(list.get(i));
  386. if (50 == list2.size() || i == list.size() - 1) {
  387. if (list2.size() > 0) {
  388. patentNewMapper.insertBatch(list2);
  389. }
  390. list2.clear();
  391. }
  392. }
  393. asyncUtils.addNoticeBatch(nl);
  394. }
  395. }
  396. private Notice addNotice(PatentNew p) {
  397. Notice n = new Notice();
  398. Date date=new Date();
  399. n.setId(UUID.randomUUID().toString());
  400. n.setCreateTime(date);
  401. n.setReaded(0);// 未读
  402. n.setNoticeType(NoticeStatus.TASK_PATENT_ERROR.getCode());
  403. Admin a = adminMapper.selectByPrimaryKey(p.getAid());
  404. StringBuffer str2 = new StringBuffer();
  405. str2.append("专利编号[").append(p.getPatentNo()).append("],名称[").append(p.getName()).append("],email[")
  406. .append(p.getEmail().trim()).append("]");
  407. if (a == null) {
  408. str2.append(",aid=").append(p.getAid()).append(",找不到管理员。");
  409. n.setAid("1");
  410. n.setContent(str2.toString());
  411. } else if (!SendEmailUtil.isEmail(p.getEmail().trim())) {
  412. str2.append(",管理员=").append(a.getName()).append(",邮箱不正确。");
  413. n.setAid("1");
  414. n.setContent(str2.toString());
  415. } else {
  416. PatentNewLog pl = new PatentNewLog();
  417. String str = StrToString(str2,p.getEndDate(),p.getStatus());
  418. pl.setContent(str);
  419. pl.setCreateTime(new Date());
  420. pl.setPid(p.getId());
  421. patentNewLogMapper.insertSelective(pl);
  422. n.setAid(p.getAid());
  423. n.setNoticeType(NoticeStatus.TASK_PATENT_REMIND.getCode());
  424. n.setContent(str);
  425. }
  426. return n;
  427. }
  428. private String StrToString(StringBuffer str, Date date, Integer status) {
  429. //状态 0 未提醒 1 剩余90天 2剩余30天 3剩余20天 4剩余10天 5剩余3天 6剩余2天 7剩余1天
  430. Integer days=0;
  431. if (status==1) days=90;
  432. else if (status==2) days=60;
  433. else if (status==3) days=30;
  434. else if (status==4) days=15;
  435. else if (status==5) days=7;
  436. if(days>0) {
  437. str.append(",续费时间还剩").append(days).append("天");
  438. }
  439. str.append(",截止时间[").append(DateUtils.formatDate(date, AFTConstants.YYYYMMDD)).append("]请在截止时间前通知客户!");
  440. return str.toString();
  441. }
  442. public Integer getSatus(Date endDate) {
  443. int count=countDays(endDate);
  444. int status=0;
  445. if (count > 60 && count <= 90) {
  446. status=1;
  447. } else if (count > 30 && count <= 60) {
  448. status=2;
  449. }else if (count > 15 && count <= 30) {
  450. status=3;
  451. }else if (count > 7 && count <= 15) {
  452. status=4;
  453. }else if (count>0&&count <= 7) {
  454. status=5;
  455. }
  456. return status;
  457. }
  458. private Integer getyear(PatentNew p) {
  459. Calendar c=Calendar.getInstance();
  460. c.setTime(p.getApplyDate());
  461. int x=c.get(Calendar.YEAR);
  462. Calendar now=Calendar.getInstance();
  463. int y=now.get(Calendar.YEAR);
  464. int z=y-x;
  465. if (z==0) z=1;
  466. Calendar end=Calendar.getInstance();
  467. end.setTime(p.getApplyDate());
  468. end.add(Calendar.YEAR, z);
  469. p.setEndDate(end.getTime());
  470. return z;
  471. }
  472. private List<PatentPaymentDetails> sumYears(PatentNew trb) {
  473. if (trb.getApplyDate()!=null) {
  474. Calendar apply =Calendar.getInstance();
  475. apply.setTime(trb.getApplyDate());
  476. List<PatentPaymentDetails> list =new ArrayList<PatentPaymentDetails>();
  477. for (int i = 1; i < 21; i++) {
  478. PatentPaymentDetails pd=new PatentPaymentDetails();
  479. if ((trb.getType()==0||trb.getType()==2)&&i<11) {
  480. addParam(trb, apply, i, pd);
  481. list.add(pd);
  482. }else if (trb.getType()==1) {
  483. addParam(trb, apply, i, pd);
  484. list.add(pd);
  485. }
  486. }
  487. patentPaymentDetailsMapper.deleteByPid(trb.getId());
  488. patentPaymentDetailsMapper.insertBatch(list);
  489. return list;
  490. }
  491. return null;
  492. }
  493. private void addParam(PatentNew trb, Calendar apply, int i, PatentPaymentDetails pd) {
  494. Calendar end=Calendar.getInstance();
  495. end.setTime(apply.getTime());
  496. end.add(Calendar.YEAR, i);
  497. pd.setPid(trb.getId());
  498. pd.setEndDate(end.getTime());
  499. pd.setPatentAmount(sumAmout(trb.getType(),trb.getTid(),i));
  500. pd.setStatus(0);
  501. pd.setYears(i);
  502. }
  503. private BigDecimal sumAmout(Integer type,Integer tid,Integer year) {
  504. if (patentYear.isEmpty()) {
  505. patentYear=patentYearPaymentMapper.selectAll();
  506. }
  507. for (PatentYearPayment p : patentYear) {
  508. if (type==p.getType()&&year==p.getYear()) {
  509. TOrderTask t=tOrderTaskMapper.selectByPrimaryKey(tid);
  510. if (t!=null&&t.getCostReduction()!=null&& t.getCostReduction()==1) {
  511. return p.getReductionAmount();
  512. }else {
  513. return p.getPatentAmount();
  514. }
  515. }
  516. }
  517. return null;
  518. }
  519. @SuppressWarnings("unchecked")
  520. @Override
  521. public Object export(PatentNewBo p, Integer pageSize, Integer pageNo) {
  522. NewExcelUtil<PatentNewBo> excel=new NewExcelUtil<PatentNewBo>(PatentNewBo.class);
  523. Map<String, Object> params = new HashMap<>();
  524. if (pageNo == null || pageNo < 0) pageNo = 1;
  525. if (pageSize == null || pageSize < 0) pageSize = 99999;
  526. pushParams(p, params);
  527. List<PatentNewBo> list= (List<PatentNewBo>) findList("selectPatentNewList", params, pageNo,
  528. pageSize);
  529. return excel.exportExcel(list, "专利列表",uploadPath);
  530. }
  531. @Override
  532. public Object pushPatentPaymentDetails(Integer id) {
  533. List<PatentPaymentDetails> list =patentPaymentDetailsMapper.selectByPid(id);
  534. if (list==null||list.isEmpty()) {
  535. PatentNew pt=patentNewMapper.selectByPrimaryKey(id);
  536. return sumYears(pt);
  537. }
  538. return list;
  539. }
  540. /** @param type 0 标记已通知 1批量发邮件 2转给营销员
  541. */
  542. @Override
  543. public Object updateBatch(String ids, Integer type, String aid) {
  544. String[] s = ids.split(",");
  545. List<String> ls = Arrays.asList(s);
  546. addPatentNewLog(type, ls, aid);
  547. if (type == 1) {
  548. List<PatentNew> lp = patentNewMapper.selectByids(ls);
  549. List<EmailBo> list = new ArrayList<EmailBo>();
  550. for (PatentNew patentNew : lp) {
  551. if (patentNew.getStatus() == 0) {
  552. throw new BusinessException("专利号[" + patentNew.getPatentNo() + "]未达到距离截止时间90天,不允许发送邮件");
  553. }
  554. if (!SendEmailUtil.isEmail(patentNew.getEmail())) {
  555. throw new BusinessException("专利号[" + patentNew.getPatentNo() + "]的邮箱不正确。");
  556. }
  557. }
  558. for (PatentNew patentNew : lp) {
  559. Admin a = adminMapper.selectByPrimaryKey(patentNew.getAid());
  560. String userName = patentNewMapper.selectUserNameByTid(patentNew.getTid());
  561. BigDecimal amout = patentNew.getPatentAmount().multiply(new BigDecimal(10000));
  562. EmailBo emailBo = new EmailBo("专利提醒", patentNew.getEmail().trim(), userName, "科德集团", a.getName(),
  563. patentNew.getName(), patentNew.getPatentNo(), amout.stripTrailingZeros().toPlainString(),
  564. patentNew.getEndDate());
  565. list.add(emailBo);
  566. }
  567. try {
  568. asyncUtils.sendList(list);
  569. } catch (MessagingException e) {
  570. e.printStackTrace();
  571. } catch (UnsupportedEncodingException e) {
  572. e.printStackTrace();
  573. }
  574. }
  575. if (type == 0 || type == 1) {
  576. patentNewMapper.updateBatch(ls);
  577. } else if (type == 2) {
  578. patentNewMapper.updateBatchByid(ls, null, aid);
  579. }
  580. return 1;
  581. }
  582. /**
  583. * 批量添加专利日志
  584. * @param type
  585. * @param ls
  586. */
  587. private void addPatentNewLog(Integer type, List<String> ls,String aid) {
  588. List<PatentNewLog> log=new ArrayList<PatentNewLog>();
  589. Date date=new Date();
  590. Admin a=adminMapper.selectByPrimaryKey(TokenManager.getAdminId());
  591. Admin s=adminMapper.selectByPrimaryKey(aid);
  592. for (String str : ls) {
  593. PatentNewLog pl=new PatentNewLog();
  594. pl.setPid(Integer.valueOf(str));
  595. pl.setCreateTime(date);
  596. String content="[%s]批量将专利%s";
  597. if(type==0)str=String.format(content,a.getName(),"标记已通知。");
  598. else if(type==1)str=String.format(content,a.getName(),"发送邮件。");
  599. else if(type==2)str=String.format(content,a.getName(),"转交给["+s.getName()+"]。");
  600. pl.setContent(str);
  601. log.add(pl);
  602. }
  603. patentNewLogMapper.insertLogBatch(log);
  604. }
  605. @Override
  606. public void updateBatchStatusByid(List<String> l2, Integer status) {
  607. patentNewMapper.updateBatchByid(l2,status,null);
  608. }
  609. /**
  610. * 超过时间重置到第二年
  611. */
  612. @Override
  613. public void pushResetPatentNew(List<String> list) {
  614. for (String ids : list) {
  615. PatentNew pn=patentNewMapper.selectByPrimaryKey(Integer.valueOf(ids));
  616. int year=pn.getYears()+1;
  617. PatentPaymentDetails ppd=patentPaymentDetailsMapper.selectByPidAndYear(pn.getId(),year);
  618. if (ppd==null) {
  619. sumYears(pn);
  620. ppd=patentPaymentDetailsMapper.selectByPidAndYear(pn.getId(),year);
  621. }
  622. PatentNew n=new PatentNew();
  623. n.setId(pn.getId());
  624. n.setStatus(0);
  625. n.setSalesmanRemind(0);
  626. n.setYears(ppd.getYears());
  627. n.setEndDate(ppd.getEndDate());
  628. n.setPatentAmount(sumAmout(pn.getType(),pn.getTid(),pn.getYears()));
  629. patentNewMapper.updateByPrimaryKeySelective(n);
  630. }
  631. }
  632. @Override
  633. public boolean checkTid(Integer tid) {
  634. TOrderTask task=tOrderTaskMapper.selectByPrimaryKey(tid);
  635. if (task==null) {
  636. return true;
  637. }
  638. return false;
  639. }
  640. @Override
  641. public int countDays(Date endDate) {
  642. // 申请时间
  643. Calendar end = Calendar.getInstance();
  644. end.setTime(endDate);
  645. // 当前时间
  646. Calendar now = Calendar.getInstance();
  647. now.setTime(new Date());
  648. now.set(Calendar.MILLISECOND, 0);
  649. now.set(Calendar.HOUR_OF_DAY, 0);
  650. now.clear(Calendar.MINUTE);
  651. now.clear(Calendar.SECOND);
  652. int days=(int) ((end.getTimeInMillis() - now.getTimeInMillis()) / (1000 * 3600 * 24));
  653. return days;
  654. }
  655. @Override
  656. public void updateCostReduction(Integer id) {
  657. List<PatentNew> list =patentNewMapper.selectByTid(id);
  658. Admin a= adminMapper.selectByPrimaryKey(TokenManager.getAdminId());
  659. for (PatentNew p : list) {
  660. PatentNew pn=new PatentNew();
  661. pn.setId(p.getId());
  662. pn.setPatentAmount(sumAmout(p.getType(), id, p.getYears()));
  663. sumYears(p);
  664. PatentNewLog pl = new PatentNewLog();
  665. pl.setPid(p.getId());
  666. StringBuffer str=new StringBuffer();
  667. str.append(a.getName()).append("修改了有无费减");
  668. pl.setContent(str.toString());
  669. patentNewLogMapper.insertSelective(pl);
  670. patentNewMapper.updateByPrimaryKeySelective(pn);
  671. }
  672. }
  673. }