mitao
2024-04-12 a7a8a50f48c1bff5ddc52b3bad1782328f8043d8
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.basic.PageDTO;
import com.ruoyi.common.enums.ShowStatusEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanUtils;
import com.ruoyi.common.utils.CollUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TbFieldCategory;
import com.ruoyi.system.dto.FieldCategoryDTO;
import com.ruoyi.system.dto.ShowHideDTO;
import com.ruoyi.system.dto.update.FieldCategoryUpdateDTO;
import com.ruoyi.system.mapper.TbFieldCategoryMapper;
import com.ruoyi.system.query.FieldCategoryQuery;
import com.ruoyi.system.service.TbFieldCategoryService;
import com.ruoyi.system.vo.FieldCategoryDetailVO;
import com.ruoyi.system.vo.FieldCategoryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 字段分类表 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-03-13
 */
@Service
public class TbFieldCategoryServiceImpl extends ServiceImpl<TbFieldCategoryMapper, TbFieldCategory> implements TbFieldCategoryService {
 
    @Override
    public void add(FieldCategoryDTO dto) {
        //一级分类
        TbFieldCategory oneCategory = new TbFieldCategory();
        oneCategory.setFieldCategoryName(dto.getFieldCategoryName());
        oneCategory.setSortOrder(dto.getSortOrder());
        this.save(oneCategory);
        //二级分类
        List<FieldCategoryDTO> children = dto.getChildren();
        if (!CollectionUtils.isEmpty(children)) {
            children.forEach(item->{
                TbFieldCategory twoCategory = new TbFieldCategory();
                twoCategory.setParentId(oneCategory.getId());
                twoCategory.setFieldCategoryName(item.getFieldCategoryName());
                this.save(twoCategory);
                //三级分类
                List<FieldCategoryDTO> next = item.getChildren();
                if (!CollectionUtils.isEmpty(next)) {
                    next.forEach(nextItem->{
                        TbFieldCategory threeCategory = new TbFieldCategory();
                        threeCategory.setParentId(twoCategory.getId());
                        threeCategory.setFieldCategoryName(nextItem.getFieldCategoryName());
                        this.save(threeCategory);
                    });
                }
            });
        }
    }
 
    @Override
    public PageDTO<FieldCategoryVO> queryPage(FieldCategoryQuery query) {
        Page<TbFieldCategory> page = this.lambdaQuery()
                .like(StringUtils.isNotBlank(query.getFieldCategoryName()), TbFieldCategory::getFieldCategoryName, query.getFieldCategoryName())
                .eq(ObjectUtils.isNotEmpty(query.getStatus()), TbFieldCategory::getStatus, query.getStatus())
                .eq(TbFieldCategory::getParentId, 0)
                .orderByDesc(TbFieldCategory::getCreateTime)
                .page(new Page<>(query.getPageNum(), query.getPageSize()));
        if (CollUtils.isEmpty(page.getRecords())) {
            return PageDTO.empty(page);
        }
        return PageDTO.of(page, FieldCategoryVO.class);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void showHide(ShowHideDTO dto) {
        updateCategoryAndChildren(dto.getId(), dto.getStatus());
    }
 
    private void updateCategoryAndChildren(Long id, ShowStatusEnum status) {
        TbFieldCategory category = this.validateParam(id);
        this.lambdaUpdate()
                .eq(TbFieldCategory::getId, id)
                .set(TbFieldCategory::getStatus, status)
                .update();
        List<TbFieldCategory> children = this.lambdaQuery().eq(TbFieldCategory::getParentId, category.getId()).list();
        if (CollUtils.isNotEmpty(children)) {
            List<Long> childIds = children.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
            childIds.forEach(childId -> updateCategoryAndChildren(childId, status));
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteChildren(Long id) {
        //认定为二级分类
        TbFieldCategory category = this.getById(id);
        if (Objects.nonNull(category)) {
            //查询是否有三级分类
            List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, id).list();
            if (CollectionUtils.isNotEmpty(threeCategoryList)) {
                List<Long> ids = threeCategoryList.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
                //删除该二级分类下面的三级分类
                this.removeByIds(ids);
            }
            //删除二级分类
            this.removeById(id);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        //一级分类
        validateParam(id);
        //查询是否有二级分类
        List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, id).list();
        if (CollectionUtils.isNotEmpty(threeCategoryList)) {
            List<Long> ids = threeCategoryList.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
            //查询三级分类
            List<TbFieldCategory> list = this.lambdaQuery().in(TbFieldCategory::getParentId, ids).list();
            if (CollectionUtils.isNotEmpty(list)) {
                List<Long> ids1 = list.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
                //删除三级分类
                this.removeByIds(ids1);
            }
            //删除二级分类
            this.removeByIds(ids);
        }
        //删除一级分类
        this.removeById(id);
    }
 
    private TbFieldCategory validateParam(Long id) {
        TbFieldCategory category = this.getById(id);
        if (Objects.isNull(category)) {
            throw new ServiceException("非法参数");
        }
        return category;
    }
 
    @Override
    public void edit(FieldCategoryUpdateDTO dto) {
        validateParam(dto.getId());
        //更新一级分类
        updateCategory(dto);
        List<FieldCategoryUpdateDTO> children = dto.getChildren();
        //更新二级分类
        if (!CollectionUtils.isEmpty(children)) {
            children.forEach(item->{
                updateCategory(item);
                //三级分类
                List<FieldCategoryUpdateDTO> next = item.getChildren();
                if (!CollectionUtils.isEmpty(next)) {
                    next.forEach(this::updateCategory);
                }
            });
        }
    }
 
    private void updateCategory(FieldCategoryUpdateDTO dto) {
        if (dto == null) return;
        this.lambdaUpdate()
                .set(TbFieldCategory::getFieldCategoryName, dto.getFieldCategoryName())
                .set(Objects.nonNull(dto.getSortOrder()),TbFieldCategory::getSortOrder, dto.getSortOrder())
                .eq(TbFieldCategory::getId, dto.getId())
                .update();
    }
 
    @Override
    public List<FieldCategoryVO> queryFieldCategories(Long id) {
        List<TbFieldCategory> list = this.lambdaQuery()
                .select(TbFieldCategory::getId,TbFieldCategory::getFieldCategoryName)
                .eq(TbFieldCategory::getParentId, id)
                .eq(TbFieldCategory::getStatus, ShowStatusEnum.SHOW)
                .list();
        return BeanUtils.copyList(list, FieldCategoryVO.class);
    }
 
    @Override
    public FieldCategoryDetailVO getDetailsById(Long id) {
        TbFieldCategory oneCategory = this.getById(id);
        if (Objects.isNull(oneCategory)) {
            return new FieldCategoryDetailVO();
        }
        FieldCategoryDetailVO vo = BeanUtils.copyBean(oneCategory, FieldCategoryDetailVO.class);
        //根据一级分类id,查询二级分类
        List<TbFieldCategory> twoCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, oneCategory.getId()).list();
        twoCategoryList.forEach(item->{
            FieldCategoryDetailVO twoCategoryVO = BeanUtils.copyBean(item, FieldCategoryDetailVO.class);
            vo.getChildren().add(twoCategoryVO);
            //根据二级分类id,查询三级分类
            List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, item.getId()).list();
            threeCategoryList.forEach(threeCategory->{
                FieldCategoryDetailVO threeCategoryVO = BeanUtils.copyBean(threeCategory, FieldCategoryDetailVO.class);
                twoCategoryVO.getChildren().add(threeCategoryVO);
            });
        });
        return vo;
    }
}