修改随机选材规则
This commit is contained in:
@@ -121,12 +121,12 @@ public class DishController {
|
||||
|
||||
|
||||
|
||||
// //按已经定好的权重比例随机挑选菜品
|
||||
// @GetMapping("/random")
|
||||
// public BaseResponse<List> randomDish(Integer num){
|
||||
// List<Dish> dishList = dishService.list();
|
||||
// return ResultUtils.success((List) dishService.randomDish(num, dishList));
|
||||
// }
|
||||
//按已经定好的权重比例随机挑选菜品
|
||||
@GetMapping("/random")
|
||||
public BaseResponse<List> randomDish(Integer num){
|
||||
List<Dish> dishList = dishService.list();
|
||||
return ResultUtils.success((List) dishService.randomDish(num, dishList));
|
||||
}
|
||||
//
|
||||
// //按菜品分类的权重比例随机抽选菜品
|
||||
// @GetMapping("/randomByWeight")
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.awt.*;
|
||||
|
||||
public interface DishService extends IService<Dish> {
|
||||
|
||||
List randomDish(Integer num, java.util.List<Dish> dishList);
|
||||
java.util.List<Dish> randomDish(Integer num, java.util.List<Dish> dishList);
|
||||
|
||||
List randomDishByWeight(DishRandomRequest dishRandomRequest);
|
||||
|
||||
|
||||
@@ -7,19 +7,60 @@ import com.yupi.springbootinit.model.entity.Dish;
|
||||
import com.yupi.springbootinit.service.DishService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
|
||||
|
||||
//todo 随机菜品
|
||||
@Override
|
||||
public List randomDish(Integer num, java.util.List<Dish> dishList) {
|
||||
return null;
|
||||
public List<Dish> randomDish(Integer num, List<Dish> dishList) {
|
||||
if (dishList == null || dishList.isEmpty() || num <= 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// 按照菜品类型分类
|
||||
Map<String, List<Dish>> dishTypeMap = new HashMap<>();
|
||||
for (Dish dish : dishList) {
|
||||
dishTypeMap.computeIfAbsent(dish.getDishType(), k -> new ArrayList<>()).add(dish);
|
||||
}
|
||||
|
||||
// 定义肉菜和其他菜品的分类
|
||||
List<Dish> meatDishes = new ArrayList<>();
|
||||
meatDishes.addAll(dishTypeMap.getOrDefault("1", Collections.emptyList()));
|
||||
meatDishes.addAll(dishTypeMap.getOrDefault("2", Collections.emptyList()));
|
||||
|
||||
List<Dish> otherDishes = new ArrayList<>();
|
||||
otherDishes.addAll(dishTypeMap.getOrDefault("3", Collections.emptyList()));
|
||||
otherDishes.addAll(dishTypeMap.getOrDefault("4", Collections.emptyList()));
|
||||
otherDishes.addAll(dishTypeMap.getOrDefault("5", Collections.emptyList()));
|
||||
|
||||
// 确保至少选择一个肉菜和一个其他菜品
|
||||
List<Dish> selectedDishes = new ArrayList<>();
|
||||
Random random = new Random();
|
||||
|
||||
if (!meatDishes.isEmpty()) {
|
||||
selectedDishes.add(meatDishes.get(random.nextInt(meatDishes.size())));
|
||||
}
|
||||
|
||||
if (!otherDishes.isEmpty()) {
|
||||
selectedDishes.add(otherDishes.get(random.nextInt(otherDishes.size())));
|
||||
}
|
||||
|
||||
// 从所有菜品中随机补充剩余菜品,避免重复
|
||||
List<Dish> allDishes = new ArrayList<>(dishList);
|
||||
allDishes.removeAll(selectedDishes);
|
||||
|
||||
while (selectedDishes.size() < num && !allDishes.isEmpty()) {
|
||||
Dish dish = allDishes.get(random.nextInt(allDishes.size()));
|
||||
selectedDishes.add(dish);
|
||||
allDishes.remove(dish);
|
||||
}
|
||||
|
||||
return selectedDishes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List randomDishByWeight(DishRandomRequest dishRandomRequest) {
|
||||
public java.awt.List randomDishByWeight(DishRandomRequest dishRandomRequest) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user