jiangping
2023-12-29 f9691d544e62d6c04dbfe45d05a6c7bc5e004291
server/services/src/main/java/com/doumee/service/business/impl/LocksServiceImpl.java
@@ -1,4 +1,6 @@
package com.doumee.service.business.impl;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.google.common.collect.Lists;
@@ -22,12 +24,22 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doumee.service.business.SitesService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -140,6 +152,7 @@
        return new ArrayList<>();
    }
    @Override
    public PageData<Locks> findPage(PageWrap<Locks> pageWrap) {
        IPage<Locks> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
@@ -170,6 +183,53 @@
        QueryWrapper<Locks> wrapper = new QueryWrapper<>(locks);
        return locksMapper.selectCount(wrapper);
    }
    public static void packFilesToZip(List<File> files,    ServletOutputStream os) throws IOException {
        try (ZipArchiveOutputStream zipOutputStream = new ZipArchiveOutputStream(os)) {
            for (File file : files) {
                ZipArchiveEntry entry = new ZipArchiveEntry(file.getName());
                zipOutputStream.putArchiveEntry(entry);
                try (FileInputStream fileInputStream = new FileInputStream(file)) {
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fileInputStream.read(buffer)) > 0) {
                        zipOutputStream.write(buffer, 0, length);
                    }
                }
                zipOutputStream.closeArchiveEntry();
            }
        }
    }
    @Override
    public void  exportImages(String siteId, HttpServletResponse response) {
        try {
            List<File> fileList = new ArrayList<>();
            List<Locks> locks = locksJoinMapper.selectList(new QueryWrapper<Locks>().lambda().eq(Locks::getSiteId,siteId).isNotNull(Locks::getInfo));
            if(locks== null || locks.size() == 0){
                throw  new BusinessException(ResponseStatus.DATA_EMPTY);
            }
            String path = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.IMG_DIR).getCode()+systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PROJECTS).getCode();
            for(Locks l : locks){
                if(StringUtils.isNotBlank(l.getInfo())){
                    File file = new File(path + l.getInfo());
                    if(file!=null && file.isFile()){
                        fileList.add(file);
                    }
                }
            }
            if(fileList == null || fileList.size() == 0){
                throw  new BusinessException(ResponseStatus.DATA_EMPTY);
            }
            String fileName =  "站点【"+siteId+"】小程序码批量导出_"+System.currentTimeMillis()+".zip" ;
            String encodeFileName = URLEncoder.encode(fileName);
            response.setHeader("Content-Disposition","attachment;filename=" + encodeFileName);
            response.setContentType("application/octet-stream");
            response.setHeader("eva-opera-type", "download");
            response.setHeader("eva-download-filename", encodeFileName);
            packFilesToZip(fileList,response.getOutputStream());
        } catch (IOException e) {
            throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR, e);
        }
    }
}