chen.lin
昨天 b003a49794f49a329e2702918ecfc8d14b371d0d
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
package com.vincent.rsf.server.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vincent.rsf.server.manager.enums.OrderWorkType;
import com.vincent.rsf.server.manager.service.OrderWorkTypeService;
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.List;
 
/**
 * 订单业务类型:优先从字典表读取(可配置),无则回退到 OrderWorkType 枚举。
 */
@Slf4j
@Service
public class OrderWorkTypeServiceImpl implements OrderWorkTypeService {
 
    @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_ORDER_WORK_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 OrderWorkType.getWorkType(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 OrderWorkType.getWorkDesc(type);
    }
 
    @Override
    public void refreshCache() {
        synchronized (CACHE_LOCK) {
            cache = null;
        }
        log.info("订单业务类型字典缓存已刷新");
    }
}