yzy_job.py 15 KB

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