49 lines
1.9 KiB
Python
49 lines
1.9 KiB
Python
from uuid import UUID
|
|
|
|
from app.models.project import ProjectModel
|
|
from core.repositories.base import BaseRepository
|
|
from core.service.mongo_db import use_collection
|
|
|
|
COLLECTION = "project"
|
|
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):
|
|
print("11111111111111111")
|
|
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()})
|
|
return await self.get(_id) |