Files
sakhteman/app/repositories/project.py
T

157 lines
6.1 KiB
Python
Raw Normal View History

2024-06-08 00:01:04 +03:30
import uuid
2024-02-15 00:17:51 +03:30
from uuid import UUID
2024-03-14 15:33:17 +03:30
from fastapi import HTTPException
2024-06-08 00:01:04 +03:30
from app.config import TaskEnv
from app.models.project import ProjectModel, UpdateSts, ProjectSts
2024-02-15 00:17:51 +03:30
from core.repositories.base import BaseRepository
from core.service.mongo_db import use_collection
2024-09-12 13:39:00 +03:30
from core.utils.table_engine import TableEngineRequestType, TableEngineExport
from core.utils.te import NewTableEngine
from typing import TypeVar
2024-02-15 00:17:51 +03:30
COLLECTION = "project"
2024-09-12 13:39:00 +03:30
T = TypeVar('T')
2024-02-15 00:17:51 +03:30
class ProjectRepository(BaseRepository[ProjectModel]):
def __init__(self):
super().__init__(collection=COLLECTION, model=ProjectModel)
def create_code(self,coding:ProjectModel):
code = str(coding.counter)
print("code1",code)
if len(code) < 4:
code = code.rjust(4, '0')
print("code2", code)
code = coding.design.nature + coding.design.type + code
return code
async def create(self,coding:ProjectModel):
coding.counter = 1
last_project = await use_collection(COLLECTION).find({}).sort("$natural",-1).to_list(None)
if last_project:
coding.counter = last_project[0]["counter"]+1
code = self.create_code(coding)
coding.design.code = 'D'+code
if coding.monitor:
coding.monitor.code = 'S'+code
coding.init_id().cu_at()
await use_collection(COLLECTION).insert_one(coding.mongo_dict())
return await self.get(coding.id)
async def update(self, _id: UUID, update: ProjectModel):
old_project = await use_collection(COLLECTION).find_one({"_id":_id})
old_project = ProjectModel(**old_project)
if update.monitor and not old_project.monitor:
code = self.create_code(update)
update.monitor.code = 'S'+code
if update.design.nature != old_project.design.nature or update.design.type != old_project.design.type :
code = self.create_code(update)
update.design.code = 'D' + code
if update.monitor:
update.monitor.code = 'S' + code
update.u_at()
await use_collection(COLLECTION).update_one({"_id":_id},{"$set":update.mongo_dict()})
2024-03-05 00:48:22 +03:30
return await self.get(_id)
2024-03-14 15:33:17 +03:30
async def update_design_sts(self, _id: UUID, update: UpdateSts):
old_project = await use_collection(COLLECTION).find_one({"_id":_id})
if not old_project:
raise HTTPException(status_code=400,detail='این پروژه وجود ندارد')
await use_collection(COLLECTION).update_one({"_id":_id},{"$set":{
'design.sts':update.sts,'design.reason_stop':update.reason_stop
}})
2024-06-08 00:01:04 +03:30
sts_class = ProjectSts(
project_id= _id,
type = 'design',
sts= update.sts,
description= update.reason_stop
).init_id().cu_at()
await use_collection(TaskEnv.CLN_PROJECT_STS).insert_one(sts_class.mongo_dict())
return await self.get(_id)
async def update_monitor_sts(self, _id: UUID, update: UpdateSts):
old_project = await use_collection(COLLECTION).find_one({"_id":_id})
if not old_project:
raise HTTPException(status_code=400,detail='این پروژه وجود ندارد')
await use_collection(COLLECTION).update_one({"_id":_id},{"$set":{
'monitor.sts':update.sts,'monitor.reason_sts':update.reason_stop
}})
sts_class = ProjectSts(
project_id= _id,
type = 'monitor',
sts= update.sts,
description= update.reason_stop
).init_id().cu_at()
await use_collection(TaskEnv.CLN_PROJECT_STS).insert_one(sts_class.mongo_dict())
return await self.get(_id)
2024-03-14 15:33:17 +03:30
async def delete(self, _id: UUID):
old_project = await use_collection(COLLECTION).find_one({"_id":_id})
if not old_project:
raise HTTPException(status_code=400,detail='این پروژه وجود ندارد')
await use_collection(COLLECTION).update_one({"_id":_id},{"$set":{
'sts':2,
}})
return True
2024-09-12 14:52:19 +03:30
async def restore(self, _id: UUID):
old_project = await use_collection(COLLECTION).find_one({"_id":_id})
if not old_project:
raise HTTPException(status_code=400,detail='این پروژه وجود ندارد')
await use_collection(COLLECTION).update_one({"_id":_id},{"$set":{
'sts':1,
}})
return True
2024-03-14 15:33:17 +03:30
2024-03-05 00:48:22 +03:30
async def list_project_design(self):
project = await use_collection(COLLECTION).find({'monitor':{"$exists":0}}).to_list(None)
return project
async def list_project_monitor(self):
project = await use_collection(COLLECTION).find({'monitor':{"$exists":1}}).to_list(None)
2024-06-08 00:01:04 +03:30
return project
async def list_sts_design(self,_id:uuid.UUID):
sts_project = await use_collection(TaskEnv.CLN_PROJECT_STS).find({
"project_id":_id,
"type":"design"
}).to_list(None)
return sts_project
async def list_sts_monitor(self,_id:uuid.UUID):
sts_project = await use_collection(TaskEnv.CLN_PROJECT_STS).find({
"project_id":_id,
"type":"monitor"
}).to_list(None)
return sts_project
async def set_sts_two_for_monitor(self,project_id:uuid.UUID):
project = await use_collection(TaskEnv.CLN_PROJECT).find_one({"_id": project_id})
if not ('sts' in project['monitor'] and project['monitor']['sts'] > 1):
await use_collection(TaskEnv.CLN_PROJECT).update_one(
{'_id': project_id},
{"$set":{"monitor.sts": 2}})
sts_class = ProjectSts(
project_id=project_id,
type='monitor',
sts=2
).init_id().cu_at()
await use_collection(TaskEnv.CLN_PROJECT_STS).insert_one(sts_class.mongo_dict())
2024-09-12 13:39:00 +03:30
return True
async def fetch_delete(self, _input: TableEngineRequestType) -> TableEngineExport[T]:
return await NewTableEngine.mongo(
query_class=use_collection(self.COLLECTION),
soft_deleted=True,
model=self.MODEL,
search_cols=['name'],
2024-09-12 13:43:41 +03:30
extra_filter=[{'sts':2}],
2024-09-12 13:39:00 +03:30
inputs=_input.te) \
.to_dict()