jinlin
2024-01-15 1a7af6fff5185bb257c16b0445140c93263a3331
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
package com.zt.life.core.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.common.constant.Constant;
import com.zt.common.exception.RenException;
import com.zt.common.service.BaseService;
import com.zt.common.utils.TreeUtils;
import com.zt.core.sys.dto.DictItemDto;
import com.zt.core.sys.dto.DictLeafDto;
import com.zt.core.sys.service.IDictTypeService;
import com.zt.life.core.dao.ModuleDao;
import com.zt.life.core.model.Module;
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 zt generator
 * @since 1.0.0 2020-07-15
 */
@Service
public class ModuleService extends BaseService<ModuleDao, Module> {
 
    @Autowired
    IDictTypeService dictTypeService;
 
    public void insert(Module entity) {
        if (this.getByCode(entity.getCode(), entity.getLevel()) != null) {
            throw new RenException("编码已存在!");
        } else {
            baseDao.insert(entity);
        }
    }
 
    public Module getByCode(String code, String level) {
        return baseDao.selectOne(new QueryWrapper<Module>().lambda().eq(Module::getCode, code).eq(Module::getLevel, level));
    }
 
    public List<Module> tree(String level) {
        DictLeafDto dictDto = (DictLeafDto) dictTypeService.getByDictType(level);
        List<Module> dataList = new ArrayList<>();
        if (dictDto != null) {
            dictDto.getDataList().forEach(dictItemDto -> dataList.add(toSystemEquipment(dictItemDto)));
        }
        List<Module> selectList = baseDao.selectList(new QueryWrapper<Module>()
                .eq(Constant.TableColumn.IS_DELETE, Constant.Bool.NO).lambda().eq(Module::getLevel, level));
        selectList.stream().forEach(item -> item.setPid(item.getType() + 10));
        dataList.addAll(selectList);
        List<Module> returnList = TreeUtils.build(dataList);
        return returnList.stream().filter(item -> item.getChildren().size() > 0).collect(Collectors.toList());
    }
 
    private Module toSystemEquipment(DictItemDto dictItemDto) {
        Module module = new Module();
        module.setPid(Long.parseLong("-1"));
        module.setId(Long.valueOf(dictItemDto.getDictValue()) + 10);
        module.setName(dictItemDto.getDictLabel());
        return module;
    }
 
}