package com.ld.igds.protocol.grainplug; import com.alibaba.fastjson.JSONObject; import com.bstek.bdf2.core.model.DefaultDept; import com.ld.igds.common.CoreCommonService; import com.ld.igds.constant.Constant; import com.ld.igds.io.notify.NotifyGrainInvoker; import com.ld.igds.models.Depot; import com.ld.igds.models.DepotConf; import com.ld.igds.models.DicSysConf; import com.ld.igds.models.Grain; import com.ld.igds.protocol.grainplug.data.GrainRespData; import com.ld.igds.sys.service.SysDeptService; import com.ld.igds.view.service.HDepotService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; /** * 粮情解析 * * @author chen */ @Slf4j @Component(GrainPlugAnalysis.BEAN_ID) public class GrainPlugAnalysis { /** * 匹配仓库编码 */ public static final String BEAN_ID = "plug.grainPlugAnalysis"; public static Map depotMap = new HashMap(); //中粮吉林粮情采集--仓库编码匹配 static { depotMap.put("59", "0040"); depotMap.put("60", "0041"); depotMap.put("61", "0042"); depotMap.put("62", "0043"); depotMap.put("63", "0044"); depotMap.put("45", "0045"); depotMap.put("46", "0046"); depotMap.put("47", "0047"); depotMap.put("48", "0048"); depotMap.put("49", "0049"); depotMap.put("50", "0050"); depotMap.put("51", "0051"); depotMap.put("52", "0052"); depotMap.put("53", "0053"); depotMap.put("54", "0054"); depotMap.put("55", "0055"); depotMap.put("64", "0056"); depotMap.put("65", "0057"); depotMap.put("66", "0058"); } @Autowired private CoreCommonService commonService; @Autowired private NotifyGrainInvoker notifyGrainInvoker; @Autowired private SysDeptService deptService; @Autowired private HDepotService depotService; /** * 粮情返回信息解析 * * @param request */ public String analysisGrain(JSONObject request) { GrainRespData grainRespData = JSONObject.parseObject(request.toString(), GrainRespData.class); if (grainRespData == null) { log.warn("粮情插件------>>>平台:粮情数据为空,无法进行解析"); return null; } if (StringUtils.isEmpty(grainRespData.getDeptId()) || StringUtils.isEmpty(grainRespData.getHouseId())) { log.warn("粮情插件----->>>平台:库区编码={},仓库ID={},粮情数据信息不全,报文无法进行解析", grainRespData.getDeptId(), grainRespData.getHouseId()); return null; } //获取库区信息 DefaultDept dept = deptService.getDeptById(grainRespData.getDeptId()); if (dept == null) { log.warn("粮情插件----->>>平台:库区编码={},系统获取不到库区信息,报文无法进行解析", grainRespData.getDeptId()); return null; } String depotId = depotMap.get(grainRespData.getHouseId()); grainRespData.setHouseId(depotId); //获取仓库信息 Depot depot = commonService.getCacheDepot(dept.getCompanyId(), depotId); if (depot == null) { log.warn("粮情插件----->>>平台:仓库ID={},系统获取不到仓库信息,报文无法进行解析", grainRespData.getHouseId()); return null; } DicSysConf sysConf = commonService.getCacheSysConf(depot.getCompanyId()); DepotConf depotConf = commonService.getCacheDepotConf(depot.getCompanyId(), depotId); //若仓库配置为空,则新增配置信息 if (depotConf == null) { depotConf = new DepotConf(); depotConf.setCompanyId(depot.getCompanyId()); depotConf.setDepotId(depot.getId()); depotService.saveConf(depotConf); depotService.flushConfCache(depot.getCompanyId()); } //直接解析-保存 doSave(grainRespData, depotConf, sysConf, depot); return null; } private void doSave(GrainRespData grainData, DepotConf depotConf, DicSysConf sysConf, Depot depot) { Grain grain = new Grain(); //设置批次号 grain.setBatchId(DateFormatUtils.format(grainData.getTime(), "yyyyMMddHHmm")); //采集时间 grain.setReceiveDate(grainData.getTime()); //层行列 String cable = grainData.getLays() + "-" + grainData.getRows() + "-" + grainData.getCols(); grain.setCable(cable); //设置筒仓规则 if (StringUtils.isNotEmpty(grainData.getLayout()) && StringUtils.isNotEmpty(grainData.getLayerPerCircle())) { grain.setCable(grainData.getLayout()); grain.setCableCir(grainData.getLayerPerCircle()); } //仓库编码、组织编码、库区编码 grain.setDepotId(depotConf.getDepotId()); grain.setCompanyId(depot.getCompanyId()); grain.setDeptId(grainData.getDeptId()); //温湿度 grain.setTempIn(grainData.getTIn()); grain.setHumidityIn(grainData.getHIn()); grain.setTempOut(grainData.getTOut()); grain.setHumidityOut(grainData.getHOut()); //粮均温 grain.setTempAve(grainData.getTAvg()); //设置粮情点位数据 String pointsData = grainData.getPoints(); if(null != depotConf.getStartPoint() &&Constant.GRAIN_START_POINT_BELOW.equals(depotConf.getStartPoint())){ //如果粮情电缆开始点位从下面开始,则每根电缆上下翻转 pointsData = reversalUpAndDown(pointsData, cable); } grain.setPoints(pointsData); String[] points = grainData.getPoints().split(","); //计算粮高温和粮低温 Double tempMax = 0.0; Double tempMin = 50.0; Double temp; for (String point : points) { temp = Double.valueOf(point); if (temp > tempMax) { tempMax = temp; } if (temp != Constant.ERROR_TEMP && temp < tempMin) { tempMin = temp; } } grain.setTempMax(tempMax); grain.setTempMin(tempMin); //判断有没有超过高温 if (null != depotConf.getTempMax() && grain.getTempMax() > depotConf.getTempMax()) { grain.setRemark("粮情检测有高温点"); } // 用户封装好数据即可 String result = notifyGrainInvoker.analysisSuccess(grain, depot, sysConf); if (null != result) { log.info("粮情插件----->>>平台:粮情解析完成,保存失败-仓库={},批次={},失败信息={}", depot.getName(), grain.getBatchId(), result); } else { log.info("粮情插件----->>>平台:粮情解析完成-仓库={},批次={}", depot.getName(), grain.getBatchId()); } } /** * 电缆从下面开始时,将粮情电缆上下翻转 * * @param pointsData 粮情数据 * @param cable 层行列配置,如:4-7-11 * @return */ private String reversalUpAndDown(String pointsData, String cable){ String str = ""; if(StringUtils.isEmpty(cable)){ str = pointsData; } String[] attCable = cable.split("-"); int cableZ = Integer.valueOf(attCable[0]); //层 String[] points = pointsData.split(","); for(int i = 0; i <= points.length - cableZ; i += cableZ){ for(int j = cableZ -1; j >= 0; j --){ str += points[i + j]; str += ","; } } return str; } }