duty_job.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from datetime import datetime, date
  4. from sqlalchemy.orm import Session
  5. from sqlalchemy import text, exists, and_, or_, not_
  6. from utils import *
  7. from utils.redis_util import *
  8. from models import *
  9. from exceptions import *
  10. from database import get_db_local
  11. from extensions import logger
  12. from common import YzyApi
  13. from config import settings
  14. import traceback
  15. from pprint import pprint
  16. from common.db import db_user, db_yzy, db_msg_center
  17. from config import settings
  18. # 值班任务
  19. # 每天00:00将当天值班人员推送一次
  20. def proc():
  21. lock_key = "duty_job_proc"
  22. if redis_lock(lock_key):
  23. logger.info(datetime.now())
  24. db = get_db_local()
  25. __duty_proc(db)
  26. db.close()
  27. redis_unlock(lock_key)
  28. def format_time(time_diff) -> str:
  29. hours, minutes, seconds = str(time_diff).split(':')
  30. return str(hours).zfill(2) + ":" + str(minutes).zfill(2)
  31. def __duty_proc(db: Session):
  32. theDay = date.today()
  33. rows = db.query(DutyShift).filter(and_(DutyShift.shift_date == theDay, DutyShift.del_flag == '0')).all()
  34. for row in rows:
  35. start_time = format_time(row.start_time)
  36. end_time = format_time(row.end_time)
  37. to_users = [row.leader_id, row.primary_staff_id, row.secondary_staff_id, row.standby_staff_id]
  38. for to_user_id in to_users:
  39. if(is_not_sent(db, to_user_id, theDay)):
  40. send_yzy_msg(db, to_user_id, start_time, end_time, str(row.shift_id))
  41. def is_not_sent(db: Session, to_user_id: str, theDay: DateTime):
  42. user_info = db_user.get_user_info(db, to_user_id)
  43. yzy_account = user_info.yzy_account
  44. where = and_(YzyMsgQueue.mobile == yzy_account,
  45. YzyMsgQueue.from_scenario == 'duty_shift',
  46. YzyMsgQueue.create_time >= theDay,
  47. YzyMsgQueue.create_time < theDay + timedelta(days=1),
  48. )
  49. row = db.query(YzyMsgQueue).filter(where).first()
  50. return row is None
  51. # 发送粤政易消息
  52. def send_yzy_msg(db: Session, to_user_id: int, start_time: str, end_time: str, foreign_key: str) -> None:
  53. user_info = db_user.get_user_info(db, to_user_id)
  54. yzy_account = user_info.yzy_account
  55. yzy_userid = db_yzy.get_userid_by_account(db, yzy_account)
  56. detail_url = "{}{}".format(settings.YZY_WEB_ROOT, "/yjxp/#/worker/duty")
  57. description = f"您今天的值班时间为{start_time}-{end_time},请准时到岗。"
  58. logger.info("{}, {}", yzy_account, description)
  59. data = {
  60. "yzy_userid": yzy_userid,
  61. "mobile": yzy_account,
  62. "content": description,
  63. "recorded_by": 0,
  64. "detail_url": detail_url,
  65. "foreign_key": foreign_key,
  66. "from_scenario": "duty_shift",
  67. "title": "值班消息"
  68. }
  69. YzyApi.add_to_msg_queue(db, data)
  70. db_msg_center.add_message(db, "值班消息", to_user_id, "值班提醒", description, foreign_key, 'duty_shift')