2018-08-07

2018-08-07  本文已影响9人  Jack_Ren
def get_search_result(search_string):
    if search_string[len(search_string) - 2] == '.':
        search_string = search_string[:-2]

    search_string = '%'.join(search_string.split())

    search_result = []

    # search for code

    codes = Code.query.filter(Code.family_code.like('%{}%'.format(search_string))).all()
    search_result += [code.family_code for code in codes]

    codes_from_erp = CodeFromErp.query.filter(CodeFromErp.family_code.like('%{}%'.format(search_string))).all()
    search_result += [code.family_code for code in codes_from_erp]
    # search for original code

    codes_from_italy_orcode = CodeFromItaly.query.filter(or_(
        CodeFromItaly.original_code1.like('%{}%'.format(search_string)),
        CodeFromItaly.original_code2.like('%{}%'.format(search_string)),
        CodeFromItaly.original_code3.like('%{}%'.format(search_string)),
        CodeFromItaly.original_code4.like('%{}%'.format(search_string)),
    )).all()

    search_result += [code.family_code for code in codes_from_italy_orcode]

    codes_from_erp_or_code = CodeFromErp.query.filter(or_(
        CodeFromErp.original_code1.like('%{}%'.format(search_string)),
        CodeFromErp.original_code2.like('%{}%'.format(search_string)),
        CodeFromErp.original_code3.like('%{}%'.format(search_string)),
        CodeFromErp.original_code4.like('%{}%'.format(search_string)),
    )).all()

    search_result += [code.family_code for code in codes_from_erp_or_code]

    # search for description

    codes_from_italy_des = CodeFromItaly.query.filter(or_(
        CodeFromItaly.description.like('%{}%'.format(search_string)),
        CodeFromItaly.description.like('%{}%'.format(search_string)),
        CodeFromItaly.description.like('%{}%'.format(search_string)),
        CodeFromItaly.description.like('%{}%'.format(search_string)),
    )).all()

    search_result += [code.family_code for code in codes_from_italy_des]

    codes_from_erp_des = CodeFromErp.query.filter(or_(
        CodeFromErp.description.like('%{}%'.format(search_string)),
        CodeFromErp.description.like('%{}%'.format(search_string)),
        CodeFromErp.description.like('%{}%'.format(search_string)),
        CodeFromErp.description.like('%{}%'.format(search_string)),
    )).all()

    search_result += [code.family_code for code in codes_from_erp_des]
    # 去除OR开头的件号
    search_result = [code for code in search_result if code[:2] != 'OR']
    print('search result<{}>'.format(set(search_result)))
    return set(search_result)
上一篇下一篇

猜你喜欢

热点阅读