This commit is contained in:
韩鹏辉
2024-03-21 10:22:29 +08:00
parent 1c47f567d8
commit b77eacdccb
270 changed files with 32916 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
package com.chaozhanggui.system.cashierservice.config;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.socket.AppWebSocketServer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
//处理前端改变购物车的行为,并记录
public class ChangeHandler extends Handler {
@Override
public void handleRequest(ConcurrentHashMap<String, List<AppWebSocketServer>> webSocketMap,
JSONObject jsonObject, ConcurrentHashMap<String,
List<JSONObject>> recordMap,
AppWebSocketServer webSocke) throws IOException {
if (jsonObject.containsKey("change")) {
ArrayList<JSONObject> jsonObjects = new ArrayList<>();
jsonObjects.add(jsonObject);
// producerMq.syncShopCar(jsonObjects);
//记录每一次购物车变化的记录
List<JSONObject> objects = recordMap.get(webSocke.getTableId());
objects.add(jsonObject);
} else {
// 无法处理,传递给下一个处理器
if (nextHandler != null) {
nextHandler.handleRequest(webSocketMap,jsonObject,recordMap,webSocke);
}
}
}
}

View File

@@ -0,0 +1,33 @@
package com.chaozhanggui.system.cashierservice.config;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.socket.AppWebSocketServer;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
//处理前端订单已完成把订单标志位置为false
public class ClearHandler extends Handler{
@Override
public void handleRequest(ConcurrentHashMap<String, List<AppWebSocketServer>> webSocketMap,
JSONObject jsonObject, ConcurrentHashMap<String,
List<JSONObject>> recordMap,
AppWebSocketServer webSocke) throws IOException {
if (jsonObject.containsKey("clear")) {
if (StringUtils.isNotBlank(webSocke.getTableId()) && webSocketMap.containsKey(webSocke.getTableId())) {
List<AppWebSocketServer> serverList = webSocketMap.get(webSocke.getTableId());
//遍历所有对象,把订单都改为未提交,为了下一次点餐
serverList.forEach(m -> m.getCreateOrder().set(false));
}
} else {
// 无法处理,传递给下一个处理器
if (nextHandler != null) {
nextHandler.handleRequest(webSocketMap,jsonObject,recordMap,webSocke);
}
}
}
}

View File

@@ -0,0 +1,79 @@
package com.chaozhanggui.system.cashierservice.config;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.socket.AppWebSocketServer;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
//处理前端创建订单
public class CreateOrderHandler extends Handler{
@Override
public void handleRequest(ConcurrentHashMap<String, List<AppWebSocketServer>> webSocketMap,
JSONObject jsonObject, ConcurrentHashMap<String,
List<JSONObject>> recordMap,
AppWebSocketServer webSocke) throws IOException {
if (jsonObject.containsKey("createOrdwebSockeer")) {
if (StringUtils.isNotBlank(webSocke.getTableId()) && webSocketMap.containsKey(webSocke.getTableId())) {
List<AppWebSocketServer> serverList = webSocketMap.get(webSocke.getTableId());
//有一个为true就说明已经有订单了
if (serverList.stream().anyMatch(m -> m.getCreateOrder().get())) {
webSocke.sendMessage("已有人提交订单,请稍后");
return;
}
}
synchronized (webSocke) {
if (StringUtils.isNotBlank(webSocke.getTableId()) && webSocketMap.containsKey(webSocke.getTableId())) {
List<AppWebSocketServer> serverList = webSocketMap.get(webSocke.getTableId());
//有一个为true就说明已经有订单了
if (serverList.stream().anyMatch(m -> m.getCreateOrder().get())) {
webSocke.sendMessage("已有人提交订单,请稍后");
return;
}
BigDecimal amount = new BigDecimal((Integer) jsonObject.get("amount"));
JSONArray shopCarList = jsonObject.getJSONArray("shopCarList");
String remark = jsonObject.get("remark").toString();
// List<ShopListDto> list=shopCarList.toJavaList(ShopListDto.class);
// //TODO 加个拦截加个shopid,抛出异常,前端展示
// setShopId(list.get(0).getShopId());
// try {
// Result order = orderFeign.createOrder(new CreateOrderDto(Long.parseLong(webSocke.getTableId()), amount, list, remark));
// if (order.getCode() == 200){
// //通知清空购物车
// AppSendInfo("订单提交成功", webSocke.getTableId());
// //清空本地的购物记录
// recordMap.get(webSocke.getTableId()).clear();
// webSocke.getCreateOrder().set(true);
// }else {
// AppSendInfo("订单提交失败",webSocke.getTableId());
// }
//
//
// }catch (Exception e){
// e.printStackTrace();
// AppSendInfo("订单提交失败",webSocke.getTableId());
// }
}
}
} else {
// 无法处理,传递给下一个处理器
if (nextHandler != null) {
nextHandler.handleRequest(webSocketMap,jsonObject,recordMap,webSocke);
}
}
}
}

View File

@@ -0,0 +1,26 @@
package com.chaozhanggui.system.cashierservice.config;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.socket.AppWebSocketServer;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
public abstract class Handler {
protected Handler nextHandler;
public Handler addNextHandler(Handler handler) {
this.nextHandler = handler;
return handler;
}
public abstract void handleRequest(ConcurrentHashMap<String, List<AppWebSocketServer>> webSocketMap,
JSONObject jsonObject, ConcurrentHashMap<String,
List<JSONObject>> recordMap,
AppWebSocketServer webSocke) throws IOException;
}

View File

@@ -0,0 +1,30 @@
package com.chaozhanggui.system.cashierservice.config;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.socket.AppWebSocketServer;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import static com.chaozhanggui.system.cashierservice.socket.AppWebSocketServer.AppSendInfo;
//兜底处理器
public class OtherHandler extends Handler{
@Override
public void handleRequest(ConcurrentHashMap<String, List<AppWebSocketServer>> webSocketMap,
JSONObject jsonObject,
ConcurrentHashMap<String, List<JSONObject>> recordMap,
AppWebSocketServer webSocke) throws IOException {
//传送给对应tableId用户的websocket
if (StringUtils.isNotBlank(webSocke.getTableId()) && webSocketMap.containsKey(webSocke.getTableId())) {
AppSendInfo("1", webSocke.getTableId(),false);
} else {
System.out.println("请求的tableId:" + webSocke.getTableId() + "不在该服务器上");
}
}
}

View File

@@ -0,0 +1,46 @@
package com.chaozhanggui.system.cashierservice.config;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.socket.AppWebSocketServer;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
//处理前端初次扫码同步购物车
public class SyncHandler extends Handler {
@Override
public void handleRequest(ConcurrentHashMap<String, List<AppWebSocketServer>> webSocketMap,
JSONObject jsonObject, ConcurrentHashMap<String,
List<JSONObject>> recordMap,
AppWebSocketServer webSocke) throws IOException {
if (jsonObject.containsKey("sync")) {
//这个是判断是否有这个桌号,也就是 是否有人点过餐
List<JSONObject> recordList = recordMap.get(webSocke.getTableId());
//指定发送对象
if (StringUtils.isNotBlank(webSocke.getTableId()) && webSocketMap.containsKey(webSocke.getTableId()) && recordList != null) {
List<AppWebSocketServer> serverList = webSocketMap.get(webSocke.getTableId());
for (AppWebSocketServer server : serverList) {
if (server.getSync().get()) {
server.sendMessage(recordList);
}
}
} else {
ArrayList<JSONObject> objects = new ArrayList<>();
recordMap.put(webSocke.getTableId(), objects);
}
webSocke.getSync().set(!webSocke.getSync().get());
} else {
// 无法处理,传递给下一个处理器
if (nextHandler != null) {
nextHandler.handleRequest(webSocketMap, jsonObject, recordMap, webSocke);
}
}
}
}

View File

@@ -0,0 +1,31 @@
package com.chaozhanggui.system.cashierservice.config;
import com.alibaba.fastjson.JSON;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;
/**
* 为了websocket发送对象
*/
public class WebSocketCustomEncoding implements Encoder.Text<Object> {
// public String encode(Object vo) 这个就是指定发送的类型
@Override
public String encode(Object vo) {
assert vo!=null;
return JSON.toJSONString(vo);
}
@Override
public void init(EndpointConfig endpointConfig) {
}
@Override
public void destroy() {
}
}