主表,
create table land_system_in.tb_dict_type
(
dic_type_id varchar(32) not null comment '字典类型id'
primary key,
dic_type_code varchar(50) null comment '字典类型key',
dict_type_name varchar(128) null comment '字典类型值',
dict_type_description varchar(256) null comment '字典类型描述',
is_lock varchar(1) default '1' null comment '是否内置(0.非内置1.内置)',
state varchar(1) default '1' null comment '1.正常2.停用',
create_by varchar(50) null comment '创建人',
create_date datetime default CURRENT_TIMESTAMP null comment '创建时间',
update_by varchar(50) null comment '修改人',
update_date datetime default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP,
last_update_date datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP,
constraint tb_dict_type_dic_type_key_uindex
unique (dic_type_code)
)
comment '数据字典主表' charset = utf8;
明细表,
create table land_system_in.tb_dict
(
dic_id varchar(32) not null comment '字典id'
primary key,
dic_type_id varchar(32) null comment '字典类型id',
dict_text varchar(256) null comment '字典文本',
dict_value int null comment '字典值',
dict_type_description varchar(256) null comment '字典描述',
sort_order int null,
is_lock varchar(1) default '1' null comment '是否内置(0.非内置1.内置)',
state varchar(1) default '1' null comment '1.正常2.停用',
create_by varchar(50) default '1' null comment '创建人',
create_date datetime default CURRENT_TIMESTAMP null comment '创建时间',
update_by varchar(50) null comment '修改人',
update_date datetime default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP,
last_update_date datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP,
has_input tinyint(1) default 0 null,
config_key varchar(200) null,
constraint tb_dict_dic_type_id_dict_text_uindex
unique (dic_type_id, dict_text),
constraint tb_dict_dic_type_id_dict_value_uindex
unique (dic_type_id, dict_value),
constraint tb_dict_tb_dict_type_dic_type_id_fk
foreign key (dic_type_id) references land_system_in.tb_dict_type (dic_type_id)
)
comment '数据字典子表' charset = utf8;
service,
/**
* <p>
* 数据字典子表 服务类
* </p>
*
* @author hl
* @since 2021-05-28
*/
public interface ITbDictService extends IService<TbDict> {
Map<String, Object> getDict(String dicTypeCode);
boolean addDict(List<DictVo> list);
Map<String, Map<Integer,String>> getDictMap();
}
实现类,
/**
* <p>
* 数据字典子表 服务实现类
* </p>
*
* @author hl
* @since 2021-05-28
*/
@Service
public class TbDictServiceImpl extends ServiceImpl<TbDictMapper, TbDict> implements ITbDictService {
@Resource
private TbDictTypeMapper tbDictTypeMapper;
@Override
public Map<String, Object> getDict(String dicTypeCode) {
List<DictVo> dictVoList = this.baseMapper.getDictByCode(dicTypeCode);
Map<String, List<DictVo>> map = dictVoList.stream().collect(Collectors.groupingBy(DictVo::getDicTypeCode));
Map<String, Object> objectMap = new LinkedHashMap<>();
map.entrySet().stream().forEach(m -> {
List<DictVo> voList = m.getValue();
objectMap.put(m.getKey(), voList);
});
return objectMap;
}
@Transactional(rollbackFor = Throwable.class)
@Override
public boolean addDict(List<DictVo> list) {
if (!CollectionUtils.isEmpty(list)) {
Map<String, List<DictVo>> map = list.stream().collect(Collectors.groupingBy(DictVo::getDicTypeCode));
//
Map<String, String> mapName = list.stream().filter(distinctByKey(e -> e.getDictTypeName())).collect(Collectors.toMap(DictVo::getDicTypeCode, DictVo::getDictTypeName));
map.entrySet().stream().forEach(m -> {
QueryWrapper<TbDictType> dictTypeQueryWrapper = new QueryWrapper<>();
dictTypeQueryWrapper.eq("dic_type_code", m.getKey());
TbDictType tbDictType = tbDictTypeMapper.selectOne(dictTypeQueryWrapper);
List<DictVo> voList = m.getValue();
List<TbDict> tbDictList = new ArrayList<>();
if (tbDictType == null) {
tbDictType = new TbDictType();
tbDictType.setDictTypeName(mapName.get(m.getKey()));
tbDictType.setDicTypeCode(m.getKey());
tbDictTypeMapper.insert(tbDictType);
}
orangizeDict(tbDictType, voList, tbDictList);
if (!CollectionUtils.isEmpty(tbDictList)) {
this.saveBatch(tbDictList);
}
});
}
return true;
}
private void orangizeDict(TbDictType tbDictType, List<DictVo> voList, List<TbDict> tbDictList) {
for (DictVo dictVo : voList) {
QueryWrapper<TbDict> tbDictQueryWrapper = new QueryWrapper<>();
tbDictQueryWrapper.eq("dic_type_id", tbDictType.getDicTypeId());
tbDictQueryWrapper.eq("dict_text", dictVo.getDictText());
TbDict tbDict = this.getOne(tbDictQueryWrapper);
if (tbDict == null) {
tbDict = new TbDict();
tbDict.setDictText(dictVo.getDictText());
tbDict.setDictValue(dictVo.getDictValue());
tbDict.setDicTypeId(tbDictType.getDicTypeId());
tbDict.setSortOrder(dictVo.getSortOrder());
tbDictList.add(tbDict);
}
}
}
@Override
public Map<String, Map<Integer, String>> getDictMap() {
List<DictVo> dictVoList = this.baseMapper.getDictByCode(null);
Map<String, List<DictVo>> map = dictVoList.stream().collect(Collectors.groupingBy(DictVo::getDicTypeCode));
Map<String, Map<Integer, String>> objectMap = new LinkedHashMap<>();
map.entrySet().stream().forEach(m -> {
List<DictVo> voList = m.getValue();
Map<Integer, String> mapDict = voList.stream().collect(Collectors.toMap(DictVo::getDictValue, DictVo::getDictText));
objectMap.put(m.getKey(), mapDict);
});
return objectMap;
}
}
字典公共类,
import com.ehousechina.cric.landsysin.service.ITbDictService;
import com.ehousechina.cric.landsysin.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author hl
* @Date 2021/6/8
* @Description: TODO
*/
@Slf4j
public class DictMapUtil {
public static Map<String, Map<Integer, String>> dictCache = new HashMap<>();
public static void load() {
dictCache.clear();
ITbDictService iTbDictService = SpringUtils.getBean(ITbDictService.class);
Map<String, Map<Integer, String>> dictMap = iTbDictService.getDictMap();
dictMap.entrySet().stream().forEach(d -> {
dictCache.put(d.getKey(), d.getValue());
});
}
//获取字典信息的具体方法
public static Map<Integer, String> getDictMap(String code) {
Map<Integer, String> dictMap = dictCache.get(code);
return dictMap;
}
//获取字典信息的具体方法
public static String getDictValue(String code, Object key) {
if (key == null || StringUtils.isEmpty(code) || "".equals(key.toString().trim())) {
return "";
}
Map<Integer, String> dictDetail = dictCache.get(code);
if (dictDetail == null) {
return "";
}
return dictDetail.get(Integer.valueOf(key.toString()));
}
//获取字典信息的具体方法
public static String getDictValueJoin(String code, String keys) {
if (StringUtils.isEmpty(keys) || StringUtils.isEmpty(code)) {
return "";
}
keys = keys.replace("[", "").replace("]", "");
List<String> value = new ArrayList<>();
Map<Integer, String> dictDetail = dictCache.get(code);
if (dictDetail == null) {
return "";
}
String[] keyArray = keys.split(",");
for (String key : keyArray) {
value.add(dictDetail.get(Integer.valueOf(key)));
}
return CommonUtil.join(value, ",");
}
}