fu-hsi-service/src/main/java/com/supervision/police/service/impl/ComDictionaryServiceImpl.java

234 lines
8.8 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.supervision.police.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.supervision.common.domain.R;
import com.supervision.common.utils.IPages;
import com.supervision.common.utils.StringUtils;
import com.supervision.police.domain.ComDictionary;
import com.supervision.police.dto.DictionaryByTypeParam;
import com.supervision.police.mapper.ComDictionaryMapper;
import com.supervision.police.service.ComDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* <p>
* 服务实现类
* </p>
*
* @author baomidou
* @since 2022-03-16
*/
@Service
public class ComDictionaryServiceImpl extends ServiceImpl<ComDictionaryMapper, ComDictionary> implements ComDictionaryService {
@Autowired
private ComDictionaryMapper comDictionaryMapper;
@Override
public R<?> findDictionaryInfo(ComDictionary dictionary, Long page, Long size) {
LambdaQueryWrapper<ComDictionary> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper
.eq(dictionary.getId() != null, ComDictionary::getId, dictionary.getId())
.eq(StringUtils.isNotBlank(dictionary.getValue()), ComDictionary::getValue, dictionary.getValue())
.eq(StringUtils.isNotBlank(dictionary.getType()), ComDictionary::getType, dictionary.getType())
.eq(dictionary.getPid() != null, ComDictionary::getPid, dictionary.getPid())
.eq(ComDictionary::getDataStatus, "1")
.isNotNull(ComDictionary::getValue);
IPage<ComDictionary> iPage = new Page<>(page, size);
iPage = comDictionaryMapper.selectPage(iPage, lambdaQueryWrapper);
return R.ok(IPages.buildDataMap(iPage.getRecords(), iPage.getRecords().size()));
}
/**
* 通过type查询字典
* @param dictionaryByTypeParam
* @return
*/
@Override
public R<?> findDictionaryListByType(DictionaryByTypeParam dictionaryByTypeParam) {
Map<String, List<ComDictionary>> resultMap = queryDictionary(dictionaryByTypeParam);
return R.ok(resultMap);
}
@Override
public Map<String, String> getDictionaryMap(String type) {
if (StrUtil.isEmpty(type)){
return null;
}
List<ComDictionary> list = super.lambdaQuery().eq(ComDictionary::getType, type).ne(ComDictionary::getPid,0L).list();
return list.stream().collect(Collectors.toMap(ComDictionary::getValue, ComDictionary::getLabel));
}
@Override
public Map<String, String> getDictionaryMapReverse(String type) {
if (StrUtil.isEmpty(type)){
return null;
}
List<ComDictionary> list = super.lambdaQuery().eq(ComDictionary::getType, type).ne(ComDictionary::getPid,0L).list();
return list.stream().collect(Collectors.toMap(ComDictionary::getLabel,ComDictionary::getValue));
}
/**
* 根据条件查询字典表,供内部调用.
* @param dictionaryByTypeParam
* @return
*/
private Map<String, List<ComDictionary>> queryDictionary(DictionaryByTypeParam dictionaryByTypeParam) {
Map<String,List<ComDictionary>> resultMap = new HashMap<>();
List<String> typeList = CollUtil.isEmpty(dictionaryByTypeParam.getList()) ?
comDictionaryMapper.queryAllType() : dictionaryByTypeParam.getList();
for (String dictType : typeList) {
List<ComDictionary> queryList;
List<ComDictionary> otherList = new ArrayList<>();
ComDictionary comDictionary = new ComDictionary();
comDictionary.setType(dictType);
comDictionary.setDataStatus(dictionaryByTypeParam.getStatus());
Wrapper<ComDictionary> wrapper = Wrappers.query(comDictionary);
queryList = this.comDictionaryMapper.selectList(wrapper);
changeTree(queryList,otherList);
if (CollUtil.size(otherList) > 1) {
// 平铺数据
if (Integer.valueOf(0).equals(dictionaryByTypeParam.getIsTree())) {
resultMap.put(dictType,queryList);
}else {
// 树形数据
resultMap.put(dictType,otherList);
}
} else {
// 平铺数据
if (Integer.valueOf(0).equals(dictionaryByTypeParam.getIsTree())){
resultMap.put(dictType,CollUtil.isNotEmpty(otherList) ? otherList.get(0).getChild() : new ArrayList<>());
}else {
// 树形数据
resultMap.put(dictType,otherList);
}
}
}
return resultMap;
}
@Override
public R<?> insertDictionary(ComDictionary dictionary) {
Assert.notEmpty(dictionary.getType(), "字典类型不能为空");
Assert.notEmpty(dictionary.getLabel(), "字典名称不能为空");
comDictionaryMapper.insert(dictionary);
return R.okMsg("新增成功");
}
@Override
public R<?> updateDictionary(ComDictionary dictionary) {
try{
comDictionaryMapper.updateById(dictionary);
return R.okMsg("更新成功");
}catch (Exception e) {
return R.fail("更新失败");
}
}
@Override
public R<?> logicDeleteDictionary(Long id) {
if (comDictionaryMapper.updateDataStatusByIdAndPid(id) > 0) {
return R.okMsg("更新成功");
} else {
return R.fail(700,"未找到该条数据的信息.");
}
}
private void changeTree(List<ComDictionary> queryList, List<ComDictionary> resultList) {
// 先找到所有的一级菜单
for (ComDictionary comDictionary : queryList) {
// 一级菜单没有parentId
if (Long.valueOf(0L).equals(comDictionary.getPid())) {
resultList.add(comDictionary);
}
}
// 为一级菜单设置子菜单getChild是递归调用的
for (ComDictionary comDictionary : resultList) {
comDictionary.setPid(0L);
comDictionary.setChild(getChild(comDictionary.getId(), queryList));
}
}
/**
* 递归查找子菜单
*
*
* @param id 当前菜单id
* @param dictionaryList 要查找的列表
* @return
*/
private List<ComDictionary> getChild(Long id, List<ComDictionary> dictionaryList) {
// 子菜单
List<ComDictionary> childList = new ArrayList<>();
for (ComDictionary comDictionary : dictionaryList) {
if (comDictionary.getPid() != null && comDictionary.getPid().equals(id)) {
childList.add(comDictionary);
}
}
// 把子菜单的子菜单再循环一遍
for (ComDictionary comDictionary : childList) {
// 直接递归
comDictionary.setChild(getChild(comDictionary.getId(), dictionaryList));
} // 递归退出条件
if (childList.size() == 0) {
return null;
}
return childList;
}
@Override
public String getName(List<ComDictionary> list, String type, String value) {
for (ComDictionary comDictionary : list) {
// 父级字典项的value是null
if (comDictionary.getType().equals(type) && comDictionary.getValue() != null
&& comDictionary.getValue().equals(value)) {
return comDictionary.getLabel();
}
}
return "";
}
@Override
public String getValue(List<ComDictionary> list, String type, String label) {
for (ComDictionary comDictionary : list) {
if (comDictionary.getType().equals(type) && comDictionary.getLabel().equals(label)) {
return comDictionary.getValue();
}
}
return "";
}
@Override
public Boolean updateCategory(ComDictionary dictionary) {
Assert.notNull(dictionary.getId(), "id不能为空");
Assert.notEmpty(dictionary.getLabel(), "字典名称不能为空");
this.lambdaUpdate().eq(ComDictionary::getId,dictionary.getId())
.set(ComDictionary::getNote,dictionary.getNote())
.set(ComDictionary::getLabel,dictionary.getLabel()).update();
return true;
}
}