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,70 @@
package com.chaozhanggui.system.cashierservice.rabbit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.dao.*;
import com.chaozhanggui.system.cashierservice.entity.*;
import com.chaozhanggui.system.cashierservice.entity.vo.CashierCarVo;
import com.chaozhanggui.system.cashierservice.exception.MsgException;
import com.chaozhanggui.system.cashierservice.redis.RedisCst;
import com.chaozhanggui.system.cashierservice.redis.RedisUtil;
import com.chaozhanggui.system.cashierservice.service.CartService;
import com.chaozhanggui.system.cashierservice.sign.Result;
import com.chaozhanggui.system.cashierservice.socket.AppWebSocketServer;
import com.chaozhanggui.system.cashierservice.util.JSONUtil;
import com.chaozhanggui.system.cashierservice.util.RedisUtils;
import com.chaozhanggui.system.cashierservice.util.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.*;
@Slf4j
@Component
@RabbitListener(queues = {RabbitConstants.CART_QUEUE_PUT})
@Service
public class CartConsumer {
@Autowired
private RedisUtil redisUtil;
@Autowired
private CartService cartService;
@RabbitHandler
public void listener(String message) {
try {
JSONObject jsonObject = JSON.parseObject(message);
String tableId = jsonObject.getString("tableId");
String shopId = jsonObject.getString("shopId");
if (jsonObject.getString("type").equals("addcart") ) {
if (!jsonObject.containsKey("num")) {
throw new MsgException("商品数量错误");
}
cartService.createCart(jsonObject);
}else if(jsonObject.getString("type").equals("createOrder")){
String cartDetail = redisUtil.getMessage(RedisCst.TABLE_CART.concat(tableId).concat("-").concat(shopId));
if (StringUtils.isEmpty(cartDetail)){
throw new MsgException("购物车为空无法下单");
}
JSONArray array = JSON.parseArray(cartDetail);
if (array.size() > 0){
cartService.createOrder(jsonObject);
}
}else if(jsonObject.getString("type").equals("clearCart")){
cartService.clearCart(jsonObject);
}
} catch (Exception e) {
e.getMessage();
}
}
}

View File

@@ -0,0 +1,266 @@
package com.chaozhanggui.system.cashierservice.rabbit;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.dao.*;
import com.chaozhanggui.system.cashierservice.entity.*;
import com.chaozhanggui.system.cashierservice.model.CategoryInfo;
import com.chaozhanggui.system.cashierservice.model.OrderDetailPO;
import com.chaozhanggui.system.cashierservice.util.DateUtils;
import com.chaozhanggui.system.cashierservice.util.FeieyunPrintUtil;
import com.chaozhanggui.system.cashierservice.util.JSONUtil;
import com.chaozhanggui.system.cashierservice.util.PrinterUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
@Component
@RabbitListener(queues = {RabbitConstants.PRINT_MECHINE_COLLECT_QUEUE_PUT})
@Service
public class PrintMechineConsumer {
@Autowired
TbShopUserMapper tbShopUserMapper;
@Autowired
private TbOrderInfoMapper tbOrderInfoMapper;
@Autowired
private TbPrintMachineMapper tbPrintMachineMapper;
@Autowired
private TbCashierCartMapper tbCashierCartMapper;
@Autowired
private TbProductSkuMapper tbProductSkuMapper;
@Autowired
private TbShopInfoMapper tbShopInfoMapper;
@Autowired
private TbProductMapper tbProductMapper;
@RabbitHandler
public void listener(String message) {
String orderId = message;
try {
Thread.sleep(1000L);
TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(Integer.valueOf(orderId));
if (ObjectUtil.isEmpty(orderInfo)) {
log.error("没有对应的订单信息");
return;
}
TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getShopId()));
if (ObjectUtil.isEmpty(shopInfo)) {
log.error("店铺信息不存在");
return;
}
List<TbPrintMachineWithBLOBs> list = tbPrintMachineMapper.selectByShopId(orderInfo.getShopId());
if (ObjectUtil.isEmpty(list) || list.size() <= 0) {
log.error("此店铺没有对应的打印机设备");
return;
}
list.parallelStream().forEach(tbPrintMachineWithBLOBs->{
if (!"network".equals(tbPrintMachineWithBLOBs.getConnectionType())) {
log.error("非网络打印机:{},{}",tbPrintMachineWithBLOBs.getAddress(),tbPrintMachineWithBLOBs.getConnectionType());
return;
}
if (!"1".equals(tbPrintMachineWithBLOBs.getStatus().toString())) {
log.error("打印机状态异常:{},{}",tbPrintMachineWithBLOBs.getAddress(),tbPrintMachineWithBLOBs.getStatus());
return;
}
JSONObject config = JSONObject.parseObject(tbPrintMachineWithBLOBs.getConfig());
String model = config.getString("model");
String printerNum = config.getString("printerNum");
String feet = config.getString("feet");
String autoCut = config.getString("autoCut");
List<CategoryInfo> categoryInfos=JSONUtil.parseJSONStr2TList(config.getJSONArray("categoryList").toString(),CategoryInfo.class);
switch (tbPrintMachineWithBLOBs.getContentType()){
case "yxyPrinter":
yxyPrinter(tbPrintMachineWithBLOBs,model,orderInfo,shopInfo,printerNum,categoryInfos);
break;
case "fePrinter":
fePrinter(tbPrintMachineWithBLOBs,model,orderInfo,shopInfo,printerNum,categoryInfos);
break;
}
});
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 博时结云打印机
* @param tbPrintMachineWithBLOBs
* @param model
* @param orderInfo
* @param shopInfo
* @param printerNum
*/
private void yxyPrinter(TbPrintMachineWithBLOBs tbPrintMachineWithBLOBs,String model,TbOrderInfo orderInfo,TbShopInfo shopInfo,String printerNum, List<CategoryInfo> categoryInfos){
String orderId=orderInfo.getId().toString();
switch (tbPrintMachineWithBLOBs.getSubType()) {
case "label": //标签打印机
break;
case "cash": //小票打印机
switch (model) {
case "normal": //普通出单
List<TbCashierCart> cashierCarts = tbCashierCartMapper.selectByOrderId(orderInfo.getId().toString(),"final");
if (ObjectUtil.isNotEmpty(cashierCarts) && cashierCarts.size() > 0) {
List<OrderDetailPO.Detail> detailList = new ArrayList<>();
cashierCarts.parallelStream().forEach(it -> {
String categoryId;
if(ObjectUtil.isEmpty(it.getCategoryId())){
categoryId= tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId();
} else {
categoryId = it.getCategoryId();
}
Long count= categoryInfos.stream().filter(c->
c.getId().toString().equals(categoryId)
).count();
if(count>0){
TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(Integer.valueOf(it.getSkuId()));
String remark = "";
if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) {
remark = tbProductSkuWithBLOBs.getSpecSnap();
}
OrderDetailPO.Detail detail = new OrderDetailPO.Detail(it.getName(), it.getNumber().toString(), it.getTotalAmount().toPlainString(), remark);
detailList.add(detail);
}
});
String balance = "0";
if ("deposit".equals(orderInfo.getPayType())) {
TbShopUser user = tbShopUserMapper.selectByPrimaryKey(orderInfo.getMemberId());
if (ObjectUtil.isNotEmpty(user) && ObjectUtil.isNotEmpty(user.getAmount())) {
balance = user.getAmount().toPlainString();
}
}
if(ObjectUtil.isNotEmpty(detailList)&&detailList.size()>0){
OrderDetailPO detailPO = new OrderDetailPO(shopInfo.getShopName(), "普通打印", orderInfo.getTableName(), orderInfo.getOrderNo(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())), "【POS-1】001", orderInfo.getOrderAmount().toPlainString(), balance, orderInfo.getPayType(), "0", detailList);
String data= PrinterUtils.getCashPrintData(detailPO,"结算单");
PrinterUtils.printTickets(1, Integer.valueOf(printerNum), tbPrintMachineWithBLOBs.getAddress(), data);
}
}
break;
case "one": //一菜一品
cashierCarts = tbCashierCartMapper.selectByOrderId(orderId,"final");
if (ObjectUtil.isNotEmpty(cashierCarts) && cashierCarts.size() > 0) {
cashierCarts.parallelStream().forEach(it -> {
String categoryId;
if(ObjectUtil.isEmpty(it.getCategoryId())){
categoryId= tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId();
} else {
categoryId = it.getCategoryId();
}
Long count= categoryInfos.stream().filter(c->
c.getId().toString().equals(categoryId)
).count();
if(count>0){
TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(Integer.valueOf(it.getSkuId()));
String remark = "";
if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) {
remark = tbProductSkuWithBLOBs.getSpecSnap();
}
String data = PrinterUtils.getPrintData(orderInfo.getTableName(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())), it.getName(), it.getNumber(), remark);
PrinterUtils.printTickets(1, Integer.valueOf(printerNum), tbPrintMachineWithBLOBs.getAddress(), data);
}
});
}
break;
case "category": //分类出单
break;
}
break;
case "kitchen": //出品打印机
break;
}
}
private void fePrinter(TbPrintMachineWithBLOBs tbPrintMachineWithBLOBs,String model,TbOrderInfo orderInfo,TbShopInfo shopInfo,String printerNum, List<CategoryInfo> categoryInfos){
String orderId=orderInfo.getId().toString();
switch (tbPrintMachineWithBLOBs.getSubType()) {
case "label": //标签打印机
List<TbCashierCart> cashierCarts = tbCashierCartMapper.selectByOrderId(orderInfo.getId().toString(),"final");
if (ObjectUtil.isNotEmpty(cashierCarts) && cashierCarts.size() > 0) {
cashierCarts.parallelStream().forEach(it->{
String categoryId;
if(ObjectUtil.isEmpty(it.getCategoryId())){
categoryId= tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId();
} else {
categoryId = it.getCategoryId();
}
Long count= categoryInfos.stream().filter(c->
c.getId().toString().equals(categoryId)
).count();
if(count>0) {
TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(Integer.valueOf(it.getSkuId()));
String remark = "";
if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) {
remark = tbProductSkuWithBLOBs.getSpecSnap();
}
for(int i=0;i<it.getNumber();i++){
FeieyunPrintUtil.printLabelMsg(tbPrintMachineWithBLOBs.getAddress(), orderInfo.getTableName(), it.getName(), 1, DateUtils.getTimes(new Date(orderInfo.getCreatedAt())), it.getSalePrice().toPlainString(), remark);
}
}
});
}
break;
case "cash": //小票打印机
break;
case "kitchen": //出品打印机
break;
}
}
}

View File

@@ -0,0 +1,85 @@
package com.chaozhanggui.system.cashierservice.rabbit;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class RabbitConfig {
@Value("${spring.rabbitmq.host}")
private String host;
@Value("${spring.rabbitmq.port}")
private int port;
@Value("${spring.rabbitmq.username}")
private String username;
@Value("${spring.rabbitmq.password}")
private String password;
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host,port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost("/");
connectionFactory.setPublisherConfirms(true);
return connectionFactory;
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
//必须是prototype类型
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
return template;
}
@Bean
public DirectExchange defaultExchange_Register() {
return new DirectExchange(RabbitConstants.CART_PUT);
}
@Bean
public Queue queuePut_Register() {
return new Queue(RabbitConstants.CART_QUEUE_PUT, true); //队列持久
}
@Bean
public Binding bindingPut_Register() {
return BindingBuilder.bind(queuePut_Register()).to(defaultExchange_Register()).with(RabbitConstants.CART_ROUTINGKEY_PUT);
}
@Bean
public DirectExchange printExchange_Register() {
return new DirectExchange(RabbitConstants.PRINT_MECHINE_COLLECT_PUT);
}
@Bean
public Queue queuePrint_Register() {
return new Queue(RabbitConstants.PRINT_MECHINE_COLLECT_QUEUE_PUT, true); //队列持久
}
@Bean
public Binding bindingPrint_Register() {
return BindingBuilder.bind(queuePrint_Register()).to(printExchange_Register()).with(RabbitConstants.PRINT_MECHINE_COLLECT_ROUTINGKEY_PUT);
}
}

View File

@@ -0,0 +1,32 @@
package com.chaozhanggui.system.cashierservice.rabbit;
public interface RabbitConstants {
/**
* 购物车
*/
public static final String CART_PUT = "cart_put";
public static final String CART_QUEUE_PUT = "cart_queue_put";
public static final String CART_ROUTINGKEY_PUT = "cart_routingkey_put";
public static final String CART_ORDER_COLLECT_PUT = "cart_order_collect_put";
public static final String CART_ORDER_COLLECT_QUEUE_PUT = "cart_order_collect_queue_put";
public static final String CART_ORDER_COLLECT_ROUTINGKEY_PUT = "cart_order_collect_routingkey_put";
public static final String PRINT_MECHINE_COLLECT_PUT="print_mechine_collect_put_wx";
public static final String PRINT_MECHINE_COLLECT_QUEUE_PUT = "print_mechine_collect_queue_put_wx";
public static final String PRINT_MECHINE_COLLECT_ROUTINGKEY_PUT = "print_mechine_collect_routingkey_put_wx";
}

View File

@@ -0,0 +1,50 @@
package com.chaozhanggui.system.cashierservice.rabbit;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RabbitProducer implements RabbitTemplate.ConfirmCallback {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private RabbitTemplate rabbitTemplate;
@Autowired
public RabbitProducer(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果为单例的话那回调就是最后设置的内容
}
public void putCart(String content) {
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
rabbitTemplate.convertAndSend(RabbitConstants.CART_PUT, RabbitConstants.CART_ROUTINGKEY_PUT, content, correlationId);
}
public void putOrderCollect(String content) {
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
rabbitTemplate.convertAndSend(RabbitConstants.CART_ORDER_COLLECT_PUT, RabbitConstants.CART_ORDER_COLLECT_ROUTINGKEY_PUT, content, correlationId);
}
public void printMechine(String content){
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
rabbitTemplate.convertAndSend(RabbitConstants.PRINT_MECHINE_COLLECT_PUT, RabbitConstants.PRINT_MECHINE_COLLECT_ROUTINGKEY_PUT, content, correlationId);
}
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
logger.info(" 回调id:" + correlationData);
if (ack) {
logger.info("消息成功消费");
} else {
logger.info("消息消费失败:" + cause);
}
}
}