chen.lin
20 小时以前 82065a03737fa1370eb9f4f01ab5332933baf08a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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<String, String> 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<DictData> cache;
    private static final Object CACHE_LOCK = new Object();
 
    @Override
    public List<DictData> listAll() {
        if (cache != null && !cache.isEmpty()) {
            return cache;
        }
        synchronized (CACHE_LOCK) {
            if (cache != null && !cache.isEmpty()) {
                return cache;
            }
            List<DictData> list = dictDataService.list(new LambdaQueryWrapper<DictData>()
                    .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("订单类型字典缓存已刷新");
    }
}