kaiyu
2021-12-28 4c890bb6132e5971d9ec8bdadf66f985f9ab92e9
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.Device;
import com.moral.api.entity.DeviceAdjustValue;
import com.moral.api.entity.Sensor;
import com.moral.api.entity.SpecialDevice;
import com.moral.api.mapper.DeviceAdjustValueMapper;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.SensorMapper;
import com.moral.api.mapper.SpecialDeviceMapper;
import com.moral.api.service.DeviceAdjustValueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-06-07
 */
@Service
public class DeviceAdjustValueServiceImpl extends ServiceImpl<DeviceAdjustValueMapper, DeviceAdjustValue> implements DeviceAdjustValueService {
 
    @Autowired(required = false)
    private DeviceAdjustValueMapper deviceAdjustValueMapper;
 
    @Autowired(required = false)
    private DeviceMapper deviceMapper;
 
    @Autowired(required = false)
    private SpecialDeviceMapper specialDeviceMapper;
 
    @Autowired(required = false)
    private SensorMapper sensorMapper;
 
    @Autowired
    RedisTemplate redisTemplate;
 
    @Override
    @Transactional
    public Map<String, Object> insertOne(DeviceAdjustValue deviceAdjustValue) {
        Map<String, Object> resultMap = new HashMap<>();
        if (ObjectUtils.isEmpty(deviceAdjustValue.getMac())){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        if (ObjectUtils.isEmpty(deviceAdjustValue.getValue())||deviceAdjustValue.getValue().equals("")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        QueryWrapper<Device> wrapper_device = new QueryWrapper<>();
        wrapper_device.eq("is_delete",Constants.NOT_DELETE);
        wrapper_device.eq("mac",deviceAdjustValue.getMac());
        QueryWrapper<SpecialDevice> wrapper_specialDevice = new QueryWrapper<>();
        wrapper_specialDevice.eq("is_delete",Constants.NOT_DELETE);
        wrapper_specialDevice.eq("mac",deviceAdjustValue.getMac());
        if (deviceMapper.selectCount(wrapper_device)==0 && specialDeviceMapper.selectCount(wrapper_specialDevice)==0){
            resultMap.put("code",ResponseCodeEnum.DEVICE_IS_NULL.getCode());
            resultMap.put("msg",ResponseCodeEnum.DEVICE_IS_NULL.getMsg());
            return resultMap;
        }
        QueryWrapper<Sensor> wrapper_sensor = new QueryWrapper<>();
        wrapper_sensor.eq("is_delete",Constants.NOT_DELETE);
        wrapper_sensor.eq("code",deviceAdjustValue.getSensorCode());
        if (sensorMapper.selectCount(wrapper_sensor)==0){
            resultMap.put("code",ResponseCodeEnum.SENSOR_IS_NOT_EXIST.getCode());
            resultMap.put("msg",ResponseCodeEnum.SENSOR_IS_NOT_EXIST.getMsg());
            return resultMap;
        }
        deviceAdjustValueMapper.insert(deviceAdjustValue);
        //操作插入日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String content = "添加了"+deviceAdjustValue.getMac()+"设备"+deviceAdjustValue.getSensorCode()+"传感器"+deviceAdjustValue.getStartTime()+"到"+deviceAdjustValue.getEndTime()+"的校准信息:"+";";
        LogUtils.saveOperationForManage(request,content,Constants.INSERT_OPERATE_TYPE);
        //刷新缓存
        refreshCache(deviceAdjustValue.getMac());
        resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
 
    @Override
    public Map<String, Object> getDataByCondition(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        int current = Integer.parseInt(map.get("current").toString());
        int size = Integer.parseInt(map.get("size").toString());
        Page<DeviceAdjustValue> page = new Page<>(current,size);
        QueryWrapper<DeviceAdjustValue> wrapper_Condition = new QueryWrapper<>();
        wrapper_Condition.eq("is_delete",Constants.NOT_DELETE);
        if (!ObjectUtils.isEmpty(map.get("mac"))){
            wrapper_Condition.eq("mac",map.get("mac"));
        }
        if (!ObjectUtils.isEmpty(map.get("sensor_code"))){
            wrapper_Condition.eq("sensor_code",map.get("sensor_code"));
        }
        if (!ObjectUtils.isEmpty(map.get("orderType"))){
            String orderType = map.get("orderType").toString();
            if (orderType.equals(Constants.ORDER_ASC)){
                wrapper_Condition.orderByAsc("start_time");
            }else {
                wrapper_Condition.orderByDesc("start_time");
            }
        }
        Page resultPage = deviceAdjustValueMapper.selectPage(page,wrapper_Condition);
        List<DeviceAdjustValue> deviceAdjustValues = resultPage.getRecords();
        List<Map<String,Object>> deviceAdjustValueList = new ArrayList<>();
        for (DeviceAdjustValue deviceAdjustValue:deviceAdjustValues) {
            Map deviceAdjustValueMap = JSON.parseObject(JSON.toJSONString(deviceAdjustValue),Map.class);
            Time startTime = new Time(Long.valueOf(Long.parseLong(deviceAdjustValueMap.get("startTime").toString())));
            deviceAdjustValueMap.put("startTime",new Time(startTime.getTime()));
            Time endTime = new Time(Long.valueOf(Long.parseLong(deviceAdjustValueMap.get("endTime").toString())));
            deviceAdjustValueMap.put("endTime",new Time(endTime.getTime()));
            deviceAdjustValueList.add(deviceAdjustValueMap);
        }
        resultMap.put("deviceAdjustValues",deviceAdjustValueList);
        int totalNumber = deviceAdjustValueMapper.selectCount(wrapper_Condition);
        resultMap.put("totalNumber",totalNumber);
        resultMap.put("current",current);
        resultMap.put("size",size);
        int totalPageNumber = totalNumber/size;
        if(totalNumber%size != 0){
            totalPageNumber += 1;
        }
        resultMap.put("totalPageNumber",totalPageNumber);
        return resultMap;
    }
 
    @Override
    public Map<String, Object> updateOne(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        if (ObjectUtils.isEmpty(map.get("id"))||map.get("id").equals("")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        DeviceAdjustValue deviceAdjustValueOld = deviceAdjustValueMapper.selectById(Integer.parseInt(map.get("id").toString()));
        deviceAdjustValueMapper.updateDeviceAdjustValue(map);
        //操作插入日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String content = "修改了"+deviceAdjustValueOld.getMac()+"设备"+deviceAdjustValueOld.getSensorCode()+"传感器的校准信息:"+";";
        for (Object key:map.keySet()) {
            if (key.toString().equals("start_time")&&map.get(key)!=null){
                content = content+"开始时间:"+deviceAdjustValueOld.getStartTime()+"->"+map.get(key)+";";
            }
            if (key.toString().equals("end_time")&&map.get(key)!=null){
                content = content+"结束时间:"+deviceAdjustValueOld.getEndTime()+"->"+map.get(key)+";";
            }
            if (key.toString().equals("value")&&map.get(key)!=null){
                content = content+"校准公式:"+deviceAdjustValueOld.getValue()+"->"+map.get(key)+";";
            }
        }
        LogUtils.saveOperationForManage(request,content,Constants.UPDATE_OPERATE_TYPE);
        //刷新缓存
        refreshCache(deviceAdjustValueOld.getMac());
        resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
 
    @Override
    public Map<String, Object> deleteOne(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        if (ObjectUtils.isEmpty(map.get("id"))||map.get("id").equals("")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        QueryWrapper<DeviceAdjustValue> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",Constants.NOT_DELETE);
        wrapper.eq("id",map.get("id"));
        DeviceAdjustValue deviceAdjustValue = deviceAdjustValueMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(deviceAdjustValue)){
            resultMap.put("code",ResponseCodeEnum.TARGET_IS_NULL.getCode());
            resultMap.put("msg",ResponseCodeEnum.TARGET_IS_NULL.getMsg());
            return resultMap;
        }
        Map<String,Object> deleteMap = new HashMap<>();
        deleteMap.put("id",map.get("id"));
        deleteMap.put("is_delete",Constants.DELETE);
        deviceAdjustValueMapper.updateDeviceAdjustValue(deleteMap);
        //操作插入日志
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String content = "删除了"+deviceAdjustValue.getMac()+"设备"+deviceAdjustValue.getSensorCode()+"传感器"+deviceAdjustValue.getStartTime()+"到"+deviceAdjustValue.getEndTime()+"的校准信息:"+";";
        LogUtils.saveOperationForManage(request,content,Constants.DELETE_OPERATE_TYPE);
        //刷新缓存
        refreshCache(deviceAdjustValue.getMac());
        resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
 
    @Override
    public Map<String, Object> getTimeSlot(Map map) {
        Map<String,Object> resultMap = new HashMap<>();
        if (ObjectUtils.isEmpty(map.get("sensor_code"))||map.get("sensor_code").equals("")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        if (ObjectUtils.isEmpty(map.get("mac"))||map.get("mac").equals("")){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        QueryWrapper<DeviceAdjustValue> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",Constants.NOT_DELETE);
        wrapper.eq("sensor_code",map.get("sensor_code"));
        wrapper.eq("mac",map.get("mac"));
        List<DeviceAdjustValue> deviceAdjustValues = deviceAdjustValueMapper.selectList(wrapper);
        List<Map<String,Object>> timeSlotList = new ArrayList<>();
        if (deviceAdjustValues.size()==0){
            resultMap.put("timeSlotList",timeSlotList);
            return resultMap;
        }
        for (DeviceAdjustValue deviceAdjustValue:deviceAdjustValues) {
            Map timeSlotMap = new HashMap();
            timeSlotMap.put("id",deviceAdjustValue.getId());
            timeSlotMap.put("startTime",deviceAdjustValue.getStartTime());
            timeSlotMap.put("endTime",deviceAdjustValue.getEndTime());
            timeSlotList.add(timeSlotMap);
        }
        resultMap.put("timeSlotList",timeSlotList);
        return resultMap;
    }
 
    @Override
    public Map<String, Object> refreshRedis() {
        Map<String,Object> resultMap = new HashMap<>();
        QueryWrapper<DeviceAdjustValue> wrapper_mac = new QueryWrapper<>();
        wrapper_mac.eq("is_delete",Constants.NOT_DELETE);
        wrapper_mac.select("DISTINCT mac");
        List<DeviceAdjustValue> deviceAdjustValues = deviceAdjustValueMapper.selectList(wrapper_mac);
        for (DeviceAdjustValue deviceAdjustValue:deviceAdjustValues) {
            //刷新缓存
            refreshCache(deviceAdjustValue.getMac());
        }
        resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
 
    public void refreshRedisAll(){
        refreshRedis();
    }
 
    private Map<String,Object> getDeviceAdjustValueFromDB(String mac){
        QueryWrapper<DeviceAdjustValue> wapper_redis = new QueryWrapper<>();
        wapper_redis.eq("mac",mac);
        wapper_redis.eq("is_delete",Constants.NOT_DELETE);
        List<DeviceAdjustValue> deviceAdjustValueList = deviceAdjustValueMapper.selectList(wapper_redis);
        if (deviceAdjustValueList.size()==0){
            return null;
        }
        Map<String, Object> deviceAdjustValueMap = new HashMap<>();
        for (DeviceAdjustValue deviceAdjustValue:deviceAdjustValueList) {
            String sensor_code = deviceAdjustValue.getSensorCode();
            List<DeviceAdjustValue> timeSlotValueList = new ArrayList<>();
            for (DeviceAdjustValue deviceAdjustValueIn:deviceAdjustValueList) {
                if (deviceAdjustValueIn.getSensorCode().equals(sensor_code)){
                    Map<String, Object> timeSlot_value = new HashMap<>();
                    timeSlot_value.put("start_time",deviceAdjustValue.getStartTime());
                    timeSlot_value.put("end_time",deviceAdjustValue.getEndTime());
                    timeSlot_value.put("value",deviceAdjustValue.getValue());
                    timeSlotValueList.add(deviceAdjustValueIn);
                }
            }
            deviceAdjustValueMap.put(sensor_code,timeSlotValueList);
        }
        return deviceAdjustValueMap;
    }
 
    private void refreshCache(String mac){
        Map<String, Object> deviceAdjustValueMap = getDeviceAdjustValueFromDB(mac);
        refreshCache(deviceAdjustValueMap,mac);
    }
 
    private void refreshCache(Map<String,Object> deviceAdjustValueMap,String mac){
        //删除缓存
        redisTemplate.delete("adjust_"+mac);
        //添加缓存
        if (!ObjectUtils.isEmpty(deviceAdjustValueMap)){
            redisTemplate.opsForHash().putAll("adjust_"+mac,deviceAdjustValueMap);
        }
    }
 
}