xuhy
2024-09-27 d0f8732a1e4c275301b53c49ee8f00727651491e
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
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.exception.ServiceException;
import com.ruoyi.system.domain.TFoundationConfig;
import com.ruoyi.system.domain.TFoundationPerson;
import com.ruoyi.system.dto.TFoundationPersonDTO;
import com.ruoyi.system.mapper.TFoundationPersonMapper;
import com.ruoyi.system.service.TFoundationConfigService;
import com.ruoyi.system.service.TFoundationPersonService;
import com.ruoyi.system.vo.TFoundationPersonVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 基础设置 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-15
 */
@Service
public class TFoundationPersonServiceImpl extends ServiceImpl<TFoundationPersonMapper, TFoundationPerson> implements TFoundationPersonService {
 
    @Autowired
    private TFoundationConfigService foundationConfigService;
 
    @Override
    public void add(TFoundationPersonDTO dto) {
 
        long count1 = this.count(Wrappers.lambdaQuery(TFoundationPerson.class)
                .eq(TFoundationPerson::getShopId, dto.getShopId())
                .eq(TFoundationPerson::getMealCount, dto.getMealCount()));
        if(count1>0){
            throw new ServiceException("该店铺已存在该用餐人数设置");
        }
 
        List<TFoundationConfig> foundationConfigs = dto.getFoundationConfigs();
        long count = foundationConfigs.stream().map(TFoundationConfig::getTypeId).distinct().count();
        if(foundationConfigs.size()!=count){
            throw new ServiceException("菜品重复设置");
        }
        // 添加人数
        this.save(dto);
        // 添加菜品
        foundationConfigs.forEach(item -> {
            item.setPersonId(dto.getId());
        });
        foundationConfigService.saveBatch(foundationConfigs);
    }
 
    @Override
    public void edit(TFoundationPersonDTO dto) {
        long count1 = this.count(Wrappers.lambdaQuery(TFoundationPerson.class)
                .eq(TFoundationPerson::getShopId, dto.getShopId())
                .eq(TFoundationPerson::getMealCount, dto.getMealCount())
                .ne(TFoundationPerson::getId,dto.getId()));
        if(count1>0){
            throw new ServiceException("该店铺已存在该用餐人数设置");
        }
 
        List<TFoundationConfig> foundationConfigs = dto.getFoundationConfigs();
        long count = foundationConfigs.stream().map(TFoundationConfig::getTypeId).distinct().count();
        if(foundationConfigs.size()!=count){
            throw new ServiceException("菜品重复设置");
        }
        // 添加人数
        this.updateById(dto);
        // 删除菜品
        foundationConfigService.remove(Wrappers.lambdaQuery(TFoundationConfig.class)
                .eq(TFoundationConfig::getPersonId,dto.getId()));
        // 添加菜品
        foundationConfigs.forEach(item -> {
            item.setPersonId(dto.getId());
        });
        foundationConfigService.saveBatch(foundationConfigs);
    }
 
    @Override
    public List<TFoundationPersonVO> getList() {
        List<TFoundationPersonVO> list = this.baseMapper.getList();
        List<Long> ids = list.stream().map(TFoundationPersonVO::getId).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(ids)){
            List<TFoundationConfig> list1 = foundationConfigService.list(Wrappers.lambdaQuery(TFoundationConfig.class)
                    .in(TFoundationConfig::getPersonId, ids));
            list.forEach(item -> {
                List<TFoundationConfig> collect = list1.stream().filter(item1 -> item1.getPersonId().equals(item.getId())).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(collect)){
                    item.setMinDish(collect.stream().sorted(Comparator.comparingInt(TFoundationConfig::getMinCount)).findFirst().get().getMinCount());
                    item.setMaxDish(collect.stream().sorted(Comparator.comparingInt(TFoundationConfig::getMaxCount).reversed()).findFirst().get().getMaxCount());
                    item.setFoundationConfigs(collect);
                }
            });
        }
        return list;
    }
}