package com.hb.proj.gather.rep; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.hb.proj.gather.model.AlarmLogVO; import com.hb.proj.gather.model.DiagramPO; import com.hb.proj.gather.model.LiquidPO; import com.hb.proj.gather.model.LiquidVO; import com.hb.proj.gather.model.SingleInsertPO; import com.hb.proj.gather.model.WellMeasurePO; import com.hb.proj.gather.process.LiquidCalculator; import com.hb.xframework.dao.core.SpringJdbcDAO; /** * 采集程序数据持久化 * @author cwen * */ @Service public class GatherDataRepService { @Autowired private SpringJdbcDAO dao; private final String MODE_SOUND_SPEED="sound_speed"; //音速法 /** * 报警日志记录入库 * @param almlogs */ public void saveAlarm(List almlogs) { if(almlogs==null||almlogs.size()==0) { return; } String sql=""" insert into tzl_alarm_log(alarm_time,alarm_desc,alarm_holder,alarm_source) values(?,?,?,?) """; dao.getJdbcTemplate().batchUpdate(sql, buildAlarmBatchParams(almlogs)); } /** * 单值采集数据入库 * @param singleInPOs */ public void save(Collection singleInPOs) { if(singleInPOs==null||singleInPOs.size()==0) { return; } String sql=""" insert into tzl_gather_data(well_param,gather_time,data_val) values(?,?,?) """; dao.getJdbcTemplate().batchUpdate(sql, buildBatchParams(singleInPOs)); } /** * 多值采集数据 入库 * @param diagramPO */ public void save(DiagramPO diagramPO) { String sql=""" insert into tzl_gather_data_multi(well_param,gather_time,data_val1,data_val2, freq,upstroke_max,downstroke_max,balance,stroke,glb_max,glb_min,turn_index,startup) values(?,?,?,?,?,?,?,?,?,?,?,?,?) """; dao.exeUpdate(sql, diagramPO.getWellParam(), diagramPO.getGatherTime(), listNum2Str(diagramPO.getDisps()), listNum2Str(diagramPO.getOths()),diagramPO.getFreq(),diagramPO.getUpstrokeMax(),diagramPO.getDwnstrokeMax(), diagramPO.getBalance(),diagramPO.getStroke(),diagramPO.getGlbMax(),diagramPO.getGlbMin(),diagramPO.getTurnIndex(),diagramPO.getStartup()); } /** * 构建单值批量入库参数 * @param singleInPOs * @return */ private List buildBatchParams(Collection singleInPOs){ List params=new ArrayList<>(singleInPOs.size()); for(SingleInsertPO po : singleInPOs) { params.add(new Object[] {po.getWellParam(),po.getGatherTimeStr(),po.getDataVal()}); } return params; } /** * 构建报警日志批量入库参数 * @return */ private List buildAlarmBatchParams(List almlogs){ List params=new ArrayList<>(almlogs.size()); for(AlarmLogVO vo : almlogs) { params.add(new Object[] {vo.getAlarmTime(),vo.getAlarmDesc(),vo.getAlarmHolder(),vo.getAlarmSource()}); } return params; } /** * 检测动液面数据是否已存在 * @param serial * @param testTime * @return */ public boolean existsLiquid(String serial,String testTime) { String sql=""" select count(1) from tzl_gather_data_liquid liq where well_id=(select well_id from tzl_gather_device d where d.device_code=? limit 1) and test_time=? """ ; return dao.queryForObject(sql, Integer.class, serial,testTime)>0; } /** * 动液面采集数据入库 * 入库前绑定扩展字段默认值(静液面深度、折算静深度、折算液面深度、校正深度、校正音速)2023.10.18 * @param liquid */ public void save(LiquidPO liquidPO) { String sql=""" insert into tzl_gather_data_liquid(well_id,test_time,liquid_datas,hoop_datas,sound_speed_dev,liquid_depth_dev,casing_press_dev,create_time,del_if,battery_voltage, sound_speed_calc,liquid_depth,liquid_depth_convert,depth_diff,flow_press_diff,liquid_depth_static,liquid_depth_static_convert) values(?,?,?,?,?,?,?,now(),false,?,?,?,?,?,?,?,?) """; List args0=Arrays.asList(liquidPO.getWellId(),liquidPO.getTestTime(),listNum2Str(liquidPO.getLiquidDatas()),listNum2Str(liquidPO.getHoopDatas()), liquidPO.getSoundSpeedDev(),liquidPO.getLiquidDepthDev(),liquidPO.getCasingPressDev(),liquidPO.getBatteryVoltage()); List args=new ArrayList<>(13); args.addAll(args0); LiquidVO lastSSLiquid=getLastLiquidCalcViaSoundSpeed(liquidPO.getWellId()); LiquidVO lastStaticLiquid=getLastLiquidStaticCalc(liquidPO.getWellId()); //校正音速 Double val=lastSSLiquid!=null && lastSSLiquid.getSoundSpeed()!=null?lastSSLiquid.getSoundSpeed():null; args.add(val); //校正液面深度 val=liquidPO.getLiquidDepthDev()*(lastSSLiquid!=null && lastSSLiquid.getSoundSpeedFactor()!=null?lastSSLiquid.getSoundSpeedFactor():1); args.add(val); WellMeasurePO lastMeasure=getLastMeasure(liquidPO.getWellId()); //折算动液面深度 val=LiquidCalculator.calcConvertLiquidDepth(val, liquidPO.getCasingPressDev(), lastMeasure); args.add(val); //深度压差 val=LiquidCalculator.calcDepthDiff(val, lastStaticLiquid); args.add(val); //流动压差 val=LiquidCalculator.calcFlowPreeDiff(val, lastMeasure); args.add(val); //静液面深度 args.add(lastStaticLiquid!=null?lastStaticLiquid.getLiquidDepth():null); //折算静深度 args.add(lastStaticLiquid!=null?lastStaticLiquid.getLiquidDepthConvert():null); dao.exeUpdate(sql, args.toArray()); } private String listNum2Str(List datas) { StringBuilder strb=new StringBuilder(datas.size()*5); for(Number d : datas) { strb.append(","+String.valueOf(d)); } return strb.substring(1); } /** * 查找最新的音速法计算过的动液面记录2023.10.18 * @param wellId * @return */ private LiquidVO getLastLiquidCalcViaSoundSpeed(String wellId) { String sql=""" select sound_speed_factor,sound_speed from tzl_gather_data_liquid where well_id=? and test_time