package com.sandu.ximon.admin.service;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.util.RandomUtil;
|
import com.alibaba.excel.EasyExcel;
|
import com.alibaba.excel.ExcelWriter;
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
import com.alibaba.fastjson.JSON;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.github.pagehelper.PageHelper;
|
import com.sandu.common.domain.CommonPage;
|
import com.sandu.common.execption.BusinessException;
|
import com.sandu.common.file.FileUploadDto;
|
import com.sandu.common.file.config.FileProperties;
|
import com.sandu.common.file.impl.AliOssFileServiceImpl;
|
import com.sandu.common.service.impl.BaseServiceImpl;
|
import com.sandu.common.util.ResUtils;
|
import com.sandu.common.util.SpringContextHolder;
|
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5LightHeartbeatReportInnerFrame;
|
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5LightDataEnum;
|
import com.sandu.ximon.admin.security.SecurityUtils;
|
import com.sandu.ximon.admin.utils.RedisUtils;
|
import com.sandu.ximon.dao.bo.LightReportDataBo;
|
import com.sandu.ximon.dao.domain.LightReportData;
|
import com.sandu.ximon.dao.domain.Pole;
|
import com.sandu.ximon.dao.domain.PoleBinding;
|
import com.sandu.ximon.dao.mapper.LightReportDataMapper;
|
import lombok.AllArgsConstructor;
|
import lombok.SneakyThrows;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.File;
|
import java.sql.Wrapper;
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.*;
|
|
/**
|
* @author chenjiantian
|
* @date 2021/12/13 16:00
|
* 灯上报数据 service
|
*/
|
@Service
|
@AllArgsConstructor
|
public class LightReportDataService extends BaseServiceImpl<LightReportDataMapper, LightReportData> {
|
|
private final FileProperties properties;
|
private final AliOssFileServiceImpl fileService;
|
|
/**
|
* 保存上报的灯心跳数据
|
*
|
* @return 是否成功
|
*/
|
public boolean saveReportData(String deviceName, A5LightHeartbeatReportInnerFrame.HeartBeatDataPackage heartBeatDataPackage) {
|
// LightReportData lightReportData = RedisUtils.getBean().get(A5LightDataEnum.LIGHT_HEART_BEAT.getCode() + deviceName, LightReportData.class);
|
|
|
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss"));
|
LightReportData lightReportData = new LightReportData();
|
|
if (lightReportData == null) {
|
lightReportData = new LightReportData();
|
lightReportData.setCreateTime(format);
|
}
|
//todo
|
lightReportData.setCreateTime(format);
|
BeanUtils.copyProperties(heartBeatDataPackage, lightReportData);
|
lightReportData.setDeviceCode(deviceName);
|
|
lightReportData.setUpdateTime(format);
|
//单灯数据保存到缓存里
|
RedisUtils.getBean().set(A5LightDataEnum.LIGHT_HEART_BEAT.getCode() + deviceName, lightReportData);
|
|
return save(lightReportData);
|
}
|
|
/**
|
* 获取指定设备码最新的一天上报数据
|
*
|
* @param deviceCodeList 设备码列表
|
* @return 上报数据
|
*/
|
public List<LightReportData> getNewestReportByDeviceCode(List<String> deviceCodeList) {
|
return baseMapper.getNewestReportByDeviceCode(deviceCodeList);
|
}
|
|
/**
|
* 获取上报数据
|
*
|
* @param keyword 关键词
|
* @param deviceCode 设备码
|
*/
|
public CommonPage listReportData(int pageNo, int pageSize, String keyword, String deviceCode) {
|
|
List<LightReportDataBo> lightReportDataBos = new ArrayList<>(pageSize);
|
|
CommonPage<String> stringCommonPage = SpringContextHolder.getBean(LightService.class).listDeviceCode(pageNo, pageSize, keyword, deviceCode);
|
List<String> macList = stringCommonPage.getList();
|
if (CollUtil.isEmpty(macList)) {
|
return new CommonPage();
|
}
|
|
for (String macCode : macList) {
|
try {
|
LightReportData lightReportData
|
= JSON.parseObject(RedisUtils.getBean().get(A5LightDataEnum.LIGHT_HEART_BEAT.getCode() + macCode), LightReportData.class);
|
|
LightReportDataBo lightReportDataBo = new LightReportDataBo();
|
if (lightReportData != null) {
|
BeanUtils.copyProperties(lightReportData, lightReportDataBo);
|
}
|
lightReportDataBo.setDeviceCode(macCode);
|
Pole pole = SpringContextHolder.getBean(PoleService.class).getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getDeviceCode, macCode));
|
if (pole != null) {
|
lightReportDataBo.setPoleName(pole.getPoleName());
|
}
|
lightReportDataBos.add(lightReportDataBo);
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
CommonPage commonPage = CommonPage.restPage(lightReportDataBos);
|
commonPage.setTotal(stringCommonPage.getTotal());
|
commonPage.setTotalPage(stringCommonPage.getTotalPage());
|
|
return commonPage;
|
}
|
|
@SneakyThrows
|
public String exportList(int pageNo, int pageSize, String keyword, String deviceCode) {
|
|
if (SecurityUtils.getClientId() != null) {
|
PoleBinding one = SpringContextHolder.getBean(PoleBindingService.class).getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, deviceCode)
|
.eq(PoleBinding::getDeviceType, 0));
|
if (one == null) {
|
throw new BusinessException("未找到绑定关系!");
|
} else {
|
Pole pole = SpringContextHolder.getBean(PoleService.class).getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getUserId, SecurityUtils.getUserId()).or(w -> {
|
w.eq(Pole::getClientId, SecurityUtils.getUserId());
|
}));
|
if (pole == null) {
|
throw new BusinessException("绑定关系不正确!");
|
}
|
}
|
}
|
|
PageHelper.startPage(pageNo, pageSize);
|
List<LightReportDataBo> list = baseMapper.listReportData(keyword, deviceCode);
|
|
File file = new File("./" + RandomUtil.randomString(12) + ".xlsx");
|
// File file = new File(properties.getUploadRootPath() + "export" + File.separator + RandomUtil.randomString(12) + ".xlsx");
|
// ClassPathResource cpr = new ClassPathResource("public/exportOrder.xlsx");
|
// FileUtil.mkParentDirs(file);
|
//
|
// ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(cpr.getInputStream()).build();
|
// WriteSheet writeSheet = EasyExcel.writerSheet().build();
|
// excelWriter.fill(list, writeSheet);
|
// excelWriter.finish();
|
|
EasyExcel.write(file, LightReportDataBo.class).sheet("模版").doWrite(list);
|
|
FileUploadDto fileUploadDto = fileService.uploadFile(file);
|
// String url = file.getPath().replace(properties.getUploadRootPath(), "");
|
String url = fileUploadDto.getFileUrl();
|
file.delete();
|
return url;
|
|
// return list;
|
}
|
|
public List<LightReportDataBo> reportDataList(List<String> deviceCodeList, DateTime startDate, DateTime endDate) {
|
return baseMapper.reportDataList(deviceCodeList, startDate, endDate);
|
}
|
}
|