From 20cd42287efb95254ea788ce2b6d0c559e378e04 Mon Sep 17 00:00:00 2001
From: xuhy <3313886187@qq.com>
Date: 星期四, 19 九月 2024 14:36:21 +0800
Subject: [PATCH] 修改接口

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TDataGeneratorServiceImpl.java |  102 +++++++++++++++++++++++++++++++++++---------------
 1 files changed, 71 insertions(+), 31 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TDataGeneratorServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TDataGeneratorServiceImpl.java
index e58f7b3..d2561a5 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TDataGeneratorServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TDataGeneratorServiceImpl.java
@@ -22,6 +22,7 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
@@ -63,6 +64,8 @@
     @Autowired
     private TGoodsTypeService goodsTypeService;
     @Autowired
+    private TShopService shopService;
+    @Autowired
     private RedisCache redisCache;
 
     @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
@@ -72,7 +75,7 @@
         TDataGenerator dataGenerator = new TDataGenerator();
         dataGenerator.setUserId(dto.getUserId());
         dataGenerator.setUserName(dto.getNickName());
-        dataGenerator.setShopId(dataGenerator.getShopId());
+        dataGenerator.setShopId(dto.getShopId());
         dataGenerator.setStartTime(dto.getStartTime());
         dataGenerator.setEndTime(dto.getEndTime());
         dataGenerator.setMinMoney(dto.getMinMoney());
@@ -87,11 +90,19 @@
         this.save(dataGenerator);
         long start = System.currentTimeMillis();
         // 查询所有的人数用餐标准
-        List<TFoundationConfigVO> foundationConfigs = foundationConfigService.getList();
+        List<TFoundationConfigVO> foundationConfigs = foundationConfigService.getList(dto.getShopId());
         // 查询所有桌子
-        List<TBoard> boards = boardService.list();
+        List<TBoard> boards = boardService.list(Wrappers.lambdaQuery(TBoard.class)
+                .eq(TBoard::getShopId,dto.getShopId()));
+        if(CollectionUtils.isEmpty(boards)){
+            throw new ServiceException("请先添加桌台信息");
+        }
         // 查询所有菜品
-        List<TGoods> goods = goodsService.list();
+        List<TGoods> goods = goodsService.list(Wrappers.lambdaQuery(TGoods.class)
+                .eq(TGoods::getShopId,dto.getShopId()));
+        if(CollectionUtils.isEmpty(goods)){
+            throw new ServiceException("请先添加菜品信息");
+        }
         // 循环待生成订单列表,添加到集合中,批量插入
         List<OrderMealGeneratorCountDTO> orderMealGeneratorCountDTOS = dto.getOrderMealGeneratorCountDTOS();
         List<TOrderMeal> orderMeals = new ArrayList<>();
@@ -109,7 +120,9 @@
                 orderMeal.setOrderNum(OrderNumConstants.MEAL + CodeGenerateUtils.generateVolumeSn());
                 orderMeal.setStatus(2);
                 List<TOrderMealGoods> orderMealGoods = new ArrayList<>();
+                // 获取当前桌的菜品
                 List<TFoundationConfigVO> foundationConfigVOS = foundationConfigs.stream().filter(e -> e.getMealCount().equals(random)).collect(Collectors.toList());
+                // 每种菜品分类需要几个菜的生成
                 foundationConfigVOS.forEach(e -> {
                     int random1 = getRandom(e.getMinCount(), e.getMaxCount());
                     List<TGoods> typeGoods = goods.stream().filter(m -> e.getTypeId().equals(m.getTypeId())).collect(Collectors.toList());
@@ -130,18 +143,22 @@
                             tOrderMealGoods.setGoodsPicture(typeGood.getGoodsPicture());
                             tOrderMealGoods.setGoodsSalePrice(typeGood.getSalePrice());
                             tOrderMealGoods.setGoodsCount(1);
+                            tOrderMealGoods.setTypeId(typeGood.getTypeId());
+                            tOrderMealGoods.setGoodsId(typeGood.getId());
                             orderMealGoods.add(tOrderMealGoods);
                         }
                     }
                 });
-                orderMeal.setMealOrderGoods(orderMealGoods);
-                orderMeal.setOrderMoney(orderMealGoods.stream().map(TOrderMealGoods::getGoodsSalePrice).reduce(BigDecimal::add).get());
-                orderMeal.setPayMoney(orderMealGoods.stream().map(TOrderMealGoods::getGoodsSalePrice).reduce(BigDecimal::add).get());
-                orderMeals.add(orderMeal);
+                if(!CollectionUtils.isEmpty(orderMealGoods)){
+                    orderMeal.setMealOrderGoods(orderMealGoods);
+                    orderMeal.setOrderMoney(orderMealGoods.stream().map(TOrderMealGoods::getGoodsSalePrice).reduce(BigDecimal::add).get());
+                    orderMeal.setPayMoney(orderMealGoods.stream().map(TOrderMealGoods::getGoodsSalePrice).reduce(BigDecimal::add).get());
+                    orderMeals.add(orderMeal);
+                }
             }
         }
         BigDecimal sumMoney = orderMeals.stream().map(TOrderMeal::getPayMoney).reduce(BigDecimal::add).get();
-        if(sumMoney.compareTo(dto.getMinMoney()) >= 0 || sumMoney.compareTo(dto.getMaxMoney()) <= 0){
+        if(sumMoney.compareTo(dto.getMinMoney()) >= 0 && sumMoney.compareTo(dto.getMaxMoney()) <= 0){
             int weiXin = getRandomPayType(orderMeals.size(), dto.getWeiXinProportion());
             int ali = getRandomPayType(orderMeals.size(), dto.getAliProportion());
             int card = getRandomPayType(orderMeals.size(), dto.getCardProportion());
@@ -215,6 +232,7 @@
             long end = System.currentTimeMillis() - start;
             System.err.println("相差时间========="+end);
         }else {
+            this.removeById(dataGenerator);
             throw new ServiceException("数据生成不在营业额范围内");
         }
 
@@ -287,6 +305,7 @@
                     tOrderStockGoods.setSalePrice(v.get(0).getGoodsSalePrice());
                     tOrderStockGoods.setThisSalePrice(v.get(0).getThisSalePrice());
                     tOrderStockGoods.setGoodsPicture(v.get(0).getGoodsPicture());
+                    tOrderStockGoods.setGoodsId(v.get(0).getGoodsId());
                     tOrderStockGoods.setStockCount(v.stream().mapToInt(TOrderSaleGoods::getGoodsCount).sum());
                     orderStockGoods.add(tOrderStockGoods);
                 });
@@ -323,6 +342,7 @@
                     tOrderStockGoods.setSalePrice(v.get(0).getGoodsSalePrice());
                     tOrderStockGoods.setThisSalePrice(v.get(0).getThisSalePrice());
                     tOrderStockGoods.setGoodsPicture(v.get(0).getGoodsPicture());
+                    tOrderStockGoods.setGoodsId(v.get(0).getGoodsId());
                     tOrderStockGoods.setStockCount(v.stream().mapToInt(TOrderSaleGoods::getGoodsCount).sum());
                     orderStockGoods.add(tOrderStockGoods);
                 });
@@ -352,6 +372,7 @@
                     tOrderStockGoods.setSalePrice(v.get(0).getGoodsSalePrice());
                     tOrderStockGoods.setThisSalePrice(v.get(0).getThisSalePrice());
                     tOrderStockGoods.setGoodsPicture(v.get(0).getGoodsPicture());
+                    tOrderStockGoods.setGoodsId(v.get(0).getGoodsId());
                     tOrderStockGoods.setStockCount(v.stream().mapToInt(TOrderSaleGoods::getGoodsCount).sum());
                     orderStockGoods1.add(tOrderStockGoods);
                 });
@@ -381,6 +402,7 @@
                     tOrderStockGoods.setSalePrice(v.get(0).getGoodsSalePrice());
                     tOrderStockGoods.setThisSalePrice(v.get(0).getThisSalePrice());
                     tOrderStockGoods.setGoodsPicture(v.get(0).getGoodsPicture());
+                    tOrderStockGoods.setGoodsId(v.get(0).getGoodsId());
                     tOrderStockGoods.setStockCount(v.stream().mapToInt(TOrderSaleGoods::getGoodsCount).sum());
                     orderStockGoods.add(tOrderStockGoods);
                 });
@@ -426,17 +448,22 @@
         PageInfo<TDataGeneratorVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
         List<TDataGeneratorVO> list = this.baseMapper.pageList(query,pageInfo);
         List<Long> ids = list.stream().map(TDataGeneratorVO::getId).collect(Collectors.toList());
+        if(CollectionUtils.isEmpty(ids)){
+            return new PageInfo<>();
+        }
         if(query.getOrderType() == 1){
             List<TOrderMeal> orderMeals = orderMealService.list(Wrappers.lambdaQuery(TOrderMeal.class)
                     .in(TOrderMeal::getGeneratorId, ids));
             List<Long> mealIds = orderMeals.stream().map(TOrderMeal::getId).collect(Collectors.toList());
-            List<TOrderMealGoods> mealGoods = orderMealGoodsService.list(Wrappers.lambdaQuery(TOrderMealGoods.class)
-                    .in(TOrderMealGoods::getOrderId, mealIds));
-            for (TDataGeneratorVO tDataGeneratorVO : list) {
-                List<Long> collect = orderMeals.stream().filter(e -> e.getGeneratorId().equals(tDataGeneratorVO.getId())).map(TOrderMeal::getId).collect(Collectors.toList());
-                BigDecimal money = mealGoods.stream().filter(e -> collect.contains(e.getOrderId()))
-                        .reduce(BigDecimal.ZERO, (x, y) -> x.add(y.getGoodsSalePrice().multiply(new BigDecimal(y.getGoodsCount()))), BigDecimal::add);
-                tDataGeneratorVO.setTotalRevenue(money);
+            if(!CollectionUtils.isEmpty(mealIds)){
+                List<TOrderMealGoods> mealGoods = orderMealGoodsService.list(Wrappers.lambdaQuery(TOrderMealGoods.class)
+                        .in(TOrderMealGoods::getOrderId, mealIds));
+                for (TDataGeneratorVO tDataGeneratorVO : list) {
+                    List<Long> collect = orderMeals.stream().filter(e -> e.getGeneratorId().equals(tDataGeneratorVO.getId())).map(TOrderMeal::getId).collect(Collectors.toList());
+                    BigDecimal money = mealGoods.stream().filter(e -> collect.contains(e.getOrderId()))
+                            .reduce(BigDecimal.ZERO, (x, y) -> x.add(y.getGoodsSalePrice().multiply(new BigDecimal(y.getGoodsCount()))), BigDecimal::add);
+                    tDataGeneratorVO.setTotalRevenue(money);
+                }
             }
         }else {
             List<TOrderSale> orderSales = orderSaleService.list(Wrappers.lambdaQuery(TOrderSale.class)
@@ -448,8 +475,10 @@
 //                List<Long> collect = orderSales.stream().filter(e -> e.getGeneratorId().equals(tDataGeneratorVO.getId())).map(TOrderSale::getId).collect(Collectors.toList());
 //                BigDecimal money = orderSaleGoods.stream().filter(e -> collect.contains(e.getOrderId()))
 //                        .reduce(BigDecimal.ZERO, (x, y) -> x.add(y.getGoodsCostPrice().multiply(new BigDecimal(y.getGoodsCount()))), BigDecimal::add);
-                BigDecimal money = orderSales.stream().map(TOrderSale::getPayMoney).reduce(BigDecimal::add).get();
-                tDataGeneratorVO.setTotalRevenue(money);
+                if(!CollectionUtils.isEmpty(orderSales)){
+                    BigDecimal money = orderSales.stream().map(TOrderSale::getPayMoney).reduce(BigDecimal::add).get();
+                    tDataGeneratorVO.setTotalRevenue(money);
+                }
             }
         }
         pageInfo.setRecords(list);
@@ -468,9 +497,15 @@
             // 修改覆盖类型为已覆盖
             orderMealService.update(Wrappers.lambdaUpdate(TOrderMeal.class).set(TOrderMeal::getIsCover, 1).eq(TOrderMeal::getGeneratorId, id));
         }else {
+            // 删除时间段的数据
+            orderStockService.remove(Wrappers.lambdaQuery(TOrderStock.class)
+                    .gt(TOrderStock::getCreateTime, dataGenerator.getStartTime())
+                    .lt(TOrderStock::getCreateTime, dataGenerator.getEndTime()));
             // 修改覆盖类型为已覆盖
             orderStockService.update(Wrappers.lambdaUpdate(TOrderStock.class).set(TOrderStock::getIsCover, 1).eq(TOrderStock::getGeneratorId, id));
         }
+        dataGenerator.setStatus(3);
+        this.updateById(dataGenerator);
     }
 
     @Override
@@ -479,16 +514,21 @@
         TDataGenerator dataGenerator = this.getById(query.getGeneratorId());
         TDataGeneratorMealDetailVO data = new TDataGeneratorMealDetailVO();
         BeanUtils.copyProperties(dataGenerator,data);
+        // 查询店铺
+        TShop shop = shopService.getById(dataGenerator.getShopId());
+        if(Objects.nonNull(shop)){
+            data.setShopName(shop.getShopName());
+        }
         // 查询订单
         PageInfo<TOrderMealVO> pageInfo = orderMealService.pageListGenerator(query);
         data.setOrderMealList(pageInfo);
         // 统计支付金额
-        Map<String, BigDecimal> map = orderMealService.getDataGeneratorMealDetail(query);
-        BigDecimal money = map.get("moneyPay");
-        BigDecimal card = map.get("cardPay");
-        BigDecimal weiXin = map.get("weiXinPay");
-        BigDecimal ali = map.get("aliPay");
-        BigDecimal other = map.get("otherPay");
+        Map<String, Double> map = orderMealService.getDataGeneratorMealDetail(query);
+        BigDecimal money = new BigDecimal(map.get("moneyPay")).setScale(2,BigDecimal.ROUND_HALF_UP);
+        BigDecimal card = new BigDecimal(map.get("cardPay")).setScale(2,BigDecimal.ROUND_HALF_UP);
+        BigDecimal weiXin = new BigDecimal(map.get("weiXinPay")).setScale(2,BigDecimal.ROUND_HALF_UP);
+        BigDecimal ali = new BigDecimal(map.get("aliPay")).setScale(2,BigDecimal.ROUND_HALF_UP);
+        BigDecimal other = new BigDecimal(map.get("otherPay")).setScale(2,BigDecimal.ROUND_HALF_UP);
         data.setMoneyPayAmount(money);
         data.setCardPayAmount(card);
         data.setWeiXinPayAmount(weiXin);
@@ -510,12 +550,12 @@
         PageInfo<TOrderSaleVO> pageInfo = orderSaleService.pageList(orderSaleQuery);
         data.setOrderMealList(pageInfo);
         // 统计支付金额
-        Map<String, BigDecimal> map = orderSaleService.getDataGeneratorMealDetail(query);
-        BigDecimal money = map.get("moneyPay");
-        BigDecimal card = map.get("cardPay");
-        BigDecimal weiXin = map.get("weiXinPay");
-        BigDecimal ali = map.get("aliPay");
-        BigDecimal other = map.get("otherPay");
+        Map<String, Double> map = orderSaleService.getDataGeneratorSaleDetail(query);
+        BigDecimal money = new BigDecimal(map.get("moneyPay")).setScale(2,BigDecimal.ROUND_HALF_UP);
+        BigDecimal card = new BigDecimal(map.get("cardPay")).setScale(2,BigDecimal.ROUND_HALF_UP);
+        BigDecimal weiXin = new BigDecimal(map.get("weiXinPay")).setScale(2,BigDecimal.ROUND_HALF_UP);
+        BigDecimal ali = new BigDecimal(map.get("aliPay")).setScale(2,BigDecimal.ROUND_HALF_UP);
+        BigDecimal other = new BigDecimal(map.get("otherPay")).setScale(2,BigDecimal.ROUND_HALF_UP);
         data.setMoneyPayAmount(money);
         data.setCardPayAmount(card);
         data.setWeiXinPayAmount(weiXin);
@@ -550,7 +590,7 @@
     }
 
     private int getRandomPayType(Integer size,BigDecimal count) {
-        BigDecimal bigDecimal = new BigDecimal(size).multiply(count.divide(new BigDecimal(100))).setScale(0, RoundingMode.HALF_UP);
+        BigDecimal bigDecimal = new BigDecimal(size).multiply(count.divide(new BigDecimal(100))).setScale(0, BigDecimal.ROUND_FLOOR);
         return Integer.parseInt(bigDecimal.toString());
     }
 

--
Gitblit v1.7.1