From 01f8dc75a36ffbdb388b6cc911cfc83ee86d9b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E5=AE=87?= <9664676+pengyuthon@user.noreply.gitee.com> Date: Fri, 20 Mar 2026 11:38:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/soong_workflow_record.py | 192 +++--------------- 1 file changed, 27 insertions(+), 165 deletions(-) diff --git a/soong_workflow/models/soong_workflow_record.py b/soong_workflow/models/soong_workflow_record.py index ae6b9beda..7b66aefa4 100644 --- a/soong_workflow/models/soong_workflow_record.py +++ b/soong_workflow/models/soong_workflow_record.py @@ -169,182 +169,44 @@ 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', 'res_model', 'res_id', 'soong_state', 'reclaim_state', 'approve_state') + @api.depends('pending_users') def _compute_is_to_do(self): - """保留原有计算逻辑,仅修复log_ids字段不存在的报错""" - current_uid = self.env.user.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: + # 获取当前用户的姓名(处理空值,避免报错) + current_user_name = self.env.user.name.strip() if self.env.user.name else '' + if not current_user_name: self.is_to_do = False return - all_employees = self.env['hr.employee'].search([('user_state', '=', 'in')]) - emp_to_user = {emp.id: emp.user_id.id for emp in all_employees if emp.user_id} - - # 第一步:批量处理条件1(approve_state=start) - condition1_records = self.filtered( - lambda r: not r.reclaim_state and r.approve_state == 'start' and - set(active_employee_ids) & set(r.employee_ids.ids + r.employee2_ids.ids) - ) - condition1_records.is_to_do = True - - # 第二步:批量处理条件2(log_ids/log_ids2) - remaining_records = self - condition1_records - remaining_records.is_to_do = False - - # 按res_model分组批量查询log(核心优化,新增字段存在性校验) - model_res_ids = defaultdict(list) - rec_model_res_map = {} - for rec in remaining_records.filtered(lambda r: not r.reclaim_state and r.res_model and r.res_id): - # 新增:先校验模型是否有log_ids/log_ids2字段,无则跳过 - try: - TargetModel = self.env[rec.res_model] - has_log1 = 'log_ids' in TargetModel._fields - has_log2 = 'log_ids2' in TargetModel._fields - # 至少有一个日志字段才加入后续处理 - if not has_log1 and not has_log2: - continue - except Exception: - # 模型不存在/无权限时也跳过 + for record in self: + record.is_to_do = False + if not record.pending_users: continue - model_res_ids[rec.res_model].append(rec.res_id) - rec_model_res_map[rec.id] = (rec.res_model, rec.res_id) - - # 批量查log满足条件的记录ID(新增字段存在性判断) - valid_rec_ids = [] - for model_name, res_ids in model_res_ids.items(): - if not res_ids: - continue - try: - TargetModel = self.env[model_name].sudo() - # 新增:分别判断log_ids/log_ids2是否存在 - has_log1 = 'log_ids' in TargetModel._fields - has_log2 = 'log_ids2' in TargetModel._fields - - log1_valid_res_ids = [] - if has_log1: # 仅当log_ids存在时才查询 - log_domain = [ - ('res_id', 'in', res_ids), - ('done_by', 'in', list(emp_to_user.values())), - ('approval_state', 'in', ['0', '1']) - ] - log1_valid_res_ids = TargetModel.log_ids.search(log_domain).mapped('res_id') - - log2_valid_res_ids = [] - if has_log2: # 仅当log_ids2存在时才查询 - log_domain = [ - ('res_id', 'in', res_ids), - ('done_by', 'in', list(emp_to_user.values())), - ('approval_state', 'in', ['0', '1']) - ] - log2_valid_res_ids = TargetModel.log_ids2.search(log_domain).mapped('res_id') - - valid_res_ids = set(log1_valid_res_ids + log2_valid_res_ids) - for rec_id, (model, res_id) in rec_model_res_map.items(): - if model == model_name and res_id in valid_res_ids: - valid_rec_ids.append(rec_id) - except Exception: - continue - self.browse(valid_rec_ids).is_to_do = True + pending_names = [name.strip() for name in record.pending_users.split(',') if name.strip()] + if current_user_name in pending_names: + record.is_to_do = True def _search_is_to_do(self, operator, value): - """搜索待办状态:仅打印总耗时""" - - current_uid = self.env.user.id - current_employees = self.env['hr.employee'].sudo().search([('user_id', '=', current_uid)]) - active_employee_ids = current_employees.ids if current_employees else [] - if not active_employee_ids: + current_user_name = self.env.user.name.strip() if self.env.user.name else '' + if not current_user_name: + # 无姓名时返回空结果 return [('id', '=', False)] - - base_domain = [ - ('reclaim_state', '=', False), - '|', - ('employee_ids', 'in', active_employee_ids), - ('employee2_ids', 'in', active_employee_ids) - ] - + domain = [] if (operator == '=' and value) or (operator == '!=' and not value): - candidate_records = self.search(base_domain) - if not candidate_records: - return [('id', '=', False)] - - condition1_ids = candidate_records.filtered( - lambda r: r.approve_state == 'start' and not r.reclaim_state - ).ids - - model_res_ids = defaultdict(list) - rec_model_res_map = {} - for rec in candidate_records - self.browse(condition1_ids): - if rec.res_model and rec.res_id: - model_res_ids[rec.res_model].append(rec.res_id) - rec_model_res_map[rec.id] = (rec.res_model, rec.res_id) - - condition2_rec_ids = [] - for model_name, res_ids in model_res_ids.items(): - if not res_ids: - continue - TargetModel = self.env[model_name].sudo() - log1_domain = [ - ('res_id', 'in', res_ids), - ('done_by', '=', current_uid), - ('approval_state', 'in', ['0', '1']) - ] - log1_valid_res_ids = TargetModel.log_ids.search(log1_domain).mapped('res_id') - log2_domain = [ - ('res_id', 'in', res_ids), - ('done_by', '=', current_uid), - ('approval_state', 'in', ['0', '1']) - ] - log2_valid_res_ids = TargetModel.log_ids2.search(log2_domain).mapped('res_id') - - valid_res_ids = set(log1_valid_res_ids + log2_valid_res_ids) - for rec_id, (model, res_id) in rec_model_res_map.items(): - if model == model_name and res_id in valid_res_ids: - condition2_rec_ids.append(rec_id) - - valid_ids = list(set(condition1_ids + condition2_rec_ids)) - domain = [('id', 'in', valid_ids)] - + domain = [ + '|', '|', + ('pending_users', '=', current_user_name), + ('pending_users', 'like', f'{current_user_name},'), + ('pending_users', 'like', f',{current_user_name}'), + ] elif (operator == '=' and not value) or (operator == '!=' and value): - candidate_records = self.search(base_domain) - if not candidate_records: - return [('id', '=', False)] - - condition1_invalid_ids = candidate_records.filtered( - lambda r: r.approve_state == 'start' and not r.reclaim_state - ).ids - - model_res_ids = defaultdict(list) - rec_model_res_map = {} - for rec in candidate_records - self.browse(condition1_invalid_ids): - if rec.res_model and rec.res_id: - model_res_ids[rec.res_model].append(rec.res_id) - rec_model_res_map[rec.id] = (rec.res_model, rec.res_id) - - condition2_invalid_ids = [] - for model_name, res_ids in model_res_ids.items(): - if not res_ids: - continue - TargetModel = self.env[model_name].sudo() - log1_domain = [ - ('res_id', 'in', res_ids), - ('done_by', '=', current_uid), - ('approval_state', 'in', ['0', '1']) - ] - log1_valid_res_ids = TargetModel.log_ids.search(log1_domain).mapped('res_id') - log2_domain = log1_domain.copy() - log2_valid_res_ids = TargetModel.log_ids2.search(log2_domain).mapped('res_id') - - valid_res_ids = set(log1_valid_res_ids + log2_valid_res_ids) - for rec_id, (model, res_id) in rec_model_res_map.items(): - if model == model_name and res_id in valid_res_ids: - condition2_invalid_ids.append(rec_id) - - invalid_ids = list(set(condition1_invalid_ids + condition2_invalid_ids)) - domain = [('id', 'not in', invalid_ids)] + domain = [ + '!', + '|', '|', + ('pending_users', '=', current_user_name), + ('pending_users', 'like', f'{current_user_name},'), + ('pending_users', 'like', f',{current_user_name}'), + ] else: domain = [('id', '=', False)] - return domain @api.depends('employee_ids', 'soong_state', 'reclaim_state')