PatentNewServiceImpl.java 26 KB

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