PatentNewServiceImpl.java 25 KB

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