yzy_job.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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():
  47. print('yzy_msg_queue_proc ')
  48. if settings.IS_PROD == False:
  49. return
  50. with get_local_db() as db:
  51. print('yzy_msg_queue_proc starting...')
  52. lock_key = "yzy_msg_queue_job"
  53. if redis_lock(lock_key):
  54. logger.info(datetime.now())
  55. rows = db.query(YzyMsgQueue).filter(YzyMsgQueue.sent_status == 0).limit(20).all()
  56. for row in rows:
  57. try:
  58. resp = YzyApi.send_textcard_message(row.yzy_userid, row.title, row.content, row.detail_url)
  59. logger.info(resp)
  60. row.sent_time = datetime.now()
  61. if resp['errcode'] == 0:
  62. row.sent_status = 1
  63. if 'jobid' not in resp:
  64. resp['jobid'] = ''
  65. row.errmsg = resp['jobid']
  66. else:
  67. row.sent_status = 9
  68. row.errmsg = resp['errmsg']
  69. db.commit()
  70. except Exception as e:
  71. traceback.print_exc()
  72. redis_unlock(lock_key)
  73. print('yzy_msg_queue_proc end.')
  74. def yzy_unit_queue_proc():
  75. if settings.IS_PROD == False:
  76. return
  77. with get_local_db() as db:
  78. updatetime = db.query(func.max(YzyOrgUserEntity.updatetime)).scalar()
  79. if updatetime is None:
  80. updatetime = datetime(2000, 1, 1, 1, 1, 1)
  81. starttime = updatetime.strftime("%Y-%m-%d %H:%M:%S")
  82. endtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  83. # 这个看起来没用
  84. # YzyApi.getauthorizedusersbyupdatetime(starttime, endtime)
  85. result = YzyApi.getappusersbyupdatetime(starttime, endtime)
  86. '''
  87. result = {
  88. "success" : True,
  89. "errcode" : 0,
  90. "errmessage" : "请求成功",
  91. "data" : [ {
  92. "userid" : "imi85r9my4a9h1ls9c1fnz",
  93. "username" : "梁文龙",
  94. "gender" : 1,
  95. "mobile" : "49779889d18a2a9cadf132014e173116",
  96. "unitid" : "v5vd6t4zkbd9gz0ijlzj73",
  97. "position" : "主任",
  98. "priority" : "1",
  99. "order" : 1073741825,
  100. "status" : 0,
  101. "updateTime" : "2025-08-22 16:24:28"
  102. }, {
  103. "userid" : "28cm254t2kc5hjc70saq8x",
  104. "username" : "黄晓东",
  105. "gender" : 1,
  106. "mobile" : "97651f2cd1206c3ab205cd581606525f",
  107. "unitid" : "v5vd6t4zkbd9gz0ijlzj73",
  108. "position" : "一级科员",
  109. "priority" : "1",
  110. "order" : 1073741814,
  111. "status" : 0,
  112. "updateTime" : "2025-08-22 16:25:18"
  113. } ]
  114. }
  115. result = {
  116. "success" : True,
  117. "errcode" : 0,
  118. "errmessage" : "请求成功",
  119. "data" : [ {
  120. "userid" : "2ajk3d9w8qvncp8t5ekjqq",
  121. "username" : "邓思远",
  122. "gender" : 1,
  123. "mobile" : "a60d8514b5fa644e2e3d76d56902391c",
  124. "unitid" : "esesuh8rzc2jipjel27jcy",
  125. "position" : "专业技术十二级",
  126. "priority" : "1",
  127. "order" : 1073741813,
  128. "status" : 0,
  129. "updateTime" : "2025-08-13 09:18:11"
  130. } ]
  131. }
  132. '''
  133. if result['errcode'] == 0:
  134. data = result['data']
  135. for user_info in data:
  136. userid = user_info['userid']
  137. username = user_info['username']
  138. gender = user_info['gender']
  139. mobile = user_info['mobile']
  140. unitid = user_info['unitid']
  141. position = user_info['position']
  142. priority = user_info['priority']
  143. order = user_info['order']
  144. status = user_info['status']
  145. updateTime = user_info['updateTime']
  146. telephonenumber = ''
  147. try:
  148. # 敏感数据加密算法(DES 对称加密)
  149. telephonenumber = YzyApi.desDecryptValue(settings.YZY_CORPSECRET, mobile)
  150. except:
  151. traceback.print_exc()
  152. unitpath = None
  153. unit_row = db.query(YzyOrgUnitEntity).filter(YzyOrgUnitEntity.unitid == unitid).first()
  154. if unit_row is not None:
  155. unitpath = unit_row.unitpath
  156. user_dict = {
  157. "userid": userid,
  158. "username": username,
  159. "displayname": username,
  160. "gender": gender,
  161. "telephonenumber": telephonenumber,
  162. "updatetime": updateTime,
  163. "unitid": unitid,
  164. "position": position,
  165. "priority": priority,
  166. "order_num": order,
  167. "unitpath": unitpath
  168. }
  169. unit_row = db.query(YzyOrgUserEntity).filter(YzyOrgUserEntity.userid == userid).first()
  170. if unit_row is None:
  171. new_unit = YzyOrgUserEntity(**user_dict)
  172. new_unit.createtime = datetime.now()
  173. db.add(new_unit)
  174. else:
  175. db.query(YzyOrgUserEntity).filter(YzyOrgUserEntity.userid == userid).update(user_dict)
  176. db.commit()
  177. # result = YzyApi.getunitsbyuintidandupdatetime("v5vd6t4zkbd9gz0ijlzj73", starttime, endtime)
  178. updatetime = db.query(func.max(YzyOrgUnitEntity.updatetime)).scalar()
  179. if updatetime is None:
  180. updatetime = datetime(2000, 1, 1, 1, 1, 1)
  181. starttime = updatetime.strftime("%Y-%m-%d %H:%M:%S")
  182. endtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  183. result = YzyApi.getappunitsbyupdatetime(starttime, endtime)
  184. '''
  185. result = {
  186. "success" : True,
  187. "errcode" : 0,
  188. "errmessage" : "请求成功",
  189. "data" : [ {
  190. "unitid" : "0751a8jxfqajk90mt022n1",
  191. "unitname" : "茂名市政务服务和数据管理局",
  192. "parentunitid" : "0",
  193. "order" : 1073741788,
  194. "unittype" : 6,
  195. "isvirtual" : 0,
  196. "unitpath" : "茂名市政务服务和数据管理局",
  197. "unitfullpath" : "广东省/地市/茂名市/茂名市政府/茂名市政务服务和数据管理局",
  198. "unitidfullpath" : "449f3370-4cf6-11e8-85c3-45ad5e3a2bd7/073rxrx2fqajk90mt022la/073rxteufqajk90mqk6u8d/073ry80jfqajk90mspyvmi/0751a8jxfqajk90mt022n1",
  199. "status" : 0,
  200. "updateTime" : "2025-07-30 10:45:54"
  201. }, {
  202. "unitid" : "0755v3blfqajk90mr1g055",
  203. "unitname" : "数字广东网络建设有限公司",
  204. "parentunitid" : "0",
  205. "order" : 1073741823,
  206. "unittype" : 6,
  207. "isvirtual" : 0,
  208. "unitpath" : "数字广东网络建设有限公司",
  209. "unitfullpath" : "广东省/企业/其他企业/数字广东网络建设有限公司",
  210. "unitidfullpath" : "449f3370-4cf6-11e8-85c3-45ad5e3a2bd7/q0jp1qahizh3fbfhmt6xne/9zeh8xizv7242pof5ye5al/0755v3blfqajk90mr1g055",
  211. "status" : 1,
  212. "updateTime" : "2025-07-30 10:46:03"
  213. }, {
  214. "unitid" : "arhd5gsi3lvxn4zk3czm1m",
  215. "unitname" : "数字广东网络建设有限公司江门市分公司",
  216. "parentunitid" : "0",
  217. "order" : 1073741815,
  218. "unittype" : 6,
  219. "isvirtual" : 0,
  220. "unitpath" : "数字广东网络建设有限公司江门市分公司",
  221. "unitfullpath" : "广东省/地市/江门市/江门市其他单位/江门市企业单位/数字广东网络建设有限公司江门市分公司",
  222. "unitidfullpath" : "449f3370-4cf6-11e8-85c3-45ad5e3a2bd7/073rxrx2fqajk90mt022la/073rxt82fqajk90mrjbt9n/ypjhaxor3ts4a98wt65ltl/smflffowrhm03mvk5xr8sr/arhd5gsi3lvxn4zk3czm1m",
  223. "status" : 0,
  224. "updateTime" : "2025-07-30 10:52:27"
  225. }, {
  226. "unitid" : "yhyseu7xncbsrwtgg85x7z",
  227. "unitname" : "茂名市应急管理局",
  228. "parentunitid" : "0",
  229. "order" : 1073741797,
  230. "unittype" : 6,
  231. "isvirtual" : 0,
  232. "unitpath" : "茂名市应急管理局",
  233. "unitfullpath" : "广东省/地市/茂名市/茂名市政府/茂名市应急管理局",
  234. "unitidfullpath" : "449f3370-4cf6-11e8-85c3-45ad5e3a2bd7/073rxrx2fqajk90mt022la/073rxteufqajk90mqk6u8d/073ry80jfqajk90mspyvmi/yhyseu7xncbsrwtgg85x7z",
  235. "status" : 0,
  236. "updateTime" : "2025-08-13 09:18:11"
  237. } ]
  238. }
  239. '''
  240. if result['errcode'] == 0:
  241. data = result['data']
  242. for unit_info in data:
  243. unitid = unit_info['unitid']
  244. unitname = unit_info['unitname']
  245. unitpath = unit_info['unitpath']
  246. unitfullpath = unit_info['unitfullpath']
  247. unitidfullpath = unit_info['unitidfullpath']
  248. updateTime = unit_info['updateTime']
  249. order = unit_info['order']
  250. unittype = unit_info['unittype']
  251. status = unit_info['status']
  252. parentunitid = unit_info['parentunitid']
  253. unit_dict = {
  254. "unitid": unitid,
  255. "unitname": unitname,
  256. "unitpath": unitpath,
  257. "unitfullpath": unitfullpath,
  258. "unitidfullpath": unitidfullpath,
  259. "order": order,
  260. "orgtype": unittype,
  261. "updatetime": updateTime,
  262. "priority": status,
  263. "parentunitid": parentunitid,
  264. "weworkpartyid": "ok"
  265. }
  266. unit_row = db.query(YzyOrgUnitEntity).filter(YzyOrgUnitEntity.unitid == unitid).first()
  267. if unit_row is None:
  268. new_unit = YzyOrgUnitEntity(**unit_dict)
  269. new_unit.createtime = datetime.now()
  270. db.add(new_unit)
  271. else:
  272. # 每次都是 0
  273. del unit_dict['parentunitid']
  274. db.query(YzyOrgUnitEntity).filter(YzyOrgUnitEntity.unitid == unitid).update(unit_dict)
  275. db.commit()
  276. unit_path_list = unitfullpath.split("/")
  277. unitid_path_list = unitidfullpath.split("/")
  278. for i in range(len(unit_path_list)):
  279. n = len(unit_path_list) - i - 1
  280. unitname = unit_path_list[n]
  281. unitid = unitid_path_list[n]
  282. parentunitid = '0'
  283. if n > 0:
  284. parentunitid = unitid_path_list[n - 1]
  285. unit_row = db.query(YzyOrgUnitEntity).filter(YzyOrgUnitEntity.unitid == unitid).first()
  286. if unit_row is None:
  287. new_unit = YzyOrgUnitEntity(unitid = unitid, unitname = unitname, unitpath = unitname, parentunitid = parentunitid, order = -1, weworkpartyid = '', updatetime=datetime.now())
  288. new_unit.createtime = datetime.now()
  289. db.add(new_unit)
  290. db.commit()
  291. else:
  292. unit_row.parentunitid = parentunitid
  293. db.commit()