Browse Source

删除时间久的作业图片(oss以及挂载nas,怕误操作,所以时间在程序里写死的,如需要删除别的时间的需要改动)

shenhao 1 year ago
parent
commit
1eb156b153

+ 2 - 0
src/main/java/com/ssj/dao/weixin/zuoyb/dao/ZuoybCourseDao.java

@@ -92,4 +92,6 @@ public interface ZuoybCourseDao extends JpaRepository<ZuoybCourse, String>{
 	 
 	 
 	 @Query(nativeQuery = true, value = "select course_hour from zyb_course  where  lib_id =?1  GROUP BY  course_hour  order by  course_hour asc ,create_time asc ")
 	 @Query(nativeQuery = true, value = "select course_hour from zyb_course  where  lib_id =?1  GROUP BY  course_hour  order by  course_hour asc ,create_time asc ")
 	 public List<String> getCourseAndLibId(String libId);
 	 public List<String> getCourseAndLibId(String libId);
+	@Query(nativeQuery = true, value = "select SUBSTR(file_key,2) as file_key  from scon_homework_picture  where  create_time like  CONCAT(?1,'%')")
+	List<String> findFileKeyList(String format);
 }
 }

+ 6 - 0
src/main/java/com/ssj/dao/weixin/zuoyb/dao/ZuoybQueryDao.java

@@ -335,4 +335,10 @@ public interface ZuoybQueryDao {
 	List<TbLibJoin> findAllCity(Map map);
 	List<TbLibJoin> findAllCity(Map map);
 
 
     Page<HowExportVO> getZuoybKnowHowPage(Map<String, Object> params, Pageable initPage);
     Page<HowExportVO> getZuoybKnowHowPage(Map<String, Object> params, Pageable initPage);
+
+	/**
+	 * 查询年月
+	 * @return
+	 */
+	Map<String, Object> findFirstMonth();
 }
 }

+ 8 - 0
src/main/java/com/ssj/dao/weixin/zuoyb/dao/impl/ZuoybQueryDaoImpl.java

@@ -1721,4 +1721,12 @@ public class ZuoybQueryDaoImpl  implements ZuoybQueryDao{
 		}
 		}
 		return dao.findPage(selSQL.toString(),queryParams.toArray(),initPage,HowExportVO.class);
 		return dao.findPage(selSQL.toString(),queryParams.toArray(),initPage,HowExportVO.class);
 	}
 	}
+
+	@Override
+	public Map<String, Object> findFirstMonth() {
+		StringBuilder selSQL = new StringBuilder();
+		List<Object> queryParams = new ArrayList<Object>();
+		selSQL.append(" SELECT  DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') as create_time from scon_homework_picture  order by create_time asc limit 1");
+		return dao.findMap(selSQL.toString(), queryParams.toArray()).get(0);
+	}
 }
 }

+ 8 - 1
src/main/java/com/ssj/service/weixin/zuoyb/service/ZuoybCourseService.java

@@ -197,5 +197,12 @@ public interface ZuoybCourseService  extends BaseService<ZuoybCourse, String> {
 	  public List<String> getCourseAndLibId(String libId);
 	  public List<String> getCourseAndLibId(String libId);
 	  
 	  
 	  public void zuoybCourseListSave(String libId);
 	  public void zuoybCourseListSave(String libId);
-	  
+
+	/**
+	 * 查找最早有作业的年月
+	 * @return
+	 */
+	public	String findFirstMonth();
+
+	List<String> findFileKeyList(String format);
 }
 }

+ 11 - 0
src/main/java/com/ssj/service/weixin/zuoyb/service/impl/ZuoybCourseServiceImpl.java

@@ -190,4 +190,15 @@ public class ZuoybCourseServiceImpl  extends BaseServiceImpl<ZuoybCourse, String
 			
 			
 	}
 	}
 
 
+	@Override
+	public String findFirstMonth() {
+		Map<String,Object> objectMap = zuoybQueryDao.findFirstMonth();
+		return objectMap.get("create_time").toString();
+	}
+
+	@Override
+	public List<String> findFileKeyList(String format) {
+		return zuoybCourseDao.findFileKeyList(format);
+	}
+
 }
 }

+ 183 - 6
src/main/java/com/ssj/sys/controller/SysLibController.java

@@ -1,11 +1,10 @@
 package com.ssj.sys.controller;
 package com.ssj.sys.controller;
 
 
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.*;
+import java.net.URLDecoder;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashMap;
@@ -17,6 +16,13 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 import javax.servlet.http.HttpSession;
 
 
+import cn.hutool.core.thread.ThreadUtil;
+import com.aliyun.oss.ClientException;
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.OSSException;
+import com.aliyun.oss.model.DeleteObjectsRequest;
+import com.aliyun.oss.model.DeleteObjectsResult;
 import com.ssj.bean.sys.fx.domain.Merchant;
 import com.ssj.bean.sys.fx.domain.Merchant;
 import com.ssj.bean.weixin.libmy.domain.*;
 import com.ssj.bean.weixin.libmy.domain.*;
 import com.ssj.service.sys.fx.service.MerchantService;
 import com.ssj.service.sys.fx.service.MerchantService;
@@ -1491,4 +1497,175 @@ public class SysLibController extends BaseController {
 		response.setCharacterEncoding("utf-8");
 		response.setCharacterEncoding("utf-8");
 	}
 	}
 
 
+	/**
+	 * 请求删除2021年前的文件,这个2021年写死。
+	 * @param
+	 * @return
+	 */
+	@RequestMapping(value = "/admin/delFile", method = RequestMethod.POST)
+	@ResponseBody
+	public Response delFile() {
+		Response response = new  Response();
+		try{
+			//进来写入到redis
+			String delState = tokenManager.getString("del_oss_file");
+			if(delState == null){
+				tokenManager.set("del_oss_file","1");
+				//另起线程去处理删除
+				ThreadUtil.execAsync(() -> {
+					/**
+					 * 查询2021年前的作业,数据量大
+					 * 先查询最早一个月的数据,然后每次加一个月份查询数据做处理,知道2021年1月。
+					 */
+					// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
+					String endpoint = "https://oss-cn-shenzhen-internal.aliyuncs.com";
+					// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
+					String accessKeyId = tokenManager.getString("sts_accessKeyId");
+					String accessKeySecret = tokenManager.getString("sts_accessKeySecret");
+					// 填写Bucket名称,例如examplebucket。
+					String bucketName = tokenManager.getString("bucketName");
+					String fileSavePath = PropertiesUtil.getValue("file_save_path");
+					// 创建OSSClient实例。
+					OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
+					try {
+						String startMonth = zuoybCourseService.findFirstMonth();
+						LocalDateTime localDate=LocalDateTime.parse(startMonth, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+						LocalDate localDate2 = localDate.toLocalDate();
+						//截止年月日,取固定值20210101 删除2021年前学生作业图片
+						LocalDate localDate1 = LocalDate.parse("20210101", DateTimeFormatter.BASIC_ISO_DATE);
+						System.out.println("日期比较"+localDate1.compareTo(localDate2));
+						while (localDate1.compareTo(localDate2)>0){
+							String format = localDate2.format(DateTimeFormatter.ofPattern("yyyy-MM"));
+							//查询该月份的作业
+							List<String> fileKeyList = zuoybCourseService.findFileKeyList(format);
+							if(fileKeyList==null || fileKeyList.size()==0){
+								localDate2 =  localDate2.plusMonths(1);
+								continue;
+							}
+							int fileSize = fileKeyList.size();
+							// 删除文件。批量删除最多一次1000个。这里一次删除500
+							int pageSize = 500;
+							int pageCount = fileSize/pageSize;
+							int leftNum = fileSize%pageSize;
+							for(int i = 0;i<pageCount;i++){
+								List<String> list2 = new ArrayList<>();
+								list2.addAll(fileKeyList.subList(i*pageSize,(i+1)*pageSize));
+								// 填写需要删除的多个文件完整路径。文件完整路径中不能包含Bucket名称。
+								DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(list2).withEncodingType("url"));
+								//这里没有查看删除返回的结果集了。List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
+								//删除本地文件
+								for(String str :list2){
+									File file = new File(fileSavePath+"/"+str);
+									if(file.exists()){
+										file.delete();
+									}
+
+								}
+
+								list2.clear();
+							}
+							if(leftNum>0){
+								List<String> list3 = new ArrayList<>();
+								list3.addAll(fileKeyList.subList(pageCount*pageSize,fileSize));
+								// 填写需要删除的多个文件完整路径。文件完整路径中不能包含Bucket名称。
+								DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(list3).withEncodingType("url"));
+								//删除本地文件
+								for(String str :list3){
+									File file = new File(fileSavePath+"/"+str);
+									if(file.exists()){
+										file.delete();
+									}
+								}
+								list3.clear();
+							}
+							fileKeyList.clear();
+
+							localDate2 =  localDate2.plusMonths(1);
+						}
+					}catch (OSSException oe) {
+						System.out.println("Caught an OSSException, which means your request made it to OSS, "
+								+ "but was rejected with an error response for some reason.");
+						System.out.println("Error Message:" + oe.getErrorMessage());
+						System.out.println("Error Code:" + oe.getErrorCode());
+						System.out.println("Request ID:" + oe.getRequestId());
+						System.out.println("Host ID:" + oe.getHostId());
+					} catch (ClientException ce) {
+						System.out.println("Caught an ClientException, which means the client encountered "
+								+ "a serious internal problem while trying to communicate with OSS, "
+								+ "such as not being able to access the network.");
+						System.out.println("Error Message:" + ce.getMessage());
+					}catch (Exception e){
+						logger.error(e.getMessage());
+						e.printStackTrace();
+						response.failure("请求失败");
+					} finally {
+						if (ossClient != null) {
+							ossClient.shutdown();
+							System.out.println("----删除成功----");
+						}
+					}
+
+				});
+			}
+			response.success();
+		}catch (Exception e){
+			logger.error(e.getMessage());
+			response.failure("请求失败");
+		}
+		return response;
+	}
+
+	public static void main(String[] args) {
+		LocalDateTime localDate=LocalDateTime.parse("2018-06-13 16:05:26", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+
+//		System.out.println(localDate);
+		LocalDate localDate2 = localDate.toLocalDate();
+		LocalDate localDate1 = LocalDate.parse("20210101", DateTimeFormatter.BASIC_ISO_DATE);
+
+		while (localDate1.compareTo(localDate2)>0){
+//			System.out.println(localDate1.compareTo(localDate2));
+			String format = localDate2.format(DateTimeFormatter.ofPattern("yyyy-MM"));
+			System.out.println(format);
+			localDate2 =  localDate2.plusMonths(1);
+		}
+//		int j =980;
+//		System.out.println(980/50);
+//		System.out.println(980%50);
+//		for(int i=1;i<=19;i++){
+//			System.out.println(19*50);
+//		}
+//		List<String> list = new ArrayList<>();
+//		list.add("1");
+//		list.add("2");
+//		list.add("3");
+//		list.add("4");
+//		list.add("5");
+//		list.add("6");
+//		list.add("7");
+//		list.add("8");
+//		list.add("9");
+//		list.add("10");
+//		list.add("11");
+//		list.add("12");
+//		list.add("13");
+//		list.add("14");
+//		int pageSize = 3;
+//		int pageCount = list.size()/pageSize;
+//		int leftNum = list.size()%pageSize;
+//		for(int i = 0;i<pageCount;i++){
+//			List<String> list2 = new ArrayList<>();
+//			list2.addAll(list.subList(i*pageSize,(i+1)*pageSize));
+//			for (String str:list2){
+//				System.out.println(str);
+//			}
+//		}
+//		if(leftNum>0){
+//			List<String> list3 = new ArrayList<>();
+//			list3.addAll(list.subList(pageCount*pageSize,list.size()));
+//			for (String str:list3){
+//				System.out.println(str);
+//			}
+//		}
+
+	}
 }
 }

+ 30 - 2
src/main/resources/templates/sys/imlibuser/schoolList.html

@@ -38,6 +38,9 @@
 		    <li>
 		    <li>
 			  <input type="button" class="scbtn" value="查询" />
 			  <input type="button" class="scbtn" value="查询" />
 		    </li>
 		    </li>
+			<li th:if="${ #httpSession.getAttribute('admin') !=null   } " >
+				<input type="button" id="delFile" class="scbtn2" value="删除2021年前的作业图片" />
+			</li>
 		</ul>
 		</ul>
 	</form>
 	</form>
 	
 	
@@ -437,8 +440,33 @@ function copyContent(ElementObj){
 	        	filePrincipalLoad(this);
 	        	filePrincipalLoad(this);
 	        }
 	        }
 	    })
 	    })
-	    
-	    
+
+		//点击删除图片
+		$("#delFile").click(function (){
+			//请求后台删除文件,只发起一次请求。后续请求直接返回
+			$.ajax({
+				url: "[[${sysUrl}]]/sys/lib/admin/delFile",
+				type: 'POST',
+				data: {},
+				processData: false,
+				contentType: false,
+				beforeSend: function () {
+					parent.layer.msg("请求在处理,请勿重复点击",{icon:2});
+				},
+				success: function (responseStr) {
+					if(responseStr.meta.success){
+						parent.layer.msg("请求成功,删除中",{icon:1});
+					}else{
+						parent.layer.msg(responseStr.meta.message,{icon:11});
+					}
+				}
+				,
+				error : function (responseStr) {
+					parent.layer.msg("导入失败,请刷新后重试!",{icon:11});
+				}
+			});
+		})
+
 	    $("#leaderimput").click(function (){
 	    $("#leaderimput").click(function (){
 			$("#leaderfile").click();
 			$("#leaderfile").click();
 		})
 		})