线下订单驱动开门
This commit is contained in:
@@ -7,13 +7,17 @@ import com.sv.entity.Barcode;
|
||||
import com.sv.entity.Device;
|
||||
import com.sv.entity.Venue;
|
||||
import com.sv.mapper.BarcodeMapper;
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
import com.sv.netty.utils.AesUtil;
|
||||
import com.sv.netty.utils.MakeCode;
|
||||
import com.sv.service.api.VenueService;
|
||||
import com.sv.service.common.DoorLockUtil;
|
||||
import com.sv.service.common.RedisCache;
|
||||
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.controller.BaseApiController;
|
||||
import com.ydd.framework.core.exception.ServiceException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -21,6 +25,7 @@ import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Controller - 场馆
|
||||
@@ -35,6 +40,8 @@ public class VenueController extends BaseApiController {
|
||||
|
||||
@Resource
|
||||
private VenueService venueService;
|
||||
@Resource
|
||||
private DoorLockUtil doorLockUtil;
|
||||
|
||||
/**
|
||||
* 分页查询场馆列表
|
||||
@@ -119,8 +126,14 @@ public class VenueController extends BaseApiController {
|
||||
join.setFlg(999);
|
||||
join.setMsg("通讯异常,门禁设备离线中");
|
||||
}else {
|
||||
String barcode = newBarcode(byDevice.getName(), EnterEnum.ENTER, venueId, memberId);
|
||||
String doorSn = byDevice.getName();
|
||||
boolean lockStat = doorLockUtil.checkDoorLock(doorSn);
|
||||
if (lockStat) {
|
||||
throw new ServiceException("有人正在使用门禁,请稍后再试");
|
||||
}
|
||||
String barcode = newBarcode(doorSn, EnterEnum.ENTER, venueId, memberId);
|
||||
join.setBarcode(barcode);
|
||||
doorLockUtil.lockDoor(doorSn);
|
||||
}
|
||||
}
|
||||
return ResponseDTO.ok().addAttribute("join", join);
|
||||
@@ -144,14 +157,19 @@ public class VenueController extends BaseApiController {
|
||||
out.setFlg(999);
|
||||
out.setMsg("通讯异常,门禁设备离线中");
|
||||
}else {
|
||||
String barcode = newBarcode(byDevice.getName(),EnterEnum.OUT,venueId,memberId);
|
||||
String doorSn = byDevice.getName();
|
||||
boolean lockStat = doorLockUtil.checkDoorLock(doorSn);
|
||||
if (lockStat) {
|
||||
throw new ServiceException("有人正在使用门禁,请稍后再试");
|
||||
}
|
||||
String barcode = newBarcode(doorSn,EnterEnum.OUT,venueId,memberId);
|
||||
out.setBarcode(barcode);
|
||||
doorLockUtil.lockDoor(doorSn);
|
||||
}
|
||||
}
|
||||
return ResponseDTO.ok().addAttribute("out", out);
|
||||
}
|
||||
|
||||
|
||||
private String newBarcode (String deviceName,EnterEnum enterEnum,Integer venueId,Integer memberId){
|
||||
String barcode = "";
|
||||
Barcode res = barcodeMapper.findBarcode(venueId, memberId);
|
||||
|
||||
@@ -4,15 +4,23 @@ import com.enums.DeviceStatusEnum;
|
||||
import com.sv.entity.Device;
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
import com.sv.netty.netty.service.MessageService;
|
||||
import com.sv.netty.utils.MakeCode;
|
||||
import com.sv.service.common.DoorLockUtil;
|
||||
import com.sv.service.oms.DeviceService;
|
||||
import com.sv.service.utils.VenueBarcodeUtil;
|
||||
import com.ydd.framework.core.common.dto.ResponseDTO;
|
||||
import com.ydd.framework.core.controller.BaseApiController;
|
||||
import com.ydd.framework.core.exception.ServiceException;
|
||||
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
|
||||
@@ -25,6 +33,8 @@ public class AdminNettyController extends BaseApiController {
|
||||
DeviceService deviceService;
|
||||
@Resource
|
||||
private MessageService messageService;
|
||||
@Resource
|
||||
DoorLockUtil doorLockUtil;
|
||||
/**
|
||||
* 进场指令发布
|
||||
*/
|
||||
@@ -81,13 +91,102 @@ public class AdminNettyController extends BaseApiController {
|
||||
* 检验客户端读取能力
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/checkAlive")
|
||||
public ResponseDTO checkAlive(@RequestParam String id) {
|
||||
Integer venueId = getVenueId(id);
|
||||
String deviceName = getDeviceName(id);
|
||||
logger.info("验证设备"+ id + "通讯情况");
|
||||
messageService.testLoad(deviceName,venueId);
|
||||
@RequestMapping("/checkAlive/{id}")
|
||||
public ResponseDTO checkAlive(@PathVariable("id") Integer id) {
|
||||
Device device = deviceService.findById(id);
|
||||
if (device == null){
|
||||
throw new ServiceException("设备不存在");
|
||||
}
|
||||
if (!DeviceStatusEnum.ONLINE.value.equals(device.getStatus())){
|
||||
throw new ServiceException("设备离线!请检查连接");
|
||||
}
|
||||
messageService.testLoad(device.getName(),device.getVenueId());
|
||||
return ResponseDTO.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载线下入场凭证(二维码)
|
||||
* @param id
|
||||
* @param response
|
||||
* @throws IOException
|
||||
* @throws InvalidFormatException
|
||||
*/
|
||||
@RequestMapping(value = "/qrcode/print/{id}")
|
||||
public void print(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException, InvalidFormatException {
|
||||
Device device = deviceService.findById(id);
|
||||
if (device == null){
|
||||
response.setContentType("text/html");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write("设备不存在!!!");
|
||||
writer.flush();
|
||||
writer.close();
|
||||
return;
|
||||
}
|
||||
String doorSn = device.getName();
|
||||
if(doorLockUtil.checkBarcode(doorSn)){
|
||||
response.setContentType("text/html");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write("生成二维码过于频繁,请稍候再试!!!");
|
||||
writer.flush();
|
||||
writer.close();
|
||||
return;
|
||||
}
|
||||
|
||||
long l = System.currentTimeMillis();
|
||||
String barcodeSn = "venue-" + l;
|
||||
String barcode = MakeCode.makeOfflineCode(barcodeSn);
|
||||
deviceService.makeDeviceBarcode(device.getName(),device.getVenueId(),barcode);
|
||||
|
||||
String documentPath = getDocument(barcodeSn);
|
||||
response.setHeader("Content-disposition","attachment; filename=" + barcodeSn + ".docx");
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||
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);
|
||||
}
|
||||
doorLockUtil.lockBarcode(doorSn);
|
||||
} 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(String barcodeSn) 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("订单号:" + barcodeSn);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.sv.netty.netty;
|
||||
import com.enums.EnterEnum;
|
||||
import com.sv.netty.config.*;
|
||||
import com.sv.netty.netty.service.MessageService;
|
||||
import com.sv.netty.utils.AesUtil;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
import com.sv.netty.utils.MakeCode;
|
||||
import io.netty.channel.*;
|
||||
@@ -62,6 +63,11 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
break;
|
||||
case SCAN_CODE:
|
||||
logger.info("客户端【" + clientIp + "】开门了...");
|
||||
String offline = MakeCode.decodeOfflineCode(message.getMessage());
|
||||
if (offline.startsWith("venue-")) {
|
||||
// 这是线下开门的逻辑
|
||||
messageService.offlineOpenDoor(message.getMessage());
|
||||
}
|
||||
VenueBarCode venueBarCode = MakeCode.decodeCode(message.getMessage());
|
||||
if (EnterEnum.ENTER.getValue() == venueBarCode.getDirection()) {
|
||||
messageService.enterVenue(message.getMessage());
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.sv.netty.netty.service;
|
||||
|
||||
import com.sv.entity.Venue;
|
||||
import com.sv.netty.config.HeartBeat;
|
||||
import com.sv.netty.config.VenueBarCode;
|
||||
import io.netty.channel.Channel;
|
||||
|
||||
import java.util.Set;
|
||||
@@ -53,4 +51,6 @@ public interface MessageService {
|
||||
public void adminOut(String deviceName, Integer venueId);
|
||||
|
||||
void testLoad(String deviceName, Integer venueId);
|
||||
|
||||
void offlineOpenDoor(String message);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@ package com.sv.netty.netty.service.impl;
|
||||
import com.enums.BarCodeStatusEnum;
|
||||
import com.enums.EnterEnum;
|
||||
import com.sv.entity.*;
|
||||
import com.sv.mapper.BarcodeEnterLogMapper;
|
||||
import com.sv.mapper.BarcodeMapper;
|
||||
import com.sv.mapper.BarcodeOfflineMapper;
|
||||
import com.sv.netty.config.*;
|
||||
import com.sv.netty.netty.service.MessageService;
|
||||
import com.sv.netty.utils.JsonUtils;
|
||||
@@ -13,6 +15,7 @@ import com.sv.service.api.MemberEnterVenueLogService;
|
||||
import com.sv.service.api.MemberMoneyLogService;
|
||||
import com.sv.service.api.MemberService;
|
||||
import com.sv.service.api.VenueService;
|
||||
import com.sv.service.common.DoorLockUtil;
|
||||
import com.sv.service.oms.DeviceService;
|
||||
import com.ydd.framework.core.exception.ServiceException;
|
||||
import io.netty.channel.Channel;
|
||||
@@ -25,7 +28,6 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/**
|
||||
* 消息实现类
|
||||
*
|
||||
@@ -62,6 +64,14 @@ public class ServerMessageHandlerAdapter implements MessageService {
|
||||
@Resource
|
||||
private BarcodeMapper barcodeMapper;
|
||||
|
||||
@Resource
|
||||
private DoorLockUtil doorLockUtil;
|
||||
|
||||
@Resource
|
||||
private BarcodeOfflineMapper barcodeOfflineMapper;
|
||||
|
||||
@Resource
|
||||
private BarcodeEnterLogMapper barcodeEnterLogMapper;
|
||||
/**
|
||||
* 处理心跳信息,存储心跳信息
|
||||
* @param clientId
|
||||
@@ -107,11 +117,16 @@ public class ServerMessageHandlerAdapter implements MessageService {
|
||||
@Transactional
|
||||
public void outVenue(String barcode) {
|
||||
VenueBarCode venueBarCode = MakeCode.decodeCode(barcode);
|
||||
String doorSn = venueBarCode.getDeviceName();
|
||||
boolean lockStat = doorLockUtil.checkDoorLock(doorSn);
|
||||
if (lockStat) {
|
||||
throw new ServiceException("有人正在操作,请稍后再试");
|
||||
}
|
||||
barcode = MakeCode.reWriteBarcode(barcode);
|
||||
if (!checkBarcode(barcode)){
|
||||
throw new ServiceException("二维码已经使用");
|
||||
}
|
||||
Channel channel = getCurrentChannel(venueBarCode.getDeviceName(),venueBarCode.getVenueId());
|
||||
Channel channel = getCurrentChannel(doorSn,venueBarCode.getVenueId());
|
||||
Member member = memberService.findByMember(venueBarCode.getMemberId());
|
||||
if (member != null) {
|
||||
MemberMoneyLog memberMoneyLog = memberMoneyLogService.selectLastLog(member.getId(), venueBarCode.getVenueId());
|
||||
@@ -125,6 +140,7 @@ public class ServerMessageHandlerAdapter implements MessageService {
|
||||
memberEnterVenueLog.setPayType(memberMoneyLog.getPayType());
|
||||
}
|
||||
memberEnterVenueLogService.save(memberEnterVenueLog);
|
||||
doorLockUtil.lockDoor(doorSn);
|
||||
logger.info("用户" + member.getNickname() + "出场");
|
||||
writeOffCode(barcode);
|
||||
ServerMessageUtils.INSTANCE.sendMsg(channel,new VenueMessage(MessageType.OUT_DOOR,"OK"));
|
||||
@@ -139,11 +155,16 @@ public class ServerMessageHandlerAdapter implements MessageService {
|
||||
@Transactional
|
||||
public void enterVenue(String barcode) {
|
||||
VenueBarCode venueBarCode = MakeCode.decodeCode(barcode);
|
||||
String doorSn = venueBarCode.getDeviceName();
|
||||
boolean lockStat = doorLockUtil.checkDoorLock(doorSn);
|
||||
if (lockStat) {
|
||||
throw new ServiceException("有人正在操作,请稍后再试");
|
||||
}
|
||||
barcode = MakeCode.reWriteBarcode(barcode);
|
||||
if (!checkBarcode(barcode)){
|
||||
throw new ServiceException("二维码已经使用");
|
||||
}
|
||||
Channel channel = getCurrentChannel(venueBarCode.getDeviceName(), venueBarCode.getVenueId());
|
||||
Channel channel = getCurrentChannel(doorSn, venueBarCode.getVenueId());
|
||||
Member member = memberService.findByMember(venueBarCode.getMemberId());
|
||||
if (member != null) {
|
||||
MemberEnterVenueLog memberEnterVenueLog = new MemberEnterVenueLog();
|
||||
@@ -153,6 +174,7 @@ public class ServerMessageHandlerAdapter implements MessageService {
|
||||
memberEnterVenueLog.setPlatformId(member.getPlatformId() == null ? 1 : member.getPlatformId());
|
||||
memberEnterVenueLog.setVenueId(venueBarCode.getVenueId());
|
||||
memberEnterVenueLogService.save(memberEnterVenueLog);
|
||||
doorLockUtil.lockDoor(doorSn);
|
||||
writeOffCode(barcode);
|
||||
logger.info("用户" + member.getNickname() + "入场");
|
||||
ServerMessageUtils.INSTANCE.sendMsg(channel,new VenueMessage(MessageType.ENTER_DOOR,"OK"));
|
||||
@@ -199,6 +221,47 @@ public class ServerMessageHandlerAdapter implements MessageService {
|
||||
ServerMessageUtils.INSTANCE.sendMsg(currentChannel,new VenueMessage(MessageType.HB,"测试链接"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 线下订单
|
||||
*/
|
||||
@Override
|
||||
public void offlineOpenDoor(String message) {
|
||||
String barcode = MakeCode.reWriteBarcode(message);
|
||||
BarcodeOffline barcodeOffline = barcodeOfflineMapper.selectByBarcode(barcode);
|
||||
String doorSn = barcodeOffline.getDeviceName();
|
||||
// 校验barcode是否失效
|
||||
Date endTime = barcodeOffline.getEndTime();
|
||||
if (endTime.before(new Date())) {
|
||||
// 二维码已经失效了
|
||||
throw new ServiceException("二维码已经失效,无法继续使用");
|
||||
}
|
||||
boolean lockStat = doorLockUtil.checkDoorLock(doorSn);
|
||||
if (lockStat) {
|
||||
throw new ServiceException("有人正在操作,请稍后再试");
|
||||
}
|
||||
BarcodeEnterLog lastByBarcode = barcodeEnterLogMapper.findLastByBarcode(barcode);
|
||||
Integer venueId = barcodeOffline.getVenueId();
|
||||
Channel channel = getCurrentChannel(doorSn, venueId);
|
||||
if (lastByBarcode != null && EnterEnum.ENTER.getValue().equals(lastByBarcode.getType())) {
|
||||
//出场
|
||||
BarcodeEnterLog barcodeEnterLog = new BarcodeEnterLog();
|
||||
barcodeEnterLog.setBarcode(barcode);
|
||||
barcodeEnterLog.setType(EnterEnum.OUT.getValue());
|
||||
barcodeEnterLog.setVenueId(venueId);
|
||||
barcodeEnterLogMapper.insert(barcodeEnterLog);
|
||||
doorLockUtil.lockDoor(doorSn);
|
||||
ServerMessageUtils.INSTANCE.sendMsg(channel,new VenueMessage(MessageType.OUT_DOOR,"OK"));
|
||||
return;
|
||||
}
|
||||
BarcodeEnterLog barcodeEnterLog = new BarcodeEnterLog();
|
||||
barcodeEnterLog.setBarcode(barcode);
|
||||
barcodeEnterLog.setType(EnterEnum.ENTER.getValue());
|
||||
barcodeEnterLog.setVenueId(venueId);
|
||||
barcodeEnterLogMapper.insert(barcodeEnterLog);
|
||||
doorLockUtil.lockDoor(doorSn);
|
||||
ServerMessageUtils.INSTANCE.sendMsg(channel,new VenueMessage(MessageType.ENTER_DOOR,"OK"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员入场
|
||||
* @return
|
||||
|
||||
Reference in New Issue
Block a user