diff --git a/soong_workflow/models/soong_workflow_record.py b/soong_workflow/models/soong_workflow_record.py index 7b66aefa4..ae6b9beda 100644 --- a/soong_workflow/models/soong_workflow_record.py +++ b/soong_workflow/models/soong_workflow_record.py @@ -169,44 +169,182 @@ class SoongWorkflowRecord(models.Model): _logger.warning(f"Failed to get urgency_level from {record.res_model}: {e}") record.urgency_level = 'normal' - @api.depends('pending_users') + @api.depends('employee_ids', 'res_model', 'res_id', 'soong_state', 'reclaim_state', 'approve_state') def _compute_is_to_do(self): - # 获取当前用户的姓名(处理空值,避免报错) - current_user_name = self.env.user.name.strip() if self.env.user.name else '' - if not current_user_name: + """保留原有计算逻辑,仅修复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: self.is_to_do = False return - for record in self: - record.is_to_do = False - if not record.pending_users: + 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: + # 模型不存在/无权限时也跳过 continue - 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 + 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 def _search_is_to_do(self, operator, value): - current_user_name = self.env.user.name.strip() if self.env.user.name else '' - if not current_user_name: - # 无姓名时返回空结果 + """搜索待办状态:仅打印总耗时""" + + 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: return [('id', '=', False)] - domain = [] + + base_domain = [ + ('reclaim_state', '=', False), + '|', + ('employee_ids', 'in', active_employee_ids), + ('employee2_ids', 'in', active_employee_ids) + ] + if (operator == '=' and value) or (operator == '!=' and not value): - domain = [ - '|', '|', - ('pending_users', '=', current_user_name), - ('pending_users', 'like', f'{current_user_name},'), - ('pending_users', 'like', f',{current_user_name}'), - ] + 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)] + elif (operator == '=' and not value) or (operator == '!=' and value): - domain = [ - '!', - '|', '|', - ('pending_users', '=', current_user_name), - ('pending_users', 'like', f'{current_user_name},'), - ('pending_users', 'like', f',{current_user_name}'), - ] + 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)] else: domain = [('id', '=', False)] + return domain @api.depends('employee_ids', 'soong_state', 'reclaim_state')