21. Enums

An enum type is a distinct type that declares a set of named constants.
[Example: The example
enum Color
{
Red,
Green,
Blue
}
declares an enum type named Color with members Red, Green, and Blue. end
example]
package com.example.problembase.service; import com.example.entry.demo.MutexUserPostRequest; import com.example.entry.demo.MutexUserPostResponse; import java.util.List; public interface EulPermMutexQueryService { List<MutexUserPostResponse> selectMutexUserPostPage(MutexUserPostRequest request); } package com.example.problembase.service; import com.example.entry.demo.UserPostRequest; import com.example.entry.demo.UserPostResponse; import java.util.List; public interface EulPermQueryService { List<UserPostResponse> selectUserPostPage(UserPostRequest request); } package com.example.problembase.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; import com.example.entry.dto.problemitem.ProblemitemDTO; import com.example.entry.dto.problemitem.ProblemitemQueryDTO; import com.example.entry.dto.problemitem.ProblemitemUpdateDTO; import com.example.entry.po.problemitem.Problemitem; import com.example.entry.vo.problemitem.ProblemitemVO; import java.util.List; /** * <p> * 技术问题记录明细 服务类 * </p> * * @author Luo * @since 2025-06-21 */ public interface IProblemitemService extends IService<Problemitem> { /** * 获取实体list * * @return */ List<ProblemitemVO> listNoPage(ProblemitemQueryDTO param); /** * 获取实体list分页 * * @param param * @return */ IPage<ProblemitemVO> list(ProblemitemQueryDTO param); /** * 获取实体详情 * * @param id * @return */ ProblemitemVO get(String id); /** * 保存实体 * * @param param * @return */ boolean save(ProblemitemDTO param); /** * 更新实体 * * @param param * @return */ boolean update(ProblemitemUpdateDTO param); /** * 根据ID删除 * * @param id * @return */ boolean deleteByID(String id); } package com.example.entry.demo; import lombok.Data; @Data public class EviReportFilecreateinfoEntity { private String fileName; private String status; private String createMessage; private String recordNum; private String fileSize; } package com.example.entry.demo; import lombok.Data; @Data public class ReportDownExcelRequest { private String fileName; private String excelType; } package com.example.exception; import com.example.enums.CommonResponseEnum; public class CommonBusinessException extends RuntimeException{ private Integer code; private String message; public CommonBusinessException(Integer code, String message) { this.code = code; this.message = message; } public CommonBusinessException(CommonResponseEnum responseEnum) { this.code = responseEnum.getCode(); this.message = responseEnum.getMessage(); } public CommonBusinessException(CommonResponseEnum responseEnum, String message) { this.code = responseEnum.getCode(); this.message = message; } } package com.example.enums; import com.example.assertion.Assert; import com.example.exception.CommonBusinessException; import lombok.Getter; import java.text.MessageFormat; @Getter public enum CommonResponseEnum implements Assert { /** * 密码错误 */ SYSTEM_ERROR(601, "密码错误"), /** * 数据异常 */ DATA_EXCEPTION(602,"数据异常"), private final Integer code; private final String message; CommonResponseEnum(Integer code, String message) { this.code = code; this.message = message; } /** * 创建异常 * * @param args * @return */ @Override public CommonBusinessException newException(Object... args) { String msg = MessageFormat.format(this.getMessage(), args); return new CommonBusinessException(this.getCode(),msg); } @Override public CommonBusinessException newException(Throwable t, Object... args) { return null; } } package com.example.entry.demo; import lombok.Data; @Data public class PaiCommSysparam { private String paraValue; } package com.example.utils; import com.example.entry.demo.PaiCommSysparam; public class ParaCommmonUtil { public static PaiCommSysparam getParameter(String moduleName,String key){ return new PaiCommSysparam(); } } 上面的是相关的引用和依赖 测试框架是MockitoJUnitRunner, 单元测试类如下: @RunWith(MockitoJUnitRunner.class) public class ExcelServiceImplTest { } 请帮我为下面的代码生成单元测试,单元测试代码写在ExcelServiceImplTest中 package com.example.problembase.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.example.entry.demo.*; import com.example.entry.po.problemitem.Problemitem; import com.example.enums.CommonResponseEnum; import com.example.exception.CommonBusinessException; import com.example.problembase.service.EulPermMutexQueryService; import com.example.problembase.service.EulPermQueryService; import com.example.problembase.service.IProblemitemService; import com.example.problembase.service.ITecHigLigService; import com.example.utils.ParaCommmonUtil; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.stereotype.Component; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.nio.file.Files; import java.nio.file.Path; import java.util.HashMap; import java.util.List; @Component @EnableAsync public class ExcelServiceImpl { @Autowired private ITecHigLigService iTecHigLigService; @Autowired private EulPermMutexQueryService eulPermMutexQueryService; @Autowired private EulPermQueryService eulPermQueryService; @Autowired private IProblemitemService iProblemitemService; private static HashMap<String, Class> reportMap = new HashMap<>(); static { reportMap.put("1", Demo2DO.class); } @Async public void typeExcelDown(EviReportFilecreateinfoEntity insertFileEntry, ReportDownExcelRequest request) { if (ObjectUtil.isEmpty(insertFileEntry.getFileName())) { throw new CommonBusinessException(CommonResponseEnum.SYSTEM_ERROR); } // String filePath = "/" + "20240231" + File.separator + "report" + insertFileEntry.getFileName() + ".xlsx"; File file = new File(filePath); Path parentPath = file.toPath().getParent(); //检查父目录是否存在,如果不存在,则尝试创建 if (parentPath != null && Files.notExists(parentPath)) { try { Files.createDirectories(parentPath); } catch (IOException e) { throw new CommonBusinessException(CommonResponseEnum.SYSTEM_ERROR); } } boolean flag = true; if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { insertFileEntry.setStatus("4"); insertFileEntry.setCreateMessage(e.getMessage()); flag = false; } } if (flag) { excelDown(request, insertFileEntry, filePath, reportMap.get(request.getExcelType())); } Problemitem insertFileEntryp = new Problemitem(); LambdaQueryWrapper<Problemitem> querywrapper = new LambdaQueryWrapper<>(); querywrapper.eq(Problemitem::getRecordNum, insertFileEntry.getRecordNum()); iProblemitemService.update(insertFileEntryp, querywrapper); } public void excelDown(ReportDownExcelRequest request, EviReportFilecreateinfoEntity insertFileEntry, String filePath, Class tClass) { long current = 1; boolean hasMoreData = true; try { ExcelWriter excelwriter = EasyExcelFactory.write(filePath, tClass).build(); WriteSheet writeSheet = EasyExcelFactory.writerSheet(insertFileEntry.getFileName()).build(); PaiCommSysparam parameter = ParaCommmonUtil.getParameter("EUMS", "EXCEL_PAGE_MAX"); Long maxSize = Long.valueOf(parameter.getParaValue()); while (hasMoreData) { List records = getList(request, current, maxSize); if (current == 1 && CollUtil.isEmpty(records)) { insertFileEntry.setStatus("2"); insertFileEntry.setCreateMessage("没有"); hasMoreData = false; } else if (current != 1 && CollUtil.isEmpty(records)) { insertFileEntry.setStatus("1"); hasMoreData = false; } else { // excelwriter.write(records, writeSheet); hasMoreData = checkMoreData(records.size(), maxSize); current++; } excelwriter.finish(); insertFileEntry.setStatus("1"); insertFileEntry.setFileSize(getFilesizeKB(new File(filePath))); } }catch(Exception e){ insertFileEntry.setStatus("4"); insertFileEntry.setCreateMessage(e.getMessage()); } } public List getList(ReportDownExcelRequest request, long current, long maxSize) { switch (request.getExcelType()){ case "1": MutexUserPostRequest req = new MutexUserPostRequest(); BeanUtils.copyProperties(request,req); req.setQuerySize(maxSize); req.setStertNum(current); return eulPermMutexQueryService.selectMutexUserPostPage(req); case "2": UserPostRequest req1 = new UserPostRequest(); BeanUtils.copyProperties(request,req1); req1.setQuerySize(maxSize); req1.setStertNum(current); return eulPermQueryService.selectUserPostPage(req1); default: throw new CommonBusinessException(CommonResponseEnum.SYSTEM_ERROR); } } public boolean checkMoreData(int size, long maxSize) { if (size == maxSize) { return true; } else { return false; } } public static String getFilesizeKB(File file) { if (file.exists()) { long fileSizeInBytes = file.length(); double fileBK = fileSizeInBytes / 1024.0; BigDecimal db = new BigDecimal(fileBK).setScale(2, BigDecimal.ROUND_HALF_UP); return db.toString(); } else { return "-1"; } } }
07-15
/home/zhouweixiang/test/git/海外导航/code/0301Code/KOTEINavi/sdk_module_main/src/main/java/com/kotei/overseas/navi/update/FileUtilszwx.java:5: 错误: 找不到符号 import android.annotation.RequiresApi; ^ 符号: 类 RequiresApi 位置: 程序包 android.annotation /home/zhouweixiang/test/git/海外导航/code/0301Code/KOTEINavi/sdk_module_main/src/main/java/com/kotei/overseas/navi/update/FileUtilszwx.java:19: 错误: 找不到符号 import net.lingala.zip4j.model.enums.WorkType; ^ 符号: 类 WorkType 位置: 程序包 net.lingala.zip4j.model.enums /home/zhouweixiang/test/git/海外导航/code/0301Code/KOTEINavi/sdk_module_main/src/main/java/com/kotei/overseas/navi/update/FileUtilszwx.java:59: 错误: 找不到符号 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) ^ 符号: 类 RequiresApi 位置: 类 FileUtilszwx package com.kotei.overseas.navi.update; import android.util.Log; import android.os.Build; import android.annotation.RequiresApi; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; // zip4j 相关导入 import net.lingala.zip4j.model.enums.WorkType; public class FileUtilszwx { private static final String TAG = "FileUtils"; private static long totalSize = 0; private static long lastCheckTime = 0; // 动态缓冲区大小定义 public static final int MIN_BUFFER_SIZE = 8 * 1024 * 1024; // 8MB public static final int MEDIUM_BUFFER_SIZE = 64 * 1024 * 1024; // 64MB public static final int MAX_BUFFER_SIZE = 128 * 1024 * 1024; // 128MB /** * 带进度拷贝文件(零拷贝+动态缓冲区) */ // 修改 copyFileWithProgress 方法 public static boolean copyFileWithProgress(File src, File dest, USBOfflineUpdater.ProgressCallback callback) { long totalSize = src.length(); long copiedSize = 0; long lastCallbackSize = 0; try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { copiedSize = zeroCopy(src, dest, callback, totalSize, lastCallbackSize); } else { copiedSize = dynamicBufferCopy(src, dest, callback, totalSize, lastCallbackSize); } return copiedSize == totalSize; } catch (IOException | InterruptedException e) { Log.e(TAG, "文件拷贝失败", e); Thread.currentThread().interrupt(); // 重新设置中断状态 return false; } } /** * 零拷贝实现 */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private static long zeroCopy(File src, File dest, USBOfflineUpdater.ProgressCallback callback, long totalSize, long lastCallbackSize) throws IOException, InterruptedException { try (FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dest).getChannel()) { long position = 0; long remaining = totalSize; while (remaining > 0 && !Thread.currentThread().isInterrupted()) { long transferred = inChannel.transferTo(position, remaining, outChannel); position += transferred; remaining -= transferred; // 百分比回调 long currentSize = totalSize - remaining; if (currentSize * 1000 / totalSize > lastCallbackSize) { if (callback != null) { callback.onProgress(currentSize, totalSize); } lastCallbackSize = currentSize * 1000 / totalSize; } } // 主动检查中断状态 if (Thread.currentThread().isInterrupted()) { throw new InterruptedException("文件拷贝被中断"); } return position; } } /** * 动态缓冲区拷贝 */ private static long dynamicBufferCopy(File src, File dest, USBOfflineUpdater.ProgressCallback callback, long totalSize, long lastCallbackSize) throws IOException, InterruptedException { int bufferSize = getBufferSize(totalSize); byte[] buffer = new byte[bufferSize]; long copiedSize = 0; try (InputStream in = new BufferedInputStream(new FileInputStream(src)); OutputStream out = new BufferedOutputStream(new FileOutputStream(dest))) { int length; while ((length = in.read(buffer)) > 0 && !Thread.currentThread().isInterrupted()) { out.write(buffer, 0, length); copiedSize += length; // 百分比回调 if (copiedSize * 1000 / totalSize > lastCallbackSize) { if (callback != null) { callback.onProgress(copiedSize, totalSize); } lastCallbackSize = copiedSize * 1000 / totalSize; } } // 主动检查中断状态 if (Thread.currentThread().isInterrupted()) { throw new InterruptedException("文件拷贝被中断"); } return copiedSize; } } /** * 动态缓冲区大小计算 */ private static int getBufferSize(long fileSize) { if (fileSize < 100 * 1024 * 1024) return MIN_BUFFER_SIZE; if (fileSize < 2 * 1024 * 1024 * 1024L) return MEDIUM_BUFFER_SIZE; return MAX_BUFFER_SIZE; } /** * 带进度解压ZIP文件(使用zip4j) */ public static boolean extractZipWithProgress(File zipFile, File targetDir, USBOfflineUpdater.ProgressCallback callback) { try { // 使用 zip4j 解压 net.lingala.zip4j.ZipFile zip = new net.lingala.zip4j.ZipFile(zipFile); // 注册进度监听器 zip.addProgressListener(progressUpdate -> { if (callback != null && progressUpdate.getWorkType() == WorkType.EXTRACT) { callback.onProgress(progressUpdate.getWorkCount(), progressUpdate.getTotalWorkCount()); } }); // 解压配置 zip.extractAll(targetDir.getAbsolutePath()); return true; } catch (Exception e) { Log.e(TAG, "zip4j 解压失败", e); return false; } } /** * 带进度压缩目录(使用zip4j) */ public static boolean compressDirectoryWithProgress(File sourceDir, File outputFile, USBOfflineUpdater.ProgressCallback callback) { try { // 创建 zip 文件 net.lingala.zip4j.ZipFile zipFile = new net.lingala.zip4j.ZipFile(outputFile); // 添加文件夹 zipFile.addFolder(sourceDir, new net.lingala.zip4j.model.ZipParameters()); // 注册进度监听器 zipFile.addProgressListener(progressUpdate -> { if (callback != null && progressUpdate.getWorkType() == WorkType.ADD) { callback.onProgress(progressUpdate.getWorkCount(), progressUpdate.getTotalWorkCount()); } }); return true; } catch (Exception e) { Log.e(TAG, "zip4j 压缩失败", e); return false; } } // 其他原有方法保持不变(删除目录、计算大小等)... // ...此处省略原有方法以保持简洁... /** * 解压ZIP文件(无进度) */ public static boolean extractZip(File zipFile, File targetDir) { return extractZipWithProgress(zipFile, targetDir, null); } /** * 计算目录大小 */ public static long getDirectorySize(File directory) { long size = 0; if (directory == null || !directory.exists()) return 0; if (directory.isDirectory()) { File[] files = directory.listFiles(); if (files != null) { for (File file : files) { size += getDirectorySize(file); } } } else { size = directory.length(); } return size; } /** * 删除目录内容(保留目录本身) */ public static boolean deleteDirectoryContents(File directory) { if (directory == null || !directory.exists() || !directory.isDirectory()) { return false; } File[] files = directory.listFiles(); if (files == null) return true; boolean success = true; for (File file : files) { if (file.isDirectory()) { success &= deleteRecursive(file); } else { if (!file.delete()) { Log.w(TAG, "删除文件失败: " + file.getAbsolutePath()); success = false; } } } return success; } /** * 递归删除文件/目录 */ public static boolean deleteRecursive(File file) { boolean success = true; if (file.isDirectory()) { File[] children = file.listFiles(); if (children != null) { for (File child : children) { success &= deleteRecursive(child); } } } if (!file.delete()) { Log.w(TAG, "删除失败: " + file.getAbsolutePath()); success = false; } return success; } }
08-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值