You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fu-hsi-service/src/main/java/com/supervision/police/service/impl/ComDictionaryServiceImpl.java

234 lines
8.8 KiB
Java

10 months ago
package com.supervision.police.service.impl;
import cn.hutool.core.collection.CollUtil;
9 months ago
import cn.hutool.core.lang.Assert;
9 months ago
import cn.hutool.core.util.StrUtil;
10 months ago
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;
9 months ago
import java.util.stream.Collectors;
10 months ago
/**
* <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);
}
9 months ago
@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));
}
10 months ago
/**
* ,.
* @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();
10 months ago
for (String dictType : typeList) {
List<ComDictionary> queryList;
10 months ago
List<ComDictionary> otherList = new ArrayList<>();
ComDictionary comDictionary = new ComDictionary();
comDictionary.setType(dictType);
comDictionary.setDataStatus(dictionaryByTypeParam.getStatus());
10 months ago
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())) {
10 months ago
resultMap.put(dictType,queryList);
}else {
// 树形数据
10 months ago
resultMap.put(dictType,otherList);
}
} else {
// 平铺数据
if (Integer.valueOf(0).equals(dictionaryByTypeParam.getIsTree())){
resultMap.put(dictType,CollUtil.isNotEmpty(otherList) ? otherList.get(0).getChild() : new ArrayList<>());
10 months ago
}else {
// 树形数据
10 months ago
resultMap.put(dictType,otherList);
}
}
}
return resultMap;
}
@Override
public R<?> insertDictionary(ComDictionary dictionary) {
9 months ago
Assert.notEmpty(dictionary.getType(), "字典类型不能为空");
Assert.notEmpty(dictionary.getLabel(), "字典名称不能为空");
10 months ago
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())) {
10 months ago
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;
}
10 months ago
}