mitao
2024-07-05 7a1e9b29aa74f0ae130f462af7bf2b1f15d17b3c
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package com.finance.system.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.finance.common.basic.PageDTO;
import com.finance.common.enums.ShowStatusEnum;
import com.finance.common.exception.ServiceException;
import com.finance.common.utils.BeanUtils;
import com.finance.common.utils.CollUtils;
import com.finance.common.utils.StringUtils;
import com.finance.system.domain.TbField;
import com.finance.system.domain.TbFieldCategory;
import com.finance.system.dto.FieldCategoryDTO;
import com.finance.system.dto.ShowHideDTO;
import com.finance.system.dto.update.FieldCategoryUpdateDTO;
import com.finance.system.mapper.TbFieldCategoryMapper;
import com.finance.system.query.FieldCategoryQuery;
import com.finance.system.service.TbFieldCategoryService;
import com.finance.system.service.TbFieldService;
import com.finance.system.vo.FieldCategoryDetailVO;
import com.finance.system.vo.FieldCategoryVO;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
/**
 * <p>
 * 字段分类表 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-03-13
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TbFieldCategoryServiceImpl extends
        ServiceImpl<TbFieldCategoryMapper, TbFieldCategory> implements TbFieldCategoryService {
 
    private final TbFieldService tbFieldService;
 
    @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
    @Transactional(rollbackFor = Exception.class)
    public void edit(FieldCategoryUpdateDTO dto) {
        log.info("主线程名{}", Thread.currentThread().getName());
        validateParam(dto.getId());
        //更新一级分类
        TbFieldCategory category1 = updateCategory(dto, null);
        CompletableFuture.runAsync(() -> handleFieldCategory(category1));
        List<FieldCategoryUpdateDTO> children = dto.getChildren();
        //更新二级分类
        if (CollUtils.isNotEmpty(children)) {
            children.forEach(item -> {
                //二级分类
                TbFieldCategory category2 = updateCategory(item, category1);
                CompletableFuture.runAsync(() -> handleFieldCategory(category2));
                //三级分类
                List<FieldCategoryUpdateDTO> next = item.getChildren();
                if (!CollectionUtils.isEmpty(next)) {
                    for (FieldCategoryUpdateDTO fieldCategoryUpdateDTO : next) {
                        TbFieldCategory category3 = updateCategory(fieldCategoryUpdateDTO,
                                category2);
                        CompletableFuture.runAsync(() -> handleFieldCategory(category3));
                    }
                }
            });
        }
    }
 
    protected void handleFieldCategory(TbFieldCategory category) {
        log.info("开始处理字段分类名称同步,线程名{}", Thread.currentThread().getName());
        //查询使用到该分类的字段
        List<TbField> list1 = tbFieldService.list(Wrappers.<TbField>lambdaQuery()
                .eq(TbField::getLevelOneCategoryId, category.getId()));
        if (CollUtils.isNotEmpty(list1)) {
            List<TbField> fieldList = list1.stream().peek(item -> {
                item.setLevelOneCategoryId(category.getId());
                item.setLevelOneCategory(category.getFieldCategoryName());
            }).collect(Collectors.toList());
            tbFieldService.updateBatchById(fieldList);
        }
        List<TbField> list2 = tbFieldService.list(Wrappers.<TbField>lambdaQuery()
                .eq(TbField::getLevelTwoCategoryId, category.getId()));
        if (CollUtils.isNotEmpty(list2)) {
            List<TbField> fieldList = list2.stream().peek(item -> {
                item.setLevelTwoCategoryId(category.getId());
                item.setLevelTwoCategory(category.getFieldCategoryName());
            }).collect(Collectors.toList());
            tbFieldService.updateBatchById(fieldList);
        }
        List<TbField> list3 = tbFieldService.list(Wrappers.<TbField>lambdaQuery()
                .eq(TbField::getLevelThreeCategoryId, category.getId()));
        if (CollUtils.isNotEmpty(list2)) {
            List<TbField> fieldList = list3.stream().peek(item -> {
                item.setLevelThreeCategoryId(category.getParentId());
                item.setLevelThreeCategory(category.getFieldCategoryName());
            }).collect(Collectors.toList());
            tbFieldService.updateBatchById(fieldList);
        }
    }
 
    private TbFieldCategory updateCategory(FieldCategoryUpdateDTO dto,
            TbFieldCategory parent) {
        TbFieldCategory category = BeanUtils.copyBean(dto, TbFieldCategory.class);
        if (Objects.isNull(dto.getId()) && Objects.nonNull(parent)) {
            assert category != null;
            category.setParentId(parent.getId());
            this.save(category);
        } else {
            this.updateById(category);
        }
        return category;
    }
 
    @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;
    }
}