package com.yanzuoguang.cloud.file; import com.yanzuoguang.cloud.CloudConfig; import com.yanzuoguang.cloud.file.dao.YzgFileDao; import com.yanzuoguang.cloud.helper.HttpFileHelper; import com.yanzuoguang.cloud.vo.YzgFileVo; import com.yanzuoguang.cloud.vo.req.*; import com.yanzuoguang.util.exception.CodeException; import com.yanzuoguang.util.exception.ExceptionHelper; import com.yanzuoguang.util.helper.DateHelper; import com.yanzuoguang.util.helper.JsonHelper; import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.vo.PageSizeData; import com.yanzuoguang.util.vo.ResponseResult; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.util.Date; /** * Excel文件操作服务 * * @author 颜佐光 */ @Component public class YzgExcelService { private final CloudConfig cloudConfig; private final YzgFileDao fileDao; private final YzgFileProcedure fileProcedure; public YzgExcelService(CloudConfig cloudConfig, YzgFileDao fileDao, YzgFileProcedure fileProcedure) { this.cloudConfig = cloudConfig; this.fileDao = fileDao; this.fileProcedure = fileProcedure; } /** * 创建文件,不需要开启事务 * * @param json * @param fileReq */ public void fileCreate(String json, YzgFileCreateReqVo fileReq) { try { YzgFileVo load = fileDao.load(fileReq, YzgFileVo.class); if (load == null) { load = JsonHelper.to(fileReq, YzgFileVo.class); fileDao.create(load); } // 生成回调参数 String serverAbsolutePath = getServerAbsolutePath(load); YzgFileCallbackReqVo callbackReq = new YzgFileCallbackReqVo( fileReq.getFileId(), fileReq.getCompanyId(), fileReq.getCallbackMQ(), fileReq.getCallbackParameter(), serverAbsolutePath ); // 判断文件状态 int fileStatus = StringHelper.toInt(load.getFileStatus()); switch (fileStatus) { case YzgFileVo.FILE_STATUS_INIT: if (fileDao.getUserCompanyCount(load) > fileReq.getCreateUserCompanyMax()) { throw new CodeException("该用户在该公司当日生成文件达到最大次数"); } if (fileDao.getCallbackCount(load) < fileReq.getCallBackMax()) { YzgFileStatusReqVo statusReqVo = new YzgFileStatusReqVo(fileReq.getFileId(), fileReq.getCompanyId()); statusReqVo.setFileStatus(YzgFileVo.FILE_STATUS_RELEASE); fileDao.updateStatus(statusReqVo); fileProcedure.fileCallback(callbackReq); } // 等待15秒后再次发出请求,追踪文件进度 fileProcedure.fileCreateDelay(json); break; case YzgFileVo.FILE_STATUS_RELEASE: fileProcedure.fileCallback(callbackReq); // 等待15秒后再次发出请求,追踪文件进度 fileProcedure.fileCreateDelay(json); break; default: break; } } catch (CodeException ex) { if (fileReq != null && !StringHelper.isEmpty(fileReq.getFileId())) { ResponseResult<?> error = ExceptionHelper.getError(ex); fileDao.updateStatus(new YzgFileStatusReqVo(fileReq.getFileId(), fileReq.getCompanyId(), error.getMessage())); } } } public void updateStatus(YzgFileStatusReqVo fileReq) { fileDao.updateStatus(fileReq); } /** * 获取文件数据 * * @param req * @return */ public PageSizeData<YzgFileVo> loadPage(YzgFileQueryReqVo req) { return fileDao.loadPage(req, YzgFileVo.class); } /** * 下载文件 * * @param req * @param response * @throws IOException */ public void donwload(YzgFileDownloadReqVo req, String downloadUserId, HttpServletResponse response) throws IOException { YzgFileVo load = fileDao.load(req, YzgFileVo.class); if (load == null) { throw new CodeException("文件不存在或者已经删除"); } int fileStatus = StringHelper.toInt(load.getFileStatus()); if (fileStatus == YzgFileVo.FILE_STATUS_ERROR) { throw new CodeException(load.getErrorMessage()); } else if (fileStatus != YzgFileVo.FILE_STATUS_OK) { throw new CodeException("文件正在生成中"); } String serverPath = getServerPath(load); File file = new File(serverPath); if (!file.exists()) { throw new CodeException("文件已经被删除,或者服务器之间没有共享文件目录"); } load.setDownloadDate(DateHelper.getNow()); load.setDownloadUserId(downloadUserId); fileDao.update(load); HttpFileHelper.localToDown(serverPath, load.getFileName(), response); } /** * 下载文件 * * @param req */ public YzgFileVo load(YzgFileDownloadReqVo req) { YzgFileVo load = fileDao.load(req, YzgFileVo.class); if (load == null) { throw new CodeException("文件不存在或者已经删除"); } // 文件生成成功后,文件是否存在,不存在时则提示 if (StringHelper.toInt(load.getFileStatus()) == YzgFileVo.FILE_STATUS_OK) { String serverPath = getServerPath(load); File file = new File(serverPath); if (!file.exists()) { throw new CodeException("文件不存在或者已经删除"); } } return load; } /** * 删除文件 * * @param req */ public void remove(YzgFileDownloadReqVo req) { YzgFileVo load = fileDao.load(req, YzgFileVo.class); if (load == null) { throw new CodeException("文件不存在或者已经删除"); } String serverPath = getServerPath(load); File file = new File(serverPath); if (file.exists()) { file.delete(); } fileDao.remove(req); } private String getServerPath(YzgFileVo load) { return String.format("%s/%s", cloudConfig.getServerUrl(), getServerAbsolutePath(load)); } private String getServerAbsolutePath(YzgFileVo load) { Date date = DateHelper.getDateTime(load.getCreateDate()); // 获取临时文件路径 String tempFolder = cloudConfig.getTempFolder(load.getCompanyId(), date); return String.format("%s/%s.xlsx", tempFolder, load.getFileId()); } }