GatherDataRepService.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package com.hb.proj.gather.rep;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collection;
  5. import java.util.List;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import com.hb.proj.gather.model.AlarmLogVO;
  9. import com.hb.proj.gather.model.DiagramPO;
  10. import com.hb.proj.gather.model.LiquidPO;
  11. import com.hb.proj.gather.model.LiquidVO;
  12. import com.hb.proj.gather.model.SingleInsertPO;
  13. import com.hb.proj.gather.model.WellMeasurePO;
  14. import com.hb.proj.gather.process.LiquidCalculator;
  15. import com.hb.xframework.dao.core.SpringJdbcDAO;
  16. /**
  17. * 采集程序数据持久化
  18. * @author cwen
  19. *
  20. */
  21. @Service
  22. public class GatherDataRepService {
  23. @Autowired
  24. private SpringJdbcDAO dao;
  25. private final String MODE_SOUND_SPEED="sound_speed"; //音速法
  26. /**
  27. * 报警日志记录入库
  28. * @param almlogs
  29. */
  30. public void saveAlarm(List<AlarmLogVO> almlogs) {
  31. if(almlogs==null||almlogs.size()==0) {
  32. return;
  33. }
  34. String sql="""
  35. insert into tzl_alarm_log(alarm_time,alarm_desc,alarm_holder,alarm_source) values(?,?,?,?)
  36. """;
  37. dao.getJdbcTemplate().batchUpdate(sql, buildAlarmBatchParams(almlogs));
  38. }
  39. /**
  40. * 单值采集数据入库
  41. * @param singleInPOs
  42. */
  43. public void save(Collection<SingleInsertPO> singleInPOs) {
  44. if(singleInPOs==null||singleInPOs.size()==0) {
  45. return;
  46. }
  47. String sql="""
  48. insert into tzl_gather_data(well_param,gather_time,data_val) values(?,?,?)
  49. """;
  50. dao.getJdbcTemplate().batchUpdate(sql, buildBatchParams(singleInPOs));
  51. }
  52. /**
  53. * 多值采集数据 入库
  54. * @param diagramPO
  55. */
  56. public void save(DiagramPO diagramPO) {
  57. String sql="""
  58. insert into tzl_gather_data_multi(well_param,gather_time,data_val1,data_val2,
  59. freq,upstroke_max,downstroke_max,balance,stroke,glb_max,glb_min,turn_index,startup)
  60. values(?,?,?,?,?,?,?,?,?,?,?,?,?)
  61. """;
  62. dao.exeUpdate(sql, diagramPO.getWellParam(), diagramPO.getGatherTime(), listNum2Str(diagramPO.getDisps()),
  63. listNum2Str(diagramPO.getOths()),diagramPO.getFreq(),diagramPO.getUpstrokeMax(),diagramPO.getDwnstrokeMax(),
  64. diagramPO.getBalance(),diagramPO.getStroke(),diagramPO.getGlbMax(),diagramPO.getGlbMin(),diagramPO.getTurnIndex(),diagramPO.getStartup());
  65. }
  66. /**
  67. * 构建单值批量入库参数
  68. * @param singleInPOs
  69. * @return
  70. */
  71. private List<Object[]> buildBatchParams(Collection<SingleInsertPO> singleInPOs){
  72. List<Object[]> params=new ArrayList<>(singleInPOs.size());
  73. for(SingleInsertPO po : singleInPOs) {
  74. params.add(new Object[] {po.getWellParam(),po.getGatherTimeStr(),po.getDataVal()});
  75. }
  76. return params;
  77. }
  78. /**
  79. * 构建报警日志批量入库参数
  80. * @return
  81. */
  82. private List<Object[]> buildAlarmBatchParams(List<AlarmLogVO> almlogs){
  83. List<Object[]> params=new ArrayList<>(almlogs.size());
  84. for(AlarmLogVO vo : almlogs) {
  85. params.add(new Object[] {vo.getAlarmTime(),vo.getAlarmDesc(),vo.getAlarmHolder(),vo.getAlarmSource()});
  86. }
  87. return params;
  88. }
  89. /**
  90. * 检测动液面数据是否已存在
  91. * @param serial
  92. * @param testTime
  93. * @return
  94. */
  95. public boolean existsLiquid(String serial,String testTime) {
  96. String sql="""
  97. select count(1) from tzl_gather_data_liquid liq
  98. where well_id=(select well_id from tzl_gather_device d where d.device_code=? limit 1) and test_time=?
  99. """ ;
  100. return dao.queryForObject(sql, Integer.class, serial,testTime)>0;
  101. }
  102. /**
  103. * 动液面采集数据入库
  104. * 入库前绑定扩展字段默认值(静液面深度、折算静深度、折算液面深度、校正深度、校正音速)2023.10.18
  105. * @param liquid
  106. */
  107. public void save(LiquidPO liquidPO) {
  108. String sql="""
  109. 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,
  110. sound_speed_calc,liquid_depth,liquid_depth_convert,depth_diff,flow_press_diff,liquid_depth_static,liquid_depth_static_convert)
  111. values(?,?,?,?,?,?,?,now(),false,?,?,?,?,?,?,?,?)
  112. """;
  113. List<Object> args0=Arrays.asList(liquidPO.getWellId(),liquidPO.getTestTime(),listNum2Str(liquidPO.getLiquidDatas()),listNum2Str(liquidPO.getHoopDatas()),
  114. liquidPO.getSoundSpeedDev(),liquidPO.getLiquidDepthDev(),liquidPO.getCasingPressDev(),liquidPO.getBatteryVoltage());
  115. List<Object> args=new ArrayList<>(13);
  116. args.addAll(args0);
  117. LiquidVO lastSSLiquid=getLastLiquidCalcViaSoundSpeed(liquidPO.getWellId());
  118. LiquidVO lastStaticLiquid=getLastLiquidStaticCalc(liquidPO.getWellId());
  119. //校正音速
  120. Double val=lastSSLiquid!=null && lastSSLiquid.getSoundSpeed()!=null?lastSSLiquid.getSoundSpeed():null;
  121. args.add(val);
  122. //校正液面深度
  123. val=liquidPO.getLiquidDepthDev()*(lastSSLiquid!=null && lastSSLiquid.getSoundSpeedFactor()!=null?lastSSLiquid.getSoundSpeedFactor():1);
  124. args.add(val);
  125. WellMeasurePO lastMeasure=getLastMeasure(liquidPO.getWellId());
  126. //折算动液面深度
  127. val=LiquidCalculator.calcConvertLiquidDepth(val, liquidPO.getCasingPressDev(), lastMeasure);
  128. args.add(val);
  129. //深度压差
  130. val=LiquidCalculator.calcDepthDiff(val, lastStaticLiquid);
  131. args.add(val);
  132. //流动压差
  133. val=LiquidCalculator.calcFlowPreeDiff(val, lastMeasure);
  134. args.add(val);
  135. //静液面深度
  136. args.add(lastStaticLiquid!=null?lastStaticLiquid.getLiquidDepth():null);
  137. //折算静深度
  138. args.add(lastStaticLiquid!=null?lastStaticLiquid.getLiquidDepthConvert():null);
  139. dao.exeUpdate(sql, args.toArray());
  140. }
  141. private <T extends Number> String listNum2Str(List<T> datas) {
  142. StringBuilder strb=new StringBuilder(datas.size()*5);
  143. for(Number d : datas) {
  144. strb.append(","+String.valueOf(d));
  145. }
  146. return strb.substring(1);
  147. }
  148. /**
  149. * 查找最新的音速法计算过的动液面记录2023.10.18
  150. * @param wellId
  151. * @return
  152. */
  153. private LiquidVO getLastLiquidCalcViaSoundSpeed(String wellId) {
  154. String sql="""
  155. select sound_speed_factor,sound_speed
  156. from tzl_gather_data_liquid
  157. where well_id=? and test_time<now() and compute_mode=?
  158. order by test_time desc limit 1
  159. """;
  160. return dao.queryForPojo(sql, LiquidVO.class,wellId,MODE_SOUND_SPEED);
  161. }
  162. /**
  163. * 查找最新的测算过静液面的动液面记录2023.10.18
  164. * @param wellId
  165. * @return
  166. */
  167. private LiquidVO getLastLiquidStaticCalc(String wellId) {
  168. String sql="""
  169. select liquid_depth,liquid_depth_convert
  170. from tzl_gather_data_liquid
  171. where well_id=? and test_time<now() and static_calc_if=true
  172. order by test_time desc limit 1
  173. """;
  174. return dao.queryForPojo(sql, LiquidVO.class,wellId);
  175. }
  176. /**
  177. * 查找最新的测量数据2023.10.18
  178. * @param wellId
  179. * @return
  180. */
  181. public WellMeasurePO getLastMeasure(String wellId) {
  182. String sql="""
  183. select * from tzl_well_measure
  184. where del_if=false and well_id=? and create_time<now()
  185. order by create_time desc limit 1
  186. """;
  187. return dao.queryForPojo(sql, WellMeasurePO.class, wellId);
  188. }
  189. }