daily/Mods/Notice/Utils.py

169 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import threading
import time
from concurrent.futures import ThreadPoolExecutor
from sqlalchemy import func, or_, cast, DATE
from sqlalchemy.orm import Session
from Models.DailyModel import Daily
from Models.DepartmentModel import Department
from Models.UserModel import User
from Utils.EmailUtils import send_email
from Utils.SqlAlchemyUtils import get_db, get_db_i
from datetime import datetime, date
test_email = ["wuchunquan@fecr.com.cn", "wangsichuan@fecr.com.cn"]
comment_notice_send = """您的日报有新的反馈:
日报内容:
{daily_type}
{daily_content}
日报时间: {daily_time}
反馈内容:
{comment_content}
反馈人: {comment_user_name}
时间: {comment_time}"""
comment_notice_send = """您于【{daily_time}】发布在【{daily_type}】的动态收到领导反馈。
领导【{comment_user_name}】反馈:
{comment_content}
发布正文:
{daily_content} """
daily_fill_send = """您好:
请于今日下班前登录微信小程序【FECR业务助手】填报您的工作内容。
具体填报步骤:
1. 使用手机号或公司邮箱登录【FECR业务助手】。
2. 在填报页面中选择您的报送类型,进入相关填报页面进行操作。
若在使用过程中出现问题请联系陈肖珏chenxiaojue@fecr.com.cn帮助您处理。
"""
daily_pdf_upload_notice = """您好
今日的运行日报已上传请于微信小程序【FECR业务助手】->【运行日报】查看
"""
# 2023年的节假日true的是放假的
holiday = {'01-01': True, '01-02': True, '01-21': True, '01-22': True, '01-23': True, '01-24': True, '01-25': True,
'01-26': True, '01-27': True, '01-28': False, '01-29': False, '04-05': True, '04-23': False, '04-29': True,
'04-30': True, '05-01': True, '05-02': True, '05-03': True, '05-06': False, '06-22': True, '06-23': True,
'06-24': True, '06-25': False, '09-29': True, '09-30': True, '10-01': True, '10-02': True, '10-03': True,
'10-04': True, '10-05': True, '10-06': True, '10-07': False, '10-08': False, '12-31': True}
def get_user_not_fill_daily():
with get_db_i() as db:
db: Session
daily_list = db.query(Daily).filter(cast(Daily.daily_time, DATE) == datetime.now().date())
filled_user_list = {item.fill_user for item in daily_list}
notice_user_list = [item for item in db.query(User).filter(User.email.not_in(filled_user_list)) if
item.name not in ['邢军'] and item.daily_fill_notice is True]
email_list = [user.email for user in notice_user_list]
name_list = [user.name for user in notice_user_list]
# print(name_list)
return notice_user_list
def is_workday():
"""
返回当天是否上班
:return:
"""
now = datetime.now()
day = now.strftime('%m-%d')
weekday = datetime.now().isoweekday()
if day in holiday:
if holiday[day]:
return False
else:
return True
else:
if weekday in [6, 7]:
return False
else:
return True
class DailyNotice:
def __init__(self, email_send_func=send_email):
self.email_send_func = email_send_func
pass
def send_email(self, title, email, content):
try:
print('邮件', email)
return self.email_send_func(title, email, content)
except Exception as e:
print('邮件发送错误', e)
return False
def notice_comment(self, title, email, content):
"""
领导反馈的通知
:return:
"""
return self.send_email(title, email, content)
def send_on_daily_pdf_upload(self):
"""
每日日报上传后的通知
:return:
"""
# 有查看权限的名单
emails = []
with get_db_i() as db:
db: Session
can_watch_departments = [item.id for item in
db.query(Department).filter(func.find_in_set('16', Department.auth_data))]
for user in db.query(User).filter(or_(func.find_in_set('16', User.auth_data),
*[func.find_in_set(str(d_id), User.department) for d_id in
can_watch_departments])):
emails.append(user.email)
# 数字化部emails = [item.email for item in db.query(User).filter(func.find_in_set('17', User.department))]
# task_args = [["", email, daily_pdf_upload_notice] for email in emails]
# 测试
task_args = [["每日运行日报已上传", email, daily_pdf_upload_notice] for email in emails]
print(task_args)
self.email_send_thread(task_args)
def start_timer_to_notice_daily_fill(self):
print('邮件发送服务已启动')
sended = False
while True:
time.sleep(15)
now = datetime.now()
if is_workday() and now.hour == 15 and (35 <= now.minute < 35 + 3) and not sended:
sended = True
try:
user_list = get_user_not_fill_daily()
args_list = [['日报填报提醒', user.email, daily_fill_send] for user in user_list if
user.daily_fill_notice]
# args_list = [['日报填报提醒', user.email, daily_fill_send] for user in user_list if user.name in ['王思川','伍春全','李凯','彭森','徐聿成','陈世杰']]
self.email_send_thread(args_list)
# send_email('日报填报提醒', "fecribd@fecr.com.cn", daily_fill_send)
except Exception as e:
print(e, 'start_timer_to_notice_daily_fill错误')
if datetime.now().hour > 15:
sended = False
def email_send_thread(self, args_list, sem=5):
with ThreadPoolExecutor(max_workers=sem) as executor:
for args in args_list:
time.sleep(1)
executor.submit(self.send_email, *args)
def init(self):
threading.Thread(target=self.start_timer_to_notice_daily_fill).start()
daily_notice = DailyNotice()