123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- 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<AlarmLogVO> 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<SingleInsertPO> 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<Object[]> buildBatchParams(Collection<SingleInsertPO> singleInPOs){
-
- List<Object[]> params=new ArrayList<>(singleInPOs.size());
-
- for(SingleInsertPO po : singleInPOs) {
- params.add(new Object[] {po.getWellParam(),po.getGatherTimeStr(),po.getDataVal()});
- }
-
- return params;
- }
-
- /**
- * 构建报警日志批量入库参数
- * @return
- */
- private List<Object[]> buildAlarmBatchParams(List<AlarmLogVO> almlogs){
- List<Object[]> 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<Object> args0=Arrays.asList(liquidPO.getWellId(),liquidPO.getTestTime(),listNum2Str(liquidPO.getLiquidDatas()),listNum2Str(liquidPO.getHoopDatas()),
- liquidPO.getSoundSpeedDev(),liquidPO.getLiquidDepthDev(),liquidPO.getCasingPressDev(),liquidPO.getBatteryVoltage());
-
- List<Object> 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 <T extends Number> String listNum2Str(List<T> 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<now() and compute_mode=?
- order by test_time desc limit 1
- """;
-
- return dao.queryForPojo(sql, LiquidVO.class,wellId,MODE_SOUND_SPEED);
-
- }
-
- /**
- * 查找最新的测算过静液面的动液面记录2023.10.18
- * @param wellId
- * @return
- */
- private LiquidVO getLastLiquidStaticCalc(String wellId) {
- String sql="""
- select liquid_depth,liquid_depth_convert
- from tzl_gather_data_liquid
- where well_id=? and test_time<now() and static_calc_if=true
- order by test_time desc limit 1
- """;
-
- return dao.queryForPojo(sql, LiquidVO.class,wellId);
- }
-
-
- /**
- * 查找最新的测量数据2023.10.18
- * @param wellId
- * @return
- */
- public WellMeasurePO getLastMeasure(String wellId) {
- String sql="""
- select * from tzl_well_measure
- where del_if=false and well_id=? and create_time<now()
- order by create_time desc limit 1
- """;
- return dao.queryForPojo(sql, WellMeasurePO.class, wellId);
- }
-
- }
|