线下订单驱动开门
This commit is contained in:
@@ -1,16 +1,24 @@
|
||||
package com.sv.oms.controller;
|
||||
import com.enums.DeviceStatusEnum;
|
||||
import com.enums.EnterEnum;
|
||||
import com.sv.service.oms.DeviceService;
|
||||
import com.ydd.framework.core.common.Pagination;
|
||||
import com.ydd.framework.core.common.dto.ResponseDTO;
|
||||
import com.ydd.framework.core.exception.ServiceException;
|
||||
import com.ydd.oms.controller.OmsController;
|
||||
import com.sv.entity.Device;
|
||||
|
||||
import com.ydd.oms.util.VenueBarcodeUtil;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.util.Units;
|
||||
import org.apache.poi.xwpf.usermodel.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Controller - 门禁设备
|
||||
@@ -95,4 +103,72 @@ public class DeviceController extends OmsController {
|
||||
return ResponseDTO.ok("出场开门成功!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新连接
|
||||
* @param deviceId
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("device/checkAlive/{id}")
|
||||
public ResponseDTO checkAlive(@PathVariable("id") Integer deviceId){
|
||||
Device device = deviceService.findById(deviceId);
|
||||
if (device == null) {
|
||||
throw new ServiceException("设备离线!请检查连接");
|
||||
}
|
||||
if (!DeviceStatusEnum.ONLINE.value.equals(device.getStatus())){
|
||||
throw new ServiceException("设备离线!请检查连接");
|
||||
}
|
||||
return ResponseDTO.ok();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/qrcode/print/{id}")
|
||||
public void print(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException, InvalidFormatException {
|
||||
String documentPath = getDocument();
|
||||
response.setHeader("Content-disposition","attachment; filename=venue.docx");
|
||||
response.setContentType("application/vnd.ms-excel");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
OutputStream out = response.getOutputStream();
|
||||
BufferedInputStream fin = new BufferedInputStream(new FileInputStream(documentPath));
|
||||
try {
|
||||
byte[] content = new byte[1024];
|
||||
int length;
|
||||
while ((length = fin.read(content, 0, content.length)) != -1) {
|
||||
out.write(content, 0, length);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
logger.info("文件下载失败", e.getMessage());
|
||||
throw e;
|
||||
} finally {
|
||||
fin.close();
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static String getDocument() throws IOException, InvalidFormatException {
|
||||
String newFilePath = "/Users/limqhz/home/test/document.docx";
|
||||
XWPFDocument document = new XWPFDocument();
|
||||
XWPFParagraph paragraph = document.createParagraph();
|
||||
paragraph.setAlignment(ParagraphAlignment.CENTER);
|
||||
XWPFRun run = paragraph.createRun();
|
||||
// 入场二维码
|
||||
run.setText("入场二维码");
|
||||
run.addBreak();
|
||||
String imgFile = VenueBarcodeUtil.generateBarcode("jdoiawjdoiawioe1","202311161.jpg");
|
||||
int imgFormat = XWPFDocument.PICTURE_TYPE_JPEG;
|
||||
run.addPicture(new FileInputStream(imgFile), imgFormat, "image description",
|
||||
Units.toEMU(200), Units.toEMU(200));
|
||||
run.addBreak(BreakType.PAGE);
|
||||
// 出场二维码
|
||||
run.setText("出场二维码");
|
||||
run.addBreak();
|
||||
String imgFile2 = VenueBarcodeUtil.generateBarcode("jdoiawjdoiawioe2","202311162.jpg");
|
||||
run.addPicture(new FileInputStream(imgFile2), imgFormat, "image description",
|
||||
Units.toEMU(200), Units.toEMU(200));
|
||||
// 保存文件
|
||||
document.write(new FileOutputStream(newFilePath));
|
||||
document.close();
|
||||
return newFilePath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
109
oms/src/main/java/com/ydd/oms/util/VenueBarcodeUtil.java
Normal file
109
oms/src/main/java/com/ydd/oms/util/VenueBarcodeUtil.java
Normal file
@@ -0,0 +1,109 @@
|
||||
package com.ydd.oms.util;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.EncodeHintType;
|
||||
import com.google.zxing.MultiFormatWriter;
|
||||
import com.google.zxing.WriterException;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||
import com.ydd.framework.core.exception.ServiceException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Hashtable;
|
||||
|
||||
public class VenueBarcodeUtil {
|
||||
|
||||
private static final int BLACK = 0xFF000000;
|
||||
private static final int WHITE = 0xFFFFFFFF;
|
||||
private static final int margin = 0;
|
||||
|
||||
public static String generateBarcode(String barcodeValue,String fileName) {
|
||||
try {
|
||||
String geneFilePath = "/Users/limqhz/home/test/" + fileName;
|
||||
makeBarcode(geneFilePath,barcodeValue);
|
||||
return geneFilePath;
|
||||
}catch (WriterException e) {
|
||||
throw new ServiceException("生成二维码图片文件有问题");
|
||||
}
|
||||
}
|
||||
|
||||
private static void makeBarcode(String path, String barcodeValue) throws WriterException {
|
||||
//二维码内容
|
||||
String content = barcodeValue;
|
||||
String format = "jpg";
|
||||
int width = 200; // 二维码宽度
|
||||
int height = 200;// 二维码高度
|
||||
// 设置二维码矩阵的信息
|
||||
BitMatrix bitMatrix = setBitMatrix(content, width, height);
|
||||
// 设置输出流
|
||||
OutputStream outStream = null;
|
||||
try {
|
||||
outStream = new FileOutputStream(new File(path));
|
||||
// 目前 针对容错等级为H reduceWhiteArea 二维码空白区域的大小 根据实际情况设置,如果二维码内容长度不固定的话 需要自己根据实际情况计算reduceWhiteArea的大小
|
||||
writeToFile(bitMatrix, format, outStream, 5);
|
||||
outStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
try {
|
||||
outStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置生成二维码矩阵信息
|
||||
* @param content 二维码图片内容
|
||||
* @param width 二维码图片宽度
|
||||
* @param height 二维码图片高度
|
||||
* @throws WriterException
|
||||
*/
|
||||
private static BitMatrix setBitMatrix(String content, int width, int height) throws WriterException {
|
||||
BitMatrix bitMatrix = null;
|
||||
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
|
||||
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定编码方式,避免中文乱码
|
||||
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 指定纠错等级 如果二维码里面的内容比较多的话推荐使用H 容错率30%, 这样可以避免一些扫描不出来的问题
|
||||
hints.put(EncodeHintType.MARGIN, margin); // 指定二维码四周白色区域大小 官方的这个方法目前没有没有作用默认设置为0
|
||||
bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
|
||||
return bitMatrix;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param matrix
|
||||
* @param format
|
||||
* @param outStream
|
||||
* @param reduceWhiteArea 二维码空白区域设置
|
||||
* @throws IOException
|
||||
*/
|
||||
|
||||
private static void writeToFile(BitMatrix matrix, String format, OutputStream outStream, int reduceWhiteArea) throws IOException {
|
||||
BufferedImage image = toBufferedImage(matrix, reduceWhiteArea);
|
||||
ImageIO.write(image, format, outStream);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param matrix
|
||||
* @param reduceWhiteArea
|
||||
* @return
|
||||
*/
|
||||
private static BufferedImage toBufferedImage(BitMatrix matrix, int reduceWhiteArea) {
|
||||
int width = matrix.getWidth();
|
||||
int height = matrix.getHeight();
|
||||
BufferedImage image = new BufferedImage(width - 2 * reduceWhiteArea, height - 2 * reduceWhiteArea, BufferedImage.TYPE_3BYTE_BGR);
|
||||
for (int x = reduceWhiteArea; x < width - reduceWhiteArea; x++) {
|
||||
for (int y = reduceWhiteArea; y < height - reduceWhiteArea; y++) {
|
||||
image.setRGB(x - reduceWhiteArea, y - reduceWhiteArea, matrix.get(x, y) ? BLACK : WHITE);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@ spring:
|
||||
# url: jdbc:mysql://yingdiandian.mysql.rds.aliyuncs.com:3306/smart_venue?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
|
||||
# username: yingdd
|
||||
# password: Yingdd2015
|
||||
url: jdbc:mysql://127.0.0.1:3306/smart_venue?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8&useAffectedRows=true
|
||||
url: jdbc:mysql://127.0.0.1:3306/smart_venue?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8&useAffectedRows=true&useSSL=false
|
||||
username: root
|
||||
password: 123456
|
||||
redis:
|
||||
|
||||
Reference in New Issue
Block a user