package com.sandu.ximon.admin.service;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.db.PageResult;
|
import com.alibaba.fastjson.JSON;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.github.pagehelper.PageHelper;
|
import com.sandu.common.domain.CommonPage;
|
import com.sandu.common.execption.BusinessException;
|
import com.sandu.common.object.BaseConditionVO;
|
import com.sandu.common.service.impl.BaseServiceImpl;
|
import com.sandu.common.util.SpringContextHolder;
|
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5LightHeartbeatReportInnerFrame;
|
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A7PlcHeartbeatReportInnerFrame;
|
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5LightDataEnum;
|
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A7PlcDataEnum;
|
import com.sandu.ximon.admin.utils.RedisUtils;
|
import com.sandu.ximon.dao.bo.LightReportDataBo;
|
import com.sandu.ximon.dao.bo.PlcReportDataBo;
|
import com.sandu.ximon.dao.domain.*;
|
import com.sandu.ximon.dao.enums.OrderByEnums;
|
import com.sandu.ximon.dao.mapper.PlcReportDataMapper;
|
import lombok.AllArgsConstructor;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.stereotype.Service;
|
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* PLC上报数据表(PlcReportData)表服务接口
|
*
|
* @author van
|
* @since 2022-12-16 10:54:58
|
*/
|
@Service
|
@AllArgsConstructor
|
public class PlcReportDataService extends BaseServiceImpl<PlcReportDataMapper,PlcReportData> {
|
|
private final PlcReportDataMapper plcReportDataMapper;
|
|
// PageResult getPage(Integer currentPage, Integer size, PlcReportData plcReportData);
|
|
|
/**
|
* 保存上报的PLC心跳数据
|
*
|
* @return 是否成功
|
*/
|
public boolean saveReportData(String deviceName, A7PlcHeartbeatReportInnerFrame.HeartBeatDataPackage heartBeatDataPackage) {
|
|
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss"));
|
|
PlcReportData plcReportData = new PlcReportData();
|
|
if (plcReportData == null) {
|
plcReportData = new PlcReportData();
|
plcReportData.setCreateTime(format);
|
}
|
plcReportData.setCreateTime(format);
|
BeanUtils.copyProperties(heartBeatDataPackage, plcReportData);
|
plcReportData.setDeviceCode(deviceName);
|
|
plcReportData.setUpdateTime(format);
|
//PLC数据保存到缓存里
|
RedisUtils.getBean().set(A7PlcDataEnum.PLC_HEART_BEAT.getCode() + deviceName, plcReportData);
|
|
return save(plcReportData);
|
|
|
}
|
|
List<PlcReportData> getNewestReportByDeviceCode(List<String> deviceCodeList){
|
return baseMapper.getNewestReportByDeviceCode(deviceCodeList);
|
}
|
/**
|
* 获取上报数据
|
*
|
* @param keyword 关键词
|
* @param deviceCode 设备码
|
*/
|
public CommonPage listReportData(int pageNo, int pageSize, String keyword, String deviceCode, Integer order, Integer seq) {
|
|
List<PlcReportDataBo> plcReportDataBos = new ArrayList<>(pageSize);
|
//排序字段
|
String orderByResult = "t1.plc_create_time";
|
//正序、倒叙
|
String orderBySeq = OrderByEnums.ASC.getCode();
|
if (order != null) {
|
switch (order) {
|
case 1:
|
orderByResult = OrderByEnums.PLC_DATA_CREATE_TIME.getCode();
|
break;
|
default:
|
}
|
}
|
if (seq != null) {
|
switch (seq) {
|
case 1:
|
orderBySeq = " ASC";
|
break;
|
case 2:
|
orderBySeq = " DESC";
|
break;
|
default:
|
break;
|
}
|
}
|
//排序方式
|
String orderBy = orderByResult + " " + orderBySeq;
|
CommonPage<String> stringCommonPage = SpringContextHolder.getBean(PlcService.class).listDeviceCode(pageNo, pageSize, keyword, deviceCode, orderBy);
|
List<String> macList = stringCommonPage.getList();
|
if (CollUtil.isEmpty(macList)) {
|
return new CommonPage();
|
}
|
for (String macCode : macList) {
|
try {
|
PlcReportData plcReportData
|
= JSON.parseObject(RedisUtils.getBean().get(A7PlcDataEnum.PLC_HEART_BEAT.getCode() + macCode), PlcReportData.class);
|
|
PlcReportDataBo plcReportDataBo = new PlcReportDataBo();
|
if (plcReportData != null) {
|
BeanUtils.copyProperties(plcReportData, plcReportDataBo);
|
}
|
plcReportDataBo.setDeviceCode(macCode);
|
Pole pole = SpringContextHolder.getBean(PoleService.class).getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getDeviceCode, macCode));
|
if (pole != null) {
|
plcReportDataBo.setPoleName(pole.getPoleName());
|
}
|
plcReportDataBos.add(plcReportDataBo);
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
CommonPage commonPage = CommonPage.restPage(plcReportDataBos);
|
commonPage.setTotal(stringCommonPage.getTotal());
|
commonPage.setTotalPage(stringCommonPage.getTotalPage());
|
commonPage.setPageNum(pageNo);
|
|
return commonPage;
|
}
|
|
public List<PlcReportData> getReportDataList(BaseConditionVO conditionVO, String macCode) {
|
if (macCode.isEmpty()) {
|
throw new BusinessException("mac不能为空");
|
}
|
|
Plc plc = SpringContextHolder.getBean(PlcService.class)
|
.getOne(Wrappers.lambdaQuery(Plc.class).eq(Plc::getDeviceCode, macCode));
|
if (plc == null) {
|
throw new BusinessException("系统中不存在该plc");
|
}
|
PageHelper.startPage(conditionVO.getPageNo(), conditionVO.getPageSize());
|
List<PlcReportData> list = list(Wrappers.lambdaQuery(PlcReportData.class).eq(PlcReportData::getDeviceCode, macCode).orderByDesc(PlcReportData::getReportDataId));
|
list.forEach(
|
plcReportData -> {
|
plcReportData.setCreateTime(plcReportData.getCreateTime1().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
plcReportData.setUpdateTime(plcReportData.getUpdateTime1().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
}
|
);
|
return list;
|
}
|
}
|