EquipmentInfoService.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : EquipmentInfoService.py
  4. @Time : 2022/4/14 17:28
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import datetime
  10. import time
  11. from Model.models import EquipmentInfoMonday, EquipmentInfoTuesday, EquipmentInfoWednesday, EquipmentInfoThursday, \
  12. EquipmentInfoFriday, EquipmentInfoSaturday, EquipmentInfoSunday
  13. from Object.utils import LocalDateTimeUtil
  14. """
  15. 设备分表查询Service
  16. 因数据量不断增加,单表保留近七天数据进行分表优化设计
  17. 进行拆分为七张表星期一至星期天进行分表存储。分担单表存储读写压力。
  18. """
  19. class EquipmentInfoService:
  20. @staticmethod
  21. def get_equipment_info_model(dt, val=0):
  22. """
  23. 根据日期判断是星期几,返回相应的Model对象
  24. @param val: 1-7代表week
  25. @param dt: 日期 例:2022-03-03
  26. @return: 星期一至星期天equipment_info对象实例
  27. """
  28. week = 1
  29. if dt:
  30. week = LocalDateTimeUtil.date_to_week(dt)
  31. if 0 < val < 8:
  32. week = val
  33. equipment_info = None
  34. if week == 1:
  35. equipment_info = EquipmentInfoMonday.objects.all()
  36. elif week == 2:
  37. equipment_info = EquipmentInfoTuesday.objects.all()
  38. elif week == 3:
  39. equipment_info = EquipmentInfoWednesday.objects.all()
  40. elif week == 4:
  41. equipment_info = EquipmentInfoThursday.objects.all()
  42. elif week == 5:
  43. equipment_info = EquipmentInfoFriday.objects.all()
  44. elif week == 6:
  45. equipment_info = EquipmentInfoSaturday.objects.all()
  46. elif week == 7:
  47. equipment_info = EquipmentInfoSunday.objects.all()
  48. return equipment_info
  49. @classmethod
  50. def find_by_start_time_equipment_info(cls, page, size, user_id, start_time, end_time, event_type,
  51. uid_list):
  52. """
  53. 通过start_time查找指定日期当天设备消息推送
  54. @param page: 页数
  55. @param size: 每页条数
  56. @param user_id: 设备用户id
  57. @param start_time: 开始时间
  58. @param end_time: 结束时间
  59. @param event_type: 事件类型
  60. @param uid_list: 设备uid列表
  61. @return: result 查询结果
  62. """
  63. if start_time and end_time:
  64. start_date = datetime.datetime.fromtimestamp(int(start_time))
  65. # 根据开始日期,获取设备信息查询对象
  66. qs = EquipmentInfoService.get_equipment_info_model(str(start_date.date()), 0)
  67. # 调用查询方法
  68. qs = cls.query_equipment_info(qs, user_id, start_time, end_time, event_type,
  69. uid_list)
  70. # 时区问题
  71. week = LocalDateTimeUtil.date_to_week(str(start_date.date()))
  72. if week > 0:
  73. # 根据筛选日期 查找昨天数据
  74. yesterday = 7 if week == 1 else week - 1
  75. yesterday_info = EquipmentInfoService.get_equipment_info_model('', yesterday)
  76. yesterday_info = cls.query_equipment_info(yesterday_info, user_id, start_time, end_time, event_type,
  77. uid_list)
  78. # 根据筛选日期 查找明天数据
  79. tomorrow = 1 if week == 7 else week + 1
  80. tomorrow_info = EquipmentInfoService.get_equipment_info_model('', tomorrow)
  81. tomorrow_info = cls.query_equipment_info(tomorrow_info, user_id, start_time, end_time, event_type,
  82. uid_list)
  83. qs = qs.union(yesterday_info, tomorrow_info, all=True)
  84. if qs.exists():
  85. count = qs.count()
  86. qs_page = cls.get_equipment_info_page(qs, page, size)
  87. return qs_page, count
  88. return None, 0
  89. @classmethod
  90. def get_equipment_info_week_all(cls, page, size, user_id, start_time, end_time, event_type,
  91. uid_list):
  92. """
  93. 分表查询近七天设备消息推送
  94. @param page: 页数
  95. @param size: 分页大小
  96. @param user_id: 设备用户id
  97. @param start_time: 事件开始时间
  98. @param end_time: 事件结束时间
  99. @param event_type: 事件类型
  100. @param uid_list: uid列表
  101. @return: qs_page, count 结果集
  102. """
  103. # 星期一设备信息查询
  104. monday_qs = EquipmentInfoService.get_equipment_info_model('', 1)
  105. monday_qs = cls.query_equipment_info(monday_qs, user_id, start_time, end_time, event_type,
  106. uid_list)
  107. # 星期二设备信息查询
  108. tuesday_qs = EquipmentInfoService.get_equipment_info_model('', 2)
  109. tuesday_qs = cls.query_equipment_info(tuesday_qs, user_id, start_time, end_time, event_type,
  110. uid_list)
  111. # 星期三设备信息查询
  112. wednesday_qs = EquipmentInfoService.get_equipment_info_model('', 3)
  113. wednesday_qs = cls.query_equipment_info(wednesday_qs, user_id, start_time, end_time, event_type,
  114. uid_list)
  115. # 星期四设备信息查询
  116. thursday_qs = EquipmentInfoService.get_equipment_info_model('', 4)
  117. thursday_qs = cls.query_equipment_info(thursday_qs, user_id, start_time, end_time, event_type,
  118. uid_list)
  119. # 星期五设备信息查询
  120. friday_qs = EquipmentInfoService.get_equipment_info_model('', 5)
  121. friday_qs = cls.query_equipment_info(friday_qs, user_id, start_time, end_time, event_type,
  122. uid_list)
  123. # 星期六设备信息查询
  124. saturday_qs = EquipmentInfoService.get_equipment_info_model('', 6)
  125. saturday_qs = cls.query_equipment_info(saturday_qs, user_id, start_time, end_time, event_type,
  126. uid_list)
  127. # 星期天设备信息查询
  128. sunday_qs = EquipmentInfoService.get_equipment_info_model('', 7)
  129. sunday_qs = cls.query_equipment_info(sunday_qs, user_id, start_time, end_time, event_type,
  130. uid_list)
  131. result = monday_qs.union(tuesday_qs, wednesday_qs, thursday_qs, friday_qs, saturday_qs, sunday_qs, all=True)
  132. count = result.count()
  133. qs_page = cls.get_equipment_info_page(result, page, size)
  134. return qs_page, count
  135. @classmethod
  136. def query_equipment_info(cls, qs, user_id, start_time, end_time, event_type,
  137. uid_list):
  138. """
  139. 设备信息条件查询,根据分表设计,默认条件event_time大于七天前时间
  140. @param qs: 设备信息查询对象
  141. @param user_id: 设备用户id
  142. @param start_time: 开始时间
  143. @param end_time: 结束时间
  144. @param event_type: 事件类型
  145. @param uid_list: 设备uid列表
  146. @return: result 设备信息结果集
  147. """
  148. now_time = int(time.time())
  149. # 获取七天前时间戳
  150. seven_days_before_time = LocalDateTimeUtil.get_before_days_timestamp(now_time, 7)
  151. # 默认查询当前表event_time大于七天前时间
  152. qs = qs.filter(event_time__gt=seven_days_before_time)
  153. if user_id:
  154. qs = qs.filter(device_user_id=user_id)
  155. if event_type:
  156. # 兼容AI查询
  157. if ',' in event_type:
  158. eventTypeList = event_type.split(',')
  159. eventTypeList = [int(i.strip()) for i in eventTypeList]
  160. qs = qs.filter(event_type__in=eventTypeList)
  161. else:
  162. qs = qs.filter(event_type=event_type)
  163. if start_time and end_time:
  164. qs = qs.filter(event_time__range=(start_time, end_time))
  165. if uid_list:
  166. uid_list = uid_list.split(',')
  167. qs = qs.filter(device_uid__in=uid_list)
  168. return qs
  169. @classmethod
  170. def get_equipment_info_page(cls, equipment_info_qs, page, size):
  171. """
  172. 获取查询结果集进行排序、分页,遍历重命名字典key(主要针对原函数返回结果集)
  173. @param equipment_info_qs: 设备信息结果集
  174. @param page: 页数
  175. @param size: 分页大小
  176. @return: qs_page 遍历后的设备信息结果集
  177. """
  178. equipment_info_qs = equipment_info_qs.values('id', 'device_uid', 'device_nick_name', 'channel', 'event_type',
  179. 'status', 'alarm',
  180. 'event_time', 'receive_time', 'is_st', 'add_time',
  181. 'storage_location', 'border_coords')
  182. equipment_info_qs = equipment_info_qs.order_by('-event_time')
  183. qs_page = equipment_info_qs[(page - 1) * size:page * size]
  184. if qs_page and len(qs_page) > 0:
  185. for item in qs_page:
  186. event_time = item['event_time']
  187. item['id'] = int(str(event_time) + str(item['id']))
  188. item['devUid'] = item['device_uid']
  189. item['devNickName'] = item['device_nick_name']
  190. item['Channel'] = item['channel']
  191. item['eventType'] = item['event_type']
  192. item['eventTime'] = item['event_time']
  193. item['receiveTime'] = item['receive_time']
  194. item['addTime'] = item['add_time']
  195. item['borderCoords'] = item['border_coords']
  196. item.pop('device_uid')
  197. item.pop('device_nick_name')
  198. item.pop('channel')
  199. item.pop('event_type')
  200. item.pop('event_time')
  201. item.pop('receive_time')
  202. item.pop('add_time')
  203. item.pop('border_coords')
  204. return qs_page