| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- package com.goafanti.patent.service.impl;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.apache.commons.lang3.time.DateFormatUtils;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellType;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- import com.goafanti.common.bo.EmailBo;
- import com.goafanti.common.bo.Error;
- import com.goafanti.common.constant.AFTConstants;
- import com.goafanti.common.dao.AdminMapper;
- import com.goafanti.common.dao.PatentNewLogMapper;
- import com.goafanti.common.dao.PatentNewMapper;
- import com.goafanti.common.dao.PatentPaymentDetailsMapper;
- import com.goafanti.common.dao.PatentYearPaymentMapper;
- import com.goafanti.common.dao.TOrderTaskMapper;
- import com.goafanti.common.error.BusinessException;
- import com.goafanti.common.model.Admin;
- import com.goafanti.common.model.PatentNew;
- import com.goafanti.common.model.PatentNewLog;
- import com.goafanti.common.model.PatentPaymentDetails;
- import com.goafanti.common.model.PatentYearPayment;
- import com.goafanti.common.model.TOrderTask;
- import com.goafanti.common.utils.AsyncUtils;
- import com.goafanti.common.utils.DateUtils;
- import com.goafanti.common.utils.ExcelUtils;
- import com.goafanti.common.utils.excel.NewExcelUtil;
- import com.goafanti.core.mybatis.BaseMybatisDao;
- import com.goafanti.core.mybatis.page.Pagination;
- import com.goafanti.core.shiro.token.TokenManager;
- import com.goafanti.patent.bo.PatentNewBo;
- import com.goafanti.patent.enums.PatentType;
- import com.goafanti.patent.service.PatentNewService;
- @Service
- public class PatentNewServiceImpl extends BaseMybatisDao<PatentNewMapper> implements PatentNewService {
- @Autowired
- private PatentNewMapper patentNewMapper;
- @Autowired
- private PatentNewLogMapper patentNewLogMapper;
- @Autowired
- private TOrderTaskMapper tOrderTaskMapper;
- @Autowired
- private PatentPaymentDetailsMapper patentPaymentDetailsMapper;
- @Autowired
- private PatentYearPaymentMapper patentYearPaymentMapper;
- @Autowired
- private AsyncUtils asyncUtils;
- @Autowired
- private AdminMapper adminMapper;
-
- private List<PatentYearPayment> patentYear=new ArrayList<PatentYearPayment>();
- @Value(value = "${upload.path}")
- private String uploadPath=null;
- @Override
- public int addPatenNew(PatentNew p) {
- p.setStatus(0);//
- p.setAid(TokenManager.getAdminId());
- return patentNewMapper.insertSelective(p);
- }
- @Override
- public int updatePatenNew(PatentNewBo p) {
- PatentNewLog pl = new PatentNewLog();
- pl.setPid(p.getId());
- Admin a = adminMapper.selectByPrimaryKey(TokenManager.getAdminId());
- PatentNew pn=patentNewMapper.selectByPrimaryKey(p.getId());
- pl.setContent(a.getName() + "修改了专利内容。");
- if (!p.getApplyDate().equals(pn.getApplyDate())
- || p.getType() != pn.getType()
- || !p.getTid().equals( pn.getTid())) {
- p.setYears(getyear(p));
- sumYears(p);
- }
- patentNewLogMapper.insertSelective(pl);
- return patentNewMapper.updateByPrimaryKeySelective(p);
- }
-
- @SuppressWarnings("unchecked")
- @Override
- public Pagination<PatentNewBo> selectPatentNew(PatentNewBo p,Integer pageSize, Integer pageNo) {
- Map<String, Object> params = new HashMap<>();
- if (pageNo == null || pageNo < 0) pageNo = 1;
- if (pageSize == null || pageSize < 0) pageSize = 10;
- pushParams(p, params);
- Pagination<PatentNewBo> pa=(Pagination<PatentNewBo>) findPage("selectPatentNewList", "selectPatentNewCount", params, pageNo,
- pageSize);
- List<PatentNewBo> list=(List<PatentNewBo>) pa.getList();
- String aid=TokenManager.getAdminId();
- for (PatentNewBo pn : list) {
- if (aid.equals(pn.getAid())) {
- pn.setIsFollow(1);
- }else {
- pn.setIsFollow(0);
- }
- }
- return pa;
- }
- private void pushParams(PatentNewBo p, Map<String, Object> params) {
- if(p.getStatus()!=null)params.put("status", p.getStatus());
- if(p.getName()!=null)params.put("name", p.getName());
- if(p.getAname()!=null)params.put("aname", p.getAname());
- if(p.getPatentNo()!=null)params.put("patentNo", p.getPatentNo());
- if(p.getDepartmentId()!=null)params.put("departmentId", p.getDepartmentId());
- if(p.getStartDate()!=null)params.put("startDate", p.getStartDate());
- if(p.getEndDate()!=null)params.put("endDate", p.getEndDate());
- if(p.getCreateStart()!=null)params.put("createStart", p.getCreateStart());
- if(p.getCreateEnd()!=null)params.put("createEnd", p.getCreateEnd()+" 23:59:59");
- if(p.getType()!=null)params.put("type", p.getType());
- if(p.getDepName()!=null)params.put("depName", p.getDepName());
- if(p.getAnnualFeeStatus()!=null)params.put("annualFeeStatus", p.getAnnualFeeStatus());
- if(p.getUserName()!=null)params.put("userName", p.getUserName());
- if(p.getSalesmanRemind()!=null)params.put("salesmanRemind", p.getSalesmanRemind());
- if(p.getIsFollow()!=null) {
- params.put("isFollow", p.getIsFollow());
- }else {
- params.put("isFollow", 0);
- }
- params.put("aid", TokenManager.getAdminId());
- if (TokenManager.hasRole(AFTConstants.SUPERADMIN) || TokenManager.hasRole(AFTConstants.PATENT_AUDITOR)) {
- params.put("shiro", 2);
- } else if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)) {
- params.put("shiro", 1);
- } else {
- params.put("shiro", 0);
- }
- }
- @Override
- public List<PatentNewBo> AllselectStartPatentNew() {
- return patentNewMapper.AllselectStartPatentNew();
- }
- @Override
- public int addPatenNewLog(PatentNewLog pl) {
- return patentNewLogMapper.insertSelective(pl);
- }
- @Override
- public List<PatentNew> AllselectEndPatentNew() {
- return patentNewMapper.AllselectEndPatentNew();
- }
- @Override
- public void insertLogBatch(List<PatentNewLog> list) {
- patentNewLogMapper.insertLogBatch(list);
- }
- @Override
- public List<PatentNewLog> selectPatentNewLog(Integer id) {
- return patentNewLogMapper.selectPatentNewLog(id);
- }
- @Override
- public void pushImportTemplate(MultipartFile file) {
- //获得Workbook工作薄对象
- Workbook workbook = ExcelUtils.getWorkBook(file);
- //创建一个对象,用来存储数据
- if(workbook != null){
- //获得当前sheet工作表
- Sheet sheet = workbook.getSheetAt(0);
- if(sheet == null){
- return;
- }
- //获得当前sheet的开始行
- int firstRowNum = sheet.getFirstRowNum();
- //获得当前sheet的结束行
- int lastRowNum = sheet.getLastRowNum();
- //处理空白行
- for (int i = firstRowNum; i <= sheet.getLastRowNum();i++) {
- Row r = sheet.getRow(i);
- if(r == null){
- // 如果是空行(即没有任何数据、格式),直接把它以下的数据往上移动
- sheet.shiftRows(i+1, sheet.getLastRowNum(),-1);
- continue;
- }
- boolean flag = false;
- for(Cell c:r){
- if(c.getCellTypeEnum() != CellType._NONE){
- flag = true;
- break;
- }
- }
- if(flag){
- i++;
- continue;
- } else{//如果是空白行(即可能没有数据,但是有一定格式)
- if(i == sheet.getLastRowNum())//如果到了最后一行,直接将那一行remove掉
- sheet.removeRow(r);
- else//如果还没到最后一行,则数据往上移一行
- sheet.shiftRows(i+1, sheet.getLastRowNum(),-1);
- }
- }
- lastRowNum=sheet.getLastRowNum();
- if (lastRowNum<2) {
- throw new BusinessException(new Error("未找到正确的参数。"));
- }
- List<PatentNew> list=new ArrayList<>();
- //循环除了第2行的所有行
- for(int rowNum = firstRowNum+2;rowNum <= lastRowNum;rowNum++){
- //获得当前行
- Row row = sheet.getRow(rowNum);
- if(row == null){
- continue;
- }
- //获得当前行的开始列
- int firstCellNum = row.getFirstCellNum();
- //获得当前行的列数
- int lastCellNum = row.getLastCellNum();
- //循环当前行 cellNum = (firstCellNum+1) 则是除开第一例
- PatentNew trb=new PatentNew();
- for(int cellNum = (firstCellNum); cellNum < lastCellNum;cellNum++){
- Cell cell = row.getCell(cellNum);
- try {
- switch (cellNum) {
- case 0:
- trb.setPatentNo(ExcelUtils.getCellValue(cell));
- break;
- case 1:
- trb.setName(ExcelUtils.getCellValue(cell).trim());
- break;
- case 2:
- trb.setType(PatentType.getCodeByDesc(ExcelUtils.getCellValue(cell)));
- break;
- case 3:
- trb.setApplyDate(DateUtils.StringToDate(ExcelUtils.getCellValue(cell), AFTConstants.YYYYMMDD));
- break;
- case 4:
- trb.setHolders(ExcelUtils.getCellValue(cell));
- break;
- case 5:
- trb.setAuthorizationDate(DateUtils.StringToDate(ExcelUtils.getCellValue(cell), AFTConstants.YYYYMMDD));
- break;
- case 6:
- trb.setInventor(ExcelUtils.getCellValue(cell));
- break;
- case 7:
- trb.setEmail(ExcelUtils.getCellValue(cell));
- break;
- case 8:
- trb.setTid(Integer.parseInt(ExcelUtils.getCellValue(cell)));
- break;
-
- }
- } catch (Exception e) {
- e.printStackTrace();
- throw new BusinessException(new Error("表格第"+(rowNum+1)+"行"+cellNum+"列输入内容不正确。"));
- }
- if (cellNum==0&&trb.getPatentNo()!=null) {
- int i=patentNewMapper.selectByPatenNo(trb.getPatentNo());
- if (i>0) {
- throw new BusinessException(new Error("表格第"+(rowNum+1)+"行[专利编号]系统已存在。"));
- }
- }
- }
- for (PatentNew pt : list) {
- if (pt.getPatentNo().equals(trb.getPatentNo())) {
- throw new BusinessException(new Error("表格第"+(rowNum+1)+"行[专利编号]重复导入。"));
- }
- }
- trb.setAid(TokenManager.getAdminId());
- trb.setInputId(trb.getAid());
- trb.setCreateTime(new Date());
- trb.setStatus(0);
- trb.setNewType(1);
- trb.setYears(getyear(trb));
- trb.setPatentAmount(sumAmout(trb.getType(),trb.getTid(),trb.getYears()));
- list.add(trb);
- }
-
- List<PatentNew> list2=new ArrayList<>();
- for (int i = 0; i < list.size(); i++) {
-
- list2.add(list.get(i));
- if (50 == list2.size() || i == list.size() - 1) {
- if (list2.size() > 0) patentNewMapper.insertBatch(list2);
- list2.clear();
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- }
- private Integer getyear(PatentNew p) {
- Calendar c=Calendar.getInstance();
- c.setTime(p.getApplyDate());
- int x=c.get(Calendar.YEAR);
- Calendar now=Calendar.getInstance();
- int y=now.get(Calendar.YEAR);
- int z=y-x;
- if (z==0) z=1;
- Calendar end=Calendar.getInstance();
- end.setTime(p.getApplyDate());
- end.add(Calendar.YEAR, z);
- p.setEndDate(end.getTime());
- return z;
- }
- private List<PatentPaymentDetails> sumYears(PatentNew trb) {
- if (trb.getApplyDate()!=null) {
- Calendar apply =Calendar.getInstance();
- apply.setTime(trb.getApplyDate());
- List<PatentPaymentDetails> list =new ArrayList<PatentPaymentDetails>();
-
- for (int i = 1; i < 21; i++) {
- PatentPaymentDetails pd=new PatentPaymentDetails();
- if ((trb.getType()==0||trb.getType()==2)&&i<11) {
- addParam(trb, apply, i, pd);
- list.add(pd);
- }else if (trb.getType()==1) {
-
- addParam(trb, apply, i, pd);
- list.add(pd);
- }
- }
- patentPaymentDetailsMapper.deleteByPid(trb.getId());
- patentPaymentDetailsMapper.insertBatch(list);
- return list;
- }
- return null;
-
- }
- private void addParam(PatentNew trb, Calendar apply, int i, PatentPaymentDetails pd) {
- Calendar end=Calendar.getInstance();
- end.setTime(apply.getTime());
- end.add(Calendar.YEAR, i);
- pd.setPid(trb.getId());
- pd.setEndDate(end.getTime());
- pd.setPatentAmount(sumAmout(trb.getType(),trb.getTid(),i));
- pd.setStatus(0);
- pd.setYears(i);
- }
-
-
- private BigDecimal sumAmout(Integer type,Integer tid,Integer year) {
- if (patentYear.isEmpty()) {
- patentYear=patentYearPaymentMapper.selectAll();
- }
- for (PatentYearPayment p : patentYear) {
- if (type==p.getType()&&year==p.getYear()) {
- TOrderTask t=tOrderTaskMapper.selectByPrimaryKey(tid);
- if (t!=null&&t.getCostReduction()!=null&& t.getCostReduction()==1) {
- return p.getReductionAmount();
- }else {
- return p.getPatentAmount();
- }
-
- }
- }
- return null;
- }
- @SuppressWarnings("unchecked")
- @Override
- public Object export(PatentNewBo p, Integer pageSize, Integer pageNo) {
- NewExcelUtil<PatentNewBo> excel=new NewExcelUtil<PatentNewBo>(PatentNewBo.class);
- Map<String, Object> params = new HashMap<>();
- if (pageNo == null || pageNo < 0) pageNo = 1;
- if (pageSize == null || pageSize < 0) pageSize = 99999;
- pushParams(p, params);
- List<PatentNewBo> list= (List<PatentNewBo>) findList("selectPatentNewList", params, pageNo,
- pageSize);
- return excel.exportExcel(list, "专利列表",uploadPath);
- }
- @Override
- public Object pushPatentPaymentDetails(Integer id) {
- List<PatentPaymentDetails> list =patentPaymentDetailsMapper.selectByPid(id);
- if (list==null||list.isEmpty()) {
- PatentNew pt=patentNewMapper.selectByPrimaryKey(id);
- return sumYears(pt);
- }
- return list;
- }
- /** @param type 0 标记已通知 1批量发邮件 2转给营销员
- */
- @Override
- public Object updateBatch(String ids, Integer type,String aid) {
- String [] s=ids.split(",");
- List<String> ls=Arrays.asList(s);
- addPatentNewLog(type, ls,aid);
- if(type==1) {
- List<EmailBo>list =new ArrayList<EmailBo>();
- List<PatentNew> lp=patentNewMapper.selectByids(ls);
- for (PatentNew patentNew : lp) {
- if (patentNew.getStatus()==0) {
- throw new BusinessException("专利号["+patentNew.getPatentNo()+"]未达到距离截止时间90天,不允许发送邮件");
- }
- Admin a = adminMapper.selectByPrimaryKey(patentNew.getAid());
- String userName=patentNewMapper.selectUserNameByTid(patentNew.getTid());
- BigDecimal amout= patentNew.getPatentAmount().multiply(new BigDecimal(10000));
- EmailBo emailBo = new EmailBo("专利提醒", patentNew.getEmail().trim(), userName, "科德集团", a.getName(),
- patentNew.getName(), patentNew.getPatentNo(),amout.stripTrailingZeros().toPlainString(),
- DateFormatUtils.format(patentNew.getEndDate(), "yyyy-MM-dd"), patentNew.getStatus());
- list.add(emailBo);
- }
- asyncUtils.sendList(list);
- }
- if (type==0||type==1) {
- patentNewMapper.updateBatch(ls);
- }else if(type==2) {
- patentNewMapper.updateBatchByid(ls, null, aid);
- }
- return 1;
- }
- /**
- * 批量添加专利日志
- * @param type
- * @param ls
- */
- private void addPatentNewLog(Integer type, List<String> ls,String aid) {
- List<PatentNewLog> log=new ArrayList<PatentNewLog>();
- Date date=new Date();
- Admin a=adminMapper.selectByPrimaryKey(TokenManager.getAdminId());
- Admin s=adminMapper.selectByPrimaryKey(aid);
- for (String str : ls) {
- PatentNewLog pl=new PatentNewLog();
- pl.setPid(Integer.valueOf(str));
- pl.setCreateTime(date);
- String content="[%s]批量将专利%s";
- if(type==0)str=String.format(content,a.getName(),"标记已通知。");
- else if(type==1)str=String.format(content,a.getName(),"发送邮件。");
- else if(type==2)str=String.format(content,a.getName(),"转交给["+s.getName()+"]。");
- pl.setContent(str);
- log.add(pl);
- }
- patentNewLogMapper.insertLogBatch(log);
- }
- @Override
- public void updateBatchStatusByid(List<String> l2, Integer status) {
- patentNewMapper.updateBatchByid(l2,status,null);
-
- }
-
- /**
- * 超过时间重置到第二年
- */
- @Override
- public void pushResetPatentNew(List<String> list) {
- for (String ids : list) {
- PatentNew pn=patentNewMapper.selectByPrimaryKey(Integer.valueOf(ids));
- int year=pn.getYears()+1;
- PatentPaymentDetails ppd=patentPaymentDetailsMapper.selectByPidAndYear(pn.getId(),year);
- if (ppd==null) {
- sumYears(pn);
- ppd=patentPaymentDetailsMapper.selectByPidAndYear(pn.getId(),year);
- }
- PatentNew n=new PatentNew();
- n.setId(pn.getId());
- n.setStatus(0);
- n.setSalesmanRemind(0);
- n.setYears(ppd.getYears());
- n.setEndDate(ppd.getEndDate());
- n.setPatentAmount(sumAmout(pn.getType(),pn.getTid(),pn.getYears()));
- patentNewMapper.updateByPrimaryKeySelective(n);
- }
- }
- }
|