门店、导购二维码下载

master
yechenhao 6 years ago
parent 01c480e06f
commit 694c861ed0

@ -56,6 +56,8 @@ public class ShiroConfig {
filterChainDefinitionMap.put("/login/bind", "anon"); filterChainDefinitionMap.put("/login/bind", "anon");
filterChainDefinitionMap.put("/user/qrCode", "anon"); filterChainDefinitionMap.put("/user/qrCode", "anon");
filterChainDefinitionMap.put("/user/sync", "anon"); filterChainDefinitionMap.put("/user/sync", "anon");
filterChainDefinitionMap.put("/store/emp/download/store/code", "anon");
filterChainDefinitionMap.put("/store/emp/download/store/seller/code", "anon");
filterChainDefinitionMap.put("/department/sync", "anon"); filterChainDefinitionMap.put("/department/sync", "anon");
// 配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 // 配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了
filterChainDefinitionMap.put("/logout", "logout"); filterChainDefinitionMap.put("/logout", "logout");

@ -18,6 +18,10 @@ public class Constants {
* code * code
*/ */
public static final String DATABASE_CODE_KEY = "code"; public static final String DATABASE_CODE_KEY = "code";
/**
* code
*/
public static final String DATABASE_TYPE_KEY = "type";
/** /**
* 0 * 0
@ -44,6 +48,11 @@ public class Constants {
*/ */
public static final String STORE_DATA_TYPE_SELLER = "seller"; public static final String STORE_DATA_TYPE_SELLER = "seller";
/**
* -
*/
public static final Integer TYPE_STAFF_SELLER = 4;
/** /**
* Type 1.()2. * Type 1.()2.
*/ */

@ -78,4 +78,18 @@ public class StoreEmployeeController extends BaseController {
log.error("批量下载店铺二维码失败", e); log.error("批量下载店铺二维码失败", e);
} }
} }
/**
*
* @param response
*/
@RequestMapping(value = "download/store/seller/code",method = RequestMethod.GET)
@ResponseBody
public void downloadSellerCode(HttpServletResponse response) {
try {
storeEmployeeService.downloadStoreStaffCode(response);
} catch (Exception e) {
log.error("批量下载店铺二维码失败", e);
}
}
} }

@ -20,11 +20,13 @@ import com.kiisoo.ic.generalize.entity.PrivilageOrganizationalMember;
import com.kiisoo.ic.generalize.mapper.RetailCompanyMapper; import com.kiisoo.ic.generalize.mapper.RetailCompanyMapper;
import com.kiisoo.ic.store.bean.PoiStoreStaffDTO; import com.kiisoo.ic.store.bean.PoiStoreStaffDTO;
import com.kiisoo.ic.store.entity.PoiStore; import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.entity.PrivilageCpUserStoreDO; import com.kiisoo.ic.store.entity.PrivilageCpUserStoreDO;
import com.kiisoo.ic.store.entity.StoreUserDTO; import com.kiisoo.ic.store.entity.StoreUserDTO;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper; import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper; import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import com.kiisoo.ic.store.mapper.PrivilageCpUserStoreDOMapper; import com.kiisoo.ic.store.mapper.PrivilageCpUserStoreDOMapper;
import com.kiisoo.ic.utils.ZipUtils;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -86,6 +88,7 @@ public class StoreEmployeeService {
@Autowired @Autowired
private RetailCompanyMapper retailCompanyMapper; private RetailCompanyMapper retailCompanyMapper;
/** /**
* *
* @param storeId id * @param storeId id
@ -147,32 +150,30 @@ public class StoreEmployeeService {
* @param response * @param response
*/ */
public void downloadStoreUserCode(HttpServletResponse response) throws IOException { public void downloadStoreUserCode(HttpServletResponse response) throws IOException {
Map<String,Object> result = new HashMap<>(); //查询所有公司
List<PrivilageOrganizational> privilageOrganizationals = retailCompanyMapper.selectList(null); List<PrivilageOrganizational> privilageOrganizationals = retailCompanyMapper.selectList(null);
if (CollectionUtils.isNotEmpty(privilageOrganizationals)){ if (CollectionUtils.isNotEmpty(privilageOrganizationals)){
//根文件夹用于存放公司
String fileUrl = "门店码-"+UUID.randomUUID().toString().replace("-", "");
String fileUrl = UUID.randomUUID().toString().replace("-", "");
File file = new File(fileUrl); File file = new File(fileUrl);
file.mkdir(); file.mkdir();
// 生成的ZIP文件名
byte[] buffer = new byte[1024];
// 生成的ZIP文件名为Demo.zip
String strZipName = "store_qrCode_"+ fileUrl +".zip"; String strZipName = "store_qrCode_"+ fileUrl +".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipName)); FileOutputStream out = new FileOutputStream(new File(strZipName));
//遍历零售公司
for (PrivilageOrganizational privilageOrganizational:privilageOrganizationals) { for (PrivilageOrganizational privilageOrganizational:privilageOrganizationals) {
if (privilageOrganizational == null || privilageOrganizational.getId() == null){ if (privilageOrganizational == null || privilageOrganizational.getId() == null){
continue; continue;
} }
//创建零售公司文件夹
File complayFile = new File(file.getPath()+"/"+privilageOrganizational.getName()); File complayFile = new File(file.getPath()+"/"+privilageOrganizational.getName());
complayFile.mkdir(); complayFile.mkdir();
//查询店铺
List<PoiStore> poiStores = poiStoreDOMapper.selectShopByCompany(privilageOrganizational.getId()); List<PoiStore> poiStores = poiStoreDOMapper.selectShopByCompany(privilageOrganizational.getId());
if (CollectionUtils.isNotEmpty(poiStores)){ if (CollectionUtils.isNotEmpty(poiStores)){
//遍历店铺查询门店码url
for (PoiStore poiStore:poiStores){ for (PoiStore poiStore:poiStores){
String url = poiStoreDOMapper.selectStoreQrCode(poiStore.getId()); String url = poiStoreDOMapper.selectStoreQrCode(poiStore.getId());
if (StringUtils.isBlank(url)){ if (StringUtils.isBlank(url)){
@ -196,34 +197,102 @@ public class StoreEmployeeService {
} }
} }
} }
File[] files = file.listFiles();
if (files !=null && files.length>0){ //将根文件转换为zip文件
for (int i =0;i<files.length;i++){ ZipUtils.toZip(file.getPath(),out,true);
File companyFile = files[i];
out.putNextEntry(new ZipEntry(companyFile.getName())); //将文件输入到response
ServletOutputStream outputStream = response.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(companyFile); byte[] buffer = new byte[1024];
File zipFile = new File(strZipName);
int len; FileInputStream inStream = new FileInputStream(zipFile);
// 读入需要下载的文件的内容打包到zip文件 int lenNew;
while ((len = fileInputStream.read(buffer)) > 0) { while ((lenNew = inStream.read(buffer)) > 0) {
out.write(buffer, 0, len); outputStream.write(buffer, 0, lenNew);
}
//删除文件,清理空间
file.delete();
zipFile.delete();
}
}
/**
*
* @param response
*/
public void downloadStoreStaffCode(HttpServletResponse response) throws IOException {
//查询所有公司
List<PrivilageOrganizational> privilageOrganizationals = retailCompanyMapper.selectList(null);
if (CollectionUtils.isNotEmpty(privilageOrganizationals)){
//根文件夹用于存放公司
String fileUrl = "导购码-"+UUID.randomUUID().toString().replace("-", "");
File file = new File(fileUrl);
file.mkdir();
// 生成的ZIP文件名
String strZipName = "store_qrCode_"+ fileUrl +".zip";
FileOutputStream out = new FileOutputStream(new File(strZipName));
//遍历零售公司
for (PrivilageOrganizational privilageOrganizational:privilageOrganizationals) {
if (privilageOrganizational == null || privilageOrganizational.getId() == null){
continue;
}
//创建零售公司文件夹
File complayFile = new File(file.getPath()+"/"+privilageOrganizational.getName());
complayFile.mkdir();
//查询店铺
List<PoiStore> poiStores = poiStoreDOMapper.selectShopByCompany(privilageOrganizational.getId());
if (CollectionUtils.isNotEmpty(poiStores)){
//遍历店铺查询门店码url
for (PoiStore poiStore:poiStores){
//创建零售公司文件夹
File storeFile = new File(complayFile.getPath()+"/"+poiStore.getName());
storeFile.mkdir();
QueryWrapper<PoiStoreStaff> staffQr = new QueryWrapper();
staffQr.eq(DATABASE_STORE_ID_KEY,poiStore.getId());
staffQr.eq(DATABASE_TYPE_KEY,TYPE_STAFF_SELLER);
List<PoiStoreStaff> poiStoreStaffs = poiStoreStaffDOMapper.selectList(staffQr);
if (CollectionUtils.isNotEmpty(poiStoreStaffs)){
for (PoiStoreStaff poiStoreStaff:poiStoreStaffs){
String url = poiStoreStaff.getEpWechatQrCode();
if (StringUtils.isBlank(url)){
continue;
}
//下载图片
BufferedInputStream in = new BufferedInputStream(employeeService.getInputStream(url));
//字节流转图片对象
Image bi = ImageIO.read(in);
//构建图片流
BufferedImage tag = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
//绘制改变尺寸后的图
tag.getGraphics().drawImage(bi, 0, 0, 300, 300, null);
//输出流
BufferedOutputStream image = new BufferedOutputStream(new FileOutputStream(storeFile.getPath() +"/" +poiStoreStaff.getStaffCode() + ".png"));
ImageIO.write(tag, "PNG", image);
in.close();
image.close();
}
}
} }
fileInputStream.close();
out.closeEntry();
} }
} }
out.flush();
out.close();
//将根文件转换为zip文件
ZipUtils.toZip(file.getPath(),out,true);
//将文件输入到response
ServletOutputStream outputStream = response.getOutputStream(); ServletOutputStream outputStream = response.getOutputStream();
byte[] bufferNew = new byte[1024]; byte[] buffer = new byte[1024];
File zipFile = new File(strZipName); File zipFile = new File(strZipName);
FileInputStream inStream = new FileInputStream(zipFile); FileInputStream inStream = new FileInputStream(zipFile);
int lenNew; int lenNew;
while ((lenNew = inStream.read(bufferNew)) > 0) { while ((lenNew = inStream.read(buffer)) > 0) {
outputStream.write(bufferNew, 0, lenNew); outputStream.write(buffer, 0, lenNew);
} }
//删除文件,清理空间
file.delete(); file.delete();
zipFile.delete(); zipFile.delete();
} }

@ -0,0 +1,131 @@
package com.kiisoo.ic.utils;
import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private static final int BUFFER_SIZE = 2 * 1024;
/**
* ZIP * @param srcDir
* @param out
* @param KeepDirStructure ,true:;
* false:(,)
* @throws RuntimeException
*/
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure)
throws RuntimeException{
long start = System.currentTimeMillis();
ZipOutputStream zos = null ;
try {
zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir);
compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils",e);
}finally{
if(zos != null){
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* ZIP * @param srcFiles
* @param out
* @throws RuntimeException
*/
public static void toZip(List<File> srcFiles , OutputStream out)throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null ;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils",e);
}finally{
if(zos != null){
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* @param sourceFile
* @param zos zip
* @param name
* @param KeepDirStructure ,true:;
* false:(,)
* @throws Exception
*/
private static void compress(File sourceFile, ZipOutputStream zos, String name,
boolean KeepDirStructure) throws Exception{
byte[] buf = new byte[BUFFER_SIZE];
if(sourceFile.isFile()){
// 向zip输出流中添加一个zip实体构造器中name为zip实体的文件的名字
zos.putNextEntry(new ZipEntry(name));
// copy文件到zip输出流中
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len);
}
// Complete the entry
zos.closeEntry();
in.close();
} else {
//是文件夹
File[] listFiles = sourceFile.listFiles();
if(listFiles == null || listFiles.length == 0){
// 需要保留原来的文件结构时,需要对空文件夹进行处理
if(KeepDirStructure){
// 空文件夹的处理
zos.putNextEntry(new ZipEntry(name + "/"));
// 没有文件不需要文件的copy
zos.closeEntry();
}
}else {
for (File file : listFiles) {
// 判断是否需要保留原来的文件结构
if (KeepDirStructure) {
// 注意file.getName()前面需要带上父文件夹的名字加一斜杠,
// 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
} else {
compress(file, zos, file.getName(),KeepDirStructure);
}
}
}
}
}
}
Loading…
Cancel
Save