db_operation.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import sys
  2. sys.path.append("..")
  3. import mytool.db_helper.mysqlhelper as mdb
  4. # 数据库操作
  5. # 可自行添加操作函数
  6. class DBOperation:
  7. def __init__(self,db_ip, db_port, db_name, db_user, db_password):
  8. self._db = mdb.DB(db_ip, db_port, db_name, db_user, db_password)
  9. # 查询
  10. def query(self, sql):
  11. return self._db.query_all_sql(sql)
  12. # 更新
  13. def update(self, sql):
  14. return self._db.execute_sql(sql)
  15. def query_parking_in_unit_tableid(self, unit,tableid):
  16. sql = "select * from space WHERE unit=%s and table_id=%s"
  17. return self._db.query_all_sql(sql, (unit,tableid))
  18. def query_command_in_unit(self, unit):
  19. sql = "select * from command_queue WHERE unit=%s"
  20. return self._db.query_all_sql(sql, unit)
  21. def query_pick_command_in_unit(self, unit):
  22. sql = "select * from command_queue where unit=%s and type=2 order by queue_id ASC"
  23. return self._db.query_all_sql(sql, unit)
  24. def query_pick_command_in_unit_and_statu(self, unit, statu):
  25. sql = "select * from command_queue where unit=%s and statu=%s and type=2 order by queue_id ASC"
  26. return self._db.query_all_sql(sql, (unit, statu))
  27. def query_sort_pick_command(self, unit):
  28. sql = "select * from command_queue where unit=%s and (statu=0 or statu=1) and type=2 order by queue_id ASC"
  29. return self._db.query_all_sql(sql, unit)
  30. def query_command_all_in_unit_and_sort(self, unit):
  31. sql = "select * from command_queue where unit=%s order by type,statu DESC,queue_id ASC"
  32. return self._db.query_all_sql(sql, unit)
  33. def query_command_all_in_unit_type_and_sort(self, unit, type):
  34. sql = "select * from command_queue where unit=%s and type=%s order by type,statu DESC,queue_id ASC"
  35. return self._db.query_all_sql(sql, (unit, type))
  36. def query_command_all(self):
  37. sql = "select * from command_queue"
  38. return self._db.query_all_sql(sql)
  39. def query_command_in_car_number(self, car_number):
  40. sql = "select * from command_queue WHERE car_number=%s"
  41. return self._db.query_all_sql(sql, car_number)
  42. def query_command_in_primary_key(self, primary_key):
  43. sql = "select * from command_queue WHERE primary_key=%s"
  44. return self._db.query_all_sql(sql, primary_key)
  45. def query_command_in_height_unit_and_statu(self, height, unit):
  46. sql = "select * from command_queue WHERE height>=%s and unit=%s and statu=0 and type=1" # type=1 存车
  47. return self._db.query_all_sql(sql, (height, unit))
  48. def query_command_in_height_minmax_unit_and_statu(self, min,max, unit):
  49. sql = "select * from command_queue WHERE height>=%s and height<=%s and unit=%s and statu=0 and type=1" # type=1 存车
  50. return self._db.query_all_sql(sql, ( min,max, unit))
  51. def query_space_in_car_number(self, car_number):
  52. sql = "select * from space WHERE car_number=%s"
  53. return self._db.query_all_sql(sql, car_number)
  54. def query_space_in_primary_key(self, primary_key):
  55. sql = "select * from space WHERE primary_key=%s"
  56. return self._db.query_all_sql(sql, primary_key)
  57. def query_space_in_unit_and_table_id(self, unit, table_id):
  58. sql = "select * from space WHERE unit=%s and table_id=%s"
  59. return self._db.query_all_sql(sql, (unit, table_id))
  60. def query_space_in_height(self, height):
  61. sql = "select * from space WHERE height>=%s"
  62. return self._db.query_all_sql(sql, height)
  63. def query_space_in_space_id(self, space_id):
  64. sql = "select * from space WHERE id=%s"
  65. return self._db.query_all_sql(sql, space_id)
  66. def query_space_in_height_and_empty(self, height):
  67. sql = "select * from space WHERE height>=%s and statu=0 and car_number IS NULL"
  68. return self._db.query_all_sql(sql, height)
  69. def query_space_in_height_unit_and_empty(self, height, unit):
  70. sql = "select * from space WHERE height>=%s and unit=%s and statu=0 and car_number IS NULL"
  71. return self._db.query_all_sql(sql, (height, unit))
  72. def query_space_in_height_unit_and_empty_2(self, min,max, unit):
  73. sql = "select * from space WHERE height>=%s and height<=%s and unit=%s and statu=0 and car_number IS NULL"
  74. return self._db.query_all_sql(sql, (min, max, unit))
  75. def query_space_in_unit(self, unit):
  76. sql = "select * from space WHERE unit=%s"
  77. return self._db.query_all_sql(sql, unit)
  78. def query_space_all(self):
  79. sql = "select * from space"
  80. return self._db.query_all_sql(sql)
  81. def query_vehicle_primary_key(self, car_number):
  82. sql = "select primary_key from vehicle where car_number=%s"
  83. return self._db.query_all_sql(sql, car_number)
  84. def update_space_status_in_space_id(self, space_id, statu):
  85. sql = "update space set statu=%s where id=%s"
  86. return self._db.execute_sql(sql, (statu, space_id))
  87. def update_space_status_car_number_in_space_id(self, space_id,car_number,statu):
  88. sql = "update space set statu=%s,car_number=%s where id=%s"
  89. return self._db.execute_sql(sql, (statu, car_number,space_id))
  90. def update_command_status_in_car_number(self,car_number,statu):
  91. sql = "update command_queue set statu=%s where car_number=%s"
  92. return self._db.execute_sql(sql, (statu, car_number))
  93. def update_space_remark_and_statu_in_space_id(self,space_id,remark,statu):
  94. sql = "update space set remark=%s,statu=%s where id=%s"
  95. return self._db.execute_sql(sql, (remark, statu,space_id))
  96. def clear_space_data(self, space_id):
  97. sql = "update space set car_number=NULL,primary_key=NULL,statu=0,space_info=NULL,measure_info=NULL,plate_color=NULL,plate_type=NULL,plate_confidence=NULL,recognition_time=NULL,plate_full_image=NULL,plate_clip_image=NULL,remark=NULL where id=%s"
  98. return self._db.execute_sql(sql, space_id)
  99. def delete_command(self, car_number):
  100. sql = "delete from command_queue where car_number=%s"
  101. return self._db.execute_sql(sql, car_number)
  102. # 查询一段时间内停车次数
  103. def query_park_count_in_time(self, in_date, out_date):
  104. sql = "select * from record where in_time_start >= %s and in_time_start <= %s"
  105. return self._db.query_all_sql(sql, (in_date,out_date))
  106. def query_pick_count_out_time(self, in_date, out_date):
  107. sql = "select * from record where out_time_start >= %s and out_time_start <= %s"
  108. return self._db.query_all_sql(sql, (in_date, out_date))
  109. def query_park_count_in_time_and_unit(self, in_date, out_date,unit):
  110. sql = "select * from record where (in_time_start >= %s and in_time_start <= %s) and unit_id=%s"
  111. return self._db.query_all_sql(sql, (in_date,out_date,unit))
  112. def query_pick_count_out_time_and_unit(self, in_date, out_date,unit):
  113. sql = "select * from record where (out_time_start >= %s and out_time_start <= %s) and unit_id=%s"
  114. return self._db.query_all_sql(sql, (in_date, out_date,unit))
  115. # 查询一段时间内停车车数
  116. # period_type = 1 :当天内
  117. # 2 :当月内
  118. # 3 :自定义起止时间
  119. def query_carQuantity_inPeriod(self, period_type, date):
  120. sql = "select count(distinct(car_number)) from record where in_time_start like %s"
  121. if period_type == "day":
  122. date = date[0:10]
  123. elif period_type == "month":
  124. date = date[0:7]
  125. date = date + "%" # 模糊查询
  126. result = self._db.query_all_sql(sql, date) # [{'count(distinct(car_number))': 4}]
  127. return result[0]["count(distinct(car_number))"]