jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.gas.impl;
 
import com.baomidou.mybatisplus.plugins.Page;
import com.ld.igds.common.CoreCommonService;
import com.ld.igds.constant.BizType;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.gas.CoreGasService;
import com.ld.igds.gas.GasDataBuilder;
import com.ld.igds.gas.dto.GasData;
import com.ld.igds.gas.dto.GasParam;
import com.ld.igds.gas.mapper.GasServiceMapper;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.models.Depot;
import com.ld.igds.models.DicSysConf;
import com.ld.igds.models.Gas;
import com.ld.igds.models.GasInfo;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.RedisUtil;
import com.ld.igds.websocket.WebSocketPacket;
import com.ld.igds.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.*;
 
@Slf4j
@Component(CoreGasService.BEAN_ID)
public class CoreGasServiceImpl implements CoreGasService {
 
    @Resource
    private GasServiceMapper gasMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private CoreCommonService commonService;
 
    @Override
    public List<GasData> pageQueryList(GasParam param) {
        Page<GasData> page = new Page<>(param.getPage(), param.getLimit());
 
        page.setSearchCount(false);
 
        List<GasData> result;
        if (null == param.getStart()) {// 表明没有设置时间参数,为了保证正常获取数据
            result = gasMapper.pageListGas(page, param);
        } else {// 如果有参数,将pageSize设置为100
            page.setSize(100);
            result = gasMapper.pageListGas(page, param);
        }
        if (null == result || result.isEmpty()) {
            return result;
        }
        // 粮情数据调整
        if (param.isTagUpdate()) {
            // 获取配置信息
            DicSysConf conf = commonService.getCacheSysConf(result.get(0).getCompanyId());
            for (GasData data : result) {
                GasDataBuilder.updatePestData(data, conf);
            }
        }
        return result;
    }
 
    @Override
    public List<GasData> queryChartList(GasParam param) {
        if (null == param.getStart()) {
            param.setLimit(20);
        } else {
            param.setLimit(1000);
        }
        Page<GasData> page = new Page<GasData>(param.getPage(),
                param.getLimit());
        return gasMapper.pageListChart(page, param);
    }
 
    @Override
    public List<GasInfo> getInfoGas(Map<String, Object> parameter) throws Exception {
        String companyId = (String) parameter.get("companyId");
        String depotId = (String) parameter.get("depotId");
        String batchId = (String) parameter.get("batchId");
        if (StringUtils.isEmpty(companyId) || StringUtils.isEmpty(depotId)
                || StringUtils.isEmpty(batchId))
            return null;
        Map<String, Object> args = new HashMap<>();
        args.put("id", ContextUtil.buildInfoId(companyId, depotId, batchId));
 
        return gasMapper.getInfoGas(args);
    }
 
    @Override
    public void saveInfoGas(List<GasInfo> result) {
        for (GasInfo gasInfo : result) {
            try {
                gasMapper.saveGasInfo(gasInfo);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
 
    @Override
    public void saveOrUpdateData(Gas gas) {
        try {
            gasMapper.saveGas(gas);
            //更新缓存中的数据
            updateCacGasData(gas);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public List<GasData> listCacheData(String companyId, String deptId) {
 
        String patten = RedisConst.buildKey(companyId, RedisConst.KEY_GAS_DATA);
 
        Set<String> keys = redisUtil.keys(patten);
 
        if (null == keys) return null;
 
        List<GasData> result = new ArrayList<>();
        GasData gasData;
        for (String key : keys) {
            gasData = (GasData) redisUtil.get(key);
            if (null == gasData) continue;
            result.add(gasData);
        }
        return result;
    }
 
    @Override
    public Map<String, GasData> cacheMapGasData(String companyId, String deptId) {
 
        String patten = RedisConst.buildKey(companyId, RedisConst.KEY_GAS_DATA);
 
        Set<String> keys = redisUtil.keys(patten);
 
        if (null == keys) return null;
 
        Map<String, GasData> result = new HashMap<>();
        GasData gasData;
        for (String key : keys) {
            gasData = (GasData) redisUtil.get(key);
            if (null == gasData) continue;
            result.put(gasData.getDepotId(), gasData);
        }
        return result;
    }
 
    public void updateCacGasData(Gas data) {
 
 
        String key = RedisConst.buildKey(data.getCompanyId(), RedisConst.KEY_GAS_DATA, data.getDepotId());
 
        GasData gasData = new GasData();
        BeanUtils.copyProperties(data, gasData);
        redisUtil.set(key, gasData);
 
 
        //推动大屏
        Depot depot = commonService.getCacheDepot(data.getCompanyId(), data.getDepotId());
        if (null == depot) {
            log.error("-----------跟新缓存没获取到当前仓库信息-------");
            return;
        }
        Map<String, GasData> mapData = this.cacheMapGasData(data.getCompanyId(), depot.getDeptId());
        // 气体缓存更新后,需要通知到大屏
        WebSocketPacket packet = new WebSocketPacket();
        packet.setBizType(BizType.SCREEN.getCode());
        packet.setCompanyId(ContextUtil.getDefaultCompanyId());
        packet.setDeptId(depot.getDeptId());
        packet.setBizId(BizType.GAS.getCode());
        packet.setOrderResp(OrderRespEnum.MSG_SUCCESS.getCode());
        packet.setData(mapData);
 
        WebSocketServer.sendByPocket(packet);
    }
 
}