package com.ld.igds.grain.service;
|
|
import com.baomidou.mybatisplus.plugins.Page;
|
import com.ld.igds.common.CoreCommonService;
|
import com.ld.igds.constant.*;
|
import com.ld.igds.data.ChartLine;
|
import com.ld.igds.data.ChartSeries;
|
import com.ld.igds.models.Depot;
|
import com.ld.igds.screen.data.DepotGrainData;
|
import com.ld.igds.grain.*;
|
import com.ld.igds.grain.dto.*;
|
import com.ld.igds.grain.mapper.GrainServiceMapper;
|
import com.ld.igds.io.constant.OrderRespEnum;
|
import com.ld.igds.models.DepotConf;
|
import com.ld.igds.models.DicSysConf;
|
import com.ld.igds.models.Grain;
|
import com.ld.igds.util.ContextUtil;
|
import com.ld.igds.util.DateUtil;
|
import com.ld.igds.util.RedisUtil;
|
import com.ld.igds.websocket.WebSocketPacket;
|
import com.ld.igds.websocket.WebSocketServer;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.util.*;
|
|
@Slf4j
|
@Component
|
public class CoreGrainServiceImpl implements CoreGrainService {
|
|
@Autowired
|
private GrainServiceMapper grainMapper;
|
@Autowired
|
private RedisUtil redisUtil;
|
@Autowired
|
private CoreCommonService commonService;
|
|
@Override
|
public Page<GrainDataReport> getDataReport(int pageSize, int pageNo,
|
Map<String, Object> parameter) {
|
|
Page<GrainDataReport> page = new Page<GrainDataReport>(pageNo, pageSize);
|
|
String ids = (String) parameter.get("ids");
|
|
// 具体小时
|
int timeHour = (int) parameter.get("time");
|
|
Date date = (Date) parameter.get(("start"));
|
if (null != date) {
|
parameter.put("start", DateUtil.getCurZero(date));
|
}
|
date = (Date) parameter.get(("end"));
|
if (null != date) {
|
parameter.put("end", DateUtil.getNextZero(date));
|
}
|
|
List<GrainDataReport> records = grainMapper.pageDataReport(page,
|
parameter);
|
|
if (null == records || records.isEmpty()) {
|
return page;
|
}
|
|
// 是否允许重复,在按照某天查询情况,只获取当天最后一条信息 0= 不允许,1=允许
|
String repeat = (String) parameter.get(("repeat"));
|
Map<String, String> tempMap = new HashMap<String, String>();
|
|
// 获取系统配置
|
DicSysConf conf = commonService.getCacheSysConf(records.get(0)
|
.getCompanyId());
|
|
// 调整数据
|
int maxZ = 3;
|
int hour = 0;
|
List<GrainDataReport> dataList = new ArrayList<GrainDataReport>();
|
for (GrainDataReport report : records) {
|
|
// 把非选择的仓库屏蔽掉,针对在粮情数据报非分页中采用
|
if (StringUtils.isNotEmpty(ids)) {
|
if (ids.indexOf(report.getDepotId()) == -1) {
|
continue;
|
}
|
}
|
|
// 判断获取的时间是否是当前选择的小时的数据
|
if (timeHour > 0) {
|
hour = DateUtil.getHour(report.getReceiveDate());
|
// log.debug("-------------条件时间 =={},--数据时间=={}",timeHour,report.getReceiveDate().toString());
|
if (timeHour != hour) {
|
continue;
|
}
|
}
|
|
// 去除重复
|
if (null != repeat && "0".equals(repeat)) {
|
if (null == tempMap.get(report.getDepotId())) {
|
tempMap.put(report.getDepotId(), report.getBatchId());
|
} else {
|
continue;
|
}
|
}
|
|
// 调整类型字段显示中文
|
report.setDepotTypeName(DepotType.getMsg(report.getDepotType()));
|
report.setFoodVariety(FoodVariety.getMsg(report.getFoodVariety()));
|
|
if (report.getMaxZ() > maxZ) {
|
maxZ = report.getMaxZ();
|
}
|
report.setMaxZ(maxZ);
|
|
GrainDataBuilder.updateReport(report, conf);
|
|
dataList.add(report);
|
}
|
|
page.setRecords(dataList);
|
return page;
|
}
|
|
@Override
|
public void deleteListData(List<GrainDataReport> list) {
|
grainMapper.deleteListData(list);
|
}
|
|
@Override
|
public Map<String, GrainData> queryCheckDateMap(GrainParam param) {
|
if (null != param.getCheckDate()) {
|
param.setStart(DateUtil.getCurZero(param.getCheckDate()));
|
param.setEnd(DateUtil.getNextZero(param.getCheckDate()));
|
param.setCheckDate(null);
|
}
|
List<GrainData> list = grainMapper.listGrainData(param);
|
|
if (null == list || list.isEmpty())
|
return null;
|
|
// 获取配置信息
|
DicSysConf dicSysConf = commonService.getCacheSysConf(param
|
.getCompanyId());
|
|
Map<String, GrainData> result = new HashMap<>();
|
DepotConf depotConf;
|
|
int checkHour = param.getCheckHour(), curHour = 0;
|
for (GrainData grainData : list) {
|
|
if (null == grainData.getDepotId()) {
|
continue;
|
}
|
|
if (null != result.get(grainData.getDepotId())) {
|
continue;
|
}
|
|
// 如果选择具体小时条件查询
|
if (checkHour > 0) {
|
curHour = DateUtil.getHour(grainData.getReceiveDate());
|
if (checkHour != curHour)
|
continue;
|
}
|
|
if (param.isTagUpdate()) {
|
depotConf = commonService.getCacheDepotConf(
|
param.getCompanyId(), grainData.getDepotId());
|
GrainDataBuilder.updateGrainData(grainData, dicSysConf,
|
depotConf);
|
}
|
|
result.put(grainData.getDepotId(), grainData);
|
}
|
|
return result;
|
}
|
|
@Override
|
public List<GrainData> listGrainData(GrainParam param) {
|
List<GrainData> list;
|
// 如果有时间参数,采取默认分页查询,但不查个数
|
long start = System.currentTimeMillis();
|
if (null == param.getStart()) {
|
Page<GrainData> page = new Page<>(param.getPage(), param.getLimit());
|
page.setSearchCount(false);
|
list = grainMapper.pageListGrainData(page, param);
|
} else {
|
list = grainMapper.listGrainData(param);
|
}
|
|
log.info("{}-粮情数据获取-查询执行时间={}", param.getDepotId(),
|
System.currentTimeMillis() - start);
|
|
if (null == list || list.isEmpty()) {
|
return list;
|
}
|
|
// 粮情数据调整
|
if (param.isTagUpdate()) {
|
start = System.currentTimeMillis();
|
// 获取配置信息
|
DicSysConf dicSysConf = commonService.getCacheSysConf(param
|
.getCompanyId());
|
|
DepotConf depotConf;
|
if (null == param.getDepotIds()) {
|
depotConf = commonService.getCacheDepotConf(
|
param.getCompanyId(), param.getDepotId());
|
|
for (GrainData data : list) {
|
GrainDataBuilder.updateGrainData(data, dicSysConf,
|
depotConf);
|
}
|
|
log.info("{}-粮情数据获取-数据更新时间={}", param.getDepotId(),
|
System.currentTimeMillis() - start);
|
} else {
|
List<GrainData> result = new ArrayList<>();
|
for (GrainData data : list) {
|
if (param.getDepotIds().indexOf(data.getDepotId()) >= 0) {
|
depotConf = commonService.getCacheDepotConf(
|
data.getCompanyId(), data.getDepotId());
|
GrainDataBuilder.updateGrainData(data, dicSysConf,
|
depotConf);
|
|
result.add(data);
|
}
|
}
|
return result;
|
}
|
}
|
|
return list;
|
}
|
|
@Override
|
public List<GrainData> listChartData(GrainParam param) {
|
|
List<GrainData> list = grainMapper.pageListChartData(param);
|
|
if (null == list || list.isEmpty()) {
|
return list;
|
}
|
return list;
|
}
|
|
@Override
|
public Map<String, GrainData> getCacheGrainDateMap(String companyId, String deptId) {
|
if (null == companyId || null == deptId) return null;
|
|
String pattern = RedisConst.buildKey(companyId, RedisConst.KEY_GRAIN);
|
|
Set<String> keys = redisUtil.keys(pattern);
|
|
if (null == keys || keys.isEmpty()) return null;
|
|
Map<String, GrainData> result = new HashMap<>();
|
GrainData data;
|
for (String key : keys) {
|
data = (GrainData) redisUtil.get(key);
|
if (null == data) continue;
|
|
result.put(data.getDepotId(), data);
|
}
|
return result;
|
}
|
|
@Override
|
public void updateCacheGrainData(Grain data) {
|
|
GrainData grainData = new GrainData();
|
BeanUtils.copyProperties(data, grainData);
|
|
String key = RedisConst.buildKey(data.getCompanyId(),
|
RedisConst.KEY_GRAIN, grainData.getDepotId());
|
redisUtil.set(key, grainData);
|
|
|
//推送大屏
|
String deptId = data.getDeptId();
|
if (null == deptId) {
|
Depot depot = commonService.getCacheDepot(data.getCompanyId(), data.getDepotId());
|
if (null == depot) {
|
return;
|
}
|
deptId = depot.getDeptId();
|
}
|
Map<String, GrainData> mapData = this.getCacheGrainDateMap(data.getCompanyId(), deptId);
|
WebSocketPacket packet = new WebSocketPacket();
|
packet.setBizType(BizType.SCREEN.getCode());
|
packet.setCompanyId(ContextUtil.getDefaultCompanyId());
|
packet.setDeptId(deptId);
|
packet.setBizId(BizType.GRAIN.getCode());
|
packet.setOrderResp(OrderRespEnum.ORDER_INPROGRESS.getCode());
|
packet.setData(mapData);
|
WebSocketServer.sendByPocket(packet);
|
}
|
|
@Override
|
public int saveOrUpdateGrain(Grain grain) throws Exception {
|
int count = grainMapper.countGrain(grain);
|
if (count == 0) {
|
grainMapper.saveGrain(grain);
|
} else {
|
grainMapper.updateGrain(grain);
|
}
|
|
updateCacheGrainData(grain);
|
return count;
|
}
|
|
@Override
|
public void saveGrain(Grain grain) throws Exception {
|
grainMapper.saveGrain(grain);
|
updateCacheGrainData(grain);
|
}
|
|
@Override
|
public Page<GrainData> pageListGrainData(GrainParam param) {
|
Page<GrainData> page = new Page<GrainData>(param.getPage(),
|
param.getLimit());
|
|
if (null != param.getCheckDate()) {
|
param.setStart(DateUtil.getCurZero(param.getCheckDate()));
|
param.setEnd(DateUtil.getNextZero(param.getCheckDate()));
|
param.setCheckDate(null);
|
}
|
|
if (null != param.getCheckMonth()) {
|
param.setStart(DateUtil.getMonthFirst(param.getCheckMonth()));
|
param.setEnd(DateUtil.getNextMonthFirst(param.getCheckMonth()));
|
param.setCheckMonth(null);
|
}
|
|
page.setSearchCount(false);
|
long start = System.currentTimeMillis();
|
List<GrainData> records = grainMapper.pageListGrainData(page, param);
|
log.debug("查询执行时间={}", System.currentTimeMillis() - start);
|
|
if (null == records)
|
return page;
|
|
// 调整数据
|
if (param.isTagUpdate()) {
|
// 获取配置信息
|
DicSysConf dicSysConf = commonService.getCacheSysConf(param
|
.getCompanyId());
|
DepotConf depotConf = commonService.getCacheDepotConf(
|
param.getCompanyId(), param.getDepotId());
|
for (GrainData data : records) {
|
GrainDataBuilder.updateGrainData(data, dicSysConf, depotConf);
|
}
|
}
|
page.setRecords(records);
|
return page;
|
}
|
|
/**
|
* 粮情采集过程中存在批次重复的情况,因此保存点位会存在主键冲突,我们忽略这个异常
|
*
|
* @param items
|
* @param grain
|
*/
|
@Override
|
public void addGrainItems(List<GrainItemInfo> items, Grain grain) {
|
for (GrainItemInfo item : items) {
|
item.setCable(grain.getCable());
|
item.setId(grain.getCompanyId() + "-" + grain.getDepotId() + "-"
|
+ grain.getBatchId());
|
item.setReceiveDate(grain.getReceiveDate());
|
item.setTempAve(grain.getTempAve());
|
try {
|
grainMapper.addItem(item);
|
} catch (Exception e) {
|
log.error("保存采集点信息出现异常:{}", e.getMessage());
|
break;
|
}
|
}
|
|
}
|
|
@Override
|
public List<GrainItemInfo> listGrainItemInfo(GrainPointParam param) {
|
return grainMapper.listGrainItemInfo(param);
|
}
|
|
@Override
|
public ChartLine chartPointLine(GrainPointParam param) {
|
// 生成ID
|
param.setId(param.getCompanyId() + "-" + param.getDepotId() + "-%");
|
|
List<GrainItemInfo> list = listGrainItemInfo(param);
|
|
if (null == list || list.isEmpty())
|
return null;
|
|
ChartLine chartLine = new ChartLine();
|
|
ChartSeries ser1 = new ChartSeries();
|
|
ChartSeries ser2 = new ChartSeries();
|
|
for (GrainItemInfo info : list) {
|
chartLine.getXaxisData()
|
.add(DateFormatUtils.format(info.getReceiveDate(),
|
"MM-dd HH:mm"));
|
|
// 屏蔽那种故障点
|
if (Constant.ERROR_TEMP == info.getTemp()) {
|
info.setTemp(0.0);
|
}
|
ser1.getData().add(info.getTemp() + "");
|
ser2.getData().add(info.getTempAve() + "");
|
}
|
chartLine.getSeries().add(ser1);
|
chartLine.getSeries().add(ser2);
|
return chartLine;
|
}
|
|
@Override
|
public GrainData listLiquidHeight(String companyId, String depotId) {
|
if (StringUtils.isEmpty(companyId)) {
|
return null;
|
}
|
if (StringUtils.isEmpty(depotId)) {
|
return null;
|
}
|
|
String key = RedisConst.buildKey(companyId,
|
RedisConst.KEY_DEPOT_HEIGHT, depotId);
|
|
return (GrainData) redisUtil.get(key);
|
}
|
|
@Override
|
public List<GrainIotData> queryIotGrainData(String companyId, String depotId) {
|
if (StringUtils.isEmpty(companyId)) {
|
return null;
|
}
|
if (StringUtils.isEmpty(depotId)) {
|
return null;
|
}
|
|
String pattern = RedisConst.buildKey(companyId,
|
RedisConst.KEY_IOT_DEPOT_TEMP_HUM, depotId);
|
|
Set<String> keys = redisUtil.keys(pattern);
|
|
List<GrainIotData> result = new ArrayList<>();
|
if (null == keys) {
|
return result;
|
}
|
|
for (String key : keys) {
|
result.add((GrainIotData) redisUtil.get(key));
|
}
|
return result;
|
}
|
|
@Override
|
public List<DepotGrainData> getDepotAndGrain(String companyId, String deptId) {
|
return grainMapper.getDepotAndGrain(companyId, deptId);
|
}
|
}
|