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