xuhy
4 天以前 508f3e225df87e0da974424981e7782fc5ce875c
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TFoundationPersonServiceImpl.java
@@ -2,18 +2,26 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.TFoundationConfig;
import com.ruoyi.system.domain.TFoundationPerson;
import com.ruoyi.system.dto.TFoundationPersonBatchDTO;
import com.ruoyi.system.dto.TFoundationPersonDTO;
import com.ruoyi.system.mapper.TFoundationPersonMapper;
import com.ruoyi.system.query.TFoundationPersonQuery;
import com.ruoyi.system.service.TFoundationConfigService;
import com.ruoyi.system.service.TFoundationPersonService;
import com.ruoyi.system.vo.SysUserVO;
import com.ruoyi.system.vo.TFoundationPersonVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
@@ -32,35 +40,62 @@
    private TFoundationConfigService foundationConfigService;
    @Override
    public void add(TFoundationPersonDTO dto) {
        // 添加人数
        this.save(dto);
        // 添加菜品
        List<TFoundationConfig> foundationConfigs = dto.getFoundationConfigs();
        foundationConfigs.forEach(item -> {
            item.setPersonId(dto.getId());
        });
        foundationConfigService.saveBatch(foundationConfigs);
    @Transactional(rollbackFor = Exception.class)
    public void add(TFoundationPersonBatchDTO dto) {
        List<TFoundationPersonDTO> foundationPersonDTOS = dto.getFoundationPersonDTOS();
        boolean b = hasDuplicateId(foundationPersonDTOS);
        if(b){
            throw new ServiceException("设置存在重复用餐人数");
        }
        for (TFoundationPersonDTO tFoundationPersonDTO : foundationPersonDTOS) {
            List<TFoundationConfig> foundationConfigs = tFoundationPersonDTO.getFoundationConfigs();
            long count = foundationConfigs.stream().map(TFoundationConfig::getTypeId).distinct().count();
            if(foundationConfigs.size()!=count){
                throw new ServiceException("菜品重复设置");
            }
            this.save(tFoundationPersonDTO);
            // 添加菜品
            foundationConfigs.forEach(item -> {
                item.setPersonId(tFoundationPersonDTO.getId());
            });
            foundationConfigService.saveBatch(foundationConfigs);
        }
    }
    @Override
    public void edit(TFoundationPersonDTO dto) {
        // 添加人数
        this.updateById(dto);
        // 删除菜品
        foundationConfigService.remove(Wrappers.lambdaQuery(TFoundationConfig.class)
                .eq(TFoundationConfig::getPersonId,dto.getId()));
        // 添加菜品
        List<TFoundationConfig> foundationConfigs = dto.getFoundationConfigs();
        foundationConfigs.forEach(item -> {
            item.setPersonId(dto.getId());
        });
        foundationConfigService.saveBatch(foundationConfigs);
    @Transactional(rollbackFor = Exception.class)
    public void edit(TFoundationPersonBatchDTO dto) {
        List<TFoundationPersonDTO> foundationPersonDTOS = dto.getFoundationPersonDTOS();
        boolean b = hasDuplicateId(foundationPersonDTOS);
        if(b){
            throw new ServiceException("设置存在重复用餐人数");
        }
        this.remove(Wrappers.lambdaQuery(TFoundationPerson.class)
                .eq(TFoundationPerson::getMealType,dto.getMealType()));
        for (TFoundationPersonDTO tFoundationPersonDTO : foundationPersonDTOS) {
            List<TFoundationConfig> foundationConfigs = tFoundationPersonDTO.getFoundationConfigs();
            long count = foundationConfigs.stream().map(TFoundationConfig::getTypeId).distinct().count();
            if(foundationConfigs.size()!=count){
                throw new ServiceException("菜品重复设置");
            }
            // 添加人数
            this.saveOrUpdate(tFoundationPersonDTO);
            // 删除菜品
            foundationConfigService.remove(Wrappers.lambdaQuery(TFoundationConfig.class)
                    .eq(TFoundationConfig::getPersonId,tFoundationPersonDTO.getId()));
            // 添加菜品
            foundationConfigs.forEach(item -> {
                item.setPersonId(tFoundationPersonDTO.getId());
            });
            foundationConfigService.saveBatch(foundationConfigs);
        }
    }
    @Override
    public List<TFoundationPersonVO> getList() {
        List<TFoundationPersonVO> list = this.baseMapper.getList();
    public PageInfo<TFoundationPersonVO> getList(TFoundationPersonQuery query) {
        PageInfo<TFoundationPersonVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<TFoundationPersonVO> list = this.baseMapper.getList(query,pageInfo);
        List<Long> ids = list.stream().map(TFoundationPersonVO::getId).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(ids)){
            List<TFoundationConfig> list1 = foundationConfigService.list(Wrappers.lambdaQuery(TFoundationConfig.class)
@@ -74,6 +109,56 @@
                }
            });
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    public boolean hasDuplicateId(List<TFoundationPersonDTO> list) {
        HashSet<Integer> countSet = new HashSet<>();
        for (TFoundationPersonDTO dto : list) {
            if (!countSet.add(dto.getMealCount())) {
                return true; // 发现重复的人数
            }
        }
        return false; // 没有发现重复的人数
    }
    @Override
    public List<TFoundationPersonVO> getSetList() {
        List<TFoundationPersonVO> list = new ArrayList<>();
        List<TFoundationPersonVO> setList = this.baseMapper.getSetList();
        for (int i = 1; i <= 2; i++) {
            TFoundationPersonVO foundationPersonVO = new TFoundationPersonVO();
            foundationPersonVO.setMealType(i);
            int finalI = i;
            List<TFoundationPersonVO> collect = setList.stream().filter(e -> e.getMealType() == finalI).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(collect)){
                Integer minPerson = collect.stream().sorted(Comparator.comparingInt(TFoundationPerson::getMealCount)).findFirst().get().getMealCount();
                foundationPersonVO.setMinPerson(minPerson);
                Integer maxPerson = collect.stream().sorted(Comparator.comparingInt(TFoundationPerson::getMealCount).reversed()).findFirst().get().getMealCount();
                foundationPersonVO.setMaxPerson(maxPerson);
            }
            List<Long> ids = collect.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> collect1 = list1.stream().filter(item1 -> item1.getPersonId().equals(item.getId())).collect(Collectors.toList());
                    if(!CollectionUtils.isEmpty(collect1)){
                        item.setMinDish(collect1.stream().sorted(Comparator.comparingInt(TFoundationConfig::getMinCount)).findFirst().get().getMinCount());
                        item.setMaxDish(collect1.stream().sorted(Comparator.comparingInt(TFoundationConfig::getMaxCount).reversed()).findFirst().get().getMaxCount());
                        item.setFoundationConfigs(collect1);
                    }
                });
            }
            list.add(foundationPersonVO);
        }
        return list;
    }
    @Override
    public List<TFoundationPersonVO> getPersonList() {
        return this.baseMapper.getSetList();
    }
}