package com.vincent.rsf.server.manager.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.vincent.rsf.server.manager.enums.OrderType; import com.vincent.rsf.server.manager.service.OrderTypeDictService; import com.vincent.rsf.server.system.constant.DictTypeCode; import com.vincent.rsf.server.system.entity.DictData; import com.vincent.rsf.server.system.service.DictDataService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 订单类型:优先从字典表读取,兼容 API 数字 1/2/3 与枚举回退。 */ @Slf4j @Service public class OrderTypeDictServiceImpl implements OrderTypeDictService { private static final Map API_NUMERIC_MAP = new HashMap<>(); static { API_NUMERIC_MAP.put("1", OrderType.ORDER_OUT.type); // 出库单 API_NUMERIC_MAP.put("2", OrderType.ORDER_IN.type); // 入库单 API_NUMERIC_MAP.put("3", OrderType.ORDER_TRANSFER.type); // 调拨单 } @Resource private DictDataService dictDataService; private volatile List cache; private static final Object CACHE_LOCK = new Object(); @Override public List listAll() { if (cache != null && !cache.isEmpty()) { return cache; } synchronized (CACHE_LOCK) { if (cache != null && !cache.isEmpty()) { return cache; } List list = dictDataService.list(new LambdaQueryWrapper() .eq(DictData::getDictTypeCode, DictTypeCode.DICT_SYS_ORDER_TYPE) .eq(DictData::getStatus, 1) .orderByAsc(DictData::getSort)); cache = list; return cache; } } @Override public String getTypeByLabel(String label) { if (label == null || label.isEmpty()) return null; for (DictData d : listAll()) { if (label.equals(d.getLabel())) return d.getValue(); } return OrderType.getTypeVal(label); } @Override public String getLabelByType(String type) { if (type == null || type.isEmpty()) return null; for (DictData d : listAll()) { if (type.equals(d.getValue())) return d.getLabel(); } return OrderType.getValType(type); } @Override public String resolveType(String input) { if (input == null) return null; String s = input.trim(); if (s.isEmpty()) return null; if (API_NUMERIC_MAP.containsKey(s)) return API_NUMERIC_MAP.get(s); String byLabel = getTypeByLabel(s); if (byLabel != null) return byLabel; if (getLabelByType(s) != null) return s; return null; } @Override public String resolveType(Integer orderType) { if (orderType == null) return null; return API_NUMERIC_MAP.get(String.valueOf(orderType)); } @Override public void refreshCache() { synchronized (CACHE_LOCK) { cache = null; } log.info("订单类型字典缓存已刷新"); } }