PatentNewServiceImpl.java 24 KB

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