cloudroam
3 天以前 3a819e4f668c15e8b77b188b322470da12bb7a43
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.mzl.flower.service.system;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.base.cache.DictCacheClient;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.system.*;
import com.mzl.flower.dto.response.system.CodeTypeDTO;
import com.mzl.flower.dto.response.system.CodeValueDTO;
import com.mzl.flower.entity.system.CodeType;
import com.mzl.flower.entity.system.CodeValue;
import com.mzl.flower.mapper.system.CodeTypeMapper;
import com.mzl.flower.mapper.system.CodeValueMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.utils.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
@Transactional
public class CodeService extends BaseService {
 
    @Autowired
    private CodeValueMapper valueMapper;
 
    @Autowired
    private CodeTypeMapper typeMapper;
 
    @Autowired
    private DictCacheClient dictCacheClient;
 
    public List<CodeValueDTO> searchValue(String typeCode) {
        return valueMapper.selectByTypeCode(typeCode);
    }
 
    public Map<String, List<CodeValueDTO>> searchMultipleValue(String typeCode) {
        List<String> types = splitParam(typeCode);
        if (types == null || types.size() == 0) {
            return new HashMap<>();
        }
 
        List<CodeValueDTO> result = valueMapper.selectByMultipleTypeCode(types);
        Map<String, List<CodeValueDTO>> map = new HashMap<>();
        if (result != null && result.size() > 0) {
            for (CodeValueDTO d : result) {
                String code = d.getTypeCode();
                List<CodeValueDTO> ll = map.computeIfAbsent(code, k -> new ArrayList<>());
                ll.add(d);
            }
        }
 
        return map;
    }
 
    public void refreshCache() {
        dictCacheClient.deleteAll();
        List<CodeValue> dictValues = valueMapper.selectList(new QueryWrapper<>());
        for (CodeValue dictValue : dictValues) {
            dictCacheClient.addDict(dictValue.getTypeCode(), dictValue.getValue(), dictValue.getLabel());
        }
    }
 
    /**
     * 新增字典类型
     */
    public void addType(CreateCodeTypeDTO dto) {
        Integer count = typeMapper.selectCount(new QueryWrapper<CodeType>()
                .eq("code", dto.getCode()));
        if(count > 0){
            throw new ValidationException("类型已存在");
        }
        CodeType type = new CodeType();
        type.setId(UUIDGenerator.getUUID());
        type.setName(dto.getName());
        type.setCode(dto.getCode());
        type.setDescription(dto.getDescription());
        type.create(SecurityUtils.getUserId());
        typeMapper.insert(type);
    }
 
    /**
     * 更新字典类型
     */
    public void updateType(UpdateCodeTypeDTO dto) {
        CodeType type = typeMapper.selectById(dto.getId());
        type.setName(dto.getName());
        type.setDescription(dto.getDescription());
        type.update(SecurityUtils.getUserId());
        typeMapper.updateById(type);
    }
 
    /**
     * 新增字典值
     */
    public void addCodeValue(CreateCodeValueDTO dto) {
        CodeValue c = new CodeValue();
        String typeCode = dto.getTypeCode();
        String stringValue = dto.getValue();
        String label = dto.getLabel();
        List<Integer> seq = valueMapper.selectSeqByCode(typeCode);
        c.setId(UUIDGenerator.getUUID());
        c.setTypeCode(dto.getTypeCode());
        c.setValue(stringValue);
        c.setLabel(label);
        c.setDescription(dto.getDescription());
        if (seq != null && seq.size() > 0) {
            c.setSeq(seq.get(0) + 1);
        } else {
            c.setSeq(0);
        }
        c.create(SecurityUtils.getUserId());
        valueMapper.insert(c);
        dictCacheClient.addDict(typeCode, stringValue, label);
    }
 
    /**
     * 更新字典值
     */
    public void updateValue(UpdateCodeValueDTO dto) {
        CodeValue c = valueMapper.selectById(dto.getId());
        String typeCode = c.getTypeCode();
        String stringValue = dto.getValue();
        String label = dto.getLabel();
        c.setLabel(label);
        c.setDescription(dto.getDescription());
        c.setValue(stringValue);
        c.update(SecurityUtils.getUserId());
        valueMapper.updateById(c);
        dictCacheClient.addDict(typeCode, stringValue, label);
    }
 
    /**
     * 按条件查询字典类型
     */
    public Page<CodeTypeDTO> searchCodeType(Page page, QueryCodeTypeDTO dto) {
        List<CodeTypeDTO> result = typeMapper.searchCodeType(page, dto);
        page.setRecords(result);
        return page;
    }
 
    /**
     * 按ID逻辑删除字典类型
     */
    public void deleteCodeType(String id) {
        CodeType type = typeMapper.selectById(id);
        if(type == null){
            throw new ValidationException("类型不存在");
        }
        List<CodeValueDTO> valueDTOS = valueMapper.selectByTypeCode(type.getCode());
        for (CodeValueDTO v : valueDTOS) {
            deleteCodeValue(v.getId());
            dictCacheClient.deleteDict(v.getTypeCode(), v.getValue());
        }
        typeMapper.deleteById(id);
    }
 
    public void deleteCodeValue(String id) {
        CodeValue v = valueMapper.selectById(id);
        if(v == null){
            throw new ValidationException("字典不存在");
        }
        String typeCode = v.getTypeCode();
        String stringValue = v.getValue();
        dictCacheClient.deleteDict(typeCode, stringValue);
 
        valueMapper.deleteById(id);
    }
}