Files
gzth/yuthon_will_task/models/yuthon_will_task.py
T
2024-12-02 14:34:45 +00:00

82 lines
4.0 KiB
Python

from odoo import api, fields, models, modules
import datetime
from datetime import datetime, timedelta, time
from odoo.exceptions import UserError
import logging
_logger = logging.getLogger(__name__)
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:
now = datetime.now() + timedelta(hours=8)
start_time = task.start_time + timedelta(hours=8)
_logger.info("定时任务执行中")
_logger.info("判断前now:{}---start:{}".format(now, start_time))
if now <= start_time:
_logger.info("判断后now:{}---start:{}".format(now, start_time))
if task.task_time_ids:
for task_time in task.task_time_ids:
duration = self._compute_duration(task_time)
day_now = (now + duration).day
day_start = start_time.day
hour_now = (now + duration).hour
hour_start = start_time.hour
min_now = (now + duration).minute
min_start = start_time.minute
if day_now == day_start and hour_now == hour_start and min_now == min_start:
task.add_comment()
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 + '\t' + str(self.start_time),
url=url, btntxt="详细信息")
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')
def button_add_comment(self):
self.ensure_one()
self.add_comment()
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'type': 'success',
'message': '已发送提醒消息',
'sticky': False,
}
}