xuhy
2024-09-19 f34453a6e807103e36e412ecef66b39e9ef1f233
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,22 @@
        this.save(dataGenerator);
        long start = System.currentTimeMillis();
        // 查询所有的人数用餐标准
        List<TFoundationConfigVO> foundationConfigs = foundationConfigService.getList();
        List<TFoundationConfigVO> foundationConfigs = foundationConfigService.getList(dto.getShopId());
        if(CollectionUtils.isEmpty(foundationConfigs)){
            throw new ServiceException("请先添加该店铺用餐标准");
        }
        // 查询所有桌子
        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 +123,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 +146,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(good->good.getGoodsSalePrice().multiply(new BigDecimal(good.getGoodsCount()))).reduce(BigDecimal::add).get());
                    orderMeal.setPayMoney(orderMealGoods.stream().map(good->good.getGoodsSalePrice().multiply(new BigDecimal(good.getGoodsCount()))).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 +235,7 @@
            long end = System.currentTimeMillis() - start;
            System.err.println("相差时间========="+end);
        }else {
            this.removeById(dataGenerator);
            throw new ServiceException("数据生成不在营业额范围内");
        }
@@ -287,6 +308,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 +345,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 +375,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 +405,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 +451,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 +478,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);
@@ -463,14 +495,20 @@
        if(dataGenerator.getOrderType() == 1){
            // 删除时间段的数据
            orderMealService.remove(Wrappers.lambdaQuery(TOrderMeal.class)
                    .gt(TOrderMeal::getCreateTime, dataGenerator.getStartTime())
                    .lt(TOrderMeal::getCreateTime, dataGenerator.getEndTime()));
                    .gt(TOrderMeal::getMealTime, dataGenerator.getStartTime())
                    .lt(TOrderMeal::getMealTime, dataGenerator.getEndTime()));
            // 修改覆盖类型为已覆盖
            orderMealService.update(Wrappers.lambdaUpdate(TOrderMeal.class).set(TOrderMeal::getIsCover, 1).eq(TOrderMeal::getGeneratorId, id));
        }else {
            // 删除时间段的数据
            orderStockService.remove(Wrappers.lambdaQuery(TOrderStock.class)
                    .gt(TOrderStock::getStockTime, dataGenerator.getStartTime())
                    .lt(TOrderStock::getStockTime, 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 +517,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 +553,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 +593,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());
    }