Odoo中一个算法的优化方案
要求:
根据样的批次号、状态生成样本的发货单;
同一批次的样本, 已经创建过发货单的样本需要排除
据测算,优化后,1k以上数据,原算法时间在30s+,优化算法以后,时间在10s以内,速度明显提高。
原算法:
@api.onchange("batch_no") def _change_batch_no(self): if self.batch_no and self.batch_no != '000': batch = self.batch_no.split(",") ids = self.env["rhwl.virus.detection"].search([('batch_no', 'in', batch), ('state', '=', "report_done")]) if not ids: self.batch_no = "" return res = [] for i in ids: l_objs = self.env["rhwl.virus.picking.line"].search([('name', '=', i.id)]) # 样本没有创建过发货单, # 样本创建过一次发货单(明细没删除、明细已删除) # 创建过一次以上发货单(明细全部删除、明细部分删除) if not l_objs: res.append({"name": i}) else: e_objs = [] for l_obj in l_objs: if l_obj.parent_id and l_obj.parent_id.state in ("draft", "confirm", "upload"): e_objs.append(l_obj) if not e_objs: res.append({"name": i}) self.line = res
算法优化:
@api.onchange("batch_no") def _change_batch_no(self): if self.batch_no and self.batch_no != '000': batch = self.batch_no.split(",") # 所有可以发送的样本 d_objs = self.env["rhwl.virus.detection"].search([('batch_no', 'in', batch), ('state', '=', "report_done")]) if not d_objs: self.batch_no = "" return line_pool = self.env["rhwl.virus.picking.line"] d_ids = [d_obj.id for d_obj in d_objs] un_objs = [] # 已经创建了发货单的样本 l_objs = line_pool.search([('name', 'in', d_ids)]) for l_obj in l_objs: if l_obj.parent_id and l_obj.parent_id.state in ("draft", "confirm", "upload"): un_objs.append(l_obj.name) if un_objs: # 求差集 ad_objs = list(set(d_objs) ^ set(un_objs)) res = [{"name": ad_obj} for ad_obj in ad_objs] else: res = [{"name": d_obj} for d_obj in d_objs] self.line = res