PatentNewServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 org.apache.commons.lang3.time.DateFormatUtils;
  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.error.BusinessException;
  30. import com.goafanti.common.model.Admin;
  31. import com.goafanti.common.model.PatentNew;
  32. import com.goafanti.common.model.PatentNewLog;
  33. import com.goafanti.common.model.PatentPaymentDetails;
  34. import com.goafanti.common.model.PatentYearPayment;
  35. import com.goafanti.common.model.TOrderTask;
  36. import com.goafanti.common.utils.AsyncUtils;
  37. import com.goafanti.common.utils.DateUtils;
  38. import com.goafanti.common.utils.ExcelUtils;
  39. import com.goafanti.common.utils.excel.NewExcelUtil;
  40. import com.goafanti.core.mybatis.BaseMybatisDao;
  41. import com.goafanti.core.mybatis.page.Pagination;
  42. import com.goafanti.core.shiro.token.TokenManager;
  43. import com.goafanti.patent.bo.PatentNewBo;
  44. import com.goafanti.patent.enums.PatentType;
  45. import com.goafanti.patent.service.PatentNewService;
  46. @Service
  47. public class PatentNewServiceImpl extends BaseMybatisDao<PatentNewMapper> implements PatentNewService {
  48. @Autowired
  49. private PatentNewMapper patentNewMapper;
  50. @Autowired
  51. private PatentNewLogMapper patentNewLogMapper;
  52. @Autowired
  53. private TOrderTaskMapper tOrderTaskMapper;
  54. @Autowired
  55. private PatentPaymentDetailsMapper patentPaymentDetailsMapper;
  56. @Autowired
  57. private PatentYearPaymentMapper patentYearPaymentMapper;
  58. @Autowired
  59. private AsyncUtils asyncUtils;
  60. @Autowired
  61. private AdminMapper adminMapper;
  62. private List<PatentYearPayment> patentYear=new ArrayList<PatentYearPayment>();
  63. @Value(value = "${upload.path}")
  64. private String uploadPath=null;
  65. @Override
  66. public int addPatenNew(PatentNew p) {
  67. p.setStatus(0);//
  68. p.setAid(TokenManager.getAdminId());
  69. return patentNewMapper.insertSelective(p);
  70. }
  71. @Override
  72. public int updatePatenNew(PatentNewBo p) {
  73. PatentNewLog pl = new PatentNewLog();
  74. pl.setPid(p.getId());
  75. Admin a = adminMapper.selectByPrimaryKey(TokenManager.getAdminId());
  76. PatentNew pn=patentNewMapper.selectByPrimaryKey(p.getId());
  77. pl.setContent(a.getName() + "修改了专利内容。");
  78. if (!p.getApplyDate().equals(pn.getApplyDate())
  79. || p.getType() != pn.getType()
  80. || !p.getTid().equals( pn.getTid())) {
  81. p.setYears(getyear(p));
  82. sumYears(p);
  83. }
  84. patentNewLogMapper.insertSelective(pl);
  85. return patentNewMapper.updateByPrimaryKeySelective(p);
  86. }
  87. @SuppressWarnings("unchecked")
  88. @Override
  89. public Pagination<PatentNewBo> selectPatentNew(PatentNewBo p,Integer pageSize, Integer pageNo) {
  90. Map<String, Object> params = new HashMap<>();
  91. if (pageNo == null || pageNo < 0) pageNo = 1;
  92. if (pageSize == null || pageSize < 0) pageSize = 10;
  93. pushParams(p, params);
  94. Pagination<PatentNewBo> pa=(Pagination<PatentNewBo>) findPage("selectPatentNewList", "selectPatentNewCount", params, pageNo,
  95. pageSize);
  96. List<PatentNewBo> list=(List<PatentNewBo>) pa.getList();
  97. String aid=TokenManager.getAdminId();
  98. for (PatentNewBo pn : list) {
  99. if (aid.equals(pn.getAid())) {
  100. pn.setIsFollow(1);
  101. }else {
  102. pn.setIsFollow(0);
  103. }
  104. }
  105. return pa;
  106. }
  107. private void pushParams(PatentNewBo p, Map<String, Object> params) {
  108. if(p.getStatus()!=null)params.put("status", p.getStatus());
  109. if(p.getName()!=null)params.put("name", p.getName());
  110. if(p.getAname()!=null)params.put("aname", p.getAname());
  111. if(p.getPatentNo()!=null)params.put("patentNo", p.getPatentNo());
  112. if(p.getDepartmentId()!=null)params.put("departmentId", p.getDepartmentId());
  113. if(p.getStartDate()!=null)params.put("startDate", p.getStartDate());
  114. if(p.getEndDate()!=null)params.put("endDate", p.getEndDate());
  115. if(p.getCreateStart()!=null)params.put("createStart", p.getCreateStart());
  116. if(p.getCreateEnd()!=null)params.put("createEnd", p.getCreateEnd()+" 23:59:59");
  117. if(p.getType()!=null)params.put("type", p.getType());
  118. if(p.getDepName()!=null)params.put("depName", p.getDepName());
  119. if(p.getAnnualFeeStatus()!=null)params.put("annualFeeStatus", p.getAnnualFeeStatus());
  120. if(p.getUserName()!=null)params.put("userName", p.getUserName());
  121. if(p.getSalesmanRemind()!=null)params.put("salesmanRemind", p.getSalesmanRemind());
  122. if(p.getIsFollow()!=null) {
  123. params.put("isFollow", p.getIsFollow());
  124. }else {
  125. params.put("isFollow", 0);
  126. }
  127. params.put("aid", TokenManager.getAdminId());
  128. if (TokenManager.hasRole(AFTConstants.SUPERADMIN) || TokenManager.hasRole(AFTConstants.PATENT_AUDITOR)) {
  129. params.put("shiro", 2);
  130. } else if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)) {
  131. params.put("shiro", 1);
  132. } else {
  133. params.put("shiro", 0);
  134. }
  135. }
  136. @Override
  137. public List<PatentNewBo> AllselectStartPatentNew() {
  138. return patentNewMapper.AllselectStartPatentNew();
  139. }
  140. @Override
  141. public int addPatenNewLog(PatentNewLog pl) {
  142. return patentNewLogMapper.insertSelective(pl);
  143. }
  144. @Override
  145. public List<PatentNew> AllselectEndPatentNew() {
  146. return patentNewMapper.AllselectEndPatentNew();
  147. }
  148. @Override
  149. public void insertLogBatch(List<PatentNewLog> list) {
  150. patentNewLogMapper.insertLogBatch(list);
  151. }
  152. @Override
  153. public List<PatentNewLog> selectPatentNewLog(Integer id) {
  154. return patentNewLogMapper.selectPatentNewLog(id);
  155. }
  156. @Override
  157. public void pushImportTemplate(MultipartFile file) {
  158. //获得Workbook工作薄对象
  159. Workbook workbook = ExcelUtils.getWorkBook(file);
  160. //创建一个对象,用来存储数据
  161. if(workbook != null){
  162. //获得当前sheet工作表
  163. Sheet sheet = workbook.getSheetAt(0);
  164. if(sheet == null){
  165. return;
  166. }
  167. //获得当前sheet的开始行
  168. int firstRowNum = sheet.getFirstRowNum();
  169. //获得当前sheet的结束行
  170. int lastRowNum = sheet.getLastRowNum();
  171. //处理空白行
  172. for (int i = firstRowNum; i <= sheet.getLastRowNum();i++) {
  173. Row r = sheet.getRow(i);
  174. if(r == null){
  175. // 如果是空行(即没有任何数据、格式),直接把它以下的数据往上移动
  176. sheet.shiftRows(i+1, sheet.getLastRowNum(),-1);
  177. continue;
  178. }
  179. boolean flag = false;
  180. for(Cell c:r){
  181. if(c.getCellTypeEnum() != CellType._NONE){
  182. flag = true;
  183. break;
  184. }
  185. }
  186. if(flag){
  187. i++;
  188. continue;
  189. } else{//如果是空白行(即可能没有数据,但是有一定格式)
  190. if(i == sheet.getLastRowNum())//如果到了最后一行,直接将那一行remove掉
  191. sheet.removeRow(r);
  192. else//如果还没到最后一行,则数据往上移一行
  193. sheet.shiftRows(i+1, sheet.getLastRowNum(),-1);
  194. }
  195. }
  196. lastRowNum=sheet.getLastRowNum();
  197. if (lastRowNum<2) {
  198. throw new BusinessException(new Error("未找到正确的参数。"));
  199. }
  200. List<PatentNew> list=new ArrayList<>();
  201. //循环除了第2行的所有行
  202. for(int rowNum = firstRowNum+2;rowNum <= lastRowNum;rowNum++){
  203. //获得当前行
  204. Row row = sheet.getRow(rowNum);
  205. if(row == null){
  206. continue;
  207. }
  208. //获得当前行的开始列
  209. int firstCellNum = row.getFirstCellNum();
  210. //获得当前行的列数
  211. int lastCellNum = row.getLastCellNum();
  212. //循环当前行 cellNum = (firstCellNum+1) 则是除开第一例
  213. PatentNew trb=new PatentNew();
  214. for(int cellNum = (firstCellNum); cellNum < lastCellNum;cellNum++){
  215. Cell cell = row.getCell(cellNum);
  216. try {
  217. switch (cellNum) {
  218. case 0:
  219. trb.setPatentNo(ExcelUtils.getCellValue(cell));
  220. break;
  221. case 1:
  222. trb.setName(ExcelUtils.getCellValue(cell).trim());
  223. break;
  224. case 2:
  225. trb.setType(PatentType.getCodeByDesc(ExcelUtils.getCellValue(cell)));
  226. break;
  227. case 3:
  228. trb.setApplyDate(DateUtils.StringToDate(ExcelUtils.getCellValue(cell), AFTConstants.YYYYMMDD));
  229. break;
  230. case 4:
  231. trb.setHolders(ExcelUtils.getCellValue(cell));
  232. break;
  233. case 5:
  234. trb.setAuthorizationDate(DateUtils.StringToDate(ExcelUtils.getCellValue(cell), AFTConstants.YYYYMMDD));
  235. break;
  236. case 6:
  237. trb.setInventor(ExcelUtils.getCellValue(cell));
  238. break;
  239. case 7:
  240. trb.setEmail(ExcelUtils.getCellValue(cell));
  241. break;
  242. case 8:
  243. trb.setTid(Integer.parseInt(ExcelUtils.getCellValue(cell)));
  244. break;
  245. }
  246. } catch (Exception e) {
  247. e.printStackTrace();
  248. throw new BusinessException(new Error("表格第"+(rowNum+1)+"行"+cellNum+"列输入内容不正确。"));
  249. }
  250. if (cellNum==0&&trb.getPatentNo()!=null) {
  251. int i=patentNewMapper.selectByPatenNo(trb.getPatentNo());
  252. if (i>0) {
  253. throw new BusinessException(new Error("表格第"+(rowNum+1)+"行[专利编号]系统已存在。"));
  254. }
  255. }
  256. }
  257. for (PatentNew pt : list) {
  258. if (pt.getPatentNo().equals(trb.getPatentNo())) {
  259. throw new BusinessException(new Error("表格第"+(rowNum+1)+"行[专利编号]重复导入。"));
  260. }
  261. }
  262. trb.setAid(TokenManager.getAdminId());
  263. trb.setInputId(trb.getAid());
  264. trb.setCreateTime(new Date());
  265. trb.setStatus(0);
  266. trb.setNewType(1);
  267. trb.setYears(getyear(trb));
  268. trb.setPatentAmount(sumAmout(trb.getType(),trb.getTid(),trb.getYears()));
  269. list.add(trb);
  270. }
  271. List<PatentNew> list2=new ArrayList<>();
  272. for (int i = 0; i < list.size(); i++) {
  273. list2.add(list.get(i));
  274. if (50 == list2.size() || i == list.size() - 1) {
  275. if (list2.size() > 0) patentNewMapper.insertBatch(list2);
  276. list2.clear();
  277. try {
  278. Thread.sleep(500);
  279. } catch (InterruptedException e) {
  280. e.printStackTrace();
  281. }
  282. }
  283. }
  284. }
  285. }
  286. private Integer getyear(PatentNew p) {
  287. Calendar c=Calendar.getInstance();
  288. c.setTime(p.getApplyDate());
  289. int x=c.get(Calendar.YEAR);
  290. Calendar now=Calendar.getInstance();
  291. int y=now.get(Calendar.YEAR);
  292. int z=y-x;
  293. if (z==0) z=1;
  294. Calendar end=Calendar.getInstance();
  295. end.setTime(p.getApplyDate());
  296. end.add(Calendar.YEAR, z);
  297. p.setEndDate(end.getTime());
  298. return z;
  299. }
  300. private List<PatentPaymentDetails> sumYears(PatentNew trb) {
  301. if (trb.getApplyDate()!=null) {
  302. Calendar apply =Calendar.getInstance();
  303. apply.setTime(trb.getApplyDate());
  304. List<PatentPaymentDetails> list =new ArrayList<PatentPaymentDetails>();
  305. for (int i = 1; i < 21; i++) {
  306. PatentPaymentDetails pd=new PatentPaymentDetails();
  307. if ((trb.getType()==0||trb.getType()==2)&&i<11) {
  308. addParam(trb, apply, i, pd);
  309. list.add(pd);
  310. }else if (trb.getType()==1) {
  311. addParam(trb, apply, i, pd);
  312. list.add(pd);
  313. }
  314. }
  315. patentPaymentDetailsMapper.deleteByPid(trb.getId());
  316. patentPaymentDetailsMapper.insertBatch(list);
  317. return list;
  318. }
  319. return null;
  320. }
  321. private void addParam(PatentNew trb, Calendar apply, int i, PatentPaymentDetails pd) {
  322. Calendar end=Calendar.getInstance();
  323. end.setTime(apply.getTime());
  324. end.add(Calendar.YEAR, i);
  325. pd.setPid(trb.getId());
  326. pd.setEndDate(end.getTime());
  327. pd.setPatentAmount(sumAmout(trb.getType(),trb.getTid(),i));
  328. pd.setStatus(0);
  329. pd.setYears(i);
  330. }
  331. private BigDecimal sumAmout(Integer type,Integer tid,Integer year) {
  332. if (patentYear.isEmpty()) {
  333. patentYear=patentYearPaymentMapper.selectAll();
  334. }
  335. for (PatentYearPayment p : patentYear) {
  336. if (type==p.getType()&&year==p.getYear()) {
  337. TOrderTask t=tOrderTaskMapper.selectByPrimaryKey(tid);
  338. if (t!=null&&t.getCostReduction()!=null&& t.getCostReduction()==1) {
  339. return p.getReductionAmount();
  340. }else {
  341. return p.getPatentAmount();
  342. }
  343. }
  344. }
  345. return null;
  346. }
  347. @SuppressWarnings("unchecked")
  348. @Override
  349. public Object export(PatentNewBo p, Integer pageSize, Integer pageNo) {
  350. NewExcelUtil<PatentNewBo> excel=new NewExcelUtil<PatentNewBo>(PatentNewBo.class);
  351. Map<String, Object> params = new HashMap<>();
  352. if (pageNo == null || pageNo < 0) pageNo = 1;
  353. if (pageSize == null || pageSize < 0) pageSize = 99999;
  354. pushParams(p, params);
  355. List<PatentNewBo> list= (List<PatentNewBo>) findList("selectPatentNewList", params, pageNo,
  356. pageSize);
  357. return excel.exportExcel(list, "专利列表",uploadPath);
  358. }
  359. @Override
  360. public Object pushPatentPaymentDetails(Integer id) {
  361. List<PatentPaymentDetails> list =patentPaymentDetailsMapper.selectByPid(id);
  362. if (list==null||list.isEmpty()) {
  363. PatentNew pt=patentNewMapper.selectByPrimaryKey(id);
  364. return sumYears(pt);
  365. }
  366. return list;
  367. }
  368. /** @param type 0 标记已通知 1批量发邮件 2转给营销员
  369. */
  370. @Override
  371. public Object updateBatch(String ids, Integer type,String aid) {
  372. String [] s=ids.split(",");
  373. List<String> ls=Arrays.asList(s);
  374. addPatentNewLog(type, ls,aid);
  375. if(type==1) {
  376. List<EmailBo>list =new ArrayList<EmailBo>();
  377. List<PatentNew> lp=patentNewMapper.selectByids(ls);
  378. for (PatentNew patentNew : lp) {
  379. if (patentNew.getStatus()==0) {
  380. throw new BusinessException("专利号["+patentNew.getPatentNo()+"]未达到距离截止时间90天,不允许发送邮件");
  381. }
  382. Admin a = adminMapper.selectByPrimaryKey(patentNew.getAid());
  383. String userName=patentNewMapper.selectUserNameByTid(patentNew.getTid());
  384. BigDecimal amout= patentNew.getPatentAmount().multiply(new BigDecimal(10000));
  385. EmailBo emailBo = new EmailBo("专利提醒", patentNew.getEmail().trim(), userName, "科德集团", a.getName(),
  386. patentNew.getName(), patentNew.getPatentNo(),amout.stripTrailingZeros().toPlainString(),
  387. DateFormatUtils.format(patentNew.getEndDate(), "yyyy-MM-dd"), patentNew.getStatus());
  388. list.add(emailBo);
  389. }
  390. asyncUtils.sendList(list);
  391. }
  392. if (type==0||type==1) {
  393. patentNewMapper.updateBatch(ls);
  394. }else if(type==2) {
  395. patentNewMapper.updateBatchByid(ls, null, aid);
  396. }
  397. return 1;
  398. }
  399. /**
  400. * 批量添加专利日志
  401. * @param type
  402. * @param ls
  403. */
  404. private void addPatentNewLog(Integer type, List<String> ls,String aid) {
  405. List<PatentNewLog> log=new ArrayList<PatentNewLog>();
  406. Date date=new Date();
  407. Admin a=adminMapper.selectByPrimaryKey(TokenManager.getAdminId());
  408. Admin s=adminMapper.selectByPrimaryKey(aid);
  409. for (String str : ls) {
  410. PatentNewLog pl=new PatentNewLog();
  411. pl.setPid(Integer.valueOf(str));
  412. pl.setCreateTime(date);
  413. String content="[%s]批量将专利%s";
  414. if(type==0)str=String.format(content,a.getName(),"标记已通知。");
  415. else if(type==1)str=String.format(content,a.getName(),"发送邮件。");
  416. else if(type==2)str=String.format(content,a.getName(),"转交给["+s.getName()+"]。");
  417. pl.setContent(str);
  418. log.add(pl);
  419. }
  420. patentNewLogMapper.insertLogBatch(log);
  421. }
  422. @Override
  423. public void updateBatchStatusByid(List<String> l2, Integer status) {
  424. patentNewMapper.updateBatchByid(l2,status,null);
  425. }
  426. /**
  427. * 超过时间重置到第二年
  428. */
  429. @Override
  430. public void pushResetPatentNew(List<String> list) {
  431. for (String ids : list) {
  432. PatentNew pn=patentNewMapper.selectByPrimaryKey(Integer.valueOf(ids));
  433. int year=pn.getYears()+1;
  434. PatentPaymentDetails ppd=patentPaymentDetailsMapper.selectByPidAndYear(pn.getId(),year);
  435. if (ppd==null) {
  436. sumYears(pn);
  437. ppd=patentPaymentDetailsMapper.selectByPidAndYear(pn.getId(),year);
  438. }
  439. PatentNew n=new PatentNew();
  440. n.setId(pn.getId());
  441. n.setStatus(0);
  442. n.setSalesmanRemind(0);
  443. n.setYears(ppd.getYears());
  444. n.setEndDate(ppd.getEndDate());
  445. n.setPatentAmount(sumAmout(pn.getType(),pn.getTid(),pn.getYears()));
  446. patentNewMapper.updateByPrimaryKeySelective(n);
  447. }
  448. }
  449. }