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
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.OrderNumConstants;
import com.ruoyi.common.enums.BoardEnum;
import com.ruoyi.common.utils.CodeGenerateUtils;
import com.ruoyi.system.domain.TBoard;
import com.ruoyi.system.domain.TOrderMeal;
import com.ruoyi.system.domain.TOrderMealGoods;
import com.ruoyi.system.domain.TOrderRemark;
import com.ruoyi.system.dto.*;
import com.ruoyi.system.mapper.TOrderMealGoodsMapper;
import com.ruoyi.system.mapper.TOrderMealMapper;
import com.ruoyi.system.service.TBoardService;
import com.ruoyi.system.service.TOrderMealGoodsService;
import com.ruoyi.system.service.TOrderMealService;
import com.ruoyi.system.service.TOrderRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 餐饮订单 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-14
 */
@Service
public class TOrderMealServiceImpl extends ServiceImpl<TOrderMealMapper, TOrderMeal> implements TOrderMealService {
 
    @Autowired
    private TOrderMealGoodsService orderMealGoodsService;
    @Autowired
    private TOrderRemarkService orderRemarkService;
    @Autowired
    private TBoardService boardService;
 
    @Override
    public void add(TOrderMealDTO dto) {
        if(Objects.isNull(dto.getBoardId())){
            throw new RuntimeException("餐桌不能为空");
        }
        // 查询餐桌
        TBoard board = boardService.getById(dto.getBoardId());
        // 如果菜品为空
        if(CollectionUtils.isEmpty(dto.getMealOrderGoodsDTOS())){
            // 开台
            board.setStatus(BoardEnum.WAITING_ORDER.getCode());
            this.save(dto);
        }else {
            board.setStatus(BoardEnum.DURING_MEAL.getCode());
            // 订单金额,订单编号
            String orderNum = OrderNumConstants.MEAL+CodeGenerateUtils.generateOrderSn();
            dto.setOrderNum(orderNum);
            List<TOrderMealGoods> orderMealGoods = new ArrayList<>();
            // 查询商品
            List<Long> goodsIds = dto.getMealOrderGoodsDTOS().stream().map(MealOrderGoodsDTO::getGoodsId).collect(Collectors.toList());
            List<TOrderMealGoods> list = orderMealGoodsService.list(Wrappers.lambdaQuery(TOrderMealGoods.class)
                    .in(TOrderMealGoods::getId, goodsIds));
            dto.getMealOrderGoodsDTOS().forEach(item -> {
                list.stream().filter(item1 -> item1.getId().equals(item.getGoodsId())).findFirst().ifPresent(item1 -> {
                    TOrderMealGoods orderMealGood = new TOrderMealGoods();
                    orderMealGood.setGoodsName(item1.getGoodsName());
                    orderMealGood.setGoodsNum(item1.getGoodsNum());
                    orderMealGood.setGoodsPicture(item1.getGoodsPicture());
                    orderMealGood.setGoodsCount(item.getGoodsCount());
                    orderMealGood.setGoodsSalePrice(item1.getGoodsSalePrice().multiply(new BigDecimal(item.getGoodsCount())));
                    orderMealGoods.add(orderMealGood);
                });
            });
            this.saveOrUpdate(dto);
            // 添加菜品
            orderMealGoods.forEach(item -> {
                item.setOrderId(dto.getId());
            });
            orderMealGoodsService.saveBatch(orderMealGoods);
            // 添加备注
            TOrderRemark tOrderRemark = new TOrderRemark();
            tOrderRemark.setOrderId(dto.getId());
            tOrderRemark.setRemark(dto.getRemark());
            tOrderRemark.setCreateTime(LocalDateTime.now());
            orderRemarkService.save(tOrderRemark);
        }
        boardService.updateById(board);
    }
 
    @Override
    public void addDish(AddDishDTO dto) {
        List<TOrderMealGoods> orderMealGoods = new ArrayList<>();
        // 查询商品
        List<Long> goodsIds = dto.getMealOrderGoodsDTOS().stream().map(MealOrderGoodsDTO::getGoodsId).collect(Collectors.toList());
        List<TOrderMealGoods> list = orderMealGoodsService.list(Wrappers.lambdaQuery(TOrderMealGoods.class)
                .in(TOrderMealGoods::getId, goodsIds));
        dto.getMealOrderGoodsDTOS().forEach(item -> {
            list.stream().filter(item1 -> item1.getId().equals(item.getGoodsId())).findFirst().ifPresent(item1 -> {
                TOrderMealGoods orderMealGood = new TOrderMealGoods();
                orderMealGood.setGoodsName(item1.getGoodsName());
                orderMealGood.setGoodsNum(item1.getGoodsNum());
                orderMealGood.setGoodsPicture(item1.getGoodsPicture());
                orderMealGood.setGoodsCount(item.getGoodsCount());
                orderMealGood.setGoodsSalePrice(item1.getGoodsSalePrice().multiply(new BigDecimal(item.getGoodsCount())));
                orderMealGoods.add(orderMealGood);
            });
        });
        // 添加菜品
        orderMealGoods.forEach(item -> {
            item.setOrderId(dto.getOrderId());
        });
        orderMealGoodsService.saveBatch(orderMealGoods);
        // 添加备注
        TOrderRemark tOrderRemark = new TOrderRemark();
        tOrderRemark.setOrderId(dto.getOrderId());
        tOrderRemark.setRemark(dto.getRemark());
        tOrderRemark.setCreateTime(LocalDateTime.now());
        orderRemarkService.save(tOrderRemark);
    }
 
    @Override
    public void checkout(CheckoutDTO dto) {
        TOrderMeal orderMeal = this.getById(dto.getOrderId());
        orderMeal.setPayType(dto.getPayType());
        orderMeal.setPayMoney(dto.getPayMoney());
        orderMeal.setStatus(2);
        this.updateById(orderMeal);
        TBoard board = boardService.getById(orderMeal.getBoardId());
        board.setStatus(BoardEnum.FREE.getCode());
        boardService.updateById(board);
    }
 
}