diff --git a/soong_workflow/models/soong_workflow_record.py b/soong_workflow/models/soong_workflow_record.py index 2077f75c5..ca4f95a07 100644 --- a/soong_workflow/models/soong_workflow_record.py +++ b/soong_workflow/models/soong_workflow_record.py @@ -130,77 +130,132 @@ class SoongWorkflowRecord(models.Model): _logger.warning(f"Failed to get urgency_level from {record.res_model}: {e}") record.urgency_level = 'normal' - @api.depends('employee_ids', 'log_ids', 'log_ids2', 'log_ids.approval_state', 'log_ids.done_by', - 'log_ids2.approval_state', 'log_ids2.done_by') + @api.depends('employee_ids', 'res_model', 'res_id', 'soong_state', 'reclaim_state') def _compute_is_to_do(self): + """计算待办状态:核心逻辑完全匹配要求""" current_uid = self.env.user.id - current_employee = self.env['hr.employee'].search([('user_id', '=', current_uid)], limit=1) - active_employee_ids = [current_employee.id] if current_employee else [] - emp_to_user = {emp.id: emp.user_id.id for emp in self.env['hr.employee'].search([]) if emp.user_id} + # 1. 获取当前用户关联的员工ID + current_employees = self.env['hr.employee'].search([('user_id', '=', current_uid)]) + active_employee_ids = current_employees.ids if current_employees else [] + if not active_employee_ids: + for record in self: + record.is_to_do = False + return + + # 2. 构建员工ID到用户ID的映射 + all_employees = self.env['hr.employee'].search([]) + emp_to_user = {emp.id: emp.user_id.id for emp in all_employees if emp.user_id} + for record in self: record.is_to_do = False + # 3. 筛选当前记录关联的有效员工ID record_emp_ids = [emp.id for emp in record.employee_ids if emp.id in active_employee_ids] if not record_emp_ids: continue - # 3. 转换为对应的用户ID列表 + # 4. 转换为对应的用户ID列表 record_user_ids = [emp_to_user[emp_id] for emp_id in record_emp_ids if emp_id in emp_to_user] if not record_user_ids: continue - is_todo = False - # 4.1 检查log_ids:筛选done_by属于当前记录关联用户,且审批状态为0/1的记录 - if record.log_ids: - target_logs = record.log_ids.filtered( - lambda l: l.done_by.id in record_user_ids and l.approval_state in ['0', '1'] - ) - if target_logs: - is_todo = True + # 5. 获取关联的目标记录(res_model + res_id) + target_record = None + if record.res_model and record.res_id: + try: + target_record = self.env[record.res_model].browse(record.res_id).exists() + except Exception: + continue - # 4.2 检查log_ids2:如果log_ids没命中,再检查log_ids2 - if not is_todo and record.log_ids2: - target_log2 = record.log_ids2.filtered( + if not target_record: + continue + + is_todo = False + # 6.1 检查log_ids:直接使用(确认存在) + target_logs = target_record.log_ids.filtered( + lambda l: l.done_by.id in record_user_ids and l.approval_state in ['0', '1'] + ) + if target_logs: + is_todo = True + + # 6.2 检查log_ids2:直接使用(确认存在) + if not is_todo: + target_log2 = target_record.log_ids2.filtered( lambda l: l.done_by.id in record_user_ids and l.approval_state in ['0', '1'] ) if target_log2: is_todo = True + + # 7. 设置最终待办状态 record.is_to_do = is_todo def _search_is_to_do(self, operator, value): - """ - 实现is_to_do字段的搜索逻辑 - :param operator: 搜索操作符(=, !=) - :param value: 搜索值(True/False) - :return: 搜索域 - """ + """实现is_to_do字段的搜索逻辑""" current_uid = self.env.user.id - current_employee = self.env['hr.employee'].search([('user_id', '=', current_uid)], limit=1) - if not current_employee: - return [('id', '=', False)] # - active_employee_ids = [current_employee.id] - emp_to_user = {emp.id: emp.user_id.id for emp in self.env['hr.employee'].search([]) if emp.user_id} - # - 员工关联:employee_ids在active_employee_ids中 - # - log_ids条件:done_by属于当前用户,且approval_state为0/1 - # - 或 log_ids2条件:done_by属于当前用户,且approval_state为0/1 - log1_domain = [ - ('log_ids.done_by', '=', current_uid), - ('log_ids.approval_state', 'in', ['0', '1']) - ] - log2_domain = [ - ('log_ids2.done_by', '=', current_uid), - ('log_ids2.approval_state', 'in', ['0', '1']) - ] - base_domain = [ - ('employee_ids', 'in', active_employee_ids), - '|', *log1_domain, *log2_domain - ] + # 1. 获取当前用户关联的员工ID + current_employees = self.env['hr.employee'].search([('user_id', '=', current_uid)]) + active_employee_ids = current_employees.ids if current_employees else [] + if not active_employee_ids: + return [('id', '=', False)] + # 2. 基础域:关联员工匹配 + base_domain = [('employee_ids', 'in', active_employee_ids)] if (operator == '=' and value) or (operator == '!=' and not value): - domain = base_domain + # 搜索待办=True的记录 + candidate_records = self.search(base_domain) + valid_ids = [] + for rec in candidate_records: + if rec.res_model and rec.res_id: + try: + target = self.env[rec.res_model].browse(rec.res_id).exists() + if not target: + continue + # 检查log_ids(直接使用) + log1_ok = any( + l.done_by.id == current_uid and l.approval_state in ['0', '1'] + for l in target.log_ids + ) + # 检查log_ids2(直接使用) + log2_ok = False + if not log1_ok: + log2_ok = any( + l.done_by.id == current_uid and l.approval_state in ['0', '1'] + for l in target.log_ids2 + ) + if log1_ok or log2_ok: + valid_ids.append(rec.id) + except Exception: + continue + domain = [('id', 'in', valid_ids)] elif (operator == '=' and not value) or (operator == '!=' and value): - domain = ['!'] + base_domain + # 搜索待办=False的记录 + candidate_records = self.search(base_domain) + invalid_ids = [] + for rec in candidate_records: + if rec.res_model and rec.res_id: + try: + target = self.env[rec.res_model].browse(rec.res_id).exists() + if not target: + continue + # 检查log_ids + log1_ok = any( + l.done_by.id == current_uid and l.approval_state in ['0', '1'] + for l in target.log_ids + ) + # 检查log_ids2 + log2_ok = False + if not log1_ok: + log2_ok = any( + l.done_by.id == current_uid and l.approval_state in ['0', '1'] + for l in target.log_ids2 + ) + if log1_ok or log2_ok: + invalid_ids.append(rec.id) + except Exception: + continue + # 排除待办=True的记录 + domain = [('id', 'not in', invalid_ids)] else: + # 不支持的操作符,返回空 domain = [('id', '=', False)] - return domain @api.depends('employee_ids', 'soong_state', 'reclaim_state')