|
@@ -0,0 +1,401 @@
|
|
|
+from datetime import datetime
|
|
|
+
|
|
|
+from utils.mysql_helper import MySQLHelper
|
|
|
+
|
|
|
+from models.source_data import SourceData, SourceItemData
|
|
|
+from models.standard_data import StandardData
|
|
|
+from models.standard_update_log import StandardUpdateLog
|
|
|
+
|
|
|
+
|
|
|
+class MysqlStore:
|
|
|
+
|
|
|
+ def __init__(self):
|
|
|
+ self._db_helper = MySQLHelper()
|
|
|
+
|
|
|
+ def query_source_data_all(self):
|
|
|
+ query = "SELECT project_no,project_name,standard_version,status,create_time FROM source_data WHERE is_del=0"
|
|
|
+ with self._db_helper:
|
|
|
+ data = []
|
|
|
+ result = self._db_helper.execute_query(query)
|
|
|
+ if not result:
|
|
|
+ return data
|
|
|
+ for item in result:
|
|
|
+ data.append(
|
|
|
+ SourceData(
|
|
|
+ project_no=item["project_no"],
|
|
|
+ project_name=item["project_name"],
|
|
|
+ standard_version=item["standard_version"],
|
|
|
+ status=item["status"],
|
|
|
+ create_time=item["create_time"],
|
|
|
+ ))
|
|
|
+ return data
|
|
|
+
|
|
|
+ def query_source_data_all_paginated(self, page: int, per_page: int, keyword: str = None, status: int = None) -> (list[SourceData], None):
|
|
|
+ offset = (page - 1) * per_page
|
|
|
+ query_count = "SELECT COUNT(*) as count FROM source_data WHERE is_del=%s"
|
|
|
+ query = "SELECT project_no,project_name,standard_version,status,create_time FROM source_data WHERE is_del=0 "
|
|
|
+ params_count = [0]
|
|
|
+ params = []
|
|
|
+ q_1=" AND (project_no LIKE %s OR project_name LIKE %s)"
|
|
|
+ q_2=" AND status=%s"
|
|
|
+ if keyword:
|
|
|
+ query_count += q_1
|
|
|
+ params_count.extend([f"%{keyword}%", f"%{keyword}%"])
|
|
|
+ query += q_1
|
|
|
+ params.extend([f"%{keyword}%", f"%{keyword}%"])
|
|
|
+
|
|
|
+ if status is not None:
|
|
|
+ query_count += q_2
|
|
|
+ params_count.append(status)
|
|
|
+ query += q_2
|
|
|
+ params.append(status)
|
|
|
+
|
|
|
+ query += " ORDER BY status,create_time DESC LIMIT %s OFFSET %s"
|
|
|
+ params.extend([per_page, offset])
|
|
|
+
|
|
|
+ with self._db_helper:
|
|
|
+ result_count = self._db_helper.fetch_one(query_count, tuple(params_count))
|
|
|
+ count = result_count["count"] if result_count else 0
|
|
|
+ result = self._db_helper.execute_query(query, tuple(params))
|
|
|
+ data = []
|
|
|
+ if not result:
|
|
|
+ return [], count
|
|
|
+ for item in result:
|
|
|
+ data.append(
|
|
|
+ SourceData(
|
|
|
+ project_no=item["project_no"],
|
|
|
+ project_name=item["project_name"],
|
|
|
+ standard_version=item["standard_version"],
|
|
|
+ status=item["status"],
|
|
|
+ create_time=item["create_time"],
|
|
|
+ ))
|
|
|
+ return data, count
|
|
|
+
|
|
|
+ def query_source_data(self, project_no: str ,with_items=False) -> SourceData|None:
|
|
|
+ query = "SELECT project_no,project_name,standard_version,status,create_time FROM source_data WHERE is_del=0 AND project_no = %s"
|
|
|
+ params = (project_no, )
|
|
|
+ query_items = "SELECT id,project_no,device_name,device_model,standard_version,standard_no FROM source_item_data WHERE project_no = %s"
|
|
|
+ with self._db_helper:
|
|
|
+ result = self._db_helper.fetch_one(query, params)
|
|
|
+ if not result:
|
|
|
+ return None
|
|
|
+ data = SourceData(result["project_no"],
|
|
|
+ result["project_name"],
|
|
|
+ result["standard_version"],
|
|
|
+ result["status"],
|
|
|
+ create_time=result["create_time"])
|
|
|
+ if not with_items:
|
|
|
+ return data
|
|
|
+ items_result = self._db_helper.execute_query(query_items, params)
|
|
|
+ if items_result:
|
|
|
+ for item in items_result:
|
|
|
+ data.add_item(
|
|
|
+ SourceItemData(
|
|
|
+ item_id=item["id"],
|
|
|
+ project_no=item["project_no"],
|
|
|
+ device_name=item["device_name"],
|
|
|
+ device_model=item["device_model"],
|
|
|
+ standard_version=item["standard_version"],
|
|
|
+ standard_no=item["standard_no"],
|
|
|
+ ))
|
|
|
+ return data
|
|
|
+
|
|
|
+ def query_source_item_data_list_by_project_paginated(self, project_no: str, page: int, per_page: int, keyword: str = None) -> (list[SourceItemData], int):
|
|
|
+ offset = (page - 1) * per_page
|
|
|
+ query_count = "SELECT COUNT(*) as count FROM source_item_data WHERE project_no = %s"
|
|
|
+ query = "SELECT id,project_no,device_name,device_model,device_unit,device_count,standard_version,standard_no FROM source_item_data WHERE project_no = %s"
|
|
|
+ params_count = (project_no,)
|
|
|
+ params = (project_no,)
|
|
|
+
|
|
|
+ if keyword:
|
|
|
+ query_count += " AND (device_name LIKE %s OR device_model LIKE %s)"
|
|
|
+ params_count += (f"%{keyword}%", f"%{keyword}%")
|
|
|
+ query += " AND (device_name LIKE %s OR device_model LIKE %s)"
|
|
|
+ params += (f"%{keyword}%", f"%{keyword}%")
|
|
|
+
|
|
|
+ query += " ORDER BY device_name,device_model LIMIT %s OFFSET %s"
|
|
|
+ params += (per_page, offset)
|
|
|
+
|
|
|
+ with self._db_helper:
|
|
|
+ result_count = self._db_helper.fetch_one(query_count, params_count)
|
|
|
+ count = result_count["count"] if result_count else 0
|
|
|
+ result = self._db_helper.execute_query(query, params)
|
|
|
+ data = []
|
|
|
+ if not result:
|
|
|
+ return data, count
|
|
|
+ for item in result:
|
|
|
+ data.append(
|
|
|
+ SourceItemData(
|
|
|
+ item_id=item["id"],
|
|
|
+ project_no=item["project_no"],
|
|
|
+ device_name=item["device_name"],
|
|
|
+ device_model=item["device_model"],
|
|
|
+ device_unit=item["device_unit"],
|
|
|
+ device_count=item["device_count"],
|
|
|
+ standard_version=item["standard_version"],
|
|
|
+ standard_no=item["standard_no"],
|
|
|
+ ))
|
|
|
+ return data, count
|
|
|
+
|
|
|
+ def query_source_item_data_list_by_project(self, project_no: str, with_empty=True) -> list[SourceItemData]:
|
|
|
+ query = "SELECT id,project_no,device_name,device_model,device_unit,device_count,standard_version,standard_no FROM source_item_data WHERE project_no = %s"
|
|
|
+ query += "" if with_empty else " AND standard_no !='' AND standard_no IS NOT NULL"
|
|
|
+ params = (project_no,)
|
|
|
+ with self._db_helper:
|
|
|
+ result = self._db_helper.execute_query(query, params)
|
|
|
+ data = []
|
|
|
+ if not result:
|
|
|
+ return data
|
|
|
+ for item in result:
|
|
|
+ data.append(
|
|
|
+ SourceItemData(
|
|
|
+ item_id=item["id"],
|
|
|
+ project_no=item["project_no"],
|
|
|
+ device_name=item["device_name"],
|
|
|
+ device_model=item["device_model"],
|
|
|
+ device_unit=item["device_unit"],
|
|
|
+ device_count=item["device_count"],
|
|
|
+ standard_version=item["standard_version"],
|
|
|
+ standard_no=item["standard_no"],
|
|
|
+ ))
|
|
|
+ return data
|
|
|
+
|
|
|
+ def insert_source_data(self, source_data: SourceData) -> bool:
|
|
|
+ query = "INSERT INTO source_data (project_no,project_name,standard_version,completion_tokens,prompt_tokens,total_tokens,create_time) VALUES (%s,%s,%s,%s,%s,%s,%s)"
|
|
|
+ params = (
|
|
|
+ source_data.project_no,
|
|
|
+ source_data.project_name,
|
|
|
+ source_data.standard_version,
|
|
|
+ source_data.completion_tokens,
|
|
|
+ source_data.prompt_tokens,
|
|
|
+ source_data.total_tokens,
|
|
|
+ datetime.now(),
|
|
|
+ )
|
|
|
+
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(query, params)
|
|
|
+ self.insert_source_data_item_list(source_data)
|
|
|
+ return True
|
|
|
+
|
|
|
+ def insert_source_data_item_list(self,source_data):
|
|
|
+ if len(source_data.items) <= 0:
|
|
|
+ return
|
|
|
+ query_items = "INSERT INTO source_item_data (project_no,device_name,device_model,device_unit,device_count,standard_version,standard_no,update_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
|
|
|
+ params_items = []
|
|
|
+ for item in source_data.items:
|
|
|
+ params_items.append((
|
|
|
+ source_data.project_no,
|
|
|
+ item.device_name,
|
|
|
+ item.device_model,
|
|
|
+ item.device_unit,
|
|
|
+ item.device_count,
|
|
|
+ source_data.standard_version,
|
|
|
+ item.standard_no,
|
|
|
+ datetime.now(),
|
|
|
+ ))
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_many(query_items, params_items)
|
|
|
+
|
|
|
+ def update_source_data(self, source_data: SourceData):
|
|
|
+ query = "UPDATE source_data SET project_name = %s, standard_version = %s ,status = %s,update_time = %s WHERE project_no = %s"
|
|
|
+ params = (
|
|
|
+ source_data.project_name,
|
|
|
+ source_data.standard_version,
|
|
|
+ source_data.status,
|
|
|
+ datetime.now(),
|
|
|
+ source_data.project_no,
|
|
|
+ )
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(query, params)
|
|
|
+
|
|
|
+ def re_insert_source_data(self, source_data: SourceData) -> bool:
|
|
|
+ if not source_data.project_no:
|
|
|
+ return False
|
|
|
+ self.update_source_data(source_data)
|
|
|
+ query = "DELETE FROM source_item_data WHERE project_no = %s"
|
|
|
+ params = (source_data.project_no,)
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(query, params)
|
|
|
+ self.insert_source_data_item_list(source_data)
|
|
|
+
|
|
|
+ def delete_source_data(self, project_no: str) -> bool:
|
|
|
+ query_1 = "UPDATE source_data SET is_del=1,delete_time=%s WHERE project_no = %s"
|
|
|
+ # query_2 = "DELETE FROM source_item_data WHERE project_no = %s"
|
|
|
+ params = (datetime.now(),project_no, )
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(query_1, params)
|
|
|
+ # self._db_helper.execute_non_query(query_2, params)
|
|
|
+ return True
|
|
|
+ def query_source_item_data_by_id(self, item_id:int) -> SourceItemData | None:
|
|
|
+ query = "SELECT id,project_no,device_name,device_model,device_unit,device_count,standard_version,standard_no FROM source_item_data WHERE id = %s"
|
|
|
+ params = (item_id,)
|
|
|
+ with self._db_helper:
|
|
|
+ result = self._db_helper.fetch_one(query, params)
|
|
|
+ if not result:
|
|
|
+ return None
|
|
|
+ data = SourceItemData(
|
|
|
+ item_id=result["id"],
|
|
|
+ project_no=result["project_no"],
|
|
|
+ device_name=result["device_name"],
|
|
|
+ device_model=result["device_model"],
|
|
|
+ device_unit=result["device_unit"],
|
|
|
+ device_count=result["device_count"],
|
|
|
+ standard_version=result["standard_version"],
|
|
|
+ standard_no=result["standard_no"],
|
|
|
+ update_time=datetime.now()
|
|
|
+ )
|
|
|
+ return data
|
|
|
+ def query_source_item_data(self, project_no: str,device_name:str, device_model:str) -> SourceItemData| None:
|
|
|
+ query = "SELECT id,project_no,device_name,device_model,device_unit,device_count,standard_version,standard_no FROM source_item_data WHERE project_no = %s AND device_name = %s AND device_model = %s"
|
|
|
+ params = (project_no,device_name, device_model )
|
|
|
+ with self._db_helper:
|
|
|
+ result = self._db_helper.fetch_one(query, params)
|
|
|
+ if not result:
|
|
|
+ return None
|
|
|
+ data = SourceItemData(
|
|
|
+ item_id=result["id"],
|
|
|
+ project_no=result["project_no"],
|
|
|
+ device_name=result["device_name"],
|
|
|
+ device_model=result["device_model"],
|
|
|
+ device_unit=result["device_unit"],
|
|
|
+ device_count=result["device_count"],
|
|
|
+ standard_version=result["standard_version"],
|
|
|
+ standard_no=result["standard_no"],
|
|
|
+ )
|
|
|
+ return data
|
|
|
+ def insert_source_item_data(self, source_item_data: SourceItemData) -> bool:
|
|
|
+ query = "INSERT INTO source_item_data (project_no,device_name,device_model,device_unit,device_count,standard_version,standard_no,update_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
|
|
|
+ params = (
|
|
|
+ source_item_data.project_no,
|
|
|
+ source_item_data.device_name,
|
|
|
+ source_item_data.device_model,
|
|
|
+ source_item_data.device_unit,
|
|
|
+ source_item_data.device_count,
|
|
|
+ source_item_data.standard_version,
|
|
|
+ source_item_data.standard_no,
|
|
|
+ datetime.now(),
|
|
|
+ )
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(query, params)
|
|
|
+ return True
|
|
|
+ def update_source_item_data(self, source_item_data: SourceItemData) -> bool:
|
|
|
+ query = "SElECT standard_no FROM source_item_data WHERE id = %s"
|
|
|
+ params = (source_item_data.id,)
|
|
|
+ u_query = "UPDATE source_item_data SET device_unit= %s,device_count= %s,standard_no = %s,update_time = %s WHERE id = %s"
|
|
|
+ u_params = (source_item_data.device_unit, source_item_data.device_count, source_item_data.standard_no, datetime.now(), source_item_data.id)
|
|
|
+ with self._db_helper:
|
|
|
+ result = self._db_helper.fetch_one(query, params)
|
|
|
+ if result:
|
|
|
+
|
|
|
+ self._db_helper.execute_non_query(u_query, u_params)
|
|
|
+ self.insert_or_update_standard_data(
|
|
|
+ StandardData(device_name=source_item_data.device_name,
|
|
|
+ device_model=source_item_data.device_model,
|
|
|
+ device_unit=source_item_data.device_unit,
|
|
|
+ standard_version=source_item_data.standard_version,
|
|
|
+ standard_no=source_item_data.standard_no),
|
|
|
+ source_item_data.project_no)
|
|
|
+ return True
|
|
|
+ def delete_source_item_data_by_id(self, item_id: int):
|
|
|
+ query = "DELETE FROM source_item_data WHERE id = %s"
|
|
|
+ params = (item_id, )
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(query, params)
|
|
|
+ return True
|
|
|
+ def delete_source_item_data(self,
|
|
|
+ project_no: str,
|
|
|
+ device_name: str,
|
|
|
+ device_model: str) -> bool:
|
|
|
+ query = "DELETE FROM source_item_data WHERE project_no = %s AND device_name = %s AND device_model = %s"
|
|
|
+ params = (project_no, device_name, device_model)
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(query, params)
|
|
|
+ return True
|
|
|
+
|
|
|
+
|
|
|
+ def query_standard_data(self, device_name: str,
|
|
|
+ device_model: str) -> list[StandardData]|None:
|
|
|
+ query = "SELECT device_model,device_name,device_unit,standard_version,standard_no FROM standard_data WHERE device_name = %s AND device_model = %s "
|
|
|
+ params = (device_name, device_model)
|
|
|
+ with self._db_helper:
|
|
|
+ results = self._db_helper.execute_query(query, params)
|
|
|
+ if not results:
|
|
|
+ return None
|
|
|
+ data = []
|
|
|
+ for result in results:
|
|
|
+ data.append(StandardData(
|
|
|
+ device_model=result["device_model"],
|
|
|
+ device_name=result["device_name"],
|
|
|
+ device_unit=result["device_unit"],
|
|
|
+ standard_version=result["standard_version"],
|
|
|
+ standard_no=result["standard_no"],
|
|
|
+ ))
|
|
|
+ return data
|
|
|
+
|
|
|
+ def insert_or_update_standard_data(self,
|
|
|
+ standard_data: StandardData,
|
|
|
+ project_no: str = None) -> bool:
|
|
|
+ query = "SELECT device_model,device_name,device_unit,standard_version,standard_no FROM standard_data WHERE device_name = %s AND device_model = %s AND standard_version = %s"
|
|
|
+ params = (standard_data.device_name, standard_data.device_model,standard_data.standard_version)
|
|
|
+ i_query = "INSERT INTO standard_data (device_name,device_model,device_unit,standard_version,standard_no,create_time) VALUES (%s,%s,%s,%s,%s,%s)"
|
|
|
+ i_params = (
|
|
|
+ standard_data.device_name,
|
|
|
+ standard_data.device_model,
|
|
|
+ standard_data.device_unit,
|
|
|
+ standard_data.standard_version,
|
|
|
+ standard_data.standard_no,
|
|
|
+ datetime.now(),
|
|
|
+ )
|
|
|
+ u_query = "UPDATE standard_data SET device_unit=%s, standard_no = %s, update_time = %s WHERE device_name = %s AND device_model = %s AND standard_version = %s"
|
|
|
+ u_params = (
|
|
|
+ standard_data.device_unit,
|
|
|
+ standard_data.standard_no,
|
|
|
+ datetime.now(),
|
|
|
+ standard_data.device_name,
|
|
|
+ standard_data.device_model,
|
|
|
+ standard_data.standard_version,
|
|
|
+ )
|
|
|
+ with self._db_helper:
|
|
|
+ data = self._db_helper.fetch_one(query, params)
|
|
|
+ if data:
|
|
|
+ self._db_helper.execute_non_query(u_query, u_params)
|
|
|
+ if project_no:
|
|
|
+ self.insert_standard_update_log(
|
|
|
+ StandardUpdateLog(
|
|
|
+ project_no=project_no,
|
|
|
+ device_name=standard_data.device_name,
|
|
|
+ device_model=standard_data.device_model,
|
|
|
+ device_unit= standard_data.device_unit,
|
|
|
+ standard_version=standard_data.standard_version,
|
|
|
+ new_standard_no=standard_data.standard_no,
|
|
|
+ old_standard_no=data["standard_no"]))
|
|
|
+ else:
|
|
|
+ self._db_helper.execute_non_query(i_query, i_params)
|
|
|
+ return True
|
|
|
+
|
|
|
+ def query_standard_update_log(self, project_no: str) -> list[StandardUpdateLog]|None:
|
|
|
+ query = "SELECT project_no,device_name,device_model,device_unit,standard_version,new_standard_no,old_standard_no,create_time FROM standard_update_log WHERE project_no = %s"
|
|
|
+ params = (project_no, )
|
|
|
+ with self._db_helper:
|
|
|
+ results = self._db_helper.execute_query(query, params)
|
|
|
+ if not results:
|
|
|
+ return None
|
|
|
+ data = [StandardUpdateLog(**result) for result in results]
|
|
|
+ return data
|
|
|
+
|
|
|
+ def insert_standard_update_log(
|
|
|
+ self, standard_update_log: StandardUpdateLog) -> bool:
|
|
|
+ query = "INSERT INTO standard_update_log (project_no,device_name,device_model,device_unit,standard_version,new_standard_no,old_standard_no,create_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
|
|
|
+ params = (
|
|
|
+ standard_update_log.project_no,
|
|
|
+ standard_update_log.device_name,
|
|
|
+ standard_update_log.device_model,
|
|
|
+ standard_update_log.device_unit,
|
|
|
+ standard_update_log.standard_version,
|
|
|
+ standard_update_log.new_standard_no,
|
|
|
+ standard_update_log.old_standard_no,
|
|
|
+ datetime.now(),
|
|
|
+ )
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(query, params)
|
|
|
+ return True
|