mitao
2024-03-14 717811c825b998c3001ad0145bd1fd8f46d1a796
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
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.PageVO;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
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.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 PageVO<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())
                .page(new Page<>(query.getPageNum(), query.getPageSize()));
        PageVO<FieldCategoryVO> pageVO = new PageVO<>();
        pageVO.setPageNo(page.getPages());
        pageVO.setPageSize(page.getSize());
        pageVO.setTotalPages(page.getTotal());
        pageVO.setTotalCount(page.getTotal());
        pageVO.setRecords(page.getRecords().stream().map(item -> {
            FieldCategoryVO fieldCategoryVO = new FieldCategoryVO();
            BeanUtils.copyBeanProp(fieldCategoryVO, item);
            return fieldCategoryVO;
        }).collect(Collectors.toList()));
        return pageVO;
    }
 
    @Override
    public void showHide(ShowHideDTO dto) {
        TbFieldCategory category = this.getById(dto.getId());
        if (Objects.isNull(category)) {
            throw new RuntimeException("非法id");
        }
        this.lambdaUpdate().eq(TbFieldCategory::getId, dto.getId()).set(TbFieldCategory::getStatus, dto.getStatus());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteChildren(Integer 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<Integer> ids = threeCategoryList.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
                //删除该二级分类下面的三级分类
                this.removeByIds(ids);
            }
            //删除二级分类
            this.removeById(id);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        //一级分类
        TbFieldCategory category = this.getById(id);
        //查询是否有二级分类
        List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, id).list();
        if (CollectionUtils.isNotEmpty(threeCategoryList)) {
            List<Integer> ids = threeCategoryList.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
            //查询三级分类
            List<TbFieldCategory> list = this.lambdaQuery().in(TbFieldCategory::getParentId, ids).list();
            if (CollectionUtils.isNotEmpty(list)) {
                List<Integer> ids1 = list.stream().map(TbFieldCategory::getId).collect(Collectors.toList());
                //删除三级分类
                this.removeByIds(ids1);
            }
            //删除二级分类
            this.removeByIds(ids);
        }
        //删除一级分类
        this.removeById(id);
    }
 
    @Override
    public void edit(FieldCategoryUpdateDTO dto) {
        //更新一级分类
        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();
    }
}