CZT
2023-11-27 c206acfaedc69c390fb67daa81bc686f58a212ef
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package com.ld.igds.common.impl;
 
import com.bstek.bdf2.core.model.DefaultCompany;
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.common.dto.DepotParam;
import com.ld.igds.common.dto.DepotSerData;
import com.ld.igds.common.dto.THDto;
import com.ld.igds.common.mapper.CommonMapper;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.models.Depot;
import com.ld.igds.models.DepotConf;
import com.ld.igds.models.DepotStore;
import com.ld.igds.models.DicSysConf;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author jiazx
 */
@Slf4j
@Service(CoreCommonService.BEAN_ID)
@SuppressWarnings("unchecked")
public class CommonDataServiceImpl implements CoreCommonService {
 
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private CommonMapper commonMapper;
 
    @Override
    public void setCacheDepot(List<Depot> list, String companyId) {
        if (null == list) return;
        String key;
        for (Depot depot : list) {
            key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT, depot.getId());
            redisUtil.set(key, depot);
        }
    }
 
    @Override
    public List<Depot> getCacheDepotList(String companyId) {
        String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT);
 
        Set<String> keys = redisUtil.keys(patten);
        if (null == keys) return null;
 
        List<Depot> list = new ArrayList<>();
        for (String key : keys) {
            list.add((Depot) redisUtil.get(key));
        }
 
        //重新排序
        Collator sortChina = Collator.getInstance(Locale.CHINA);
        Collections.sort(list, (a, b) -> sortChina.compare(a.getId(), b.getId()));
        return list;
    }
 
    @Override
    public List<Depot> getCacheDepotList(String companyId, String deptId) {
        if (null == deptId) return null;
        String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT);
 
        Set<String> keys = redisUtil.keys(patten);
        if (null == keys) return null;
 
        List<Depot> list = new ArrayList<>();
        Depot depot;
        for (String key : keys) {
            depot = (Depot) redisUtil.get(key);
            if (null == depot) continue;
            if (deptId.equals(depot.getDeptId())) {
                list.add(depot);
            }
        }
 
        //重新排序
        Collator sortChina = Collator.getInstance(Locale.CHINA);
        Collections.sort(list, (a, b) -> sortChina.compare(a.getId(), b.getId()));
        return list;
    }
 
    @Override
    public List<Depot> getCacheCommonBuildingDepot(String companyId, String depotId) {
        //若仓库未配置仓房编码,则直接返回对应仓库
        Depot depot = getCacheDepot(companyId, depotId);
 
        if (StringUtils.isEmpty(depot.getBuildingId())) {
            List<Depot> list = new ArrayList<>();
            list.add(depot);
            return list;
        }
 
        List<Depot> list = getCacheDepotList(companyId);
        if (null == list) {
            return null;
        }
        String buildingId = null;
        Map<String, List<Depot>> mapList = new HashMap<>();// 定义一个以建筑物ID为KEY的MAP集合对象
 
        for (Depot item : list) {
            if (item.getId().equals(depotId)) {
                buildingId = item.getBuildingId();
            }
            if (null == mapList.get(item.getBuildingId())) {
                mapList.put(item.getBuildingId(), new ArrayList<>());
            }
            mapList.get(item.getBuildingId()).add(item);
        }
        return mapList.get(buildingId);
    }
 
    @Override
    public Depot getCacheDepot(String companyId, String depotId) {
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT, depotId);
        return (Depot) redisUtil.get(key);
    }
 
    @Override
    public Depot getDepotById(String companyId, String depotId) {
        return commonMapper.getDepotById(companyId, depotId);
    }
 
    @Override
    public void setCacheDepotConf(List<DepotConf> list, String companyId) {
        if (null != list) {
            Depot depot;
            String key;
            for (DepotConf depotConf : list) {
                depot = this.getCacheDepot(companyId, depotConf.getDepotId());
                if (null != depot) {
                    depotConf.setDepotName(depot.getName());
                    depotConf.setDepotType(depot.getDepotType());
                }
                key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_CONF, depotConf.getDepotId());
                redisUtil.set(key, depotConf);
            }
        }
    }
 
    @Override
    public DepotConf getCacheDepotConf(String companyId, String depotId) {
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_CONF, depotId);
        return (DepotConf) redisUtil.get(key);
    }
 
    @Override
    public DepotConf getCacheDepotConfBySerId(String companyId, String serId) {
        List<DepotConf> data = getCacheDepotConf(companyId);
        if (null == data) return null;
 
        return data.stream().filter(item -> (serId.equals(item.getGrainSer()) || serId.equals(item.getGasSer()) || serId.equals(item.getEsSer())))
                .findAny().orElse(null);
    }
 
    @Override
    public List<DepotConf> getCacheDepotConfBySerId2(String companyId, String serId) {
        List<DepotConf> list = getCacheDepotConf(companyId);
 
        if (null == list || list.isEmpty()) return null;
        return list.stream()
                .filter(item -> null != item.getGrainSer() && item.getGrainSer().equals(serId))
                .collect(Collectors.toList());
    }
 
    @Override
    public List<DepotConf> getCacheDepotConf(String companyId) {
        String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_CONF);
 
        Set<String> keys = redisUtil.keys(patten);
 
        if (null == keys || keys.isEmpty()) return null;
 
        List<DepotConf> result = new ArrayList<>();
        for (String key : keys) {
            result.add((DepotConf) redisUtil.get(key));
        }
 
        return result;
    }
 
    @Override
    public List<DepotSerData> getCacheDepotSerByDepots(String companyId, List<String> depotIds) {
 
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST);
 
        List<DepotSerData> listAll = (List<DepotSerData>) redisUtil.get(key);
        if (null == listAll || listAll.isEmpty()) {
            log.error("没有获取到仓库与分机的关系数据,请联系管理员!");
            return null;
        }
 
        Map<String, DepotSerData> tempMap = new HashMap<>();
        for (DepotSerData depotSerData : listAll) {
            key = companyId + "-" + depotSerData.getSerId();
            for (String depotId : depotIds) {
                if (depotSerData.getDepotId().equals(depotId)) {
                    tempMap.put(key, depotSerData);
                }
            }
        }
 
        return new ArrayList<>(tempMap.values());
    }
 
    @Override
    public void updateDepotStatus(String companyId, String depotId, String status) {
        commonMapper.updateDepotStatus(companyId, depotId, status);
 
        //刷新缓存
        List<Depot> list = getCacheDepotList(companyId);
 
        Depot temp;
        for (int i = 0; i < list.size(); i++) {
            temp = list.get(i);
            if (temp.getId().equals(depotId)) {
                temp.setDepotStatus(status);
                list.set(i, temp);
                break;
            }
        }
        this.setCacheDepot(list, companyId);
    }
 
    @Override
    public void updateDepot(Depot depot) {
        int result = commonMapper.updateDepot(depot);
        if (0 == result) {
            commonMapper.addDepot(depot);
        }
    }
 
    @Override
    public void updateDepotConf(DepotConf depotConf) {
        int result = commonMapper.updateDepotConf(depotConf);
        if (0 == result) {
            commonMapper.addDepotConf(depotConf);
        }
    }
 
    @Override
    public void setCacheSysConf(String companyId, DicSysConf data) {
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_SYS_CONF);
        redisUtil.set(key, data);
    }
 
    @Override
    public DicSysConf getCacheSysConf(String companyId) {
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_SYS_CONF);
        DicSysConf data = (DicSysConf) redisUtil.get(key);
        if (null == data) {
            log.error("没有获取到当前组织={}的系统配置系统,取默认值。", companyId);
            return new DicSysConf(companyId);
        }
        return data;
    }
 
    @Override
    public void setCacheTH(THDto data) {
        String key = RedisConst.buildKeyByPrefix(
                RedisConst.KEY_DEPOT_TH_PREFIX,
                data.getSerId() + "_" + data.getThConf());
 
        redisUtil.set(key, data, 35 * 60);
    }
 
    @Override
    public THDto getCacheTH(String companyId, String thSerId, String thConf) {
 
        if (StringUtils.isEmpty(thConf))
            thConf = "1";
 
        String key = RedisConst.buildKeyByPrefix(
                RedisConst.KEY_DEPOT_TH_PREFIX, thSerId + "_" + thConf);
 
        return (THDto) redisUtil.get(key);
    }
 
 
    @Override
    public void updateDepotStorage(String companyId, String depotId,
                                   Double curStorage) {
        if (null == companyId) {
            companyId = ContextUtil.getDefaultCompanyId();
        }
        DepotParam param = new DepotParam();
        param.setCompanyId(companyId);
        param.setDepotId(depotId);
        param.setWeight(curStorage);
 
        commonMapper.updateDepotStorage(param);
    }
 
 
    @Override
    public void setCacheCompany(List<DefaultCompany> list) {
        String key = RedisConst.buildKey(Constant.DEFAULT_PARENT_CODE, "COMPANY_LIST");
        redisUtil.set(key, list);
    }
 
    @Override
    public List<DefaultCompany> getCompanyList() {
        String key = RedisConst.buildKey(Constant.DEFAULT_PARENT_CODE, "COMPANY_LIST");
 
        return (List<DefaultCompany>) redisUtil.get(key);
    }
 
    @Override
    public DepotStore getLastDepotStore(String depotId) {
        return commonMapper.getLastDepotStore(depotId);
    }
 
    @Override
    public void addDepotStore(DepotStore store, boolean updateDepot) {
        if (null == store.getUpdateDate()) store.setUpdateDate(new Date());
        if (null == store.getId()) store.setId(ContextUtil.getUUID());
        if (null == store.getManageType() || "".equals(store.getManageType())) {
            store.setManageType("01");
        }
 
        commonMapper.addDepotStore(store);
 
 
        if (updateDepot) {
            DepotParam param = new DepotParam();
            param.setCompanyId(store.getCompanyId());
            param.setDepotId(store.getDepotId());
            param.setWeight(store.getStorageReal());
            param.setDepotStatus(store.getDepotStatus());
 
            commonMapper.updateDepotStorage(param);
        }
    }
}