yzy_job.py 13 KB

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