Odoo中一个算法的优化方案

要求:

根据样的批次号、状态生成样本的发货单;
同一批次的样本, 已经创建过发货单的样本需要排除

 

原算法:

@api.onchange("batch_no")
def _change_batch_no(self):
    if self.batch_no and self.batch_no != '000':
        batch = self.batch_no.split(",")
        s_objs = self.env["rhwl.virus.detection"].search([('batch_no', 'in', batch), ('state', '=', "report_done")])
        if not s_objs:
            self.batch_no = ""
            return
        res = []
        for s_obj in s_objs:
            l_objs = self.env["rhwl.virus.picking.line"].search([('name', '=', s_obj.id)])
            for l_obj in l_objs:
                if l_obj.parent_id and l_obj.parent_id.state in ("draft", "confirm", "upload"):
                    continue
                res.append({"name": s_obj})
        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
        res = []
        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]

        self.line = res

 

据测算,优化后,1k以上数据速度明显提高。

上一篇:Dao层抽取BaseDao公共方法


下一篇:多线程中生产消费模型