|
@@ -1,22 +1,29 @@
|
|
|
package com.xy.service;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.github.yitter.idgen.YitIdHelper;
|
|
|
import com.xy.dto.FileExportDto;
|
|
|
import com.xy.entity.FileExport;
|
|
|
+import com.xy.enums.FileExportStatus;
|
|
|
+import com.xy.enums.FileExportType;
|
|
|
import com.xy.mapper.FileExportMapper;
|
|
|
-import com.xy.utils.Emptys;
|
|
|
-import com.xy.utils.MybatisPlusQuery;
|
|
|
-import com.xy.utils.PageBean;
|
|
|
-import com.xy.utils.R;
|
|
|
+import com.xy.util.ExcelUtils;
|
|
|
+import com.xy.util.FileUtils;
|
|
|
+import com.xy.utils.*;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.Cleanup;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.util.List;
|
|
|
|
|
|
import static com.xy.utils.Beans.copy;
|
|
@@ -37,6 +44,8 @@ import static com.xy.utils.PlusBeans.toPageBean;
|
|
|
@Api(tags = "文件导出")
|
|
|
public class FileExportServiceImpl extends ServiceImpl<FileExportMapper, FileExport> implements FileExportService {
|
|
|
|
|
|
+ private final FileUtils fileUtils;
|
|
|
+
|
|
|
@Override
|
|
|
@ApiOperation("对象查询")
|
|
|
public R<FileExportDto.Vo> obj(FileExportDto.Vo vo) {
|
|
@@ -80,4 +89,58 @@ public class FileExportServiceImpl extends ServiceImpl<FileExportMapper, FileExp
|
|
|
updateById(updateInfo);
|
|
|
return R.ok();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @ApiOperation("更新文件导出状态")
|
|
|
+ public R<Boolean> updateExportStatus(Long id, String exportStatus) {
|
|
|
+ FileExport update = new FileExport().setId(id).setExportStatus(exportStatus);
|
|
|
+ return R.ok(this.updateById(update));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @ApiOperation("更新文件导出状态")
|
|
|
+ public R<Boolean> updateExportStatus(Long id, String exportStatus, String fileUrl) {
|
|
|
+ FileExport update = new FileExport().setId(id).setExportStatus(exportStatus).setFileUrl(fileUrl);
|
|
|
+ return R.ok(this.updateById(update));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param sheetName Excel sheet 名
|
|
|
+ * @param head Excel head 头
|
|
|
+ * @param data 数据列表
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+ public <T> void exportExcelAsync(String sheetName, Class<T> head, List<T> data) {
|
|
|
+ //新增导出记录
|
|
|
+ long id = YitIdHelper.nextId();
|
|
|
+ String fileName = fileUtils.getFileName(id);
|
|
|
+ String pathName = fileUtils.getPathName(fileName);
|
|
|
+ try {
|
|
|
+ FileExportDto.Save fileExport = new FileExportDto.Save().setId(id)
|
|
|
+ .setExportStatus(FileExportStatus.EXPORT_ING.getCode())
|
|
|
+ .setFileType(FileExportType.DEVICE_INFO.getCode()).setFileName(fileName);
|
|
|
+ this.save(fileExport);
|
|
|
+ @Cleanup OutputStream out = new FileOutputStream(pathName);
|
|
|
+ //本地生成文件
|
|
|
+ ExcelUtils.writeOutputStream(out, sheetName, head, data);
|
|
|
+ this.updateExportStatus(id, FileExportStatus.EXPORT_LOCAL_SUCESS.getCode());
|
|
|
+
|
|
|
+ FileInputStream fis = new FileInputStream(pathName);
|
|
|
+ //对象存储云端上传
|
|
|
+ String cloudUrl = AliOssUtil.upload(fis, ExcelUtils.EXCEL_PREFIX + fileName);
|
|
|
+ if (StrUtil.isEmpty(cloudUrl)) {
|
|
|
+ this.updateExportStatus(id, FileExportStatus.EXPORT_FAILED.getCode());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //更新状态与文件地址
|
|
|
+ this.updateExportStatus(id, FileExportStatus.EXPORT_CLOUD_SUCESS.getCode(), cloudUrl);
|
|
|
+ //删除本地文件
|
|
|
+ fileUtils.deleteFile(pathName);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ this.updateExportStatus(id, FileExportStatus.EXPORT_FAILED.getCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|