2021与蓝度共同重构项目,服务端
zhanzhiqin
2021-12-23 d90445938cc9472b099bd9d8f437c6d6863e1c1e
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
package com.sandu.ximon.admin.service;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.redis.RedisService;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.dto.LightDataDto;
import com.sandu.ximon.admin.param.LightRemarkParam;
import com.sandu.ximon.admin.redis.LightKey;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.dao.bo.LightBo;
import com.sandu.ximon.dao.domain.Light;
import com.sandu.ximon.dao.domain.LightReportData;
import com.sandu.ximon.dao.mapper.LightMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author chenjiantian
 * @date 2021/12/13 16:00
 * 灯设备service
 */
@Service
@Slf4j
@AllArgsConstructor
public class LightService extends BaseServiceImpl<LightMapper, Light> {
 
    private final RedisService redisService;
    private final LightReportDataService lightReportDataService;
 
    /**
     * 录入当前设备码的路灯数据
     * @param deviceName mac
     */
    public void saveLight(String deviceName) {
        Boolean hasKey = redisService.hasKey(LightKey.REPORT_MAC.key(deviceName));
        if(!hasKey){
            int count = count(Wrappers.lambdaQuery(Light.class).eq(Light::getDeviceCode, deviceName));
            log.info("redis查不到路灯数据{}={}",count,deviceName);
            // 当前路灯表没有录入设备吗
            if(count == 0){
                Light light = new Light();
                light.setDeviceCode(deviceName);
                save(light);
            }
            redisService.set(LightKey.REPORT_MAC.key(deviceName),1,LightKey.REPORT_MAC.expireSeconds());
        }
    }
 
    /**
     * 获取路灯列表
     *
     * @return 返回组合数据dto
     */
    public List<LightBo> listLight(int pageNo, int pageSize, String keyword) {
        Long clientId = SecurityUtils.getClientId();
 
        PageHelper.startPage(pageNo, pageSize);
 
        List<LightBo> listLight = baseMapper.listLight(clientId,keyword);
 
        // 获取最近的上报时间
        List<String> deviceCodeList = listLight.stream().map(Light::getDeviceCode).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(deviceCodeList)){
            List<LightReportData> reportDataList =  lightReportDataService.getNewestReportByDeviceCode(deviceCodeList);
            for (LightBo lightBo : listLight) {
                for (LightReportData lightReportData : reportDataList) {
                    if(StrUtil.equals(lightBo.getDeviceCode(),lightReportData.getDeviceCode())){
                        lightBo.setReportTime(lightReportData.getCreateTime());
                        break;
                    }
                }
            }
        }
 
        return listLight;
    }
 
    public boolean addRemark(LightRemarkParam param) {
        Light light = getById(param.getLightId());
        if(light == null){
            throw new BusinessException("找不到路灯");
        }
        Light update = new Light();
        update.setLightId(param.getLightId());
        update.setRemark(param.getRemark());
        return updateById(update);
    }
}