kongzy
2023-11-24 ebe94e19812a1b24257d60831ec932756855e94b
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
package com.gkhy.assess.system.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.gkhy.assess.common.exception.ApiException;
import com.gkhy.assess.system.domain.SysRegion;
import com.gkhy.assess.system.domain.SysUser;
import com.gkhy.assess.system.mapper.SysRegionMapper;
import com.gkhy.assess.system.service.SysRegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
 
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 系统地区表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2023-11-24 13:45:45
 */
@Service
public class SysRegionServiceImpl extends ServiceImpl<SysRegionMapper, SysRegion> implements SysRegionService {
 
    @Override
    public List<SysRegion> regionTree(SysRegion region) {
        LambdaQueryWrapper<SysRegion> lambdaQueryWrapper = Wrappers.<SysRegion>lambdaQuery();
        if(StrUtil.isNotBlank(region.getName())){
            lambdaQueryWrapper.like(SysRegion::getName,region.getName());
        }
        lambdaQueryWrapper.orderBy(true, true,SysRegion::getSort);
        List<SysRegion> regions= list(lambdaQueryWrapper);
        //筛选出所有一级标签
        return regions.stream()
                .filter(tagEntity -> tagEntity.getParentId()==0L)
                .peek(tagEntity -> tagEntity.setChildren(this.listRegionChildren(tagEntity,regions)))
                .sorted(Comparator.comparing(SysRegion::getSort))
                .collect(Collectors.toList());
    }
 
    public List<SysRegion> listRegionChildren(SysRegion region,List<SysRegion> regions){
        //递归查找子类
        return regions.stream()
                .filter(regionEntity -> Objects.equals(regionEntity.getParentId(), region.getId()))
                .peek(regionEntity -> regionEntity.setChildren(this.listRegionChildren(regionEntity,regions)))
                //.sorted(Comparator.comparing(SysRegion::getSort))
                .collect(Collectors.toList());
    }
 
    @Override
    public int addRegion(SysRegion region) {
        if(!checkRegionUnique(new SysRegion().setName(region.getName()))){
            throw new ApiException("已存在相同地区名称");
        }
        boolean b=save(region);
        if(!b){
            throw new ApiException("新增地区失败");
        }
        return 0;
    }
 
    public boolean checkRegionUnique(SysRegion region){
        Long userId = region.getId()==null? -1L : region.getId();
        SysRegion info = baseMapper.checkRegionUnique(region.getName(),region.getParentId()==null?0L:region.getParentId());
        if (info!=null && info.getId().longValue() != userId.longValue())
        {
            return false;
        }
        return true;
    }
 
    @Override
    public int editRegion(SysRegion region) {
        if(!checkRegionUnique(new SysRegion().setName(region.getName()))){
            throw new ApiException("已存在相同地区名称");
        }
        boolean b=updateById(region);
        if(!b){
            throw new ApiException("修改地区失败");
        }
        return 0;
    }
 
    @Override
    public int deleteRegionById(Long reginId) {
        Long count=count(Wrappers.<SysRegion>lambdaQuery()
                .eq(true,SysRegion::getParentId,reginId));
        if(count>0){
            throw new ApiException("下级存在区县数据");
        }
        boolean b=removeById(reginId);
        if(!b){
            throw new ApiException("删除地区失败");
        }
        return 0;
    }
 
    @Override
    public List<SysRegion> getChildRegionById(Long regionId) {
        return list(Wrappers.<SysRegion>lambdaQuery()
                .eq(true,SysRegion::getParentId,regionId));
    }
}