yzy_job.py 15 KB

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