package org.jeecg.modules.mdc.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import org.apache.commons.lang.StringUtils;
|
import org.jeecg.common.system.vo.DictModel;
|
import org.jeecg.modules.mdc.dto.MdcEquipmentStatisticalDto;
|
import org.jeecg.modules.mdc.entity.*;
|
import org.jeecg.modules.mdc.mapper.MdcEquipmentStatisticalInfoMapper;
|
import org.jeecg.modules.mdc.service.*;
|
import org.jeecg.modules.mdc.util.DateUtils;
|
import org.jeecg.modules.system.service.ISysDictService;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.*;
|
|
/**
|
* @Description: 设备单日运行数据表
|
* @Author: lius
|
* @Date: 2023-04-14
|
* @Version: V1.0
|
*/
|
@Service
|
public class MdcEquipmentStatisticalInfoServiceImpl extends ServiceImpl<MdcEquipmentStatisticalInfoMapper, MdcEquipmentStatisticalInfo> implements IMdcEquipmentStatisticalInfoService {
|
|
@Resource
|
private IEquipmentService equipmentService;
|
|
@Resource
|
private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService;
|
|
@Resource
|
private IMdcSystemParametersService mdcSystemParametersService;
|
|
@Resource
|
private ISysDictService sysDictService;
|
|
@Resource
|
private IEquipmentStatisticalInfoService equipmentStatisticalInfoService;
|
|
/**
|
* 计算设备单日运行数据
|
*/
|
@Override
|
@Transactional(rollbackFor = {Exception.class})
|
public void runningAllEquipmentStatisticalProcess(String dateTime) {
|
if (StringUtils.isNotBlank(dateTime)) {
|
try {
|
Date initDate = DateUtils.toDate(dateTime, "yyyyMMdd");
|
if (initDate != null) {
|
this.remove(new LambdaQueryWrapper<MdcEquipmentStatisticalInfo>().
|
eq(MdcEquipmentStatisticalInfo::getTheDate, dateTime));
|
}
|
} catch (Exception e) {
|
log.error("参数格式不对", e);
|
}
|
}
|
List<Equipment> equipmentList = equipmentService.list();
|
List<MdcEquipmentStatisticalInfo> result = new ArrayList<>();
|
for (Equipment equipment : equipmentList) {
|
List<MdcEquipmentStatisticalInfo> equipmentStatisticalInfoList = equipmentStatisticalProcess(equipment, dateTime);
|
if (equipmentStatisticalInfoList != null && !equipmentStatisticalInfoList.isEmpty()) {
|
result.addAll(equipmentStatisticalInfoList);
|
}
|
}
|
if (!result.isEmpty()) {
|
this.saveBatch(result);
|
List<DictModel> dictList = sysDictService.queryDictItemsByCode("data_synchronization_flag");
|
if (dictList != null && !dictList.isEmpty() && "0".equals(dictList.get(0).getValue())) {
|
List<EquipmentStatisticalInfo> list = this.dataHandle(result);
|
equipmentStatisticalInfoService.saveBatch(list);
|
}
|
}
|
}
|
|
private List<EquipmentStatisticalInfo> dataHandle(List<MdcEquipmentStatisticalInfo> list) {
|
List<EquipmentStatisticalInfo> result = new ArrayList<>();
|
list.forEach(item -> {
|
EquipmentStatisticalInfo equipmentStatisticalInfo = new EquipmentStatisticalInfo();
|
equipmentStatisticalInfo.setEquipment(item.getEquipmentId());
|
equipmentStatisticalInfo.setTheDate(item.getTheDate());
|
equipmentStatisticalInfo.setClosedLong(item.getCloseLong());
|
equipmentStatisticalInfo.setOpeningLong(item.getOpenLong());
|
equipmentStatisticalInfo.setErroringLong(item.getErrorLong());
|
equipmentStatisticalInfo.setProcessingLong(item.getProcessLong());
|
equipmentStatisticalInfo.setWaitingLong(item.getWaitLong());
|
result.add(equipmentStatisticalInfo);
|
});
|
return result;
|
}
|
|
private List<MdcEquipmentStatisticalInfo> equipmentStatisticalProcess(Equipment equipment, String dateTime) {
|
Date initDate = null;
|
//取最后的统计数据
|
if (StringUtils.isBlank(dateTime)) {
|
MdcEquipmentStatisticalInfo nearestDate = this.baseMapper.getMaxStaticsData(equipment.getEquipmentid());
|
if (nearestDate != null) {
|
initDate = DateUtils.toDate(nearestDate.getTheDate(), "yyyyMMdd");
|
initDate = DateUtils.plusTime(initDate, 1);
|
} else {
|
//初次取值 取最早时间
|
MdcEquipmentRunningSection equipmentRunningSection = mdcEquipmentRunningSectionService.getFirstData(equipment.getEquipmentid());
|
if (equipmentRunningSection != null) {
|
initDate = equipmentRunningSection.getStartTime();
|
}
|
}
|
} else {
|
try {
|
initDate = DateUtils.toDate(dateTime, "yyyyMMdd");
|
initDate = DateUtils.plusTime(initDate, 0);
|
} catch (Exception e) {
|
log.error("参数格式不对", null);
|
return null;
|
}
|
}
|
if (initDate == null) {
|
return null;
|
}
|
MdcSystemParameters mdcSystemParameters = mdcSystemParametersService.getOne(new LambdaQueryWrapper<MdcSystemParameters>().eq(MdcSystemParameters::getCode, "equip_log_statis_time"));
|
if (mdcSystemParameters == null) {
|
log.error("mdc_system_parameters 表中数据缺失");
|
return null;
|
} else {
|
String planTime = mdcSystemParameters.getValue();
|
Date startDate = DateUtils.setTimeForDay(initDate, planTime);
|
//问题点 判定当前时间的小时分钟是否超过系统规定时间
|
Date now = new Date();
|
Date endDate;
|
if (StringUtils.isBlank(dateTime)) {
|
Date planDate = DateUtils.setTimeForDay(DateUtils.plusTime(now, 0), planTime);
|
long second = DateUtils.differentSecond(now, planDate);
|
if (second <= 0) {
|
endDate = planDate;
|
} else {
|
endDate = DateUtils.setTimeForDay(DateUtils.plusTime(now, -1), planTime);
|
}
|
List<MdcEquipmentRunningSection> equipmentRunningSectionList = mdcEquipmentRunningSectionService.listForEquipmentStatisticalInfo(equipment.getEquipmentid(), startDate, endDate);
|
return this.statisticsData(equipmentRunningSectionList, planTime, startDate, endDate, equipment);
|
} else {
|
//待验证
|
Date end = DateUtils.plusTime(initDate, 1);
|
endDate = DateUtils.setTimeForDay(end, planTime);
|
List<MdcEquipmentRunningSection> equipmentRunningSectionList = mdcEquipmentRunningSectionService.listForEquipmentStatisticalInfo(equipment.getEquipmentid(), startDate, endDate);
|
return this.statisticsData(equipmentRunningSectionList, planTime, startDate,
|
DateUtils.setTimeForDay(DateUtils.plusTime(initDate, 0), planTime), equipment);
|
}
|
}
|
}
|
|
/**
|
* 时间切割
|
*/
|
private List<MdcEquipmentStatisticalInfo> statisticsData(List<MdcEquipmentRunningSection> equipmentRunningSectionList, String planTime, Date startDate, Date endDate, Equipment equipment) {
|
Map<String, List<Map<String, Long>>> map = new HashMap<>();
|
for (MdcEquipmentRunningSection temp : equipmentRunningSectionList) {
|
Date s1 = temp.getStartTime();
|
//s2设置的节点时间
|
Date s2 = DateUtils.setTimeForDay(s1, planTime);
|
Date e1 = temp.getEndTime();
|
//e2 设置的节点时间
|
Date e2 = DateUtils.getNextDay(s2);
|
if (s1.getTime() < s2.getTime()) {
|
if (e1.getTime() < s2.getTime()) {
|
String previousDay = DateUtils.format(DateUtils.getPreviousDay(s1), "yyyyMMdd");
|
List list = map.get(previousDay);
|
if (list == null) {
|
list = new ArrayList();
|
}
|
Map<String, Long> previous = new HashMap<>();
|
previous.put("status", (long) temp.getStatus());
|
previous.put("duration", (e1.getTime() - s1.getTime()) / 1000);
|
list.add(previous);
|
map.put(previousDay, list);
|
} else {
|
String previousDay = DateUtils.format(DateUtils.getPreviousDay(s1), "yyyyMMdd");
|
List list = map.get(previousDay);
|
if (list == null) {
|
list = new ArrayList();
|
}
|
Map<String, Long> previous = new HashMap<>();
|
previous.put("status", (long) temp.getStatus());
|
previous.put("duration", (s2.getTime() - s1.getTime()) / 1000);
|
list.add(previous);
|
map.put(previousDay, list);
|
String day = DateUtils.format(s1, "yyyyMMdd");
|
List list1 = map.get(day);
|
if (list1 == null) {
|
list1 = new ArrayList();
|
}
|
Map<String, Long> now = new HashMap<>();
|
now.put("status", (long) temp.getStatus());
|
now.put("duration", (e1.getTime() - s2.getTime()) / 1000);
|
list1.add(now);
|
map.put(day, list1);
|
}
|
} else {
|
if (e2.getTime() > e1.getTime()) {
|
//今天
|
String nowDay = DateUtils.format(s1, "yyyyMMdd");
|
List list1 = map.get(nowDay);
|
if (list1 == null) {
|
list1 = new ArrayList();
|
}
|
Map<String, Long> now = new HashMap<>();
|
now.put("status", (long) temp.getStatus());
|
now.put("duration", (e1.getTime() - s1.getTime()) / 1000);
|
list1.add(now);
|
map.put(nowDay, list1);
|
} else {
|
// 今天
|
String nowDay = DateUtils.format(s1, "yyyyMMdd");
|
List list1 = map.get(nowDay);
|
if (list1 == null) {
|
list1 = new ArrayList();
|
}
|
Map<String, Long> now = new HashMap<>();
|
now.put("status", (long) temp.getStatus());
|
now.put("duration", (e2.getTime() - s1.getTime()) / 1000);
|
list1.add(now);
|
map.put(nowDay, list1);
|
//明天
|
String nextDay = DateUtils.format(DateUtils.getNextDay(s1), "yyyyMMdd");
|
List list2 = map.get(nextDay);
|
if (list2 == null) {
|
list2 = new ArrayList();
|
}
|
Map<String, Long> next = new HashMap<>();
|
next.put("status", (long) temp.getStatus());
|
next.put("duration", (e1.getTime() - e2.getTime()) / 1000);
|
list2.add(next);
|
map.put(nextDay, list2);
|
}
|
}
|
}
|
int maxday = 0;
|
Date now = new Date();
|
int dateInt1 = Integer.parseInt(DateUtils.format(now, "yyyyMMdd"));
|
int lastDay;
|
Date planDate = DateUtils.setTimeForDay(DateUtils.plusTime(now, 0), planTime);
|
Long second = DateUtils.differentSecond(planDate, now);
|
if (second < 0) {
|
lastDay = Integer.parseInt(DateUtils.format(DateUtils.plusTime(now, -1), "yyyyMMdd"));
|
} else {
|
lastDay = dateInt1;
|
}
|
Set<String> keys = map.keySet();
|
for (String key : keys) {
|
int tmp = Integer.parseInt(key);
|
if (tmp == dateInt1 || tmp == lastDay) {
|
continue;
|
}
|
if (tmp > maxday) {
|
maxday = tmp;
|
}
|
}
|
boolean flag = true;
|
Date date = startDate;
|
List<MdcEquipmentStatisticalInfo> equipmentStatisticalInfos = new ArrayList<>();
|
while (flag) {
|
int dateInt = Integer.parseInt(DateUtils.format(date, "yyyyMMdd"));
|
if (date.getTime() <= endDate.getTime() && dateInt <= maxday) {
|
List<Map<String, Long>> dayList = map.get(String.valueOf(dateInt));
|
MdcEquipmentStatisticalInfo equipmentStatisticalInfo = new MdcEquipmentStatisticalInfo();
|
equipmentStatisticalInfo.setEquipmentId(equipment.getEquipmentid());
|
equipmentStatisticalInfo.setTheDate(String.valueOf(dateInt));
|
long waitingLong = 0L;
|
long processingLong = 0L;
|
long closedLong = 0L;
|
long errorLong = 0L;
|
for (Map<String, Long> n : dayList) {
|
long du = n.get("duration");
|
int status = new Long(n.get("status")).intValue();
|
if (status == 2) {
|
waitingLong += du;
|
}
|
if (status == 3) {
|
processingLong += du;
|
}
|
if (status == 0) {
|
closedLong += du;
|
}
|
if (status == 22) {
|
errorLong += du;
|
}
|
}
|
|
//如待机时间+加工时间+关机时间不等于86400,需将多余时间进行补充
|
long remainingDate = 86400 - waitingLong - processingLong - closedLong;
|
if (remainingDate != 0L) {
|
//如果关机时间大于待机时间+加工时间,将多余时间加至关机时间,否则,加入待机时间
|
if (waitingLong + processingLong < closedLong) {
|
closedLong += remainingDate;
|
}else {
|
waitingLong += remainingDate;
|
}
|
}
|
|
equipmentStatisticalInfo.setWaitLong(new BigDecimal(waitingLong));
|
equipmentStatisticalInfo.setProcessLong(new BigDecimal(processingLong));
|
equipmentStatisticalInfo.setCloseLong(new BigDecimal(closedLong));
|
equipmentStatisticalInfo.setOpenLong(equipmentStatisticalInfo.getWaitLong().add(equipmentStatisticalInfo.getProcessLong()));
|
equipmentStatisticalInfo.setErrorLong(new BigDecimal(errorLong));
|
equipmentStatisticalInfo.setCreateTime(new Date());
|
equipmentStatisticalInfos.add(equipmentStatisticalInfo);
|
} else {
|
flag = false;
|
}
|
date = DateUtils.getNextDay(date);
|
}
|
return equipmentStatisticalInfos;
|
}
|
|
/**
|
* 查询设备运行时间
|
*
|
* @param equipmentId
|
* @param validDate
|
* @return
|
*/
|
@Override
|
public Integer selectProcessLong(String equipmentId, String validDate) {
|
Integer processLong = this.baseMapper.selectProcessLong(equipmentId, validDate);
|
if (processLong == null) {
|
return 0;
|
} else {
|
return Integer.parseInt(new BigDecimal(processLong).divide(new BigDecimal("60"), 0, RoundingMode.HALF_UP).toString());
|
}
|
}
|
|
@Override
|
public MdcEquipmentStatisticalDto findByEquipmentAndMonth(String equipmentId, String date) {
|
return this.baseMapper.findByEquipmentAndMonth(equipmentId, date);
|
}
|
|
@Override
|
public List<MdcEquipmentStatisticalInfo> findByEquipmentAndDate(List<String> equipmentList, String start, String end) {
|
return this.baseMapper.findByEquipmentAndDate(equipmentList, start, end);
|
}
|
|
@Override
|
public List<MdcEquipmentStatisticalInfo> findByEquIdAndDate(String equipmentId, String start, String end) {
|
return this.baseMapper.findByEquIdAndDate(equipmentId, start, end);
|
}
|
|
@Override
|
public MdcEquipmentStatisticalInfo findByEquIdAndMonth(String equipmentId, String month) {
|
return this.baseMapper.findByEquIdAndMonth(equipmentId, month);
|
}
|
|
@Override
|
public List<MdcEquipmentStatisticalInfo> findMdcEquipmentStatisticalInfo(String equipmentId, String start, String end) {
|
return this.baseMapper.findMdcEquipmentStatisticalInfo(equipmentId, start.replaceAll("-", ""), end.replaceAll("-", ""));
|
}
|
}
|