SysEnumerationServiceImpl.java 3.49 KB
package com.lego.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lego.system.service.ISysEnumerationService;
import com.lego.common.constant.CacheNames;
import com.lego.common.constant.SystemConstants;
import com.lego.system.domain.SysEnumeration;
import com.lego.system.domain.bo.SysEnumerationQueryBo;
import com.lego.system.mapper.SysEnumerationMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 枚举Service业务层处理
 *
 * @author gyongyi
 * @date 2024-07-19
 */
@RequiredArgsConstructor
@Service
public class SysEnumerationServiceImpl implements ISysEnumerationService {

    private final SysEnumerationMapper baseMapper;

    @Cacheable(cacheNames = CacheNames.SYS_ENUMERATION_TYPE, key = "#enumerationType")
    @Override
    public List<Tree<String>> queryEnumerationTreeList(String enumerationType) {
        LambdaQueryWrapper<SysEnumeration> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysEnumeration::getType, enumerationType);
        List<SysEnumeration> enumerationList = baseMapper.selectList(lqw);
        return buildDeptTreeSelect(enumerationList);
    }

    @Override
    public Tree<String> queryById(String id) {
        SysEnumeration enumeration = baseMapper.selectById(id);
        return new Tree<String>(){{
            setId(StrUtil.toStringOrNull(enumeration.getEnumerationId()));
            setParentId(StrUtil.toStringOrNull(enumeration.getParentId()));
            // 扩展属性 ...
            putExtra(SystemConstants.ENUMERATION_LABEL, enumeration.getLabel());
            putExtra(SystemConstants.ENUMERATION_VALUE, enumeration.getValue());
        }};
    }

    @Override
    public List<Tree<String>> queryList(SysEnumerationQueryBo query) {
        LambdaQueryWrapper<SysEnumeration> lqw = buildQueryWrapper(query);
        List<SysEnumeration> enumerationList = baseMapper.selectList(lqw);
        return buildDeptTreeSelect(enumerationList);
    }

    private LambdaQueryWrapper<SysEnumeration> buildQueryWrapper(SysEnumerationQueryBo bo) {
        LambdaQueryWrapper<SysEnumeration> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentId() != null, SysEnumeration::getParentId, bo.getParentId());
        return lqw;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param enumerationList
     * @return 下拉树结构列表
     */
    private List<Tree<String>> buildDeptTreeSelect(List<SysEnumeration> enumerationList) {
        if (CollUtil.isEmpty(enumerationList)) {
            return CollUtil.newArrayList();
        }
        //配置
        return TreeUtil.build(enumerationList, StrUtil.toStringOrNull(enumerationList.get(0).getParentId()), new TreeNodeConfig(),
                (treeNode, tree) -> {
                    tree.setId(StrUtil.toStringOrNull(treeNode.getEnumerationId()));
                    tree.setParentId(StrUtil.toStringOrNull(treeNode.getParentId()));
                    // 扩展属性 ...
                    tree.putExtra(SystemConstants.ENUMERATION_LABEL, treeNode.getLabel());
                    tree.putExtra(SystemConstants.ENUMERATION_VALUE, treeNode.getValue());
                });
    }
}