/** * 点击删除 * * @param uid * @param pid * @return */ public HashMap<String, Object> deletePrintFile(int uid, int pid) { HashMap<String, Object> result = Maps.newHashMap(); Map<String, Integer> params = Maps.newHashMap(); logger.info("PrintFileService deletePrintFile uid:{}, pid:{} ", uid, pid); PrintFile printFile = printFileDao.loadPrintFile(pid); if (printFile == null) { result.put("status", 1); result.put("message", "删除文件失败,您要删除的文件不存在"); logger.info("PrintFileServiceImpl deletePrintFile fail file is not existed "); return result; } try { logger.info( "PrintFileService deletePrintFile delete from aliyun file:{}", printFile.getPath().substring(CPConstant.OSS_URL.length())); // 需要从阿里云上删除文件 OSSUtils.deleteObject( OSSUtils.getOSSClient(), printFile.getPath().substring(CPConstant.OSS_URL.length())); params.put("uid", uid); params.put("pid", pid); List<String> uids = printFileDao.loadUidsByPid(pid); // 同一个文件被多人使用,则只删除关联 if (uids.size() > 1) { printFileDao.deleteTUP(params); logger.info( "PrintFileService deletePrintFile uids:{}, the file is used by not one people, only delete the Relationship"); } else { // 当且仅当文件只有一个人,才删除文件 printFileDao.deleteTUP(params); printFileDao.deletePrintFile(pid); logger.info( "PrintFileService deletePrintFile uids:{}, the file is used by one people, delete file and Relationship"); } } catch (Exception e) { result.put("status", 1); result.put("message", "删除文件失败,详情请看日志"); logger.error("PrintFileServiceImpl deletePrintFile fail e:{} ", e); return result; } result.put("status", 0); result.put("message", "删除文件成功"); logger.info("PrintFileServiceImpl deletePrintFile success!!! result:{}", result); return result; }
/** * 四种情况 1.删除打印完立即删除的文件,不删数据库,不删除关联,只是将path变为空 2.保存三天后删除待打印的文件,要删数据库,删除所有关联 * 3.保存三天后删除已上传的文件,要删数据库,删除所有关联 4.保存三天后删除已打印中文件,不删数据库,不删除关联,将path变为空 */ public void timingDelete() { long start = System.currentTimeMillis(); HashMap<String, Object> param1 = Maps.newHashMap(); HashMap<String, Object> param2 = Maps.newHashMap(); HashMap<String, Object> param3 = Maps.newHashMap(); logger.info("PrintFileService timingDelete start..." + DateUtils.getNowTime()); // 1.删除打印完立即删除的文件,不删数据库,不删除关联,只删除当前关联 param1.put("status", 2); // 已打印 param1.put("isDelete", 0); // 打印完立即删除 List<Integer> pidsPrinted = printFileDao.findPidsPrinted(param1); logger.info("PrintFileServiceImpl timingDelete pidsPrinted:{} ", pidsPrinted); if (pidsPrinted.size() > 0) { for (int i = 0; i < pidsPrinted.size(); i++) { PrintFile temp = printFileDao.loadPrintFile(pidsPrinted.get(i)); // 先删文件,只删除存在阿里云上面的文件 OSSUtils.deleteObject( OSSUtils.getOSSClient(), temp.getPath().substring(CPConstant.OSS_URL.length())); // 不删数据,只是将path变为空 temp.setPath(""); printFileDao.updatePrintFile(temp); } } logger.info( "PrintFileServiceImpl timingDelete delete pidsPrinted success, the files path is null"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } // 2.3.删除超过3天的待打印,已上传文件,需要将文件删除 param2.put("overdueTime", DateUtils.getNowTime()); // 超过了三天 param2.put("status1", "0"); // 已上传 param2.put("status2", "1"); // 待打印 List<Integer> pidsBy3Days = printFileDao.findPidsBy3Days(param2); logger.info("PrintFileServiceImpl timingDelete pidsBy3Days:{} ", pidsBy3Days); if (pidsBy3Days.size() > 0) { for (int i = 0; i < pidsBy3Days.size(); i++) { PrintFile temp = printFileDao.loadPrintFile(pidsBy3Days.get(i)); // 先删除阿里云上的文件 OSSUtils.deleteObject( OSSUtils.getOSSClient(), temp.getPath().substring(CPConstant.OSS_URL.length())); // 在删该文件下所有用户关联 printFileDao.deleteUidsByPid(pidsBy3Days.get(i)); // 删数据 printFileDao.deletePrintFile(pidsBy3Days.get(i)); } } logger.info( "PrintFileServiceImpl timingDelete delete pidsBy3Days success, the files is deleted"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } // 4.保存三天后删除已打印中文件,不删数据库,不删除关联,将path变为空 param3.put("overdueTime", DateUtils.getNowTime()); // 超过了三天 param3.put("status", "2"); // 已打印 param2.put("isDelete", "1"); // 打印完保存三天 List<Integer> pidsBy3DaysPrinted = printFileDao.findPidsBy3DaysPrinted(param3); logger.info("PrintFileServiceImpl timingDelete pidsBy3DaysPrinted:{} ", pidsBy3DaysPrinted); if (pidsBy3DaysPrinted.size() > 0) { for (int i = 0; i < pidsBy3DaysPrinted.size(); i++) { PrintFile temp = printFileDao.loadPrintFile(pidsBy3DaysPrinted.get(i)); // 先删除阿里云上的文件 OSSUtils.deleteObject( OSSUtils.getOSSClient(), temp.getPath().substring(CPConstant.OSS_URL.length())); // 不删数据,只是将path变为空 temp.setPath(""); printFileDao.updatePrintFile(temp); } } logger.info( "PrintFileServiceImpl timingDelete delete pidsBy3DaysPrinted success! the files path is null"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } logger.info( "PrintFileService timingDelete end... " + DateUtils.getNowTime() + " and it cost " + (System.currentTimeMillis() - start)); }