xuhy
2024-08-27 96483f10fdb66727c3767826fcec6c99958e74b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.OrderNumConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.CodeGenerateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.dto.OrderMealGeneratorCountDTO;
import com.ruoyi.system.dto.OrderMealGeneratorDTO;
import com.ruoyi.system.dto.TOrderMealDTO;
import com.ruoyi.system.mapper.TDataGeneratorMapper;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.TFoundationConfigVO;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 营业数据生成 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-27
 */
@Service
public class TDataGeneratorServiceImpl extends ServiceImpl<TDataGeneratorMapper, TDataGenerator> implements TDataGeneratorService {
 
    @Autowired
    private TBoardService boardService;
    @Autowired
    private TFoundationConfigService foundationConfigService;
    @Autowired
    private TGoodsService goodsService;
    @Autowired
    private TOrderMealService orderMealService;
    @Autowired
    private TOrderMealGoodsService orderMealGoodsService;
 
    @Override
    public void dataGenerator(OrderMealGeneratorDTO dto) {
        long start = System.currentTimeMillis();
        // 查询所有的人数用餐标准
        List<TFoundationConfigVO> foundationConfigs = foundationConfigService.getList();
        // 查询所有桌子
        List<TBoard> boards = boardService.list();
        // 查询所有菜品
        List<TGoods> goods = goodsService.list();
        // 循环待生成订单列表,添加到集合中,批量插入
        List<OrderMealGeneratorCountDTO> orderMealGeneratorCountDTOS = dto.getOrderMealGeneratorCountDTOS();
        List<TOrderMeal> orderMeals = new ArrayList<>();
        for (OrderMealGeneratorCountDTO orderMealGeneratorCountDTO : orderMealGeneratorCountDTOS) {
            TBoard board = boards.stream().filter(e -> e.getId().equals(orderMealGeneratorCountDTO.getBoardId())).findFirst().get();
            Integer orderCount = orderMealGeneratorCountDTO.getOrderCount();
            // 查询当前桌的用餐人数
            for (int i = 1; i <= orderCount; i++) {
                int random = getRandom(board.getMinPerson(), board.getMaxPerson());
                TOrderMeal orderMeal = new TOrderMeal();
                orderMeal.setBoardId(board.getId());
                orderMeal.setMealType(1);
                orderMeal.setMealPerson(random);
                orderMeal.setOrderNum(OrderNumConstants.MEAL + CodeGenerateUtils.generateOrderSn());
                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());
                    typeGoods = randomSelection(typeGoods, random1);
                    for (TGoods typeGood : typeGoods) {
                        int count = 0;
                        for (TOrderMealGoods good : orderMealGoods) {
                            if (good.getGoodsNum().equals(typeGood.getGoodsNum())) {
                                count+=1;
                                good.setGoodsCount(good.getGoodsCount() + 1);
                                break; // 找到后直接跳出循环
                            }
                        }
                        if (count==0) {
                            TOrderMealGoods tOrderMealGoods = new TOrderMealGoods();
                            tOrderMealGoods.setGoodsNum(typeGood.getGoodsNum());
                            tOrderMealGoods.setGoodsName(typeGood.getGoodsName());
                            tOrderMealGoods.setGoodsPicture(typeGood.getGoodsPicture());
                            tOrderMealGoods.setGoodsSalePrice(typeGood.getSalePrice());
                            tOrderMealGoods.setGoodsCount(1);
                            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);
            }
        }
        BigDecimal sumMoney = orderMeals.stream().map(TOrderMeal::getPayMoney).reduce(BigDecimal::add).get();
        if(sumMoney.compareTo(dto.getMinTurnover()) >= 0 || sumMoney.compareTo(dto.getMaxTurnover()) <= 0){
            int weiXin = getRandomPayType(orderMeals.size(), dto.getWeiXinProportion());
            int ali = getRandomPayType(orderMeals.size(), dto.getAliProportion());
            int card = getRandomPayType(orderMeals.size(), dto.getCardProportion());
            int money = getRandomPayType(orderMeals.size(), dto.getMoneyProportion());
            int other = getRandomPayType(orderMeals.size(), dto.getOtherProportion());
            switch (orderMeals.size()-(weiXin+ali+card+money+other)){
                case 1:
                    weiXin += 1;
                    break;
                case 2:
                    weiXin += 1;
                    ali += 1;
                    break;
                case 3:
                    weiXin += 1;
                    ali += 1;
                    card += 1;
                    break;
                case 4:
                    weiXin += 1;
                    ali += 1;
                    card += 1;
                    money += 1;
                    break;
                case 5:
                    weiXin += 1;
                    ali += 1;
                    card += 1;
                    money += 1;
                    other += 1;
                    break;
            }
            List<TOrderMeal> weiXinOrderMeals = orderMeals.subList(0, weiXin);
            List<TOrderMeal> aliOrderMeals = orderMeals.subList(weiXin, weiXin+ali);
            List<TOrderMeal> cardOrderMeals = orderMeals.subList(weiXin+ali, weiXin+ali+card);
            List<TOrderMeal> moneyOrderMeals = orderMeals.subList(weiXin+ali+card, weiXin+ali+card+money);
            List<TOrderMeal> otherOrderMeals = orderMeals.subList(weiXin+ali+card+money, weiXin+ali+card+money+other);
            weiXinOrderMeals.forEach(e->e.setPayType(3));
            aliOrderMeals.forEach(e->e.setPayType(2));
            cardOrderMeals.forEach(e->e.setPayType(4));
            moneyOrderMeals.forEach(e->e.setPayType(1));
            otherOrderMeals.forEach(e->e.setPayType(5));
            List<TOrderMeal> allOrderMeals = new ArrayList<>();
            allOrderMeals.addAll(weiXinOrderMeals);
            allOrderMeals.addAll(aliOrderMeals);
            allOrderMeals.addAll(cardOrderMeals);
            allOrderMeals.addAll(moneyOrderMeals);
            allOrderMeals.addAll(otherOrderMeals);
            orderMealService.saveBatch(allOrderMeals);
 
            for (TOrderMeal weiXinOrderMeal : allOrderMeals) {
                weiXinOrderMeal.getMealOrderGoods().forEach(e->e.setOrderId(weiXinOrderMeal.getId()));
            }
            orderMealGoodsService.saveBatch(allOrderMeals.stream().map(TOrderMeal::getMealOrderGoods).flatMap(Collection::stream).collect(Collectors.toList()));
            long end = System.currentTimeMillis() - start;
            System.err.println("相差时间========="+end);
        }else {
            throw new ServiceException("数据生成不在营业额范围内");
        }
 
    }
 
    private int getRandomPayType(Integer size,BigDecimal count) {
        BigDecimal bigDecimal = new BigDecimal(size).multiply(count.divide(new BigDecimal(100))).setScale(0, RoundingMode.HALF_UP);
        return Integer.parseInt(bigDecimal.toString());
    }
 
    private int getRandom(int min,int max) {
        // 生成一个min到max之间的随机数
        return new Random().nextInt(max - min + 1) + min;
    }
 
    private static <T> List<T> randomSelection(List<T> list, int size) {
        List<T> selected = new ArrayList<>();
        int length = list.size();
        for (int i = 0; i < size; i++) {
            Collections.shuffle(list);
            // 如果需求的大小超过了集合的长度,则只能取到集合的全部元素
            int subListSize = Math.min(size - selected.size(), length);
            selected.addAll(list.subList(0, subListSize));
        }
        return selected;
    }
 
    public static void main(String[] args) {
//        System.err.println(new Random().nextInt(1 - 0 + 1) + 0);
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.err.println(list.subList(1,2));
    }
}