duty_job.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_local_db
  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. with get_local_db() as db:
  25. __duty_old_proc(db)
  26. __duty_proc(db)
  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. # 删除今天之前的值班提醒
  32. def __duty_old_proc(db: Session):
  33. theDay = date.today()
  34. db.query(MsgCenter).filter(and_(MsgCenter.msg_type == '值班消息', MsgCenter.recv_time < theDay, MsgCenter.del_flag == '0', MsgCenter.recv_status == 0)).update({"del_flag": 1, "update_time": datetime.now()})
  35. db.commit()
  36. # 新建今天的值班提醒
  37. def __duty_proc(db: Session):
  38. theDay = date.today()
  39. rows = db.query(DutyShift).filter(and_(DutyShift.shift_date == theDay, DutyShift.del_flag == '0')).all()
  40. for row in rows:
  41. start_time = format_time(row.start_time)
  42. end_time = format_time(row.end_time)
  43. to_users = [row.leader_id, row.primary_staff_id, row.secondary_staff_id, row.standby_staff_id]
  44. for to_user_id in to_users:
  45. if(is_not_sent(db, to_user_id, theDay)):
  46. send_yzy_msg(db, to_user_id, start_time, end_time, str(row.shift_id))
  47. def is_not_sent(db: Session, to_user_id: str, theDay: DateTime):
  48. user_info = db_user.get_user_info(db, to_user_id)
  49. yzy_account = user_info.yzy_account
  50. where = and_(YzyMsgQueue.mobile == yzy_account,
  51. YzyMsgQueue.from_scenario == 'duty_shift',
  52. YzyMsgQueue.create_time >= theDay,
  53. YzyMsgQueue.create_time < theDay + timedelta(days=1),
  54. )
  55. row = db.query(YzyMsgQueue).filter(where).first()
  56. return row is None
  57. # 发送粤政易消息
  58. def send_yzy_msg(db: Session, to_user_id: int, start_time: str, end_time: str, foreign_key: str) -> None:
  59. user_info = db_user.get_user_info(db, to_user_id)
  60. yzy_account = user_info.yzy_account
  61. yzy_userid = db_yzy.get_userid_by_account(db, yzy_account)
  62. detail_url = "{}{}".format(settings.YZY_WEB_ROOT, "/yjxp/#/worker/duty")
  63. description = f"您今天的值班时间为{start_time}-{end_time},请准时到岗。"
  64. logger.info("{}, {}", yzy_account, description)
  65. data = {
  66. "yzy_userid": yzy_userid,
  67. "mobile": yzy_account,
  68. "content": description,
  69. "recorded_by": 0,
  70. "detail_url": detail_url,
  71. "foreign_key": foreign_key,
  72. "from_scenario": "duty_shift",
  73. "title": "值班消息"
  74. }
  75. YzyApi.add_to_msg_queue(db, data)
  76. db_msg_center.add_message(db, "值班消息", to_user_id, "值班提醒", description, foreign_key, 'duty_shift')