jiangping
2024-12-10 eb275c6d06d3c27fd30bbf4975d27c93d7f56eb2
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
package com.doumee.service.business.impl;
 
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.CategoryMapper;
import com.doumee.dao.business.model.Category;
import com.doumee.service.business.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 分类信息表Service实现
 * @author 江蹄蹄
 * @date 2023/11/30 15:33
 */
@Service
public class CategoryServiceImpl implements CategoryService {
 
    @Autowired
    private CategoryMapper categoryMapper;
 
    @Override
    public Integer create(Category category) {
        checkUnique(category);
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(loginUserInfo ==null){
            loginUserInfo = category.getLoginUserInfo();
        }
        Category insert = new Category();
        insert.setCreator(loginUserInfo.getId());
        insert.setCreateDate(new Date());
        insert.setEditor(loginUserInfo.getId());
        insert.setEditDate(new Date());
        insert.setIsdeleted(Constants.ZERO);
        insert.setName(category.getName());
        insert.setRemark(category.getRemark());
        insert.setStatus(Constants.ZERO);
        insert.setSortnum(category.getSortnum());
        insert.setImgurl(category.getImgurl());
        insert.setType(category.getType());
        insert.setParentId(category.getParentId());
        categoryMapper.insert(insert);
        return insert.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        categoryMapper.update(null,new UpdateWrapper<Category>().lambda().set(Category::getIsdeleted,Constants.ONE)
                .eq(Category::getId,id)
        );
    }
 
    @Override
    public void delete(Category category) {
        UpdateWrapper<Category> deleteWrapper = new UpdateWrapper<>(category);
        categoryMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        categoryMapper.update(null,new UpdateWrapper<Category>().lambda().set(Category::getIsdeleted,Constants.ONE)
                .in(Category::getId,ids)
        );
    }
 
    @Override
    public void updateById(Category category) {
        checkUnique(category);
        categoryMapper.updateById(category);
    }
 
    @Override
    public void updateByIdInBatch(List<Category> categorys) {
        if (CollectionUtils.isEmpty(categorys)) {
            return;
        }
        for (Category category: categorys) {
            this.updateById(category);
        }
    }
 
    @Override
    public Category findById(Integer id) {
        return categoryMapper.selectById(id);
    }
 
    @Override
    public Category findOne(Category category) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>(category);
        return categoryMapper.selectOne(wrapper);
    }
 
    @Override
    public List<Category> findList(Category category) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>(category);
        return categoryMapper.selectList(wrapper);
    }
 
    @Override
    public List<Category> queryList(Category category) {
        List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().lambda()
                .eq(Objects.nonNull(category)&&Objects.nonNull(category.getType()),Category::getType,category.getType())
                .isNull(Objects.nonNull(category)&&Objects.isNull(category.getParentId()),Category::getParentId)
                .eq(Objects.nonNull(category)&&Objects.nonNull(category.getParentId()),Category::getParentId,category.getParentId())
                .eq(Category::getIsdeleted,Constants.ZERO)
        );
        return categoryList;
    }
 
    @Override
    public PageData<Category> findPage(PageWrap<Category> pageWrap) {
        IPage<Category> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.lambda().eq(Category::getIsdeleted,Constants.ZERO)
                .eq(Objects.nonNull(pageWrap.getModel().getType()),Category::getType,pageWrap.getModel().getType())
                .isNull(Category::getParentId);
        PageData<Category> categoryPageData = PageData.from(categoryMapper.selectPage(page, queryWrapper));
        //查询所有二级数据
        List<Category> categoryList = categoryMapper.selectList(
                new QueryWrapper<Category>().lambda()
                        .eq(Objects.nonNull(pageWrap.getModel().getType()),Category::getType,pageWrap.getModel().getType())
                        .eq(Category::getIsdeleted,Constants.ZERO).isNotNull(Category::getParentId));
 
        for (Category category:categoryPageData.getRecords()) {
            category.setChildCategoryList(
                    categoryList.stream().filter(i->Constants.equalsInteger(i.getParentId(),category.getId())).collect(Collectors.toList())
            );
        }
 
        return categoryPageData;
    }
 
    @Override
    public long count(Category category) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>(category);
        return categoryMapper.selectCount(wrapper);
    }
    @Override
    public  List<Category> findChileList(Category model) {
        List<Category> list =findList(model);
        List<Category> data = new ArrayList<>();
        if(list!=null){
            for(Category category : list){
                if(category.getParentId()!=null){
                    Category pcate = getParentById(category.getParentId(),list);
                    if(pcate!=null){
                        category.setParentName(StringUtils.defaultString(pcate.getName(),""));
                        category.setName(StringUtils.defaultString(category.getName(),""));
                        category.setGroupName( category.getParentName()+"/"+category.getName());
                        data.add(category);
                    }
                }
            }
        }
        return  data;
    }
 
    private Category getParentById(Integer parentId, List<Category> list) {
        if(list!=null){
            for(Category category : list){
                if(Constants.equalsInteger(parentId,category.getId())){
                   return  category;
                }
            }
        }
        return  null;
    }
 
    private void checkUnique(Category category){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Category::getIsdeleted,Constants.ZERO)
                .eq(Category::getType,category.getType())
                .eq(Category::getName,category.getName());
 
        List<Category> categories = categoryMapper.selectList(wrapper);
 
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(categories)){
            throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"分类信息已存在");
        }
    }
}