contents.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. from .base import BaseMySQLClient
  2. class Contents(BaseMySQLClient):
  3. async def insert_content(self, doc_id, text, text_type, title, dataset_id):
  4. query = """
  5. INSERT IGNORE INTO contents
  6. (doc_id, text, text_type, title, dataset_id)
  7. VALUES (%s, %s, %s, %s, %s);
  8. """
  9. return await self.pool.async_save(
  10. query=query, params=(doc_id, text, text_type, title, dataset_id)
  11. )
  12. async def update_content_info(self, doc_id, text, text_type, title, dataset_id):
  13. query = """
  14. UPDATE contents
  15. SET text = %s, text_type = %s, title = %s, dataset_id = %s, status = %s
  16. WHERE doc_id = %s;
  17. """
  18. return await self.pool.async_save(
  19. query=query,
  20. params=(text, text_type, title, dataset_id, self.INIT_STATUS, doc_id),
  21. )
  22. async def update_content_status(self, doc_id, ori_status, new_status):
  23. query = """
  24. UPDATE contents
  25. SET status = %s
  26. WHERE doc_id = %s AND status = %s;
  27. """
  28. return await self.pool.async_save(
  29. query=query, params=(new_status, doc_id, ori_status)
  30. )
  31. async def update_dataset_status(self, dataset_id, ori_status, new_status):
  32. query = """
  33. UPDATE contents
  34. SET status = %s
  35. WHERE dataset_id = %s AND status = %s;
  36. """
  37. return await self.pool.async_save(
  38. query=query, params=(new_status, dataset_id, ori_status)
  39. )
  40. async def update_doc_status(self, doc_id, ori_status, new_status):
  41. """
  42. this function is to change the using status of each document
  43. :param doc_id:
  44. :param ori_status:
  45. :param new_status:
  46. :return:
  47. """
  48. query = """
  49. UPDATE contents SET doc_status = %s WHERE doc_id = %s AND doc_status = %s;
  50. """
  51. return await self.pool.async_save(
  52. query=query, params=(new_status, doc_id, ori_status)
  53. )
  54. async def select_count(self, dataset_id, doc_status=1):
  55. query = """
  56. SELECT count(*) AS count FROM contents WHERE dataset_id = %s AND doc_status = %s;
  57. """
  58. rows = await self.pool.async_fetch(query=query, params=(dataset_id, doc_status))
  59. return rows[0]["count"] if rows else 0
  60. async def select_content_by_doc_id(self, doc_id):
  61. query = """SELECT * FROM contents WHERE doc_id = %s;"""
  62. return await self.pool.async_fetch(query=query, params=(doc_id,))
  63. async def select_contents(
  64. self,
  65. page_num: int,
  66. page_size: int,
  67. order_by=None,
  68. dataset_id: int = None,
  69. doc_status: int = 1,
  70. ):
  71. """
  72. 分页查询 contents 表,并返回分页信息
  73. :param page_num: 页码,从 1 开始
  74. :param page_size: 每页数量
  75. :param order_by: 排序条件,例如 {"id": "desc"} 或 {"created_at": "asc"}
  76. :param dataset_id: 数据集 ID
  77. :param doc_status: 文档状态(默认 1)
  78. :return: dict,包含 entities、total_count、page、page_size、total_pages
  79. """
  80. if order_by is None:
  81. order_by = {"id": "desc"}
  82. offset = (page_num - 1) * page_size
  83. # 动态拼接 where 条件
  84. where_clauses = ["doc_status = %s"]
  85. params = [doc_status]
  86. if dataset_id:
  87. where_clauses.append("dataset_id = %s")
  88. params.append(dataset_id)
  89. where_sql = " AND ".join(where_clauses)
  90. # 动态拼接 order by
  91. order_field, order_direction = list(order_by.items())[0]
  92. order_sql = f"ORDER BY {order_field} {order_direction.upper()}"
  93. # 查询总数
  94. count_query = f"SELECT COUNT(*) as total_count FROM contents WHERE {where_sql};"
  95. count_result = await self.pool.async_fetch(
  96. query=count_query, params=tuple(params)
  97. )
  98. total_count = count_result[0]["total_count"] if count_result else 0
  99. # 查询分页数据
  100. query = f"""
  101. SELECT * FROM contents
  102. WHERE {where_sql}
  103. {order_sql}
  104. LIMIT %s OFFSET %s;
  105. """
  106. params.extend([page_size, offset])
  107. entities = await self.pool.async_fetch(query=query, params=tuple(params))
  108. total_pages = (total_count + page_size - 1) // page_size # 向上取整
  109. return {
  110. "entities": entities,
  111. "total_count": total_count,
  112. "page": page_num,
  113. "page_size": page_size,
  114. "total_pages": total_pages,
  115. }