|
@@ -0,0 +1,221 @@
|
|
|
|
+package com.caimei.modules.archive.service;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.*;
|
|
|
|
+
|
|
|
|
+import com.aliyun.oss.OSS;
|
|
|
|
+import com.aliyun.oss.OSSClientBuilder;
|
|
|
|
+import com.caimei.modules.archive.entity.CmOrderArchiveFile;
|
|
|
|
+import com.caimei.modules.archive.utils.OssArchiveUtil;
|
|
|
|
+import com.caimei.modules.oss.entity.CmOssArchivePdf;
|
|
|
|
+import com.caimei.modules.oss.utils.OSSUtils;
|
|
|
|
+import com.caimei.modules.product.entity.Product;
|
|
|
|
+import com.caimei.utils.CodeUtil;
|
|
|
|
+import com.caimei.utils.StringUtil;
|
|
|
|
+import com.thinkgem.jeesite.common.config.Global;
|
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
+import org.apache.commons.lang3.RandomUtils;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+
|
|
|
|
+import com.thinkgem.jeesite.common.persistence.Page;
|
|
|
|
+import com.thinkgem.jeesite.common.service.CrudService;
|
|
|
|
+import com.caimei.modules.archive.entity.CmOrderArchive;
|
|
|
|
+import com.caimei.modules.archive.dao.CmOrderArchiveDao;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 订单资料Service
|
|
|
|
+ * @author Aslee
|
|
|
|
+ * @version 2021-07-30
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+@Transactional(readOnly = true)
|
|
|
|
+public class CmOrderArchiveService extends CrudService<CmOrderArchiveDao, CmOrderArchive> {
|
|
|
|
+ @Resource
|
|
|
|
+ private CmOrderArchiveDao cmOrderArchiveDao;
|
|
|
|
+
|
|
|
|
+ public CmOrderArchive get(String id) {
|
|
|
|
+ return super.get(id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<CmOrderArchive> findList(CmOrderArchive cmOrderArchive) {
|
|
|
|
+ return super.findList(cmOrderArchive);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Page<CmOrderArchive> findPage(Page<CmOrderArchive> page, CmOrderArchive cmOrderArchive) {
|
|
|
|
+ return super.findPage(page, cmOrderArchive);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static String endpoint = Global.getConfig("aliyun.endpoint");
|
|
|
|
+ private static String accessKeyId = Global.getConfig("aliyun.accessKeyId");
|
|
|
|
+ private static String accessKeySecret = Global.getConfig("aliyun.accessKeySecret");
|
|
|
|
+ private static String privateBucket = Global.getConfig("aliyun.bucketName");
|
|
|
|
+ private static String config = Global.getConfig("cm.config");
|
|
|
|
+
|
|
|
|
+ @Transactional(readOnly = false)
|
|
|
|
+ public void save(CmOrderArchive cmOrderArchive) {
|
|
|
|
+ if (cmOrderArchive.getIsNewRecord()) {
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
|
|
|
+ StringBuilder orderArchiveNo = new StringBuilder(format.format(new Date()));
|
|
|
|
+ Random random = new Random();
|
|
|
|
+ for (int i = 0; i < 8; i++) {
|
|
|
|
+ orderArchiveNo.append(random.nextInt(10));
|
|
|
|
+ }
|
|
|
|
+ cmOrderArchive.setArchiveNo(orderArchiveNo.toString());
|
|
|
|
+ cmOrderArchive.setAddTime(new Date());
|
|
|
|
+ }
|
|
|
|
+ super.save(cmOrderArchive);
|
|
|
|
+ String fileIds = cmOrderArchive.getFileIds();
|
|
|
|
+ for (String id : fileIds.split(",")) {
|
|
|
|
+ if (StringUtil.isNotBlank(id)) {
|
|
|
|
+ cmOrderArchiveDao.updateArchiveFile(Integer.valueOf(id),Integer.valueOf(cmOrderArchive.getId()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional(readOnly = false)
|
|
|
|
+ public void delete(CmOrderArchive cmOrderArchive) {
|
|
|
|
+ super.delete(cmOrderArchive);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<String> getShopOrderIdList(Integer shopOrderId) {
|
|
|
|
+ return cmOrderArchiveDao.getShopOrderIdList(shopOrderId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<CmOrderArchiveFile> getArchiveFileList(String archiveId) {
|
|
|
|
+ return cmOrderArchiveDao.getArchiveFileList(Integer.parseInt(archiveId));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional(readOnly = false)
|
|
|
|
+ public Map<String, Object> upload(MultipartFile multipartFile, String fileName, Integer orderArchiveId, String fileIds) {
|
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
|
+ Integer fileIdByFileName = cmOrderArchiveDao.checkFileName(fileName, orderArchiveId, fileIds);
|
|
|
|
+ if (null != fileIdByFileName) {
|
|
|
|
+ map.put("success", false);
|
|
|
|
+ map.put("msg", "文件已存在");
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
|
|
+ String fileAllName = multipartFile.getOriginalFilename();
|
|
|
|
+ String fileType = fileAllName.substring(fileAllName.lastIndexOf(".") + 1);
|
|
|
|
+ String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
+ String filePath = uuid + "." + fileType;
|
|
|
|
+ String contentType = OssArchiveUtil.getContentType(fileAllName);
|
|
|
|
+ try {
|
|
|
|
+ //保存本地
|
|
|
|
+ File file = OssArchiveUtil.ossUpload(multipartFile);
|
|
|
|
+ logger.info("默认路径>>>" + file.getAbsolutePath());
|
|
|
|
+ //上传oss
|
|
|
|
+ String url = OssArchiveUtil.ossUpload(filePath, "archiveFile/", file, contentType);
|
|
|
|
+ //删除本地文件
|
|
|
|
+ OssArchiveUtil.deleteFile(file);
|
|
|
|
+ //保存关联关系
|
|
|
|
+ CmOrderArchiveFile archiveFile = new CmOrderArchiveFile();
|
|
|
|
+ archiveFile.setFileName(fileName);
|
|
|
|
+ archiveFile.setOssName(filePath);
|
|
|
|
+ archiveFile.setOssUrl(url);
|
|
|
|
+ archiveFile.setUploadTime(new Date());
|
|
|
|
+ cmOrderArchiveDao.insertArchiveFile(archiveFile);
|
|
|
|
+ map.put("success", true);
|
|
|
|
+ map.put("msg", "操作成功");
|
|
|
|
+ map.put("archiveFile", archiveFile);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ map.put("success", false);
|
|
|
|
+ map.put("msg", "操作失败");
|
|
|
|
+ logger.info("上传异常!!!");
|
|
|
|
+ }
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Transactional(readOnly = false)
|
|
|
|
+ public void deleteFile(Integer fileId) {
|
|
|
|
+ CmOrderArchiveFile archiveFile = cmOrderArchiveDao.getArchiveFileById(fileId);
|
|
|
|
+ if (archiveFile != null) {
|
|
|
|
+ //删除oss服务器上的文件
|
|
|
|
+ OssArchiveUtil.deleteSingleFile("archiveFile/", archiveFile.getOssName());
|
|
|
|
+ cmOrderArchiveDao.deleteArchiveFile(fileId);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Page<CmOrderArchiveFile> findArchiveFilePage(Page<CmOrderArchiveFile> cmOrderArchiveFilePage, CmOrderArchiveFile cmOrderArchiveFile) {
|
|
|
|
+ cmOrderArchiveFile.setPage(cmOrderArchiveFilePage);
|
|
|
|
+ cmOrderArchiveFilePage.setList(cmOrderArchiveDao.getArchiveFileList(cmOrderArchiveFile.getOrderArchiveId()));
|
|
|
|
+ return cmOrderArchiveFilePage;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void downFile(Integer fileId, HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
|
+ CmOrderArchiveFile archiveFile = cmOrderArchiveDao.getArchiveFileById(fileId);
|
|
|
|
+ String fileName = archiveFile.getFileName();
|
|
|
|
+ OssArchiveUtil.downFile("archiveFile/", archiveFile.getOssName(), fileName);
|
|
|
|
+ download(request, response, fileName);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void download(HttpServletRequest request, HttpServletResponse response, String fileName) throws IOException {
|
|
|
|
+ File file = new File("./" + fileName);
|
|
|
|
+ // 文件存在才下载
|
|
|
|
+ if (file.exists()) {
|
|
|
|
+ OutputStream out = null;
|
|
|
|
+ FileInputStream in = null;
|
|
|
|
+ try {
|
|
|
|
+ // 1.读取要下载的内容
|
|
|
|
+ in = new FileInputStream(file);
|
|
|
|
+
|
|
|
|
+ // 2. 告诉浏览器下载的方式以及一些设置
|
|
|
|
+ // 解决文件名乱码问题,获取浏览器类型,转换对应文件名编码格式,IE要求文件名必须是utf-8, firefo要求是iso-8859-1编码
|
|
|
|
+ String agent = request.getHeader("user-agent");
|
|
|
|
+ if (agent.contains("FireFox")) {
|
|
|
|
+ fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
|
|
|
|
+ } else {
|
|
|
|
+ fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
+ }
|
|
|
|
+ // 设置下载文件的mineType,告诉浏览器下载文件类型
|
|
|
|
+ String mineType = request.getServletContext().getMimeType(fileName);
|
|
|
|
+ response.setContentType(mineType);
|
|
|
|
+ // 设置一个响应头,无论是否被浏览器解析,都下载
|
|
|
|
+ response.setHeader("Content-disposition", "attachment; filename=" + fileName);
|
|
|
|
+ // 将要下载的文件内容通过输出流写到浏览器
|
|
|
|
+ out = response.getOutputStream();
|
|
|
|
+ int len = 0;
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
+ while ((len = in.read(buffer)) > 0) {
|
|
|
|
+ out.write(buffer, 0, len);
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ if (out != null) {
|
|
|
|
+ out.close();
|
|
|
|
+ }
|
|
|
|
+ if (in != null) {
|
|
|
|
+ in.close();
|
|
|
|
+ }
|
|
|
|
+ file.delete();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void downAllFile(Integer orderArchiveId, HttpServletRequest request, HttpServletResponse response) {
|
|
|
|
+ List<CmOrderArchiveFile> archiveFileList = cmOrderArchiveDao.getArchiveFileList(orderArchiveId);
|
|
|
|
+ archiveFileList.forEach(archiveFile->{
|
|
|
|
+ try {
|
|
|
|
+ downFile(Integer.parseInt(archiveFile.getId()), request, response);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String getAllFileIds(Integer orderArchiveId) {
|
|
|
|
+ return cmOrderArchiveDao.getAllFileIds(orderArchiveId);
|
|
|
|
+ }
|
|
|
|
+}
|