记录一下字典转换的使用,对于状态或者类型必不可少的要使用字典
Mapper
@Slf4j
public class BaseMapper {
public static String dictItemIsExists(String dictConstants, String code) {
log.info("dictConstants: {}, code: {}", dictConstants, code);
String value = DictHelper.getDicItemNameByDicTypeCodeAndDicItemCode(dictConstants, code);
if (value == null) {
throw new AsolSearchException(Message.getMessage("dictItem.not.found", new Object[]{dictConstants + " --- " + code}));
}
return value;
}
}
DicHelper
@Component
public class DictHelper {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private DictItemService dictItemService;
@Autowired
private DictCacheConfig dictCacheConfig;
@Autowired
private DictTypeService dictTypeService;
private static DictHelper dictHelper;
private static Long effperiod;
@PostConstruct
public void init() {
logger.info("------------初始化字典数据工具类-------------------");
dictHelper = this;
dictHelper.dictItemService = this.dictItemService;
dictHelper.dictCacheConfig = this.dictCacheConfig;
/**
* 取得设定的时效
*/
effperiod = new Long(dictHelper.dictCacheConfig.getEffperiod() == null ? "0" : dictHelper.dictCacheConfig.getEffperiod());
}
/**
* 保存时效起始时间
*/
private static Map<String, Long> periods = new HashMap<>();
/**
* 缓存代码
*/
private static Map<String, Map<String, DictItemDto>> parameters = new HashMap<>();
/**
* List转换为Map
*
* @param dictItemList
* @return
*/
private static Map<String, DictItemDto> dictListToMap(List<DictItemDto> dictItemList) {
if (dictItemList == null || dictItemList.size() <= 0)
return null;
Map<String, DictItemDto> dicItemMap = new HashMap<>();
for (DictItemDto dicItem : dictItemList) {
dicItemMap.put(dicItem.getDictItemCode(), dicItem);
}
return dicItemMap;
}
/**
* Map转换为List
*
* @param dictItemMap
* @return
*/
private static List<DictItemDto> dictMapToList(Map<String, DictItemDto> dictItemMap) {
if (dictItemMap == null || dictItemMap.size() <= 0)
return null;
List<DictItemDto> list = new LinkedList<>();
list.addAll(dictItemMap.values());
return list;
}
/**
* 从数据库 加载/重新加载 字典到缓存(重新加载缓存)
*
* @param dictTypeCode 字典类型编码
*/
private static void setEffective(String dictTypeCode) {
List<DictItemDto> dictItems = dictHelper.dictItemService.findDictItemListByTypeId(dictTypeCode);
if (dictItems != null) {
parameters.put(dictTypeCode, dictListToMap(dictItems));
periods.put(dictTypeCode, Long.valueOf((System.currentTimeMillis())));
}
}
/**
* 与劳务系统对接的特殊情况
* 需要Value获取Code
* @param dictTypeCode
* @param dictItemName
* @return
*/
public static String getDicItemCodeByDicTypeCodeAndDicItemName(String dictTypeCode, String dictItemName) {
loadDic(dictTypeCode);
Map<String, DictItemDto> mapList = parameters.get(dictTypeCode);
for(Map.Entry<String, DictItemDto> entry : mapList.entrySet()){
String mapKey = entry.getKey();
DictItemDto mapValue = entry.getValue();
if (mapValue.getDictItemName().equalsIgnoreCase(dictItemName)) {
return mapKey;
}
}
return null;
}
/**
* 判断时效性
*
* @param dictTypeCode 字典类型编码
* @return
*/
private static boolean isEffective(String dictTypeCode) {
if (!periods.containsKey(dictTypeCode))
return false;
if (effperiod == 0)
return true;
return System.currentTimeMillis() - (periods.get(dictTypeCode)).longValue() < effperiod;
}
/**
* 加载指定代码到缓存
*
* @param dictTypeCode 字典类型编码
*/
private static void loadDic(String dictTypeCode) {
List<DictItemDto> dicItems = dictHelper.dictItemService.findDictItemListByTypeId(dictTypeCode);
if (dicItems != null) {
parameters.put(dictTypeCode, dictListToMap(dicItems));
}
}
/**
* 过滤掉失效的的字典项
*
* @param items
* @return
*/
public static List<DictItemDto> filterDisabled(List<DictItemDto> items) {
if (items == null || items.size() <= 0)
return null;
List<DictItemDto> dis = new LinkedList<>();
for (DictItemDto di : items) {
if (di.getIsActive()) {
dis.add(di);
}
}
return dis;
}
/**
* 通过字典类型编码获取字典项列表(已缓存)
*
* @param dictTypeCode 字典类型编码
* @param filterDisabled 是否包含失效的
* @return
*/
public static List<DictItemDto> listAllDictItemDTOByDictTypeCode(String dictTypeCode, boolean filterDisabled) {
List<DictItemDto> items = null;
if (!isEffective(dictTypeCode)) {
setEffective(dictTypeCode);
}
if (parameters.containsKey(dictTypeCode)) {
items = dictMapToList(parameters.get(dictTypeCode));
}
Collections.sort(items, new SortCollections());
return filterDisabled ? items : filterDisabled(items);
}
/**
* 通过字典类型编码获取所有有效的字典项列表(已缓存、已过滤)
*
* @param dicTypeCode 字典类型编码
* @return
*/
public static List<DictItemDto> listEffectiveDicItemDTOByDicTypeCode(String dicTypeCode) {
List<DictItemDto> list = listAllDictItemDTOByDictTypeCode(dicTypeCode, false);
Collections.sort(list, new SortCollections());
return list;
}
/**
* 通过字典类型编码获取字典类型名称
*/
public static String getDicTypeNameByDicTypeCode(String dictTypeCode){
if (dictTypeCode.trim().length() == 0 || "null".equalsIgnoreCase(dictTypeCode)) {
return null;
}
if (!isEffective(dictTypeCode)) {
setEffective(dictTypeCode);
}
return dictHelper.dictTypeService.findDictTypeNameByTypecode(dictTypeCode);
}
/**
* 通过字典类型编码和字典项编码获取字典项名称(已缓存)
*
* @param dictTypeCode 字典类型编码
* @param dictItemCode 字典项编码
* @return
*/
public static String getDicItemNameByDicTypeCodeAndDicItemCode(String dictTypeCode, String dictItemCode) {
if (dictItemCode.trim().length() == 0 || "null".equalsIgnoreCase(dictItemCode)) {
return null;
}
if (!isEffective(dictTypeCode)) {
setEffective(dictTypeCode);
}
if (!parameters.containsKey(dictTypeCode)) {
loadDic(dictTypeCode);
}
Map<String, DictItemDto> mapList = parameters.get(dictTypeCode);
if (mapList != null && mapList.containsKey(dictItemCode)) {
return mapList.get(dictItemCode).getDictItemName();
}
return null;
}
/**
* 通过字典类型和父级字典项编码获取子级字典项列表(已缓存、已过滤)
*
* @param dictTypeCode 字典类型编码
* @param parentDictItemCode 父级字典项编码
* @param filterDisabled 是否包含失效的
* @return
* @throws DataAccessException
*/
public static List<DictItemDto> listDicItemDTOByDicTypeCodeAndParentDicItemCode(String dictTypeCode,
String parentDictItemCode, boolean filterDisabled) {
if (dictTypeCode.trim().length() == 0 || "null".equalsIgnoreCase(dictTypeCode)) {
return null;
}
if (!isEffective(dictTypeCode)) {
setEffective(dictTypeCode);
}
if (!parameters.containsKey(dictTypeCode)) {
loadDic(dictTypeCode);
}
Map<String, DictItemDto> mapList = parameters.get(dictTypeCode);
List<DictItemDto> childrenItems = new LinkedList<>();
if (mapList != null) {
for (DictItemDto dto : mapList.values()) {
if (parentDictItemCode.trim().length() == 0) {
if (dto.getParentDictItemCode().trim().length() == 0) {
childrenItems.add(dto);
}
} else {
if (dto.getParentDictItemCode().trim().length() != 0 && parentDictItemCode.equals(dto.getParentDictItemCode())) {
childrenItems.add(dto);
}
}
}
}
Collections.sort(childrenItems, new SortCollections());
return childrenItems.size() == 0 ? null : (filterDisabled ? childrenItems : filterDisabled(childrenItems));
}
/**
* 字典项排序规则定义类
*
* @author wangx
*/
static class SortCollections implements Comparator<DictItemDto> {
@Override
public int compare(DictItemDto dto1, DictItemDto dto2) {
if (dto1.getSort() == null && dto2.getSort() == null) {
return dto1.getDictItemCode().compareTo(dto2.getDictItemCode());
} else if (dto1.getSort() == null) {
return -1;
} else if (dto2.getSort() == null) {
return 1;
} else {
return dto1.getSort().compareTo(dto2.getSort());
}
}
}
public static String getDictItemCodeByDictTypeCodeAndDictItemName(String dictTypeCode, String dictItemName) {
if (dictItemName.trim().length() == 0 || "null".equalsIgnoreCase(dictItemName)) {
return null;
}
if (!isEffective(dictTypeCode)) {
setEffective(dictTypeCode);
}
if (!parameters.containsKey(dictTypeCode)) {
loadDic(dictTypeCode);
}
Map<String, DictItemDto> mapList = parameters.get(dictTypeCode);
if (mapList != null) {
for (String key : mapList.keySet()) {
if (dictItemName.equals(mapList.get(key).getDictItemName())) return key;
}
}
return null;
}
/**
* 取得代码表示的中文释义(已缓存、已过滤)
*
* @param dictTypeCode 代码类型
* @param dictItemCode 要翻译的代码值
* @return
*/
public static String viewName(String dictTypeCode, String dictItemCode) {
if (StringUtils.isEmpty(dictItemCode) || "null".equalsIgnoreCase(dictItemCode)) {
return null;
}
if (!isEffect(dictTypeCode)) {
setEffect(dictTypeCode, true);
}
if (!parameters.containsKey(dictTypeCode)) {
loadDic(dictTypeCode, true);
}
if (parameters.containsKey(dictTypeCode)) {
Map<String, DictItemDto> mapList = parameters.get(dictTypeCode);
if (mapList != null && mapList.containsKey(dictItemCode)) {
return mapList.get(dictItemCode).getDictItemName();
}
}
return null;
}
/**
* 判断时效性
*
* @return
*/
private static boolean isEffect(String dictTypeCode) {
if (!periods.containsKey(dictTypeCode))
return false;
if (effperiod == 0)
return true;
return System.currentTimeMillis() - (periods.get(dictTypeCode)).longValue() < effperiod;
}
/**
* 从数据库 加载/重新加载 字典到缓存(重新加载缓存)
*
* @param isInDelete true:加载所有的字典,false:过滤掉已删除的字典
*/
private static void setEffect(String dictTypeCode, boolean isInDelete) {
List<DictItemDto> dicItems = listDicItemFromDb(dictTypeCode, isInDelete);
if (dicItems != null) {
parameters.put(dictTypeCode, dictListToMap(dicItems));
periods.put(dictTypeCode, Long.valueOf((System.currentTimeMillis())));
}
}
/**
* 查找所有下级字典代码,从数据库查找最新代码(已排序、未缓存)
*
* @param dicTypeCode
* @param isInDelete true:加载所有的字典,false:过滤掉已删除的字典
* @return
*/
public static List<DictItemDto> listDicItemFromDb(String dicTypeCode, boolean isInDelete) {
return dictHelper.dictItemService.findDictItemListByTypeId(dicTypeCode, isInDelete);
}
/**
* 加载指定代码到缓存(已排序、未缓存、已过滤)
*
* @param dictTypeCode
* @param isInDelete true:加载所有的字典,false:过滤掉已删除的字典
*/
private static void loadDic(String dictTypeCode, boolean isInDelete) {
List<DictItemDto> dicItems = listDicItemFromDb(dictTypeCode, isInDelete);
if (dicItems != null) {
parameters.put(dictTypeCode, dictListToMap(dicItems));
}
}
}
DicItemService
public interface DictItemService {
/**
* 通过字典类型编码获取字典项列表
*
* @param dictTypeCode
* @return
*/
List<DictItemDto> findDictItemListByTypeId(String dictTypeCode);
List<DictItemDto> findDictItemListByTypeId(String dictTypeCode, boolean isInDelete);
/**
* 获取数据字典项列表
*
* @param dictTypeId
* @param dictItemCode
* @param dictItemName
* @param page
* @param size
* @return
*/
List<DictItemDto> findDicItemListByCondition(Long dictTypeId, String dictItemCode, String dictItemName, int page, int size);
/**
* 获取数据字典项列表
*
* @param dictTypeId
* @param dictItemCode
* @param dictItemName
* @param page
* @param size
* @return
*/
Page<DictItemDto> pageFindDicItemListByCondition(Long dictTypeId, String dictItemCode, String dictItemName, int page, int size);
/**
* 新增字典项信息
*
* @param dictItemDto
* @return
*/
DictItemDto create(DictItemDto dictItemDto);
/**
* 删除字典项信息,需要删除的字典项id,如果删除多个使用,号分割
*
* @param ids
*/
void delete(List<Long> ids);
/**
* 修改字典项信息
*
* @param dictItemDto
* @return
*/
DictItemDto update(DictItemDto dictItemDto);
/**
* 获取字典项信息
*
* @param dictItemId
* @return
*/
DictItemDto get(Long dictItemId);
}
DicItemServiceImpl
@Service
public class DictItemServiceImpl implements DictItemService {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
DictItemRepository dictItemRepository;
@Override
@Transactional
public List<DictItemDto> findDictItemListByTypeId(String dictTypeCode) {
List<DictItem> dictItemDtoList = dictItemRepository.listDictItemByDictTypeCodeAndIsActive(dictTypeCode.trim(), true);
if (dictItemDtoList.isEmpty()) {
throw new AsolResourceNotFoundException(Message.getMessage("dictItem.not.found", new Object[]{dictTypeCode}));
}
return BeanHelper.copyProperties(dictItemDtoList, DictItemDto.class);
}
@Override
@Transactional
public List<DictItemDto> findDictItemListByTypeId(String dictTypeCode, boolean isInDelete) {
List<DictItem> dictItemDtoList = dictItemRepository.listDictItemByDictTypeCodeAndIsActive(dictTypeCode.trim(), isInDelete);
if (dictItemDtoList.isEmpty()) {
throw new AsolResourceNotFoundException(Message.getMessage("dictItem.not.found", new Object[]{dictTypeCode}));
}
return BeanHelper.copyProperties(dictItemDtoList, DictItemDto.class);
}
@Override
@Transactional
public List<DictItemDto> findDicItemListByCondition(Long dictTypeId, String dictItemCode, String dictItemName, int page, int size) {
final StringBuffer hql = new StringBuffer();
final Map<String, Object> hqlParams = new HashMap<String, Object>();
hql.append("from DictItem di where 1=1 ");
// StringMap params=ttb.getCondition();
if (dictTypeId != null) {
hql.append(" and di.dictTypeId = :dictTypeId");
hqlParams.put("dictTypeId", dictTypeId);
}
if (dictItemName != null) {
hql.append(" and di.dictItemName like :dictItemName");
hqlParams.put("dictItemName", "%" + dictItemName.trim() + "%");
}
if (dictItemCode != null) {
hql.append(" and di.dictItemCode = :dictItemCode");
hqlParams.put("dictItemCode", dictItemCode.trim());
}
// if (size != 0) {
// ttb.setTotal(DaoUtil.countHQLQueryByCondition(
// new StringBuilder().append("select count(*) ").append(hql)
// .toString(), hqlParams));
// }
// if (ttb.getTotal() == 0) {
// return null;
// }
hql.append(" order by di.sort");
return BeanHelper.copyProperties(DaoUtil.listHQLQueryByCondition(
String.valueOf(hql), hqlParams,
page, size), DictItemDto.class);
}
@Override
@Transactional
public Page<DictItemDto> pageFindDicItemListByCondition(Long dictTypeId, String dictItemCode, String dictItemName, int page, int size) {
Pageable pageable = PageRequest.of(page - 1, size);
Specification<DictItem> searchSpec = new AsolSpecification<DictItem>();
if (dictTypeId != null) {
AsolSpecification<DictItem> spec = new AsolSpecification<DictItem>();
spec.add(new SearchCriteria("dictTypeCode", dictTypeId, SearchOperation.EQUAL));
searchSpec = searchSpec.and(spec);
}
if (dictItemCode != null && !dictItemCode.isEmpty()) {
AsolSpecification<DictItem> spec = new AsolSpecification<DictItem>();
spec.add(new SearchCriteria("dictItemCode", dictItemCode.trim(), SearchOperation.EQUAL));
searchSpec = searchSpec.and(spec);
}
if (dictItemName != null && !dictItemName.isEmpty()) {
AsolSpecification<DictItem> spec = new AsolSpecification<DictItem>();
spec.add(new SearchCriteria("dictItemName", dictItemName.trim(), SearchOperation.MATCH));
searchSpec = searchSpec.and(spec);
}
Page<DictItem> result = dictItemRepository.findAll(searchSpec, pageable);
return result.map(dictItem -> BeanHelper.copyProperties(dictItem, DictItemDto.class));
}
@Override
@Transactional
public DictItemDto create(DictItemDto dictItemDto) {
if (dictItemDto != null) {
DictItem di = dictItemRepository.findByDictItemCodeAndDictTypeId(dictItemDto.getDictItemCode(), dictItemDto.getDictTypeId());
if (di != null) {
throw new AsolResourceFoundException(Message.getMessage("dictItem.found", new Object[]{dictItemDto.getDictTypeId(), dictItemDto.getDictItemCode()}));
}
DictItem dictItem = BeanHelper.copyProperties(dictItemDto, DictItem.class);
return BeanHelper.copyProperties(dictItemRepository.save(dictItem), DictItemDto.class);
}
return null;
}
@Override
@Transactional
public void delete(List<Long> ids) {
if (ids != null) {
// 批量删除字典项
dictItemRepository.deleteByIdIn(ids);
}
}
@Override
@Transactional
public DictItemDto update(DictItemDto dictItemDto) {
if (dictItemDto != null) {
DictItem di = dictItemRepository.findByDictItemCodeAndDictTypeId(dictItemDto.getDictItemCode(), dictItemDto.getDictTypeId());
if (di != null && di.getId().longValue() != dictItemDto.getId().longValue()) {
throw new AsolResourceFoundException(Message.getMessage("dictItem.found", new Object[]{dictItemDto.getDictTypeId(), dictItemDto.getDictItemCode()}));
}
DictItem dictItem = BeanHelper.copyProperties(dictItemDto, DictItem.class);
return BeanHelper.copyProperties(dictItemRepository.save(dictItem), DictItemDto.class);
}
return null;
}
@Override
@Transactional
public DictItemDto get(Long dictItemId) {
if (dictItemId != null) {
Optional<DictItem> optionalDictItem = dictItemRepository.findAllById(dictItemId);
if (optionalDictItem.isPresent()) {
return BeanHelper.copyProperties(optionalDictItem.get(), DictItemDto.class);
}
}
return null;
}
}
DicItemServiceRepository
@Repository
public interface DictItemRepository extends JpaRepository<DictItem, Long>, AsolJpaRepository<DictItem>, JpaSpecificationExecutor<DictItem> {
void deleteByDictTypeId(Long dicTypeId);
@Query(value = "select di.* from it_sys_dict_item di,it_sys_dict_type dt where di.dict_type_id=dt.id and dt.dict_type_code=?1 and di.is_active=?2 order by di.sort", nativeQuery = true)
List<DictItem> listDictItemByDictTypeCodeAndIsActive(String dictTypeCode, boolean isActive);
DictItem findByDictItemCodeAndDictTypeId(String itemCode, Long itemTypeId);
void deleteByIdIn(List<Long> ids);
}
有了这些以后只需要将DicTypeCode保存为静态常量就可以了,使用时只需要带着DicTypeCode去调用字典就可以帮我们转换了。