Files
gzth/yuthon_will_task/models/yuthon_will_task.py
T
2024-11-25 18:20:19 +08:00

69 lines
3.4 KiB
Python

from odoo import api, fields, models, modules
import datetime
from datetime import datetime, timedelta, time
from odoo.exceptions import UserError
class YuthonWillTask(models.Model):
_name = 'yuthon.will.task'
_description = '日程'
_rec_name = 'task_content'
_inherit = ['mail.thread.main.attachment', 'mail.activity.mixin']
priority = fields.Selection([('general', '不紧急不重要'), ('important', '重要不紧急'),
('urgent', '紧急不重要'), ('very_urgent', '重要且紧急')], string='优先级')
task_type = fields.Many2one('task.type.setting', string='任务类型')
task_content = fields.Text(string='任务内容')
remarks = fields.Text(string='备注')
creation_time = fields.Date(string='创建时间')
employee_ids = fields.Many2many('hr.employee', string='人员', default=lambda self: self.env.user.employee_id)
task_time_ids = fields.Many2many('yuthon.task.time', string='提醒')
start_time = fields.Datetime(string='开始时间', default=fields.Date.today())
end_time = fields.Datetime(string='结束时间', default=lambda self: datetime.combine(datetime.now().date(), time(11, 0, 0)))
def will_task_time(self):
tasks = self.env['yuthon.will.task'].search([])
for task in tasks:
for task_time in task.task_time_ids:
time_difference = task.end_time - datetime.now()
duration = self._compute_duration(task_time)
if time_difference <= duration and time_difference >= timedelta(seconds=0):
task.add_comment() # 将task_time对象传递给add_comment方法
break # 触发一个提醒后就跳出内循环
def _compute_duration(self, task_time):
if task_time.task_unit == 'min':
return timedelta(minutes=task_time.number)
elif task_time.task_unit == 'hour':
return timedelta(hours=task_time.number)
elif task_time.task_unit == 'day':
return timedelta(days=task_time.number)
else:
return timedelta.max
def add_comment(self):
self.ensure_one()
for employee_id in self.employee_ids:
if not employee_id.wecom_userid:
raise UserError("请同步{}的企业微信".format(employee_id.name))
url = "https://oa.thtzjt.com/mail/view?model={}&res_id={}".format(self._name, self.id)
self.sudo().env["wecom.apps"].sync_send_message_textcard(category='task', user_id=employee_id.wecom_userid,
title="日程事项提醒", description=self.task_content,
url=url, btntxt="详细信息")
def button_add_comment(self):
self.ensure_one()
self.add_comment()
for record in self:
# 发送用户通知
partner_ids = record.employee_ids.user_id.partner_id.ids
record.with_context(lang=record.env.lang)._message_auto_subscribe_notify(partner_ids, 'yuthon_will_task.mail_action_yuthon_will_task_add_comment')
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'type': 'success',
'message': '已发送提醒消息',
'sticky': False,
}
}