package com.iailab.module.data.indexdata.common;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.iailab.dto.*;
|
import com.iailab.module.data.common.enums.CommonConstant;
|
import com.iailab.api.IFeignProddispApi;
|
import com.iailab.framework.common.util.date.DateUtils;
|
import com.iailab.module.data.point.dto.DaPointDTO;
|
import com.iailab.module.data.point.service.DaPointService;
|
import com.iailab.module.data.indexdata.entity.IndexDataItemEntity;
|
import com.iailab.module.data.indexdata.entity.IndexDataValueEntity;
|
import com.iailab.module.data.indexdata.service.IndexDataItemService;
|
import com.iailab.module.data.indexdata.service.IndexDataValueService;
|
import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
|
import com.iailab.module.data.influxdb.service.InfluxDBService;
|
import com.iailab.module.infra.api.config.ConfigApi;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.joda.time.LocalDate;
|
import javax.annotation.Resource;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.CollectionUtils;
|
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author PanZhibao
|
* @Description
|
* @createTime 2022年09月11日 14:11:00
|
*/
|
@Slf4j
|
@Component
|
public class IndexDataUtils {
|
|
private BigDecimal hourMins = new BigDecimal("60");
|
|
private String DATA_TYPE_BOOLEAN = "boolean";
|
|
private final int granularity = 5;
|
|
@Resource
|
private InfluxDBService influxDBService;
|
|
@Resource
|
private IndexDataValueService indexDataValueService;
|
|
@Resource
|
private IFeignProddispApi feignProddispApi;
|
|
@Resource
|
private ConfigApi configApi;
|
|
@Resource
|
private DaPointService daPointService;
|
|
@Resource
|
private IndexDataItemService indexDataItemService;
|
|
public void doCal(Date startTime, Date endTime, String CLOCK_CODE, String DICT_DC) throws Exception {
|
log.info("startTime=" + DateUtils.format(startTime, DateUtils.FORMAT_YEAR_MONTH_DAY));
|
log.info("endTime=" + DateUtils.format(endTime, DateUtils.FORMAT_YEAR_MONTH_DAY));
|
// 获取数据项
|
List<IndexDataItemEntity> indexDataItemList = indexDataItemService.getListByClockConfig(CLOCK_CODE);
|
if (CollectionUtils.isEmpty(indexDataItemList)) {
|
log.warn("indexDataItemEntityList为空, clockCode=" + CLOCK_CODE);
|
throw new Exception("indexDataItemEntityList为空, clockCode=" + CLOCK_CODE);
|
}
|
indexDataItemList.forEach(dataItem -> {
|
switch (dataItem.getItemType()) {
|
case IndexDataConstant.RUN_TIME:
|
// 运行时长
|
this.createRunTime(CLOCK_CODE, dataItem, "all" ,startTime, endTime);
|
break;
|
case IndexDataConstant.RUN_TIME_ZC:
|
// 运行时长正常
|
this.createRunTime(CLOCK_CODE, dataItem, "zc",startTime, endTime);
|
break;
|
case IndexDataConstant.RUN_TIME_KZ:
|
// 运行时长空载
|
this.createRunTime(CLOCK_CODE, dataItem, "kz",startTime, endTime);
|
break;
|
case IndexDataConstant.POWER_CONSUME:
|
// 电耗
|
this.createPowerConsume(CLOCK_CODE, dataItem, startTime, endTime);
|
break;
|
case IndexDataConstant.WASH_TRANSFER_IN:
|
// 调入量
|
this.createWashTransferIn(CLOCK_CODE, dataItem, startTime, endTime);
|
break;
|
case IndexDataConstant.WASH_TO_WASH:
|
// 入洗量
|
this.createWashToWash(CLOCK_CODE, dataItem, startTime, endTime);
|
break;
|
case IndexDataConstant.WASH_PROD_VOLUME:
|
// 生产量
|
this.createWashProdVolume(CLOCK_CODE, dataItem, startTime, endTime);
|
break;
|
case IndexDataConstant.WAREHOUSE_POSITION:
|
// 仓位
|
this.createWarehousePosition(CLOCK_CODE, dataItem, startTime, endTime);
|
break;
|
default:
|
break;
|
}
|
});
|
|
// 统计库存
|
this.createWarehouseStock(DateUtils.format(startTime), DICT_DC);
|
|
// 统计总电量
|
FeignConsumeTargetDTO dto = new FeignConsumeTargetDTO();
|
Map param = new HashMap();
|
param.put("rq",new LocalDate());
|
param.put("bc",DICT_DC);
|
BigDecimal hdl = feignProddispApi.sumData(param);
|
dto.setRq(DateUtils.format(startTime));
|
dto.setBc(DICT_DC);
|
dto.setXhzb("hdl");
|
dto.setDw("kw.h");
|
dto.setNbdrl(hdl);
|
dto.setNbtzl(new BigDecimal(0));
|
dto.setWbdrl(new BigDecimal(0));
|
dto.setWbtzl(new BigDecimal(0));
|
dto.setCjr(CommonConstant.CJS_SYS);
|
dto.setCjsj(new Date());
|
dto.setXgsj(new Date());
|
feignProddispApi.createConsumeTarget(dto);
|
|
}
|
|
public void doHourVolume(Date startTime, Date endTime, String CLOCK_CODE, String DICT_DC) throws Exception {
|
log.info("startTime=" + DateUtils.format(startTime, DateUtils.FORMAT_YEAR_MONTH_DAY));
|
log.info("endTime=" + DateUtils.format(endTime, DateUtils.FORMAT_YEAR_MONTH_DAY));
|
// 获取数据项
|
List<IndexDataItemEntity> indexDataItemList = indexDataItemService.getListByClockConfig(CLOCK_CODE);
|
if (CollectionUtils.isEmpty(indexDataItemList)) {
|
log.warn("indexDataItemEntityList为空, clockCode=" + CLOCK_CODE);
|
throw new Exception("indexDataItemEntityList为空, clockCode=" + CLOCK_CODE);
|
}
|
indexDataItemList.forEach(dataItem -> {this.createHourVolume(CLOCK_CODE, dataItem, startTime, endTime);});
|
|
}
|
public void createRunTime(String clockCode, IndexDataItemEntity dataItem, String runTimeType,Date startTime, Date endTime) {
|
try{
|
log.info("计算运行时长开始,itemCode=" + dataItem.getPointCode());
|
InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
|
pojo.setPoint(dataItem.getPointCode());
|
pojo.setType(DATA_TYPE_BOOLEAN);
|
List<Map<String, Object>> pointData = influxDBService.queryPointValues(pojo, startTime, endTime);
|
if (CollectionUtils.isEmpty(pointData)) {
|
log.warn("历史值为空,PointCode=" + dataItem.getPointCode());
|
return;
|
}
|
int mins = this.getRunMins(pointData);
|
int times = this.getStartUpTimes(pointData);
|
|
Map<String, BigDecimal> valueMap = new HashMap<>(2);
|
valueMap.put("sumValue", new BigDecimal(mins));
|
valueMap.put("startUpTimes", new BigDecimal(times));
|
IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
|
indexDataValueService.add(indexDataValue);
|
|
FeignRunTimeDTO dto = new FeignRunTimeDTO();
|
dto.setRq(DateUtils.format(startTime));
|
dto.setBc(dataItem.getBc());
|
dto.setScxm(dataItem.getXtCode());
|
dto.setRunTimeType(runTimeType);
|
dto.setXss(Long.valueOf((mins / 60)));
|
dto.setFzs(Long.valueOf((mins % 60)));
|
dto.setZsc(Long.valueOf(mins));
|
dto.setCjr(CommonConstant.CJS_SYS);
|
dto.setCjsj(new Date());
|
dto.setXgsj(new Date());
|
feignProddispApi.createRunTime(dto);
|
log.info("计算运行时长结束,itemCode=" + dataItem.getPointCode());
|
} catch (Exception ex) {
|
log.info("计算运行时长异常,itemCode=" + dataItem.getPointCode());
|
ex.printStackTrace();
|
}
|
}
|
|
public void createConsumeTarget(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
|
try{
|
log.info("计算消耗指标开始,itemCode=" + dataItem.getPointCode());
|
|
// 插入指标数据表
|
Map<String, BigDecimal> valueMap = new HashMap<>(2);
|
valueMap.put("sumValue", new BigDecimal(0));
|
IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
|
indexDataValueService.add(indexDataValue);
|
|
FeignConsumeTargetDTO dto = new FeignConsumeTargetDTO();
|
dto.setRq(DateUtils.format(startTime));
|
dto.setBc(dataItem.getBc());
|
dto.setXhzb(dataItem.getExt1());
|
dto.setDw(dataItem.getUnit());
|
dto.setNbdrl(new BigDecimal(0));
|
dto.setNbtzl(new BigDecimal(0));
|
dto.setWbdrl(new BigDecimal(0));
|
dto.setWbtzl(new BigDecimal(0));
|
dto.setCjr(CommonConstant.CJS_SYS);
|
dto.setCjsj(new Date());
|
dto.setXgsj(new Date());
|
feignProddispApi.createConsumeTarget(dto);
|
log.info("计算消耗指标结束,itemCode=" + dataItem.getPointCode());
|
} catch (Exception ex) {
|
log.info("计算消耗指标异常,itemCode=" + dataItem.getPointCode());
|
ex.printStackTrace();
|
}
|
}
|
|
public void createPowerConsume(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
|
try{
|
DaPointDTO daPoint = daPointService.getByNo(dataItem.getPointCode());
|
|
if (daPoint == null) {
|
log.info("daPoint is null");
|
return;
|
}
|
InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
|
pojo.setPoint(daPoint.getPointNo());
|
pojo.setType(daPoint.getPointType());
|
// 取10min内最大值
|
Object startPointValue = influxDBService.queryPointMaxValue(pojo, new Date(startTime.getTime() - 1000 * 60 * 10));
|
Object endPointValue = influxDBService.queryPointMaxValue(pojo, new Date(endTime.getTime() - 1000 * 60 * 10));
|
BigDecimal startValue = startPointValue == null ? BigDecimal.ZERO : new BigDecimal(startPointValue.toString());
|
BigDecimal endValue = endPointValue == null ? BigDecimal.ZERO : new BigDecimal(endPointValue.toString());
|
BigDecimal value = new BigDecimal(0);
|
if(endValue.compareTo(startValue) == 1){
|
value = endValue.subtract(startValue);
|
}
|
// 插入指标数据表
|
Map<String, BigDecimal> valueMap = new HashMap<>(1);
|
valueMap.put("sumValue", value);
|
IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
|
indexDataValueService.add(indexDataValue);
|
|
String xt = configApi.queryConfigByCode(dataItem.getExt1());
|
// 插入统计表
|
FeignPowerConsumeDTO dto = new FeignPowerConsumeDTO();
|
dto.setRq(DateUtils.format(startTime));
|
dto.setBc(dataItem.getBc());
|
dto.setXt(xt);
|
dto.setNbdrl(value);
|
dto.setNbtzl(new BigDecimal(0));
|
dto.setWbdrl(value);
|
dto.setWbtzl(new BigDecimal(0));
|
dto.setCjr(CommonConstant.CJS_SYS);
|
dto.setCjsj(new Date());
|
dto.setXgsj(new Date());
|
feignProddispApi.createPowerConsume(dto);
|
log.info("计算电耗结束,itemCode=" + dataItem.getPointCode());
|
} catch (Exception ex) {
|
log.info("计算电耗异常,itemCode=" + dataItem.getPointCode());
|
ex.printStackTrace();
|
}
|
}
|
public void createHourVolume(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
|
try{
|
/**
|
* 861 精煤
|
* 862 混煤
|
* 903 矸石
|
* 603ABC 煤泥板数
|
*/
|
log.info("计算小时量开始,itemCode=" + dataItem.getPointCode());
|
List<String> pointNos = new ArrayList<>();
|
pointNos.add(dataItem.getPointCode());
|
if(StringUtils.isNotBlank(dataItem.getExt1())) {
|
pointNos.add(dataItem.getExt1());
|
}
|
if(StringUtils.isNotBlank(dataItem.getExt2())) {
|
pointNos.add(dataItem.getExt2());
|
}
|
if(StringUtils.isNotBlank(dataItem.getExt3())) {
|
pointNos.add(dataItem.getExt3());
|
}
|
if(StringUtils.isNotBlank(dataItem.getExt4())) {
|
pointNos.add(dataItem.getExt4());
|
}
|
if(StringUtils.isNotBlank(dataItem.getExt5())) {
|
pointNos.add(dataItem.getExt5());
|
}
|
List<DaPointDTO> pointList = daPointService.getByNos(pointNos);
|
if (CollectionUtils.isEmpty(pointList)) {
|
log.warn("pointNos查询为空!");
|
return;
|
}
|
List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> {
|
InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
|
pojo.setPoint(item.getPointNo());
|
pojo.setType(item.getDataType());
|
return pojo;
|
}).collect(Collectors.toList());
|
Map<String, Object> data = influxDBService.queryPointsSpread(influxParams, startTime, endTime);
|
log.info("查询完成!");
|
|
log.info("插入统计表");
|
FeignHourVolumeDTO dto = new FeignHourVolumeDTO();
|
dto.setRq(DateUtils.format(startTime));
|
dto.setBc(this.getBcByTime(endTime));
|
dto.setSj(endTime);
|
if (StringUtils.isNotBlank(dataItem.getPointCode()) && data.get(dataItem.getPointCode()) != null) {
|
dto.setRxl(new BigDecimal(data.get(dataItem.getPointCode()).toString()));
|
log.info(" 插入指标数据表");
|
Map<String, BigDecimal> valueMap = new HashMap<>(1);
|
valueMap.put("sumValue", new BigDecimal(data.get(dataItem.getPointCode()).toString()));
|
IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
|
indexDataValueService.add(indexDataValue);
|
}
|
if (StringUtils.isNotBlank(dataItem.getExt1()) && data.get(dataItem.getExt1()) != null) {
|
dto.setJml(new BigDecimal(data.get(dataItem.getExt1()).toString()));
|
}
|
if (StringUtils.isNotBlank(dataItem.getExt2()) && data.get(dataItem.getExt2()) != null) {
|
dto.setHml(new BigDecimal(data.get(dataItem.getExt2()).toString()));
|
}
|
if (StringUtils.isNotBlank(dataItem.getExt3()) && data.get(dataItem.getExt3()) != null) {
|
dto.setGsl(new BigDecimal(data.get(dataItem.getExt3()).toString()));
|
}
|
if (StringUtils.isNotBlank(dataItem.getExt4()) && data.get(dataItem.getExt4()) != null) {
|
dto.setFjbs(new BigDecimal(data.get(dataItem.getExt4()).toString()));
|
}
|
if (StringUtils.isNotBlank(dataItem.getExt5()) && data.get(dataItem.getExt5()) != null) {
|
dto.setMnbs(new BigDecimal(data.get(dataItem.getExt5()).toString()));
|
}
|
dto.setCjr(CommonConstant.CJS_SYS);
|
dto.setCjsj(new Date());
|
dto.setXgsj(new Date());
|
feignProddispApi.createHourVolume(dto);
|
log.info("计算小时量结束,itemCode=" + dataItem.getPointCode());
|
} catch (Exception ex) {
|
log.info("计算小时量异常,itemCode=" + dataItem.getPointCode());
|
ex.printStackTrace();
|
}
|
}
|
|
|
private String getBcByTime(Date dateTime) {
|
String bc = "bb";
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(dateTime);
|
if (calendar.get(Calendar.HOUR_OF_DAY) >= 21 || calendar.get(Calendar.HOUR_OF_DAY) <= 8) {
|
bc = "yb";
|
}
|
return bc;
|
}
|
|
public void createWashTransferIn(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
|
try{
|
log.info("计算调入量开始,itemCode=" + dataItem.getPointCode());
|
// 查询测点里的累计产量
|
DaPointDTO daPoint = daPointService.getByNo(dataItem.getPointCode());
|
|
if (daPoint == null) {
|
log.info("daPoint is null");
|
return;
|
}
|
InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
|
pojo.setPoint(daPoint.getPointNo());
|
pojo.setType(daPoint.getDataType());
|
// 取10min内最大值
|
Object pointValue = influxDBService.queryPointMaxValue(pojo, new Date(endTime.getTime() - 1000 * 60 * 10));
|
BigDecimal sumValue = pointValue == null ? BigDecimal.ZERO : new BigDecimal(pointValue.toString());
|
|
// 插入指标数据表
|
Map<String, BigDecimal> valueMap = new HashMap<>(1);
|
valueMap.put("sumValue", sumValue);
|
IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
|
indexDataValueService.add(indexDataValue);
|
|
String mz = configApi.queryConfigByCode(dataItem.getExt1());
|
// 插入统计表
|
FeignWashTransferInDTO dto = new FeignWashTransferInDTO();
|
dto.setRq(DateUtils.format(startTime));
|
dto.setBc(dataItem.getBc());
|
dto.setDrly(mz);
|
dto.setNbdrl(sumValue);
|
dto.setNbtzl(new BigDecimal(0));
|
dto.setWbdrl(sumValue);
|
dto.setWbtzl(new BigDecimal(0));
|
dto.setCjr(CommonConstant.CJS_SYS);
|
dto.setCjsj(new Date());
|
dto.setXgsj(new Date());
|
feignProddispApi.createWashTransferIn(dto);
|
log.info("计算调入量结束,itemCode=" + dataItem.getPointCode());
|
} catch (Exception ex) {
|
log.info("计算调入量异常,itemCode=" + dataItem.getPointCode());
|
ex.printStackTrace();
|
}
|
}
|
|
public void createWashToWash(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
|
try{
|
log.info("计算入洗量开始,itemCode=" + dataItem.getPointCode());
|
// 查询测点里的累计产量
|
DaPointDTO daPoint = daPointService.getByNo(dataItem.getPointCode());
|
if (daPoint == null) {
|
log.info("daPoint is null");
|
return;
|
}
|
InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
|
pojo.setPoint(daPoint.getPointNo());
|
pojo.setType(daPoint.getDataType());
|
// 取10min内最大值
|
Object pointValue = influxDBService.queryPointMaxValue(pojo, new Date(endTime.getTime() - 1000 * 60 * 10));
|
BigDecimal sumValue = pointValue == null ? BigDecimal.ZERO : new BigDecimal(pointValue.toString());
|
log.info("插入指标数据表");
|
Map<String, BigDecimal> valueMap = new HashMap<>(1);
|
log.info("sumValue"+sumValue);
|
valueMap.put("sumValue", sumValue);
|
IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
|
indexDataValueService.add(indexDataValue);
|
List<FeignMcMzDTO> mzList = feignProddispApi.selectMcMzByMc(dataItem.getExt1());
|
log.info("dataItem.getExt1()"+dataItem.getExt1());
|
|
//获取总入洗量
|
pojo.setPoint(dataItem.getExt2());
|
pojo.setType("float");
|
Object rxlPointValue = influxDBService.queryPointMaxValue(pojo, new Date(endTime.getTime() - 1000 * 60 * 10));
|
BigDecimal sumRXL = rxlPointValue == null ? BigDecimal.ZERO : new BigDecimal(rxlPointValue.toString());
|
|
mzList.stream().forEach(
|
mz->{
|
log.info("插入统计表,mc:"+mz.getMc()+";mz:"+mz.getMz());
|
FeignWashToWashDTO dto = new FeignWashToWashDTO();
|
dto.setRq(DateUtils.format(startTime));
|
dto.setBc(dataItem.getBc());
|
dto.setXxjm(mz.getMz());
|
dto.setNbdrl(sumValue.multiply(BigDecimal.valueOf(mz.getProportion())).divide(BigDecimal.valueOf(mz.getDen())) );
|
dto.setRxbl(dto.getNbdrl().divide(sumRXL, 2, RoundingMode.HALF_UP));
|
dto.setRxly(dataItem.getExt1());
|
dto.setNbtzl(new BigDecimal(0));
|
dto.setWbdrl(dto.getNbdrl());
|
dto.setWbtzl(new BigDecimal(0));
|
dto.setCjr(CommonConstant.CJS_SYS);
|
dto.setCjsj(new Date());
|
dto.setXgsj(new Date());
|
feignProddispApi.createWashToWash(dto);
|
}
|
);
|
|
|
log.info("计算入洗量结束,itemCode=" + dataItem.getPointCode());
|
} catch (Exception ex) {
|
log.info("计算入洗量异常,itemCode=" + dataItem.getPointCode());
|
ex.printStackTrace();
|
}
|
}
|
|
public void createWashProdVolume(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
|
try{
|
log.info("计算生产量开始,itemCode=" + dataItem.getPointCode());
|
// 查询测点里的累计产量
|
DaPointDTO daPoint = daPointService.getByNo(dataItem.getPointCode());
|
if (daPoint == null) {
|
log.info("daPoint is null");
|
return;
|
}
|
InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
|
pojo.setPoint(daPoint.getPointNo());
|
pojo.setType(daPoint.getDataType());
|
// 取10min内最大值
|
Object pointValue = influxDBService.queryPointMaxValue(pojo, new Date(endTime.getTime() - 1000 * 60 * 10));
|
BigDecimal sumValue = pointValue == null ? BigDecimal.ZERO : new BigDecimal(pointValue.toString());
|
|
log.info("插入指标数据表");
|
Map<String, BigDecimal> valueMap = new HashMap<>(1);
|
valueMap.put("sumValue", sumValue);
|
IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
|
indexDataValueService.add(indexDataValue);
|
|
log.info("插入统计表");
|
FeignWashProdVolumeDTO dto = new FeignWashProdVolumeDTO();
|
dto.setRq(DateUtils.format(startTime));
|
dto.setBc(dataItem.getBc());
|
dto.setClmz(dataItem.getExt1());
|
dto.setNbdrl(sumValue);
|
dto.setNbtzl(new BigDecimal(0));
|
dto.setWbdrl(sumValue);
|
dto.setWbtzl(new BigDecimal(0));
|
dto.setCjr(CommonConstant.CJS_SYS);
|
dto.setCjsj(new Date());
|
dto.setXgsj(new Date());
|
feignProddispApi.createWashProdVolume(dto);
|
log.info("计算生产量结束,itemCode=" + dataItem.getPointCode());
|
} catch (Exception ex) {
|
log.info("计算生产量异常,itemCode=" + dataItem.getPointCode());
|
ex.printStackTrace();
|
}
|
}
|
|
public void createWarehousePosition(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
|
try{
|
log.info("计算仓位开始,itemCode=" + dataItem.getPointCode());
|
DaPointDTO daPoint = daPointService.getByNo(dataItem.getPointCode());
|
if (daPoint == null) {
|
log.info("daPoint is null");
|
return;
|
}
|
InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
|
pojo.setPoint(daPoint.getPointNo());
|
pojo.setType(daPoint.getDataType());
|
List<Map<String, Object>> pointValues = influxDBService.queryPointValues(pojo, new Date(endTime.getTime() - 1000 * 30), endTime);
|
String point = JSON.toJSONString(pointValues);
|
log.info("pointValues :"+point);
|
if (CollectionUtils.isEmpty(pointValues)) {
|
log.info("pointValues is null");
|
return;
|
}
|
|
Map<String, Object> pointValue = pointValues.get(pointValues.size() - 1);
|
JSONObject json = new JSONObject(pointValue);
|
log.info("插入指标数据表:"+json);
|
Map<String, BigDecimal> valueMap = new HashMap<>(1);
|
BigDecimal sumValue = new BigDecimal(pointValue.get("value").toString());
|
valueMap.put("sumValue",sumValue);
|
IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
|
indexDataValueService.add(indexDataValue);
|
log.info("mc:"+dataItem.getExt1());
|
FeignWarehouseItemDTO warehouseItem = feignProddispApi.selectWarehouseItemByMc(dataItem.getExt1());
|
log.info("插入仓位统计表开始");
|
FeignWarehousePositionDTO dto = new FeignWarehousePositionDTO();
|
dto.setRq(DateUtils.format(startTime));
|
dto.setBc(dataItem.getBc());
|
dto.setMc(dataItem.getExt1());
|
dto.setMclx(dataItem.getExt2());
|
log.info(Integer.toString(warehouseItem.getCapacity()));
|
dto.setCrl(Integer.toString(warehouseItem.getCapacity()));
|
BigDecimal dqcc = sumValue.multiply(new BigDecimal(dto.getCrl())).divide(new BigDecimal(100), 1, BigDecimal.ROUND_HALF_UP);
|
dto.setNbgl(dqcc);
|
dto.setWbgl(dqcc);
|
dto.setCjr(CommonConstant.CJS_SYS);
|
dto.setCjsj(new Date());
|
dto.setXgsj(new Date());
|
feignProddispApi.createWarehousePosition(dto);
|
log.info("计算仓位结束,itemCode=" + dataItem.getPointCode());
|
} catch (Exception ex) {
|
log.info("计算仓位异常,itemCode=" + dataItem.getPointCode());
|
ex.printStackTrace();
|
}
|
}
|
|
public void createWarehouseStock(String rq, String bc) {
|
try{
|
log.info("计算库存开始,rq=" + rq);
|
|
Map<String, Object> params = new HashMap<>();
|
params.put("rq", rq);
|
params.put("bc", bc);
|
List<FeignWarehousePositionDTO> list = feignProddispApi.selectWarehouseGroupByCp(params);
|
list.forEach(item -> {
|
log.info("遍历" + item.getMc()+"煤仓煤种");
|
List<FeignMcMzDTO> mzList = feignProddispApi.selectMcMzByMc(item.getMc());
|
mzList.stream().forEach(
|
mz->{
|
log.info("插入" + mz.getMz()+"库存");
|
FeignWarehouseStockDTO dto = new FeignWarehouseStockDTO();
|
dto.setRq(rq);
|
dto.setBc(bc);
|
dto.setLsh(new SimpleDateFormat("yyyyMMddhhmmssSSS").format(new Date()));
|
dto.setBs(item.getMclx());
|
dto.setKcmz(mz.getMz());
|
dto.setNbzrkc(new BigDecimal(0));
|
dto.setNbjrrk(new BigDecimal(0));
|
log.info(mz.getProportion().toString());
|
log.info(mz.getDen().toString());
|
BigDecimal math = item.getNbgl().multiply(BigDecimal.valueOf(mz.getProportion()).divide(BigDecimal.valueOf(mz.getDen())));
|
BigDecimal nbjrkc = math.setScale(2, RoundingMode.HALF_UP);
|
dto.setNbjrkc(nbjrkc);
|
dto.setNbtzl(new BigDecimal(0));
|
dto.setWbzrkc(new BigDecimal(0));
|
dto.setWbjrrk(new BigDecimal(0));
|
dto.setWbjrkc(dto.getNbjrkc());
|
dto.setWbtzl(new BigDecimal(0));
|
dto.setCjr(CommonConstant.CJS_SYS);
|
dto.setCjsj(new Date());
|
dto.setXgsj(new Date());
|
feignProddispApi.createWarehouseStock(dto);
|
}
|
);
|
});
|
log.info("计算库存结束,rq=" + rq);
|
} catch (Exception ex) {
|
log.info("计算库存异常,rq=" + rq);
|
ex.printStackTrace();
|
}
|
}
|
|
public int getRunMins(List<Map<String, Object>> pointData) {
|
int seconds = 0;
|
for (Map<String, Object> item:
|
pointData) {
|
if (Boolean.parseBoolean(item.get("value").toString())) {
|
seconds = seconds + granularity;
|
}
|
}
|
return (seconds / 60);
|
}
|
|
/**
|
* getAvgOutput
|
*
|
* @param indexDataItemEntity
|
* @param output
|
* @param hours
|
* @return
|
*/
|
public BigDecimal getAvgOutput(IndexDataItemEntity indexDataItemEntity, BigDecimal output, BigDecimal hours) {
|
BigDecimal result = BigDecimal.ZERO;
|
if (hours.compareTo(BigDecimal.ZERO) > 0) {
|
result = output.divide(hours, 0, BigDecimal.ROUND_HALF_UP);
|
}
|
return result;
|
}
|
|
/**
|
* buildIndexDataValueEntity
|
*
|
* @param indexDataItemEntity
|
* @param clock
|
* @param valueMap
|
* @return
|
*/
|
public IndexDataValueEntity buildIndexDataValueEntity(IndexDataItemEntity indexDataItemEntity, String clock,
|
Map<String, BigDecimal> valueMap) {
|
IndexDataValueEntity entity = new IndexDataValueEntity();
|
entity.setId(UUID.randomUUID().toString().replace("-",""));
|
entity.setConfigId(indexDataItemEntity.getId());
|
entity.setUnit2(indexDataItemEntity.getUnit2());
|
entity.setSsdw(indexDataItemEntity.getSsdw());
|
entity.setItemCode(indexDataItemEntity.getItemCode());
|
entity.setItemName(indexDataItemEntity.getItemName());
|
entity.setUnit(indexDataItemEntity.getUnit());
|
entity.setClock(clock);
|
entity.setSumValue(valueMap.get("sumValue"));
|
entity.setAvgValue(valueMap.get("avgValue"));
|
entity.setReportValue(valueMap.get("reportValue"));
|
entity.setReportHourValue(valueMap.get("reportHourValue"));
|
entity.setCollectValue(valueMap.get("collectValue"));
|
entity.setCollectHourValue(valueMap.get("collectHourValue"));
|
entity.setStartUpTimes(valueMap.get("startUpTimes"));
|
entity.setCreateTime(new Date());
|
return entity;
|
}
|
|
/**
|
* getAvgValue
|
*
|
* @param dataMap
|
* @return
|
*/
|
public BigDecimal getAvgValue (LinkedHashMap<String, BigDecimal> dataMap) {
|
BigDecimal avgValue = BigDecimal.ZERO;
|
double sumValue = 0d;
|
if (!CollectionUtils.isEmpty(dataMap)) {
|
int size = dataMap.size();
|
for (Map.Entry<String, BigDecimal> item : dataMap.entrySet()) {
|
if (item.getValue() != null && item.getValue().compareTo(BigDecimal.ZERO) > 0) {
|
sumValue = sumValue + item.getValue().doubleValue();
|
}
|
}
|
avgValue = new BigDecimal(sumValue).divide(new BigDecimal(size), 2, BigDecimal.ROUND_HALF_UP);
|
}
|
return avgValue;
|
}
|
|
/**
|
* getRunHours
|
*
|
* @param dataMap
|
* @return
|
*/
|
public BigDecimal getRunHours (LinkedHashMap<String, BigDecimal> dataMap) {
|
BigDecimal result = BigDecimal.ZERO;
|
if (!CollectionUtils.isEmpty(dataMap)) {
|
int mins = 0;
|
for (Map.Entry<String, BigDecimal> item : dataMap.entrySet()) {
|
if (item.getValue() != null && item.getValue().compareTo(BigDecimal.ZERO) > 0) {
|
mins++;
|
}
|
}
|
result = new BigDecimal(mins).divide(hourMins, 2, BigDecimal.ROUND_HALF_UP);
|
}
|
return result;
|
}
|
|
/**
|
* getStartUpTimes
|
*
|
* @param pointData
|
* @return
|
*/
|
public int getStartUpTimes (List<Map<String, Object>> pointData) {
|
int times = 0;
|
Boolean lastValue = Boolean.FALSE;
|
for (Map<String, Object> item:
|
pointData) {
|
Boolean itemValue = Boolean.parseBoolean(item.get("value").toString());
|
if (itemValue.compareTo(lastValue) != 0) {
|
times++;
|
}
|
lastValue = itemValue;
|
}
|
return times;
|
}
|
|
/**
|
* 完善数据
|
*
|
* @param length
|
* @param sourceDataMap
|
* @param startTime
|
* @param endTime
|
* @return
|
*/
|
public LinkedHashMap<String, BigDecimal> completionData(int length, LinkedHashMap<String, BigDecimal> sourceDataMap, Date startTime, Date endTime) {
|
if (CollectionUtils.isEmpty(sourceDataMap) || length <= sourceDataMap.size()) {
|
return sourceDataMap;
|
}
|
log.info("补全数据, length =" + length + "; size = " + sourceDataMap.size() + "; startTime = " + startTime.getTime() + "; endTime = " + endTime.getTime());
|
|
//找出缺少项
|
long oneMin = 1000 * 60;
|
long start = startTime.getTime();
|
long end = endTime.getTime();
|
long mins = ((end - start) / oneMin);
|
LinkedHashMap<String, BigDecimal> dataMap = new LinkedHashMap<>();
|
for (int i = 0; i < mins; i ++) {
|
String key = DateUtils.format(new Date(start + oneMin * i), "");
|
dataMap.put(key, sourceDataMap.get(key));
|
}
|
|
//补充缺少项
|
int k = 0;
|
Map.Entry<String, BigDecimal> lastItem = null;
|
for (Map.Entry<String, BigDecimal> item : dataMap.entrySet()) {
|
if (k == 0 && item.getValue() == null) {
|
item.setValue(getFirstValue(dataMap));
|
} else if (item.getValue() == null) {
|
item.setValue(lastItem.getValue());
|
}
|
k ++;
|
lastItem = item;
|
}
|
log.info("补全完成");
|
return dataMap;
|
}
|
|
/**
|
* getFirstValue
|
*
|
* @param dataMap
|
* @return
|
*/
|
private BigDecimal getFirstValue(LinkedHashMap<String, BigDecimal> dataMap) {
|
for (Map.Entry<String, BigDecimal> item : dataMap.entrySet()) {
|
if (item.getValue() != null) {
|
return item.getValue();
|
}
|
}
|
return null;
|
}
|
}
|