package com.dl.system.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.dl.common.constant.CacheNames; import com.dl.common.core.domain.PageQuery; import com.dl.common.core.domain.entity.SysDictData; import com.dl.common.core.page.TableDataInfo; import com.dl.common.exception.ServiceException; import com.dl.common.utils.StringUtils; import com.dl.common.utils.redis.CacheUtils; import com.dl.system.mapper.SysDictDataMapper; import com.dl.system.service.ISysDictDataService; import lombok.RequiredArgsConstructor; import org.springframework.cache.annotation.CachePut; import org.springframework.stereotype.Service; import java.util.List; /** * 字典 业务层处理 * * @author Lion Li */ @RequiredArgsConstructor @Service public class SysDictDataServiceImpl implements ISysDictDataService { private final SysDictDataMapper baseMapper; @Override public TableDataInfo selectPageDictDataList(SysDictData dictData, PageQuery pageQuery) { LambdaQueryWrapper lqw = new LambdaQueryWrapper() .eq(StringUtils.isNotBlank(dictData.getDictType()), SysDictData::getDictType, dictData.getDictType()) .like(StringUtils.isNotBlank(dictData.getDictLabel()), SysDictData::getDictLabel, dictData.getDictLabel()) .eq(StringUtils.isNotBlank(dictData.getStatus()), SysDictData::getStatus, dictData.getStatus()) .orderByAsc(SysDictData::getDictSort); Page page = baseMapper.selectPage(pageQuery.build(), lqw); return TableDataInfo.build(page); } /** * 根据条件分页查询字典数据 * * @param dictData 字典数据信息 * @return 字典数据集合信息 */ @Override public List selectDictDataList(SysDictData dictData) { return baseMapper.selectList(new LambdaQueryWrapper() .eq(StringUtils.isNotBlank(dictData.getDictType()), SysDictData::getDictType, dictData.getDictType()) .like(StringUtils.isNotBlank(dictData.getDictLabel()), SysDictData::getDictLabel, dictData.getDictLabel()) .eq(StringUtils.isNotBlank(dictData.getStatus()), SysDictData::getStatus, dictData.getStatus()) .orderByAsc(SysDictData::getDictSort)); } /** * 根据字典类型和字典键值查询字典数据信息 * * @param dictType 字典类型 * @param dictValue 字典键值 * @return 字典标签 */ @Override public String selectDictLabel(String dictType, String dictValue) { return baseMapper.selectOne(new LambdaQueryWrapper() .select(SysDictData::getDictLabel) .eq(SysDictData::getDictType, dictType) .eq(SysDictData::getDictValue, dictValue)) .getDictLabel(); } /** * 根据字典数据ID查询信息 * * @param dictCode 字典数据ID * @return 字典数据 */ @Override public SysDictData selectDictDataById(String dictCode) { return baseMapper.selectById(dictCode); } /** * 批量删除字典数据信息 * * @param dictCodes 需要删除的字典数据ID */ @Override public void deleteDictDataByIds(String[] dictCodes) { for (String dictCode : dictCodes) { SysDictData data = selectDictDataById(dictCode); baseMapper.deleteById(dictCode); CacheUtils.evict(CacheNames.SYS_DICT, data.getDictType()); } } /** * 新增保存字典数据信息 * * @param data 字典数据信息 * @return 结果 */ @CachePut(cacheNames = CacheNames.SYS_DICT, key = "#data.dictType") @Override public List insertDictData(SysDictData data) { int row = baseMapper.insert(data); if (row > 0) { return baseMapper.selectDictDataByType(data.getDictType()); } throw new ServiceException("操作失败"); } /** * 修改保存字典数据信息 * * @param data 字典数据信息 * @return 结果 */ @CachePut(cacheNames = CacheNames.SYS_DICT, key = "#data.dictType") @Override public List updateDictData(SysDictData data) { int row = baseMapper.updateById(data); if (row > 0) { return baseMapper.selectDictDataByType(data.getDictType()); } throw new ServiceException("操作失败"); } }