from db.models.lot_data_model import LOT_DATA_MODEL, LOT_DATA from db.session import with_session @with_session def add_kb_to_db(session, data: LOT_DATA): # 创建知识库实例 kb = LOT_DATA_MODEL(**data.__dict__) session.add(kb) @with_session def delete_kb_from_db(session, id): kb = session.query(LOT_DATA_MODEL).filter_by(id=id).first() if kb: session.delete(kb) return True # 直接查询所有数据 @with_session def get_kb_detail(session): all_data = session.query(LOT_DATA_MODEL).all() data = [LOT_DATA(**d.__dict__) for d in all_data] return data # 根据时间查询数据 @with_session def get_kb_detail_by_time(session, start_time, end_time): all_data = session.query(LOT_DATA_MODEL).filter( start_time <= LOT_DATA_MODEL.create_time, LOT_DATA_MODEL.create_time <= end_time).all() data = [LOT_DATA(**d.__dict__) for d in all_data] return data # 删除数据 @with_session def delete_kb_detail_by_time(session, start_time, end_time): d = session.query(LOT_DATA_MODEL).filter(start_time <= LOT_DATA_MODEL.create_time, LOT_DATA_MODEL.create_time <= end_time).delete() return d # 查询数据 @with_session def get_data_by_field(session, field, value): query = session.query(LOT_DATA_MODEL).filter(getattr(LOT_DATA_MODEL, field) == value) result = query.all() return result