|
|
@@ -2,9 +2,7 @@ from datetime import datetime
|
|
|
|
|
|
from utils.mysql_helper import MySQLHelper
|
|
|
|
|
|
-from models.project_data import ProjectModel, ProjectItemModel
|
|
|
-from models.standard_data import StandardModel
|
|
|
-from models.standard_update_log import StandardUpdateLogModel
|
|
|
+from models.project_data import ProjectModel, SubProjectModel, SubProjectItemModel
|
|
|
|
|
|
|
|
|
class MysqlStore:
|
|
|
@@ -12,19 +10,87 @@ class MysqlStore:
|
|
|
def __init__(self):
|
|
|
self._db_helper = MySQLHelper()
|
|
|
|
|
|
- def query_project_all(self):
|
|
|
- query = "SELECT project_no,project_name,work_catalog,work_content,standard_version,status,create_time FROM project WHERE is_del=0"
|
|
|
+ def query_project_all_paginated(self, page: int, per_page: int, keyword: str = None):
|
|
|
+ offset = (page - 1) * per_page
|
|
|
+ sql_count = "SELECT COUNT(*) as count FROM project WHERE is_del=%s"
|
|
|
+ sql = "SELECT id,project_name,description,delete_by,delete_time,create_by,create_time,update_by,update_time FROM project WHERE is_del=%s"
|
|
|
+ params_count = (0,)
|
|
|
+ params = (0,)
|
|
|
+ if keyword:
|
|
|
+ sql_count += " AND (project_name LIKE %s)"
|
|
|
+ sql += " AND (project_name LIKE %s)"
|
|
|
+ params_count += (f"%{keyword}%",)
|
|
|
+ params += (f"%{keyword}%",)
|
|
|
+ sql += " ORDER BY create_time DESC LIMIT %s OFFSET %s"
|
|
|
+ params += (per_page, offset)
|
|
|
with self._db_helper:
|
|
|
+ result_count = self._db_helper.fetch_one(sql_count, params_count)
|
|
|
+ count = result_count["count"] if result_count else 0
|
|
|
+ result = self._db_helper.execute_query(sql, params)
|
|
|
data = []
|
|
|
- result = self._db_helper.execute_query(query)
|
|
|
if not result:
|
|
|
- return data
|
|
|
+ return data, count
|
|
|
for item in result:
|
|
|
data.append(
|
|
|
ProjectModel(
|
|
|
project_id=item["id"],
|
|
|
- project_no=item["project_no"],
|
|
|
project_name=item["project_name"],
|
|
|
+ description=item["description"],
|
|
|
+ delete_by=item["delete_by"],
|
|
|
+ delete_time=item["delete_time"],
|
|
|
+ create_by=item["create_by"],
|
|
|
+ create_time=item["create_time"],
|
|
|
+ update_by=item["update_by"],
|
|
|
+ update_time=item["update_time"],
|
|
|
+ ))
|
|
|
+ return data, count
|
|
|
+
|
|
|
+ def query_project_by_id(self, project_id: int) -> ProjectModel | None:
|
|
|
+ sql = "SELECT id,project_name,description,delete_by,delete_time,create_by,create_time,update_by,update_time FROM project WHERE is_del=0 AND id = %s"
|
|
|
+ params = (project_id,)
|
|
|
+ with self._db_helper:
|
|
|
+ result = self._db_helper.fetch_one(sql, params)
|
|
|
+ if not result:
|
|
|
+ return None
|
|
|
+ return ProjectModel(
|
|
|
+ project_id=result["id"],
|
|
|
+ project_name=result["project_name"],
|
|
|
+ description=result["description"],
|
|
|
+ delete_by=result["delete_by"],
|
|
|
+ delete_time=result["delete_time"],
|
|
|
+ create_by=result["create_by"],
|
|
|
+ create_time=result["create_time"],
|
|
|
+ update_by=result["update_by"],
|
|
|
+ update_time=result["update_time"],
|
|
|
+ )
|
|
|
+ def insert_project(self, project_data: ProjectModel):
|
|
|
+ sql = "INSERT INTO project (project_name,description,create_by,create_time,update_by,update_time) VALUES (%s,%s,%s,%s,%s,%s)"
|
|
|
+ params = (project_data.project_name,project_data.description, project_data.create_by, datetime.now(), project_data.update_by, datetime.now())
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
+ def update_project(self, project_data: ProjectModel):
|
|
|
+ sql = "UPDATE project SET project_name=%s,description=%s,update_by=%s,update_time=%s WHERE id=%s"
|
|
|
+ params = (project_data.project_name, project_data.description,project_data.update_by, datetime.now(), project_data.id)
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
+ def delete_project(self, project_id: int,delete_by: str=""):
|
|
|
+ sql = "UPDATE project SET is_del=1,project.delete_by=%s,delete_time=%s WHERE id=%s"
|
|
|
+ params = (delete_by, datetime.now(), project_id)
|
|
|
+ with self._db_helper:
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
+ def query_sub_project_all(self):
|
|
|
+ sql = "SELECT id,project_id,sub_project_name,work_catalog,work_content,standard_version,status,create_time FROM sub_project WHERE is_del=0"
|
|
|
+ with self._db_helper:
|
|
|
+ data = []
|
|
|
+ result = self._db_helper.execute_query(sql)
|
|
|
+ if not result:
|
|
|
+ return data
|
|
|
+ for item in result:
|
|
|
+ data.append(
|
|
|
+ SubProjectModel(
|
|
|
+ sub_id=item["id"],
|
|
|
+ project_id=item["project_id"],
|
|
|
+ sub_project_name=item["sub_project_name"],
|
|
|
work_catalog=item["work_catalog"],
|
|
|
work_content=item["work_content"],
|
|
|
standard_version=item["standard_version"],
|
|
|
@@ -32,42 +98,42 @@ class MysqlStore:
|
|
|
create_time=item["create_time"],
|
|
|
))
|
|
|
return data
|
|
|
- def query_project_all_paginated(self, page: int, per_page: int, keyword: str = None, status: int = None) -> (list[ProjectModel], None):
|
|
|
+ def query_sub_project_all_paginated(self, project_id: int, page: int, per_page: int, keyword: str = None, status: int = None) -> (list[SubProjectModel], None):
|
|
|
offset = (page - 1) * per_page
|
|
|
- query_count = "SELECT COUNT(*) as count FROM project WHERE is_del=%s"
|
|
|
- query = "SELECT id,project_no,project_name,work_catalog,work_content,standard_version,status,create_time FROM project WHERE is_del=0 "
|
|
|
- params_count = [0]
|
|
|
- params = []
|
|
|
- q_1=" AND (project_no LIKE %s OR project_name LIKE %s)"
|
|
|
+ sql_count = "SELECT COUNT(*) as count FROM sub_project WHERE is_del=0 AND project_id=%s"
|
|
|
+ sql = "SELECT id,project_id,sub_project_name,work_catalog,work_content,standard_version,status,create_time FROM sub_project WHERE is_del=0 AND project_id=%s"
|
|
|
+ params_count = (project_id,)
|
|
|
+ params = (project_id,)
|
|
|
+ q_1=" AND (sub_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}%"])
|
|
|
+ sql_count += q_1
|
|
|
+ params_count += (f"%{keyword}%",)
|
|
|
+ sql += q_1
|
|
|
+ params += (f"%{keyword}%",)
|
|
|
|
|
|
if status is not None:
|
|
|
- query_count += q_2
|
|
|
- params_count.append(status)
|
|
|
- query += q_2
|
|
|
- params.append(status)
|
|
|
+ sql_count += q_2
|
|
|
+ params_count += (status,)
|
|
|
+ sql += q_2
|
|
|
+ params += (status,)
|
|
|
|
|
|
- query += " ORDER BY status,create_time DESC LIMIT %s OFFSET %s"
|
|
|
- params.extend([per_page, offset])
|
|
|
+ sql += " ORDER BY status,create_time DESC LIMIT %s OFFSET %s"
|
|
|
+ params += (per_page, offset)
|
|
|
|
|
|
with self._db_helper:
|
|
|
- result_count = self._db_helper.fetch_one(query_count, tuple(params_count))
|
|
|
+ result_count = self._db_helper.fetch_one(sql_count, params_count)
|
|
|
count = result_count["count"] if result_count else 0
|
|
|
- result = self._db_helper.execute_query(query, tuple(params))
|
|
|
+ result = self._db_helper.execute_query(sql, params)
|
|
|
data = []
|
|
|
if not result:
|
|
|
return [], count
|
|
|
for item in result:
|
|
|
data.append(
|
|
|
- ProjectModel(
|
|
|
- project_id=item["id"],
|
|
|
- project_no=item["project_no"],
|
|
|
- project_name=item["project_name"],
|
|
|
+ SubProjectModel(
|
|
|
+ sub_id=item["id"],
|
|
|
+ project_id=item["project_id"],
|
|
|
+ sub_project_name=item["sub_project_name"],
|
|
|
work_catalog=item["work_catalog"],
|
|
|
work_content=item["work_content"],
|
|
|
standard_version=item["standard_version"],
|
|
|
@@ -75,87 +141,109 @@ class MysqlStore:
|
|
|
create_time=item["create_time"],
|
|
|
))
|
|
|
return data, count
|
|
|
- def query_project(self, project_id: int, with_items=False) -> ProjectModel | None:
|
|
|
- query = "SELECT id,project_no,project_name,work_catalog,work_content,standard_version,status,create_time FROM project WHERE is_del=0 AND id = %s"
|
|
|
- params = (project_id,)
|
|
|
- query_items = "SELECT id,project_id,device_name,device_model,standard_version,standard_no FROM project_item WHERE project_id = %s"
|
|
|
+ def query_sub_project(self, sub_project_id: int, with_items=False) -> SubProjectModel | None:
|
|
|
+ sql = "SELECT id,project_id,sub_project_name,work_catalog,work_content,standard_version,status,create_time FROM sub_project WHERE is_del=0 AND id = %s"
|
|
|
+ params = (sub_project_id,)
|
|
|
+ sql_items = "SELECT id,project_id,sub_project_id,device_name,device_model,standard_version,standard_no,process_status,process_time,send_status,send_time FROM sub_project_item WHERE sub_project_id = %s"
|
|
|
with self._db_helper:
|
|
|
- result = self._db_helper.fetch_one(query, params)
|
|
|
+ result = self._db_helper.fetch_one(sql, params)
|
|
|
if not result:
|
|
|
return None
|
|
|
- data = ProjectModel(result["project_no"],
|
|
|
- result["project_name"],
|
|
|
- result["work_catalog"],
|
|
|
- result["work_content"],
|
|
|
- result["standard_version"],
|
|
|
- result["status"],
|
|
|
- project_id=result["id"],
|
|
|
- create_time=result["create_time"])
|
|
|
+ data = SubProjectModel(
|
|
|
+ project_id=result["project_id"],
|
|
|
+ sub_project_name=result["sub_project_name"],
|
|
|
+ work_catalog=result["work_catalog"],
|
|
|
+ work_content=result["work_content"],
|
|
|
+ standard_version=result["standard_version"],
|
|
|
+ status=result["status"],
|
|
|
+ sub_id=result["id"],
|
|
|
+ create_time=result["create_time"])
|
|
|
if not with_items:
|
|
|
return data
|
|
|
- items_result = self._db_helper.execute_query(query_items, params)
|
|
|
+ items_result = self._db_helper.execute_query(sql_items, params)
|
|
|
if items_result:
|
|
|
for item in items_result:
|
|
|
data.add_item(
|
|
|
- ProjectItemModel(
|
|
|
+ SubProjectItemModel(
|
|
|
item_id=item["id"],
|
|
|
- project_id=item["project_no"],
|
|
|
+ project_id=item["project_id"],
|
|
|
+ sub_project_id=item["sub_project_id"],
|
|
|
device_name=item["device_name"],
|
|
|
device_model=item["device_model"],
|
|
|
standard_version=item["standard_version"],
|
|
|
standard_no=item["standard_no"],
|
|
|
+ process_status=item["process_status"],
|
|
|
+ process_time=item["process_time"],
|
|
|
+ send_status=item["send_status"],
|
|
|
+ send_time=item["send_time"],
|
|
|
))
|
|
|
return data
|
|
|
- def query_project_items_by_project_paginated(self, project_id: int, page: int, per_page: int, keyword: str = None) -> (list[ProjectItemModel], int):
|
|
|
+ def query_sub_project_items_by_project_paginated(self, sub_project_id: int, page: int, per_page: int, keyword: str = None, process_status: int = None,send_status: int = None) -> (list[SubProjectItemModel], int):
|
|
|
offset = (page - 1) * per_page
|
|
|
- query_count = "SELECT COUNT(*) as count FROM project_item WHERE project_id = %s"
|
|
|
- query = "SELECT id,project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no FROM project_item WHERE project_id = %s"
|
|
|
- params_count = (project_id,)
|
|
|
- params = (project_id,)
|
|
|
+ sql_count = "SELECT COUNT(*) as count FROM sub_project_item WHERE sub_project_id = %s"
|
|
|
+ sql = "SELECT id,project_id,sub_project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no,process_status,process_time,send_status,send_time FROM sub_project_item WHERE sub_project_id = %s"
|
|
|
+ params_count = (sub_project_id,)
|
|
|
+ params = (sub_project_id,)
|
|
|
|
|
|
if keyword:
|
|
|
- query_count += " AND (device_name LIKE %s OR device_model LIKE %s)"
|
|
|
+ sql_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)"
|
|
|
+ sql += " AND (device_name LIKE %s OR device_model LIKE %s)"
|
|
|
params += (f"%{keyword}%", f"%{keyword}%")
|
|
|
+ if process_status is not None:
|
|
|
+ sql_count += " AND process_status=%s"
|
|
|
+ params_count += (process_status,)
|
|
|
+ sql += " AND process_status=%s"
|
|
|
+ params += (process_status,)
|
|
|
+ if send_status is not None:
|
|
|
+ sql_count += " AND send_status=%s"
|
|
|
+ params_count += (send_status,)
|
|
|
+ sql += " AND send_status=%s"
|
|
|
+ params += (send_status,)
|
|
|
|
|
|
- query += " ORDER BY device_name,device_model LIMIT %s OFFSET %s"
|
|
|
+ sql += " 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)
|
|
|
+ result_count = self._db_helper.fetch_one(sql_count, params_count)
|
|
|
count = result_count["count"] if result_count else 0
|
|
|
- result = self._db_helper.execute_query(query, params)
|
|
|
+ result = self._db_helper.execute_query(sql, params)
|
|
|
data = []
|
|
|
if not result:
|
|
|
return data, count
|
|
|
for item in result:
|
|
|
data.append(
|
|
|
- ProjectItemModel(
|
|
|
+ SubProjectItemModel(
|
|
|
item_id=item["id"],
|
|
|
project_id=item["project_id"],
|
|
|
+ sub_project_id=item["sub_project_id"],
|
|
|
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"],
|
|
|
+ process_status=item["process_status"],
|
|
|
+ process_time=item["process_time"],
|
|
|
+ send_status=item["send_status"],
|
|
|
+ send_time=item["send_time"],
|
|
|
))
|
|
|
return data, count
|
|
|
- def query_project_items_by_project(self, project_id: int, with_empty=True) -> list[ProjectItemModel]:
|
|
|
- query = "SELECT id,project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no FROM project_item WHERE project_id = %s"
|
|
|
- query += "" if with_empty else " AND standard_no !='' AND standard_no IS NOT NULL"
|
|
|
- params = (project_id,)
|
|
|
+ def query_sub_project_items_by_project(self, sub_project_id: int, with_empty=True) -> list[SubProjectItemModel]:
|
|
|
+ sql = "SELECT id,project_id,sub_project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no FROM sub_project_item WHERE sub_project_id = %s"
|
|
|
+ sql += "" if with_empty else " AND standard_no !='' AND standard_no IS NOT NULL"
|
|
|
+ params = (sub_project_id,)
|
|
|
with self._db_helper:
|
|
|
- result = self._db_helper.execute_query(query, params)
|
|
|
+ result = self._db_helper.execute_query(sql, params)
|
|
|
data = []
|
|
|
if not result:
|
|
|
return data
|
|
|
for item in result:
|
|
|
data.append(
|
|
|
- ProjectItemModel(
|
|
|
+ SubProjectItemModel(
|
|
|
item_id=item["id"],
|
|
|
project_id=item["project_id"],
|
|
|
+ sub_project_id=item["sub_project_id"],
|
|
|
device_name=item["device_name"],
|
|
|
device_model=item["device_model"],
|
|
|
device_unit=item["device_unit"],
|
|
|
@@ -164,286 +252,142 @@ class MysqlStore:
|
|
|
standard_no=item["standard_no"],
|
|
|
))
|
|
|
return data
|
|
|
- def insert_project(self, project: ProjectModel) -> bool:
|
|
|
- query = "INSERT INTO project (project_no,project_name,work_catalog,work_content,standard_version,completion_tokens,prompt_tokens,total_tokens,create_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
|
|
|
+ def insert_sub_project(self, sub_project: SubProjectModel) -> int:
|
|
|
+ sql = "INSERT INTO sub_project (project_id,sub_project_name,work_catalog,work_content,standard_version,create_time,create_by) VALUES (%s,%s,%s,%s,%s,%s,%s)"
|
|
|
params = (
|
|
|
- project.project_no,
|
|
|
- project.project_name,
|
|
|
- project.work_catalog,
|
|
|
- project.work_content,
|
|
|
- project.standard_version,
|
|
|
- project.completion_tokens,
|
|
|
- project.prompt_tokens,
|
|
|
- project.total_tokens,
|
|
|
+ sub_project.project_id,
|
|
|
+ sub_project.sub_project_name,
|
|
|
+ sub_project.work_catalog,
|
|
|
+ sub_project.work_content,
|
|
|
+ sub_project.standard_version,
|
|
|
datetime.now(),
|
|
|
+ sub_project.create_by,
|
|
|
)
|
|
|
-
|
|
|
with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query, params)
|
|
|
- self.insert_project_item_list(project)
|
|
|
- return True
|
|
|
- def insert_project_item_list(self, project):
|
|
|
- if len(project.items) <= 0:
|
|
|
+ new_id = self._db_helper.execute_non_query(sql, params)
|
|
|
+ self.insert_sub_project_item_list(sub_project)
|
|
|
+ return new_id
|
|
|
+ def insert_sub_project_item_list(self, sub_project):
|
|
|
+ if len(sub_project.items) <= 0:
|
|
|
return
|
|
|
- query_items = "INSERT INTO project_item (project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no,update_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
|
|
|
+ sql = "INSERT INTO sub_project_item (project_id,sub_project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no,update_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
|
|
|
params_items = []
|
|
|
- for item in project.items:
|
|
|
+ for item in sub_project.items:
|
|
|
params_items.append((
|
|
|
- project.id,
|
|
|
+ sub_project.project_id,
|
|
|
+ sub_project.id,
|
|
|
item.device_name,
|
|
|
item.device_model,
|
|
|
item.device_unit,
|
|
|
item.device_count,
|
|
|
- project.standard_version,
|
|
|
+ sub_project.standard_version,
|
|
|
item.standard_no,
|
|
|
datetime.now(),
|
|
|
))
|
|
|
with self._db_helper:
|
|
|
- self._db_helper.execute_many(query_items, params_items)
|
|
|
- def update_project(self, project: ProjectModel):
|
|
|
- query = "UPDATE project SET project_name = %s, work_catalog = %s, work_content = %s, standard_version = %s ,status = %s,update_time = %s WHERE id = %s"
|
|
|
+ self._db_helper.execute_many(sql, params_items)
|
|
|
+ def update_sub_project(self, sub_project: SubProjectModel):
|
|
|
+ sql = "UPDATE sub_project SET sub_project_name = %s, work_catalog = %s, work_content = %s, standard_version = %s ,status = %s,update_time = %s WHERE id = %s"
|
|
|
params = (
|
|
|
- project.project_name,
|
|
|
- project.work_catalog,
|
|
|
- project.work_content,
|
|
|
- project.standard_version,
|
|
|
- project.status,
|
|
|
+ sub_project.sub_project_name,
|
|
|
+ sub_project.work_catalog,
|
|
|
+ sub_project.work_content,
|
|
|
+ sub_project.standard_version,
|
|
|
+ sub_project.status,
|
|
|
datetime.now(),
|
|
|
- project.id,
|
|
|
+ sub_project.id,
|
|
|
)
|
|
|
with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query, params)
|
|
|
- def update_project_status(self,project_id:int, status:int):
|
|
|
- query = "UPDATE project SET status = %s,update_time = %s WHERE id = %s"
|
|
|
- params = (status, datetime.now(), project_id,)
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
+ def update_sub_project_status(self, sub_project_id:int, status:int):
|
|
|
+ sql = "UPDATE sub_project SET status = %s,update_time = %s WHERE id = %s"
|
|
|
+ params = (status, datetime.now(), sub_project_id,)
|
|
|
with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query, params)
|
|
|
- def re_insert_project(self, project: ProjectModel) -> bool:
|
|
|
- if not project.id:
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
+ def re_insert_sub_project(self, sub_project: SubProjectModel) -> bool:
|
|
|
+ if not sub_project.id:
|
|
|
return False
|
|
|
- self.update_project(project)
|
|
|
- query = "DELETE FROM project_item WHERE project_id = %s"
|
|
|
- params = (project.id,)
|
|
|
+ self.update_sub_project(sub_project)
|
|
|
+ sql = "DELETE FROM sub_project_item WHERE sub_project_id = %s"
|
|
|
+ params = (sub_project.id,)
|
|
|
with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query, params)
|
|
|
- self.insert_project_item_list(project)
|
|
|
- def delete_project(self, project_id: int) -> bool:
|
|
|
- query_1 = "UPDATE project SET is_del=1,delete_time=%s WHERE id = %s"
|
|
|
- # query_2 = "DELETE FROM project_item WHERE project_no = %s"
|
|
|
- params = (datetime.now(), project_id,)
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
+ self.insert_sub_project_item_list(sub_project)
|
|
|
+ def delete_sub_project(self, sub_project_id: int, delete_by: str="") -> bool:
|
|
|
+ sql = "UPDATE sub_project SET is_del=1,delete_by=%s,delete_time=%s WHERE id = %s"
|
|
|
+ params = (delete_by, datetime.now(), sub_project_id,)
|
|
|
with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query_1, params)
|
|
|
- # self._db_helper.execute_non_query(query_2, params)
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
return True
|
|
|
- def query_project_item_by_id(self, item_id:int) -> ProjectItemModel | None:
|
|
|
- query = "SELECT id,project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no FROM project_item WHERE id = %s"
|
|
|
+ def query_sub_project_item_by_id(self, item_id:int) -> SubProjectItemModel | None:
|
|
|
+ sql = "SELECT id,project_id,sub_project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no,process_status,process_time,send_status,send_time FROM sub_project_item WHERE id = %s"
|
|
|
params = (item_id,)
|
|
|
with self._db_helper:
|
|
|
- result = self._db_helper.fetch_one(query, params)
|
|
|
+ result = self._db_helper.fetch_one(sql, params)
|
|
|
if not result:
|
|
|
return None
|
|
|
- data = ProjectItemModel(
|
|
|
+ data = SubProjectItemModel(
|
|
|
item_id=result["id"],
|
|
|
project_id=result["project_id"],
|
|
|
+ sub_project_id=result["project_id"],
|
|
|
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"],
|
|
|
+ process_status=result["process_status"],
|
|
|
+ process_time=result["process_time"],
|
|
|
+ send_status=result["send_status"],
|
|
|
+ send_time=result["send_time"],
|
|
|
update_time=datetime.now()
|
|
|
)
|
|
|
return data
|
|
|
- def query_project_item(self, project_id: int, device_name:str, device_model:str) -> ProjectItemModel | None:
|
|
|
- query = "SELECT id,project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no FROM project_item WHERE project_id = %s AND device_name = %s AND device_model = %s"
|
|
|
- params = (project_id,device_name, device_model )
|
|
|
- with self._db_helper:
|
|
|
- result = self._db_helper.fetch_one(query, params)
|
|
|
- if not result:
|
|
|
- return None
|
|
|
- data = ProjectItemModel(
|
|
|
- item_id=result["id"],
|
|
|
- project_id=result["project_id"],
|
|
|
- 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_project_item(self, project_item: ProjectItemModel) -> bool:
|
|
|
- query = "INSERT INTO project_item (project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no,update_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
|
|
|
+ def insert_sub_project_item(self, project_item: SubProjectItemModel) -> bool:
|
|
|
+ sql = "INSERT INTO sub_project_item (project_id,sub_project_id,device_name,device_model,device_unit,device_count,standard_version,standard_no,process_status,send_status,update_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
|
|
|
params = (
|
|
|
project_item.project_id,
|
|
|
+ project_item.sub_project_id,
|
|
|
project_item.device_name,
|
|
|
project_item.device_model,
|
|
|
project_item.device_unit,
|
|
|
project_item.device_count,
|
|
|
project_item.standard_version,
|
|
|
project_item.standard_no,
|
|
|
+ project_item.process_status,
|
|
|
+ project_item.send_status,
|
|
|
datetime.now(),
|
|
|
)
|
|
|
with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query, params)
|
|
|
- return True
|
|
|
- def update_project_item(self, project_item: ProjectItemModel) -> bool:
|
|
|
- query = "SElECT standard_no FROM project_item WHERE id = %s"
|
|
|
- params = (project_item.id,)
|
|
|
- u_query = "UPDATE project_item SET device_name= %s,device_model= %s,device_unit= %s,device_count= %s,standard_no = %s,update_time = %s WHERE id = %s"
|
|
|
- u_params = (project_item.device_name, project_item.device_model,project_item.device_unit, project_item.device_count, project_item.standard_no, datetime.now(), project_item.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(
|
|
|
- StandardModel(device_name=project_item.device_name,
|
|
|
- device_model=project_item.device_model,
|
|
|
- device_unit=project_item.device_unit,
|
|
|
- standard_version=project_item.standard_version,
|
|
|
- standard_no=project_item.standard_no),
|
|
|
- project_item.project_id)
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
return True
|
|
|
- def delete_project_item_by_id(self, item_id: int):
|
|
|
- query = "DELETE FROM project_item WHERE id = %s"
|
|
|
- params = (item_id, )
|
|
|
- with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query, params)
|
|
|
- return True
|
|
|
-
|
|
|
- def update_project_item_standard_no_batch(self,items:list[ProjectItemModel]):
|
|
|
- data =datetime.now()
|
|
|
- for item in items:
|
|
|
- query = "UPDATE project_item SET standard_no = %s,update_time = %s WHERE id = %s"
|
|
|
- params = (item.standard_no, data, item.id)
|
|
|
- with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query, params)
|
|
|
-
|
|
|
- def query_standard_data_by_no(self, standard_no: str,version:str):
|
|
|
- query = "SELECT device_model,device_name,device_unit,standard_version,standard_no FROM standard_data WHERE standard_no = %s AND standard_version = %s"
|
|
|
- params = (standard_no,version,)
|
|
|
- with self._db_helper:
|
|
|
- result = self._db_helper.fetch_one(query, params)
|
|
|
- if not result:
|
|
|
- return None
|
|
|
- data = StandardModel(
|
|
|
- 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 query_standard_data(self, device_name: str,
|
|
|
- device_model: str) -> list[StandardModel] | 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(StandardModel(
|
|
|
- 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_standard_data(self, standard_data: StandardModel) -> bool:
|
|
|
- query = "INSERT INTO standard_data (device_name,device_model,device_unit,standard_version,standard_no,create_time) VALUES (%s,%s,%s,%s,%s,%s)"
|
|
|
+ def update_sub_project_item(self, project_item: SubProjectItemModel) -> bool:
|
|
|
+ sql = "UPDATE sub_project_item SET device_name= %s,device_model= %s,device_unit= %s,device_count= %s,standard_no = %s,update_time = %s WHERE id = %s"
|
|
|
params = (
|
|
|
- standard_data.device_name,
|
|
|
- standard_data.device_model,
|
|
|
- standard_data.device_unit,
|
|
|
- standard_data.standard_version,
|
|
|
- standard_data.standard_no,
|
|
|
+ project_item.device_name,
|
|
|
+ project_item.device_model,
|
|
|
+ project_item.device_unit,
|
|
|
+ project_item.device_count,
|
|
|
+ project_item.standard_no,
|
|
|
datetime.now(),
|
|
|
+ project_item.id
|
|
|
)
|
|
|
with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query, params)
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
return True
|
|
|
-
|
|
|
- def update_standard_data(self, standard_data: StandardModel) -> bool:
|
|
|
- query = "UPDATE standard_data SET device_name = %s, device_model = %s, device_unit= %s WHERE standard_no = %s AND standard_version = %s"
|
|
|
- params = (
|
|
|
- standard_data.device_name,
|
|
|
- standard_data.device_model,
|
|
|
- standard_data.device_unit,
|
|
|
- standard_data.standard_no,
|
|
|
- standard_data.standard_version,
|
|
|
- )
|
|
|
- with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query, params)
|
|
|
- return True
|
|
|
-
|
|
|
- def insert_or_update_standard_data(self,
|
|
|
- standard_data: StandardModel,
|
|
|
- project_id: int = 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,
|
|
|
- )
|
|
|
+ def delete_sub_project_item_by_id(self, item_id: int):
|
|
|
+ sql = "DELETE FROM sub_project_item WHERE id = %s"
|
|
|
+ params = (item_id, )
|
|
|
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_id:
|
|
|
- self.insert_standard_update_log(
|
|
|
- StandardUpdateLogModel(
|
|
|
- project_id=project_id,
|
|
|
- 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)
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
return True
|
|
|
-
|
|
|
- def query_standard_update_log(self, project_id: int) -> list[StandardUpdateLogModel] | None:
|
|
|
- query = "SELECT project_id,device_name,device_model,device_unit,standard_version,new_standard_no,old_standard_no,create_time FROM standard_update_log WHERE project_id = %s"
|
|
|
- params = (project_id, )
|
|
|
+ def update_sub_project_item_process_status(self, item_id:int, status:int):
|
|
|
+ sql = "UPDATE sub_project_item SET process_status = %s,process_time = %s WHERE id = %s"
|
|
|
+ params = (status, datetime.now(), item_id,)
|
|
|
with self._db_helper:
|
|
|
- results = self._db_helper.execute_query(query, params)
|
|
|
- if not results:
|
|
|
- return None
|
|
|
- data = [StandardUpdateLogModel(**result) for result in results]
|
|
|
- return data
|
|
|
-
|
|
|
- def insert_standard_update_log(
|
|
|
- self, standard_update_log: StandardUpdateLogModel) -> bool:
|
|
|
- query = "INSERT INTO standard_update_log (project_id,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_id,
|
|
|
- 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(),
|
|
|
- )
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|
|
|
+ def update_sub_project_item_send_status(self, item_id:int, status:int):
|
|
|
+ sql = "UPDATE sub_project_item SET send_status = %s,send_time = %s WHERE id = %s"
|
|
|
+ params = (status, datetime.now(), item_id,)
|
|
|
with self._db_helper:
|
|
|
- self._db_helper.execute_non_query(query, params)
|
|
|
- return True
|
|
|
+ self._db_helper.execute_non_query(sql, params)
|