package com.mzl.flower.service.system;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.mzl.flower.base.cache.DictCacheClient;
|
import com.mzl.flower.config.exception.ValidationException;
|
import com.mzl.flower.config.security.SecurityUtils;
|
import com.mzl.flower.dto.request.system.*;
|
import com.mzl.flower.dto.response.system.CodeTypeDTO;
|
import com.mzl.flower.dto.response.system.CodeValueDTO;
|
import com.mzl.flower.entity.system.CodeType;
|
import com.mzl.flower.entity.system.CodeValue;
|
import com.mzl.flower.mapper.system.CodeTypeMapper;
|
import com.mzl.flower.mapper.system.CodeValueMapper;
|
import com.mzl.flower.service.BaseService;
|
import com.mzl.flower.utils.UUIDGenerator;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
@Service
|
@Transactional
|
public class CodeService extends BaseService {
|
|
@Autowired
|
private CodeValueMapper valueMapper;
|
|
@Autowired
|
private CodeTypeMapper typeMapper;
|
|
@Autowired
|
private DictCacheClient dictCacheClient;
|
|
public List<CodeValueDTO> searchValue(String typeCode) {
|
return valueMapper.selectByTypeCode(typeCode);
|
}
|
|
public Map<String, List<CodeValueDTO>> searchMultipleValue(String typeCode) {
|
List<String> types = splitParam(typeCode);
|
if (types == null || types.size() == 0) {
|
return new HashMap<>();
|
}
|
|
List<CodeValueDTO> result = valueMapper.selectByMultipleTypeCode(types);
|
Map<String, List<CodeValueDTO>> map = new HashMap<>();
|
if (result != null && result.size() > 0) {
|
for (CodeValueDTO d : result) {
|
String code = d.getTypeCode();
|
List<CodeValueDTO> ll = map.computeIfAbsent(code, k -> new ArrayList<>());
|
ll.add(d);
|
}
|
}
|
|
return map;
|
}
|
|
public void refreshCache() {
|
dictCacheClient.deleteAll();
|
List<CodeValue> dictValues = valueMapper.selectList(new QueryWrapper<>());
|
for (CodeValue dictValue : dictValues) {
|
dictCacheClient.addDict(dictValue.getTypeCode(), dictValue.getValue(), dictValue.getLabel());
|
}
|
}
|
|
/**
|
* 新增字典类型
|
*/
|
public void addType(CreateCodeTypeDTO dto) {
|
Integer count = typeMapper.selectCount(new QueryWrapper<CodeType>()
|
.eq("code", dto.getCode()));
|
if(count > 0){
|
throw new ValidationException("类型已存在");
|
}
|
CodeType type = new CodeType();
|
type.setId(UUIDGenerator.getUUID());
|
type.setName(dto.getName());
|
type.setCode(dto.getCode());
|
type.setDescription(dto.getDescription());
|
type.create(SecurityUtils.getUserId());
|
typeMapper.insert(type);
|
}
|
|
/**
|
* 更新字典类型
|
*/
|
public void updateType(UpdateCodeTypeDTO dto) {
|
CodeType type = typeMapper.selectById(dto.getId());
|
type.setName(dto.getName());
|
type.setDescription(dto.getDescription());
|
type.update(SecurityUtils.getUserId());
|
typeMapper.updateById(type);
|
}
|
|
/**
|
* 新增字典值
|
*/
|
public void addCodeValue(CreateCodeValueDTO dto) {
|
CodeValue c = new CodeValue();
|
String typeCode = dto.getTypeCode();
|
String stringValue = dto.getValue();
|
String label = dto.getLabel();
|
List<Integer> seq = valueMapper.selectSeqByCode(typeCode);
|
c.setId(UUIDGenerator.getUUID());
|
c.setTypeCode(dto.getTypeCode());
|
c.setValue(stringValue);
|
c.setLabel(label);
|
c.setDescription(dto.getDescription());
|
if (seq != null && seq.size() > 0) {
|
c.setSeq(seq.get(0) + 1);
|
} else {
|
c.setSeq(0);
|
}
|
c.create(SecurityUtils.getUserId());
|
valueMapper.insert(c);
|
dictCacheClient.addDict(typeCode, stringValue, label);
|
}
|
|
/**
|
* 更新字典值
|
*/
|
public void updateValue(UpdateCodeValueDTO dto) {
|
CodeValue c = valueMapper.selectById(dto.getId());
|
String typeCode = c.getTypeCode();
|
String stringValue = dto.getValue();
|
String label = dto.getLabel();
|
c.setLabel(label);
|
c.setDescription(dto.getDescription());
|
c.setValue(stringValue);
|
c.update(SecurityUtils.getUserId());
|
valueMapper.updateById(c);
|
dictCacheClient.addDict(typeCode, stringValue, label);
|
}
|
|
/**
|
* 按条件查询字典类型
|
*/
|
public Page<CodeTypeDTO> searchCodeType(Page page, QueryCodeTypeDTO dto) {
|
List<CodeTypeDTO> result = typeMapper.searchCodeType(page, dto);
|
page.setRecords(result);
|
return page;
|
}
|
|
/**
|
* 按ID逻辑删除字典类型
|
*/
|
public void deleteCodeType(String id) {
|
CodeType type = typeMapper.selectById(id);
|
if(type == null){
|
throw new ValidationException("类型不存在");
|
}
|
List<CodeValueDTO> valueDTOS = valueMapper.selectByTypeCode(type.getCode());
|
for (CodeValueDTO v : valueDTOS) {
|
deleteCodeValue(v.getId());
|
dictCacheClient.deleteDict(v.getTypeCode(), v.getValue());
|
}
|
typeMapper.deleteById(id);
|
}
|
|
public void deleteCodeValue(String id) {
|
CodeValue v = valueMapper.selectById(id);
|
if(v == null){
|
throw new ValidationException("字典不存在");
|
}
|
String typeCode = v.getTypeCode();
|
String stringValue = v.getValue();
|
dictCacheClient.deleteDict(typeCode, stringValue);
|
|
valueMapper.deleteById(id);
|
}
|
}
|