Commit 208f9517 authored by licc's avatar licc

二维码导出

parent c5bd5c0e
...@@ -140,21 +140,21 @@ ...@@ -140,21 +140,21 @@
<artifactId>shiro-spring</artifactId> <artifactId>shiro-spring</artifactId>
</dependency> </dependency>
<!-- POI --> <!-- POI -->
<dependency> <!-- <dependency>-->
<groupId>org.apache.poi</groupId> <!-- <groupId>org.apache.poi</groupId>-->
<artifactId>poi</artifactId> <!-- <artifactId>poi</artifactId>-->
<version>3.9</version> <!-- <version>3.9</version>-->
</dependency> <!-- </dependency>-->
<dependency> <!-- <dependency>-->
<groupId>org.apache.poi</groupId> <!-- <groupId>org.apache.poi</groupId>-->
<artifactId>poi-ooxml</artifactId> <!-- <artifactId>poi-ooxml</artifactId>-->
<version>3.9</version> <!-- <version>3.9</version>-->
</dependency> <!-- </dependency>-->
<dependency> <!-- <dependency>-->
<groupId>org.apache.poi</groupId> <!-- <groupId>org.apache.poi</groupId>-->
<artifactId>poi-ooxml-schemas</artifactId> <!-- <artifactId>poi-ooxml-schemas</artifactId>-->
<version>3.9</version> <!-- <version>3.9</version>-->
</dependency> <!-- </dependency>-->
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
......
...@@ -118,7 +118,7 @@ ...@@ -118,7 +118,7 @@
</foreach> </foreach>
</insert> </insert>
<select id="getByProductNo" resultType="cn.wisenergy.model.app.AntiFake"> <select id="getByProductNo" resultMap="antiMap">
select select
<include refid="cols_all"/> <include refid="cols_all"/>
from from
...@@ -137,7 +137,7 @@ ...@@ -137,7 +137,7 @@
</where> </where>
</select> </select>
<select id="getList" resultType="cn.wisenergy.model.app.AntiFake"> <select id="getList" resultMap="antiMap">
select select
<include refid="cols_all"/> <include refid="cols_all"/>
from from
...@@ -149,8 +149,9 @@ ...@@ -149,8 +149,9 @@
</where> </where>
</select> </select>
<select id="getProductNos" resultType="cn.wisenergy.model.app.AntiFake"> <select id="getProductNos" resultMap="antiMap">
select product_no select
<include refid="cols_all"/>
from from
<include refid="table"/> <include refid="table"/>
<where> <where>
...@@ -162,7 +163,7 @@ ...@@ -162,7 +163,7 @@
</where> </where>
</select> </select>
<select id="getByShaValue" resultType="cn.wisenergy.model.app.AntiFake"> <select id="getByShaValue" resultMap="antiMap">
select select
<include refid="cols_all"/> <include refid="cols_all"/>
from from
......
package cn.wisenergy.model.app; package cn.wisenergy.model.app;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -50,6 +51,7 @@ public class AntiFake implements Serializable { ...@@ -50,6 +51,7 @@ public class AntiFake implements Serializable {
* 防伪码 * 防伪码
*/ */
@ApiModelProperty(value = "防伪码", name = "shaValue") @ApiModelProperty(value = "防伪码", name = "shaValue")
@ExcelProperty(value = "防伪码")
private String shaValue; private String shaValue;
/** /**
......
...@@ -63,4 +63,6 @@ public interface AntiFakeService { ...@@ -63,4 +63,6 @@ public interface AntiFakeService {
* @return * @return
*/ */
R<Boolean> report(String str, HttpServletResponse response); R<Boolean> report(String str, HttpServletResponse response);
void exportReceivable(String str, HttpServletResponse response);
} }
...@@ -13,6 +13,7 @@ import cn.wisenergy.model.vo.ReportCodeVo; ...@@ -13,6 +13,7 @@ import cn.wisenergy.model.vo.ReportCodeVo;
import cn.wisenergy.service.Manager.AntiFakeManger; import cn.wisenergy.service.Manager.AntiFakeManger;
import cn.wisenergy.service.app.AntiFakeService; import cn.wisenergy.service.app.AntiFakeService;
import cn.wisenergy.service.util.CodeUtils; import cn.wisenergy.service.util.CodeUtils;
import cn.wisenergy.service.util.ExcelUtils;
import cn.wisenergy.service.util.QRCodeUtils; import cn.wisenergy.service.util.QRCodeUtils;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
...@@ -203,15 +204,18 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i ...@@ -203,15 +204,18 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
return R.ok(false); return R.ok(false);
} }
//获取防伪二维码生产 //获取防伪码
List<AntiFake> result = antiFakeMapper.getProductNos(array); List<AntiFake> result = antiFakeMapper.getProductNos(array);
if (CollectionUtils.isEmpty(result)) { if (CollectionUtils.isEmpty(result)) {
return R.ok(0, true); return R.ok(0, true);
} }
List<ReportCodeVo> resultBo = new ArrayList<>();
for (AntiFake anti : result) { for (AntiFake anti : result) {
String url = REQUEST_URL + anti.getShaValue(); String url = REQUEST_URL + anti.getShaValue();
anti.setShaValue(url); ReportCodeVo reportCodeVo = new ReportCodeVo();
reportCodeVo.setShaValue(url);
resultBo.add(reportCodeVo);
} }
//生成Excel //生成Excel
...@@ -220,21 +224,13 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i ...@@ -220,21 +224,13 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
response.setContentType("application/vnd.ms-excel"); response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
// 使用java8新特性的stream流去处理数据,把空的数据过滤掉
List<ReportCodeVo> resultBo = new ArrayList<>();
for (AntiFake antiFake : result) {
ReportCodeVo reportCodeVo = new ReportCodeVo();
reportCodeVo.setShaValue(antiFake.getShaValue());
resultBo.add(reportCodeVo);
}
//创建文件名称 //创建文件名称
long lon = System.currentTimeMillis(); long lon = System.currentTimeMillis();
response.setHeader("Content-disposition", "attachment;filename=" + lon + ".xlsx"); response.setHeader("Content-disposition", "attachment;filename=" + lon + ".xlsx");
// sheet名称 // sheet名称
EasyExcel.write(response.getOutputStream(), ReportCodeVo.class).sheet(Long.toString(lon)).doWrite(resultBo); EasyExcel.write(response.getOutputStream(), AntiFake.class).sheet(Long.toString(lon)).doWrite(resultBo);
//修改二维码使用状态 //修改二维码使用状态
boolean bool = antiFakeManger.updateAntiFake(array); boolean bool = antiFakeManger.updateAntiFake(array);
if (!bool) { if (!bool) {
...@@ -246,6 +242,34 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i ...@@ -246,6 +242,34 @@ public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> i
} }
} }
@Override
public void exportReceivable(String str, HttpServletResponse response) {
if (StringUtils.isBlank(str)){
return;
}
//把字符转化为数字
List<Long> array = StringUtil.strToLongArray(str);
if (CollectionUtils.isEmpty(array)) {
return;
}
//获取防伪码
List<AntiFake> result = antiFakeMapper.getProductNos(array);
if (CollectionUtils.isEmpty(result)) {
return;
}
List<ReportCodeVo> resultBo = new ArrayList<>();
for (AntiFake anti : result) {
String url = REQUEST_URL + anti.getShaValue();
ReportCodeVo reportCodeVo = new ReportCodeVo();
reportCodeVo.setShaValue(url);
resultBo.add(reportCodeVo);
}
ExcelUtils.export(response, resultBo, "aadfgh", ReportCodeVo.class);
}
/** /**
* 分页处理方法 * 分页处理方法
* *
......
...@@ -6,14 +6,11 @@ import cn.wisenergy.service.app.LastMonthUserInfoService; ...@@ -6,14 +6,11 @@ import cn.wisenergy.service.app.LastMonthUserInfoService;
import cn.wisenergy.service.app.MonthUserLevelService; import cn.wisenergy.service.app.MonthUserLevelService;
import cn.wisenergy.service.app.UserLevelService; import cn.wisenergy.service.app.UserLevelService;
import cn.wisenergy.service.app.UserLevelTaskService; import cn.wisenergy.service.app.UserLevelTaskService;
import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.model.IndexedUDFFinder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Service @Service
......
package cn.wisenergy.service.util;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import io.undertow.util.Headers;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.http.MediaType;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* Excel工具类
*
* @author: ZHY
* @date: 2020-08-24 17:31
* @version:
**/
public class ExcelUtils {
private final static HorizontalCellStyleStrategy HORIZONTAL_CELL_STYLE_STRATEGY;
static {
// 头的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 12);
headWriteCellStyle.setWriteFont(headWriteFont);
// 内容的策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 内容字体
WriteFont contentWriteFont = new WriteFont();
// 字体大小
contentWriteFont.setFontHeightInPoints((short) 11);
contentWriteCellStyle.setWriteFont(contentWriteFont);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HORIZONTAL_CELL_STYLE_STRATEGY = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
}
/**
* 导出
*
* @param response
* @param data 数据
* @param fileName 文件名
* @param t 导出对象
**/
public static <T> void export(HttpServletResponse response, List<T> data, String fileName, Class<T> t) {
try {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
response.setHeader(Headers.CONTENT_DISPOSITION_STRING, "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
EasyExcel.write(response.getOutputStream(), t)
.registerWriteHandler(HORIZONTAL_CELL_STYLE_STRATEGY)
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.sheet(fileName).doWrite(data);
} catch (Exception e) {
// 重置response
response.reset();
throw new RuntimeException("下载失败");
}
}
}
\ No newline at end of file
...@@ -36,13 +36,13 @@ public class AntiFakeController { ...@@ -36,13 +36,13 @@ public class AntiFakeController {
return antiFakeService.createCode(codeVo); return antiFakeService.createCode(codeVo);
} }
@ApiOperation(value = "扫描防伪二维码", notes = "扫描防伪二维码", httpMethod = "POST") @ApiOperation(value = "扫描防伪二维码", notes = "扫描防伪二维码", httpMethod = "GET")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "shaValue", value = "防伪码", dataType = "String",required = true), @ApiImplicitParam(name = "shaValue", value = "防伪码", dataType = "String",required = true),
@ApiImplicitParam(name = "securityCode", value = "安全码", dataType = "String",required = true) @ApiImplicitParam(name = "securityCode", value = "安全码", dataType = "String",required = true)
}) })
@ApiImplicitParam(name = "shaValue", value = "防伪码", dataType = "String") @ApiImplicitParam(name = "shaValue", value = "防伪码", dataType = "String")
@PostMapping("/admin/scanCode") @GetMapping("/admin/scanCode")
public R<String> scanCode(String shaValue,String securityCode) { public R<String> scanCode(String shaValue,String securityCode) {
log.info("shop-mall[]AntiFakeController[]scanCode[]input.param.shaValue:" + shaValue,securityCode); log.info("shop-mall[]AntiFakeController[]scanCode[]input.param.shaValue:" + shaValue,securityCode);
if (StringUtils.isBlank(shaValue)) { if (StringUtils.isBlank(shaValue)) {
...@@ -77,4 +77,16 @@ public class AntiFakeController { ...@@ -77,4 +77,16 @@ public class AntiFakeController {
return antiFakeService.report(ids, response); return antiFakeService.report(ids, response);
} }
// @ApiOperation(value = "导出二维码", notes = "获取防伪二维码分页列表", httpMethod = "GET")
// @ApiImplicitParam(name = "ids", value = " 二维码主键id :\"1,2,3,4,5,6,7,8,9,\"", dataType = "String")
// @GetMapping("admin/antiFakeService")
// public void antiFakeService(String ids, HttpServletResponse response) {
// log.info("shop-mall[]AntiFakeController[]antiFakeService[]input.param.ids:" + ids);
// if (StringUtils.isBlank(ids)) {
// return ;
// }
//
// antiFakeService.exportReceivable(ids, response);
// }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment