wuyouming666
2023-11-27 7bde42462e2f0997324755ab629d2f2c6e4fcc76
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
package com.example.springboot.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.vo.CategoryVo;
import com.example.springboot.security.util.SecurityUtil;
import com.example.springboot.service.CategoryService;
import com.example.springboot.service.UserService;
import com.example.springboot.mapper.CategoryMapper;
import com.example.springboot.entity.Category;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryMapper categoryDao;
 
    @Override
    public IPage<Category> selectPage(CategoryVo categoryVO) {
        List<Long> longs = userService.selectChild(SecurityUtil.getCurrentUser().getId(), true);
        return lambdaQuery().like(StrUtil.isNotBlank(categoryVO.getName()), Category::getName, categoryVO.getName())
                .in(Category::getCreator, longs)
                .eq(null != categoryVO.getState(), Category::getState, categoryVO.getState())
                .eq(null != categoryVO.getParentId(), Category::getParentId, categoryVO.getParentId())
                .between(null != categoryVO.getStartTime() && null != categoryVO.getEndTime(), Category::getCreateTime, categoryVO.getStartTime(), categoryVO.getCreateTime())
                .orderByDesc(Category::getCreateTime)
                .page(new Page<>(categoryVO.getPageNum(), categoryVO.getPageSize()));
    }
 
    @Override
    public List<Category> selectList(Long id, Boolean bool) {
        Category category = getById(id);
        List<Category> categories = selectByPath(category.getId(), category.getPath());
        if (bool) categories.add(category);
        return categories;
    }
 
    @Override
    public List<Category> selectByPath(Long id, String path) {
        return categoryDao.selectByPath((path == null ? "" : path) + id + "-");
    }
 
    @Override
    public List<Category> selectChilds(Long creator) {
        List<Long> longs = userService.selectChild(creator, true);
        if (CollectionUtil.isNotEmpty(longs)) return new ArrayList<>();
        List<Category> categories = lambdaQuery()
                .or(categoryLambdaQueryWrapper -> categoryLambdaQueryWrapper.isNull(Category::getParentId).or().eq(Category::getParentId, ""))
                .in(Category::getCreator, longs)
                .list();
        for (Category category : categories) {
            List<Category> categories1 = selectChild(category.getId());
            category.setCategorys(categories1);
        }
        return categories;
    }
 
    @Override
    public List<Category> selectChild(Long id) {
        List<Category> categoryList = lambdaQuery().eq(Category::getParentId, id).list();
        if (CollectionUtil.isEmpty(categoryList)) return null;
        for (Category category1 : categoryList) {
            List<Category> categoryList1 = selectChild(category1.getId());
            category1.setCategorys(categoryList1);
        }
        return categoryList;
    }
}