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);
|
}
|
}
|