GtController.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.gct.tools.etlcamelhuge.controller;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.gct.common.util.SGTUtil;
  5. import com.gct.tools.etlcamelhuge.MQ.MessageBody;
  6. import com.gct.tools.etlcamelhuge.MQ.MessageProducer;
  7. import com.gct.tools.etlcamelhuge.entity.DiagnoseMsg;
  8. import com.gct.tools.etlcamelhuge.entity.GTBody;
  9. import io.swagger.annotations.Api;
  10. import io.swagger.annotations.ApiOperation;
  11. import io.swagger.annotations.ApiParam;
  12. import org.springframework.jdbc.core.JdbcTemplate;
  13. import org.springframework.web.bind.annotation.*;
  14. import javax.annotation.Resource;
  15. import javax.sql.DataSource;
  16. import java.math.BigDecimal;
  17. import java.time.LocalDateTime;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.stream.Collectors;
  22. /**
  23. * class name: GtController
  24. *
  25. * @author gxt
  26. * @version 1.0
  27. * @since 2021/8/26 下午2:50 周四
  28. */
  29. @RestController
  30. @RequestMapping("/GTData")
  31. @Api(value = "GtController",description = "功图数据操作controller")
  32. public class GtController {
  33. private JdbcTemplate jdbcTemplate;
  34. @Resource(name = "centralbase")
  35. DataSource dataSource;
  36. @Resource(name = "gtsj")
  37. DataSource dataSourceOfGTSJ;
  38. @Resource(name = "diagnoseMessageProducer")
  39. private MessageProducer producer;
  40. @PostMapping("/sendGTSJ")
  41. @ApiOperation(value = "从 RunTime 数据表查询数据并且发送功图数据到 MQ 中")
  42. public JSONObject sendGTSJ(@RequestBody GTBody gtBody){
  43. String startDate = gtBody.getStartDate();
  44. String endDate = gtBody.getEndDate();
  45. List<String> wellList = gtBody.getWellList().stream().map(x->x.toString()).collect(Collectors.toList());
  46. JSONObject jsonObject = new JSONObject();
  47. jdbcTemplate = new JdbcTemplate(dataSource);
  48. long sumData = 0;
  49. try {
  50. if (wellList.isEmpty() || wellList.size()==0 ){
  51. String sql = String.format("select so.well_id,so.well_common_name,so.org_id,ti.prod_date,ti.stroke_frequency,ti.stroke_length,ti.sgt from centralbase.cb_temp_well_mech_runtime ti, centralbase.cb_cd_well_source so " +
  52. " where ti.well_id = so.well_id and ti.prod_date between '%s' and '%s' " ,startDate,endDate);
  53. sumData = sendDataToMQ(sql);
  54. }else {
  55. for (int i = 0; i < wellList.size(); i++) {
  56. String sql = String.format("select so.well_id,so.well_common_name,so.org_id,ti.prod_date,ti.stroke_frequency,ti.stroke_length,ti.sgt from centralbase.cb_temp_well_mech_runtime ti, centralbase.cb_cd_well_source so " +
  57. " where ti.well_id = so.well_id and ti.well_id = '%s' and ti.prod_date between '%s' and '%s' ", wellList.get(i), startDate, endDate);
  58. sumData = sendDataToMQ(sql);
  59. }
  60. }
  61. }catch (Exception e){
  62. e.printStackTrace();
  63. jsonObject.put("error",e.getMessage());
  64. }finally {
  65. jsonObject.put("发送数据为sumData",sumData);
  66. }
  67. return jsonObject;
  68. }
  69. public long sendDataToMQ(String sql){
  70. int sumData = 0;
  71. List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
  72. for (Map<String, Object> map : list) {
  73. String wellName = map.get("well_common_name").toString();
  74. String wellId = map.get("well_id").toString();
  75. String orgId = map.get("org_id").toString();
  76. String prodDate = map.get("prod_date").toString().substring(0, 19);
  77. Double strokeLength = Double.valueOf(map.get("stroke_length").toString());
  78. Double strokeFrequency = Double.valueOf(map.get("stroke_frequency").toString());
  79. String sgt = map.get("sgt").toString();
  80. if (sgt == null || sgt.length() == 0) {
  81. sgt = "0,0,0,0,0,0,0,0,0,0";
  82. }
  83. DiagnoseMsg diagnoseMsg = new DiagnoseMsg(wellId, wellName, orgId, prodDate, sgt, LocalDateTime.now().toString(), strokeLength, strokeFrequency);
  84. producer.send((MessageBody) diagnoseMsg);
  85. sumData++;
  86. }
  87. return sumData;
  88. }
  89. @GetMapping("/getGTSJ")
  90. @ApiOperation(value = "从实时的机采数据生产的表中获取数据放入到 Runtime 表中")
  91. public JSONObject getGTSJ(@RequestBody GTBody gtBody){
  92. String startDate = gtBody.getStartDate();
  93. String endDate = gtBody.getEndDate();
  94. JSONArray wellList = gtBody.getWellList();
  95. JSONObject jsonObject = new JSONObject();
  96. jdbcTemplate = new JdbcTemplate(dataSourceOfGTSJ);
  97. int curPage = 0;
  98. int pageSize = 5000;
  99. int sumData = 0;
  100. try {
  101. for (int i = 0; i < wellList.size(); i++) {
  102. String sql = String.format("select distinct well_name,dyna_create_time,check_date,displacement,disp_load,stroke,frequency,susp_max_load,susp_min_load from public.pc_fd_pumpjack_dyna_dia_t where dyna_create_time > '%s' and well_name = '%s' offset %d limit %d ", startDate, wellList.get(i));
  103. List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
  104. for (Map<String, Object> map : list) {
  105. String prod_date = map.get("dyna_create_time").toString().split("\\+")[0];
  106. map.put("dyna_create_time", prod_date);
  107. if (map.get("displacement") != null && !map.get("displacement").equals("") && map.get("disp_load") != null && !map.get("disp_load").equals("")) {
  108. String[] displacements = map.get("displacement").toString().split(";");//10 四舍五入
  109. String[] disp_loads = map.get("disp_load").toString().split(";");
  110. Double susp_max_load = max(disp_loads);
  111. Double susp_min_load = min(disp_loads);
  112. String sgt = "";
  113. for (int y = 0; y < displacements.length; y++) {
  114. sgt = sgt + displacements[y] + "," + disp_loads[y] + ",";
  115. }
  116. String[] s = sgt.split(",");
  117. String w = "";
  118. for (int x = 0; x < s.length; x++) {
  119. w += new BigDecimal(Math.round(Double.parseDouble(s[x]) * 100)).stripTrailingZeros().toPlainString() + ",";
  120. }
  121. Double[][] doubles = SGTUtil.encodeToDoubleArray(w);
  122. map.put("sgt", SGTUtil.encodeToString(doubles));
  123. map.put("susp_max_load",susp_max_load);
  124. map.put("susp_min_load",susp_min_load);
  125. }
  126. if (map.get("stroke") == null) map.put("stroke", "0.0");
  127. if (map.get("frequency") == null) map.put("frequency", "0.0");
  128. if (map.get("susp_max_load") == null) map.put("susp_max_load", "0.0");
  129. if (map.get("susp_min_load") == null) map.put("susp_min_load", "0.0");
  130. if (map.get("frequency") != null){
  131. BigDecimal bd=new BigDecimal(map.get("frequency").toString());
  132. double frequency=bd.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue();
  133. map.put("frequency",frequency);
  134. }
  135. if (map.get("stroke") != null){
  136. double stroke1 = Double.parseDouble(map.get("stroke").toString());
  137. BigDecimal bd=new BigDecimal(stroke1);
  138. double stroke=bd.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue();
  139. map.put("stroke",stroke);
  140. }
  141. jdbcTemplate.update("insert into centralbase.cb_temp_well_mech_runtime(well_id,prod_date,stroke_length,stroke_frequency,susp_max_load,susp_min_load,sgt) " +
  142. "values (?,?,?,?,?,?,?)",map.get("well_name").toString(),map.get("dyna_create_time"),map.get("stroke"),map.get("frequency"),map.get("susp_max_load"),map.get("susp_min_load"),map.get("sgt"));
  143. }
  144. sumData += list.size();
  145. if (list.size()< pageSize){
  146. break;
  147. }
  148. curPage ++ ;
  149. }
  150. }catch (Exception e){
  151. jsonObject.put("error",e.getMessage());
  152. }finally {
  153. jsonObject.put("sumData",sumData);
  154. }
  155. return jsonObject;
  156. }
  157. public Double min(String[] strings){
  158. double[] doubles = new double[strings.length];
  159. for (int i = 0; i < strings.length; i++) {
  160. doubles[i] = Double.parseDouble(strings[i]);
  161. }
  162. return Arrays.stream(doubles).min().getAsDouble();
  163. }
  164. //获取最大载荷
  165. public Double max(String[] strings){
  166. double[] doubles = new double[strings.length];
  167. for (int i = 0; i < strings.length; i++) {
  168. doubles[i] = Double.parseDouble(strings[i]);
  169. }
  170. return Arrays.stream(doubles).max().getAsDouble();
  171. }
  172. }