plt_service.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2024/6/7
  3. # @Author : 魏志亮
  4. import datetime
  5. from utils.db.ConnectMysql import ConnectMysql
  6. plt = ConnectMysql("plt")
  7. def update_timeout_trans_data():
  8. sql = """
  9. UPDATE data_transfer
  10. SET trans_sys_status = 2,err_info='运行超时失败',transfer_state=2
  11. WHERE
  12. (
  13. (transfer_type = 'second' AND TIMESTAMPDIFF(HOUR, transfer_start_time, NOW()) > 24)
  14. OR
  15. (transfer_type = 'minute' AND TIMESTAMPDIFF(HOUR, transfer_start_time, NOW()) > 6)
  16. )
  17. AND trans_sys_status = 0
  18. """
  19. plt.execute(sql)
  20. def update_trans_status_running(batch_no, trans_type, schedule_exec=True):
  21. if schedule_exec:
  22. exec_sql = """
  23. update data_transfer set transfer_state = 0,trans_sys_status = 0 ,transfer_start_time = now(),err_info='',
  24. engine_count =0,time_granularity=0,transfer_finish_time=null,
  25. data_min_time= null,data_max_time= null,transfer_data_count=null
  26. where batch_code = %s and transfer_type = %s
  27. """
  28. plt.execute(exec_sql, (batch_no, trans_type))
  29. def update_trans_status_error(batch_no, trans_type, message="", save_db=True):
  30. if save_db:
  31. exec_sql = """
  32. update data_transfer set transfer_state = 2,trans_sys_status=2 ,err_info= %s,transfer_finish_time=now()
  33. where batch_code = %s and transfer_type = %s
  34. """
  35. message = message if len(message) <= 200 else message[0:200]
  36. plt.execute(exec_sql, (message, batch_no, trans_type))
  37. def update_trans_status_success(batch_no, trans_type, wind_count=0, time_granularity=0,
  38. min_date=datetime.datetime.now(),
  39. max_date=datetime.datetime.now(),
  40. total_count=0, save_db=True):
  41. if save_db:
  42. if min_date is not None:
  43. exec_sql = """
  44. update data_transfer set transfer_state = 1,trans_sys_status = 1,transfer_progress=100,err_info = '',engine_count =%s,time_granularity=%s,transfer_finish_time=now(),
  45. data_min_time= %s,data_max_time= %s,transfer_data_count=%s
  46. where batch_code = %s and transfer_type = %s
  47. """
  48. plt.execute(exec_sql, (wind_count, time_granularity, min_date, max_date, total_count, batch_no, trans_type))
  49. else:
  50. exec_sql = """
  51. update data_transfer set transfer_state = 1,trans_sys_status = 1,transfer_progress = 100,err_info = '',engine_count =%s,time_granularity=%s,transfer_finish_time=now()
  52. where batch_code = %s and transfer_type = %s
  53. """
  54. plt.execute(exec_sql, (wind_count, time_granularity, batch_no, trans_type))
  55. def update_trans_transfer_progress(batch_no, trans_type, transfer_progress=0, save_db=True):
  56. if save_db:
  57. exec_sql = """
  58. update data_transfer set transfer_progress =%s where batch_code = %s and transfer_type = %s
  59. """
  60. plt.execute(exec_sql, (int(transfer_progress), batch_no, trans_type))
  61. # 获取执行的数据
  62. def get_batch_exec_data(run_count: int = 1) -> dict:
  63. query_running_sql = "select count(1) as count from data_transfer where trans_sys_status = 0"
  64. query_next_exec_sql = """
  65. SELECT
  66. t.*,a.field_name,b.batch_name
  67. FROM
  68. data_transfer t INNER JOIN wind_field a on t.field_code = a.field_code
  69. inner join wind_field_batch b on t.batch_code = b.batch_code
  70. WHERE
  71. t.trans_sys_status in (-1,1,2) and t.transfer_state = 0
  72. AND t.transfer_addr != ''
  73. ORDER BY
  74. t.update_time
  75. LIMIT 1
  76. """
  77. data = plt.execute(query_running_sql)
  78. now_count = int(data[0]['count'])
  79. if now_count >= run_count:
  80. return None
  81. else:
  82. data = plt.execute(query_next_exec_sql)
  83. if type(data) == tuple:
  84. return {}
  85. return data[0]
  86. def get_data_by_batch_no_and_type(batch_no, transfer_type):
  87. query_exec_sql = f"""
  88. SELECT
  89. t.*,a.field_name,b.batch_name
  90. FROM
  91. data_transfer t INNER JOIN wind_field a on t.field_code = a.field_code
  92. inner join wind_field_batch b on t.batch_code = b.batch_code
  93. WHERE
  94. t.trans_sys_status in (-1,1,2) and t.transfer_state = 2 and t.batch_code = '{batch_no}' and t.transfer_type = '{transfer_type}'
  95. AND t.transfer_addr != ''
  96. """
  97. data = plt.execute(query_exec_sql)
  98. if type(data) == tuple:
  99. return None
  100. return data[0]
  101. ## 合并多个batch_使用
  102. def get_hebing_data_by_batch_no_and_type(batch_no, transfer_type):
  103. query_exec_sql = f"""
  104. SELECT
  105. t.*,a.field_name,b.batch_name
  106. FROM
  107. data_transfer t INNER JOIN wind_field a on t.field_code = a.field_code
  108. inner join wind_field_batch b on t.batch_code = b.batch_code
  109. WHERE
  110. t.trans_sys_status = 1 and t.transfer_state = 1 and t.batch_code = '{batch_no}' and t.transfer_type = '{transfer_type}'
  111. AND t.transfer_addr != ''
  112. """
  113. data = plt.execute(query_exec_sql)
  114. if type(data) == tuple:
  115. return None
  116. return data[0]
  117. def get_all_wind(field_code, need_rated_param=True):
  118. query_sql = """
  119. SELECT t.engine_code,t.engine_name,t.rated_capacity,a.rated_cut_out_windspeed
  120. from wind_engine_group t LEFT JOIN wind_engine_mill a on t.mill_type_code = a.mill_type_code
  121. where t.field_code = %s and t.del_state = 0
  122. """
  123. dict_datas = plt.execute(query_sql, (field_code,))
  124. wind_result = dict()
  125. power_result = dict()
  126. for data in dict_datas:
  127. wind_result[str(data['engine_name'])] = str(data['engine_code'])
  128. if need_rated_param:
  129. power_result[str(data['engine_code'])] = (
  130. float(data['rated_capacity']), float(data['rated_cut_out_windspeed']))
  131. return wind_result, power_result
  132. def get_all_wind_company():
  133. query_sql = "SELECT t.field_name FROM wind_field t where t.del_state = 0"
  134. datas = plt.execute(query_sql)
  135. if datas:
  136. return [v for data in datas for k, v in data.items()]
  137. else:
  138. return ['吉山风电场', '和风元宝山', '唐龙三期风电场', '密马风电场', '招远风电场', '昌平坳风场', '昌西一风电场',
  139. '虹梯官风电场', '长清风电场']
  140. def get_base_wind_and_power(wind_turbine_number):
  141. query_sql = "SELECT rated_wind_speed,rated_capacity FROM wind_engine_group where engine_code = %s order by rated_wind_speed"
  142. dict_datas = plt.execute(query_sql, (wind_turbine_number,))
  143. if type(dict_datas) == tuple:
  144. return None
  145. return dict_datas
  146. if __name__ == '__main__':
  147. # print(get_batch_exec_data(run_count=1))
  148. #
  149. # print("**********************")
  150. # print(get_batch_exec_data(run_count=2))
  151. # print("**********************")
  152. print(get_data_by_batch_no_and_type("test_", "second"))
  153. # print(update_trans_status_success("test_唐龙-定时任务测试", "second", 10))
  154. begin = datetime.datetime.now()
  155. print(get_all_wind('WOF091200006'))