教育训练处考试制证系统后端
zhangf
2024-09-11 1a316551c8e46b793904090cfa84781bf77fef2a
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
package com.gkhy.exam.institutionalaccess.service.serviceImpl;
 
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.institutionalaccess.entity.ThSubjectType;
import com.gkhy.exam.institutionalaccess.mapper.ThSubjectTypeMapper;
import com.gkhy.exam.institutionalaccess.model.vo.ThSubjectTypeVO;
import com.gkhy.exam.institutionalaccess.service.ThSubjectTypeService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author zf
 * @email 1603559716@qq.com
 * @date 2024/8/6 15:09
 * @description ThSubjectTypeServiceImpl
 */
@Service("ThSubjectTypeService")
public class ThSubjectTypeServiceImpl extends ServiceImpl<ThSubjectTypeMapper, ThSubjectType> implements ThSubjectTypeService {
    @Autowired
    private RedisCache redisCache;
    @Override
    public void subjectTypeCache() {
        List<ThSubjectType> thSubjectTypes = baseMapper.selectList(new LambdaQueryWrapper<>());
        redisCache.setCacheObject(CacheConstants.THREE_SUBJECT_TYPE, thSubjectTypes);
    }
 
    @Override
    public List<ThSubjectType> getSubjectTypeList() {
        JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(CacheConstants.THREE_SUBJECT_TYPE);
        if (StringUtils.isNotNull(arrayCache))
        {
            return arrayCache.toList(ThSubjectType.class);
        }else {
            return baseMapper.selectList(new LambdaQueryWrapper<>());
 
        }
 
    }
 
    @Override
    public List<ThSubjectTypeVO> getTree() {
        List<ThSubjectType> subjectTypeList = getSubjectTypeList();
        List<ThSubjectTypeVO> children = getChildren(subjectTypeList, "0");
 
        return children;
    }
    private List<ThSubjectTypeVO> getChildren(List<ThSubjectType> thSubjectTypes, String parentCode){
 
        List<ThSubjectTypeVO> thSubjectTypeVOList = new ArrayList<>();
        List<ThSubjectType> selectList =  thSubjectTypes
                .stream()
                .filter(thSubjectType -> thSubjectType.getParentCode().equals(parentCode))
                .collect(Collectors.toList());
        for (ThSubjectType thSubjectType : selectList) {
            ThSubjectTypeVO thSubjectTypeVO = new ThSubjectTypeVO();
            thSubjectTypeVO.setId(thSubjectType.getId());
            thSubjectTypeVO.setName(thSubjectType.getName());
            thSubjectTypeVO.setCode(thSubjectType.getCode());
            thSubjectTypeVO.setParentCode(thSubjectType.getParentCode());
            //获取孩子节点
            thSubjectTypeVO.setChildren(getChildren(thSubjectTypes, thSubjectType.getCode()));
            thSubjectTypeVOList.add(thSubjectTypeVO);
        }
        return thSubjectTypeVOList;
    }
 
 
 
}