yzy_job.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from datetime import datetime
  4. from sqlalchemy.sql import func
  5. from sqlalchemy.orm import Session
  6. from utils import *
  7. from utils.redis_util import *
  8. from models import *
  9. from exceptions import *
  10. from database import get_local_db
  11. from extensions import logger
  12. from common import YzyApi
  13. from config import settings
  14. import traceback
  15. def proc():
  16. if settings.IS_PROD == False:
  17. return
  18. YzyApi.get_cache_access_token()
  19. lock_key = "yzy_job_proc"
  20. if redis_lock(lock_key):
  21. logger.info(datetime.now())
  22. detail_url = YzyApi.format_redirect_url("/leader/index")
  23. '''
  24. yzy_user_id = "etk4130970pjg84tgrhapo"
  25. description = "预案名称: 茂名市自然灾害救助应急预案\n响应级别: Ⅰ级响应\n消息内容: 单位您好!《茂名市自然灾害救助应急预案》现已全面启动,特此通知您单位迅速响应,全力做好预案工作要点:负责救灾工作宣传报道协调工作。"
  26. YzyApi.send_textcard_message(yzy_user_id, "预案响应消息", description, detail_url)
  27. yzy_user_id = "eb4kehgy6wj4qn0jhx1dk6" # 暂时写死梦梅的账号
  28. description = "预案名称: 茂名市自然灾害救助应急预案\n响应级别: Ⅰ级响应\n消息内容: 单位您好!《茂名市自然灾害救助应急预案》现已全面启动,特此通知您单位迅速响应,全力做好预案工作要点:负责救灾工作宣传报道协调工作。"
  29. # ret = YzyApi.send_textcard_message(yzy_user_id, "预案响应消息", description, detail_url)
  30. # logger.info(ret)
  31. data = {
  32. "yzy_userid": yzy_user_id,
  33. "mobile": "13528373954",
  34. "content": description,
  35. "recorded_by": 1,
  36. "detail_url": detail_url,
  37. "foreign_key": "1",
  38. "from_scenario": "yjya",
  39. "title": "预案响应消息"
  40. }
  41. if settings.IS_STAGE:
  42. YzyApi.add_to_msg_queue(db, data)
  43. db.close()
  44. '''
  45. redis_unlock(lock_key)
  46. def yzy_msg_queue_proc(db = get_local_db()):
  47. if settings.IS_PROD == False:
  48. return
  49. lock_key = "yzy_msg_queue_job"
  50. if redis_lock(lock_key):
  51. logger.info(datetime.now())
  52. rows = db.query(YzyMsgQueue).filter(YzyMsgQueue.sent_status == 0).limit(20).all()
  53. for row in rows:
  54. try:
  55. resp = YzyApi.send_textcard_message(row.yzy_userid, row.title, row.content, row.detail_url)
  56. logger.info(resp)
  57. row.sent_time = datetime.now()
  58. if resp['errcode'] == 0:
  59. row.sent_status = 1
  60. if 'jobid' not in resp:
  61. resp['jobid'] = ''
  62. row.errmsg = resp['jobid']
  63. else:
  64. row.sent_status = 9
  65. row.errmsg = resp['errmsg']
  66. db.commit()
  67. except Exception as e:
  68. traceback.print_exc()
  69. redis_unlock(lock_key)
  70. def yzy_unit_queue_proc():
  71. if settings.IS_PROD == False:
  72. return
  73. with get_local_db() as db:
  74. updatetime = db.query(func.max(YzyOrgUnitEntity.updatetime)).scalar()
  75. if updatetime is None:
  76. updatetime = datetime(2000, 1, 1, 1, 1, 1)
  77. starttime = updatetime.strftime("%Y-%m-%d %H:%M:%S")
  78. endtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  79. # YzyApi.getauthorizedusersbyupdatetime(starttime, endtime)
  80. # YzyApi.getappusersbyupdatetime(starttime, endtime)
  81. result = {
  82. "success" : True,
  83. "errcode" : 0,
  84. "errmessage" : "请求成功",
  85. "data" : [ {
  86. "userid" : "2ajk3d9w8qvncp8t5ekjqq",
  87. "username" : "邓思远",
  88. "gender" : 1,
  89. "mobile" : "a60d8514b5fa644e2e3d76d56902391c",
  90. "unitid" : "esesuh8rzc2jipjel27jcy",
  91. "position" : "专业技术十二级",
  92. "priority" : "1",
  93. "order" : 1073741813,
  94. "status" : 0,
  95. "updateTime" : "2025-08-13 09:18:11"
  96. } ]
  97. }
  98. # result = YzyApi.getunitsbyuintidandupdatetime("0751a8jxfqajk90mt022n1", starttime, endtime)
  99. result = YzyApi.getappunitsbyupdatetime(starttime, endtime)
  100. '''
  101. result = {
  102. "success" : True,
  103. "errcode" : 0,
  104. "errmessage" : "请求成功",
  105. "data" : [ {
  106. "unitid" : "0751a8jxfqajk90mt022n1",
  107. "unitname" : "茂名市政务服务和数据管理局",
  108. "parentunitid" : "0",
  109. "order" : 1073741788,
  110. "unittype" : 6,
  111. "isvirtual" : 0,
  112. "unitpath" : "茂名市政务服务和数据管理局",
  113. "unitfullpath" : "广东省/地市/茂名市/茂名市政府/茂名市政务服务和数据管理局",
  114. "unitidfullpath" : "449f3370-4cf6-11e8-85c3-45ad5e3a2bd7/073rxrx2fqajk90mt022la/073rxteufqajk90mqk6u8d/073ry80jfqajk90mspyvmi/0751a8jxfqajk90mt022n1",
  115. "status" : 0,
  116. "updateTime" : "2025-07-30 10:45:54"
  117. }, {
  118. "unitid" : "0755v3blfqajk90mr1g055",
  119. "unitname" : "数字广东网络建设有限公司",
  120. "parentunitid" : "0",
  121. "order" : 1073741823,
  122. "unittype" : 6,
  123. "isvirtual" : 0,
  124. "unitpath" : "数字广东网络建设有限公司",
  125. "unitfullpath" : "广东省/企业/其他企业/数字广东网络建设有限公司",
  126. "unitidfullpath" : "449f3370-4cf6-11e8-85c3-45ad5e3a2bd7/q0jp1qahizh3fbfhmt6xne/9zeh8xizv7242pof5ye5al/0755v3blfqajk90mr1g055",
  127. "status" : 1,
  128. "updateTime" : "2025-07-30 10:46:03"
  129. }, {
  130. "unitid" : "arhd5gsi3lvxn4zk3czm1m",
  131. "unitname" : "数字广东网络建设有限公司江门市分公司",
  132. "parentunitid" : "0",
  133. "order" : 1073741815,
  134. "unittype" : 6,
  135. "isvirtual" : 0,
  136. "unitpath" : "数字广东网络建设有限公司江门市分公司",
  137. "unitfullpath" : "广东省/地市/江门市/江门市其他单位/江门市企业单位/数字广东网络建设有限公司江门市分公司",
  138. "unitidfullpath" : "449f3370-4cf6-11e8-85c3-45ad5e3a2bd7/073rxrx2fqajk90mt022la/073rxt82fqajk90mrjbt9n/ypjhaxor3ts4a98wt65ltl/smflffowrhm03mvk5xr8sr/arhd5gsi3lvxn4zk3czm1m",
  139. "status" : 0,
  140. "updateTime" : "2025-07-30 10:52:27"
  141. }, {
  142. "unitid" : "yhyseu7xncbsrwtgg85x7z",
  143. "unitname" : "茂名市应急管理局",
  144. "parentunitid" : "0",
  145. "order" : 1073741797,
  146. "unittype" : 6,
  147. "isvirtual" : 0,
  148. "unitpath" : "茂名市应急管理局",
  149. "unitfullpath" : "广东省/地市/茂名市/茂名市政府/茂名市应急管理局",
  150. "unitidfullpath" : "449f3370-4cf6-11e8-85c3-45ad5e3a2bd7/073rxrx2fqajk90mt022la/073rxteufqajk90mqk6u8d/073ry80jfqajk90mspyvmi/yhyseu7xncbsrwtgg85x7z",
  151. "status" : 0,
  152. "updateTime" : "2025-08-13 09:18:11"
  153. } ]
  154. }
  155. '''
  156. if result['errcode'] == 0:
  157. data = result['data']
  158. for unit_info in data:
  159. unitid = unit_info['unitid']
  160. unitname = unit_info['unitname']
  161. unitpath = unit_info['unitpath']
  162. unitfullpath = unit_info['unitfullpath']
  163. unitidfullpath = unit_info['unitidfullpath']
  164. updateTime = unit_info['updateTime']
  165. order = unit_info['order']
  166. unittype = unit_info['unittype']
  167. status = unit_info['status']
  168. parentunitid = unit_info['parentunitid']
  169. unit_dict = {
  170. "unitid": unitid,
  171. "unitname": unitname,
  172. "unitpath": unitpath,
  173. "unitfullpath": unitfullpath,
  174. "unitidfullpath": unitidfullpath,
  175. "order": order,
  176. "orgtype": unittype,
  177. "updatetime": updateTime,
  178. "priority": status,
  179. "parentunitid": parentunitid,
  180. "weworkpartyid": "ok"
  181. }
  182. unit_row = db.query(YzyOrgUnitEntity).filter(YzyOrgUnitEntity.unitid == unitid).first()
  183. if unit_row is None:
  184. new_unit = YzyOrgUnitEntity(**unit_dict)
  185. new_unit.createtime = datetime.now()
  186. db.add(new_unit)
  187. else:
  188. # 每次都是 0
  189. del unit_dict['parentunitid']
  190. db.query(YzyOrgUnitEntity).filter(YzyOrgUnitEntity.unitid == unitid).update(unit_dict)
  191. db.commit()
  192. unit_path_list = unitfullpath.split("/")
  193. unitid_path_list = unitidfullpath.split("/")
  194. for i in range(len(unit_path_list)):
  195. n = len(unit_path_list) - i - 1
  196. unitname = unit_path_list[n]
  197. unitid = unitid_path_list[n]
  198. parentunitid = '0'
  199. if n > 0:
  200. parentunitid = unitid_path_list[n - 1]
  201. unit_row = db.query(YzyOrgUnitEntity).filter(YzyOrgUnitEntity.unitid == unitid).first()
  202. if unit_row is None:
  203. new_unit = YzyOrgUnitEntity(unitid = unitid, unitname = unitname, unitpath = unitname, parentunitid = parentunitid, order = -1, weworkpartyid = '', updatetime=datetime.now())
  204. new_unit.createtime = datetime.now()
  205. db.add(new_unit)
  206. db.commit()
  207. else:
  208. unit_row.parentunitid = parentunitid
  209. db.commit()