108 lines
4.6 KiB
Python
108 lines
4.6 KiB
Python
import copy
|
|
import uuid
|
|
from typing import List
|
|
from uuid import UUID
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from app.config import TaskEnv
|
|
from app.models.design_progress import DesignProgressModel, CreateDesignProgress, RecordModel, GetRecord, \
|
|
UpdateDesignProgress
|
|
from core.repositories.base import BaseRepository
|
|
from core.service.mongo_db import use_collection
|
|
from core.utils.crudify import mongo_soft_deleter
|
|
|
|
COLLECTION = "design_progress"
|
|
|
|
class DesignProgressRepository(BaseRepository):
|
|
def __init__(self):
|
|
super().__init__(collection=COLLECTION, model=CreateDesignProgress)
|
|
|
|
async def create(self, _inp: CreateDesignProgress):
|
|
exist = await use_collection(COLLECTION).find_one({
|
|
"project_id": _inp.project_id,
|
|
"type": _inp.type,
|
|
"sub_type": _inp.sub_type
|
|
})
|
|
if not exist:
|
|
project = await use_collection(TaskEnv.CLN_PROJECT).find_one({"_id":_inp.project_id})
|
|
progress = DesignProgressModel(
|
|
project_id=_inp.project_id,
|
|
type=_inp.type,
|
|
parent_id=_inp.parent_id,
|
|
sub_type=_inp.sub_type,
|
|
state = project['design']['state']
|
|
)
|
|
progress.init_id().cu_at()
|
|
await use_collection(COLLECTION).insert_one(progress.mongo_dict())
|
|
get_record = GetRecord(
|
|
project_id=_inp.project_id,
|
|
type=_inp.type,
|
|
sub_type=_inp.sub_type
|
|
)
|
|
return await self.get(get_record)
|
|
|
|
async def get(self, get_record: GetRecord):
|
|
progress = await use_collection(COLLECTION).find_one(
|
|
{"project_id": get_record.project_id, "type": get_record.type, "sub_type": get_record.sub_type})
|
|
if progress:
|
|
progress['records'] = await use_collection('design_progress_record').find(
|
|
{"design_progress_id": progress["_id"]}).to_list(None)
|
|
return progress
|
|
|
|
async def update(self, _id: UUID, _inp: UpdateDesignProgress):
|
|
start_at = None
|
|
design_progress = await use_collection(COLLECTION).find_one({"_id": _id})
|
|
design_progress = DesignProgressModel(**design_progress)
|
|
if design_progress.type == 1:
|
|
records = await use_collection('design_progress_record').find_one(
|
|
{"design_progress_id": _id})
|
|
start_at = records['send']['date_at']
|
|
else:
|
|
design_progress_before = await use_collection(COLLECTION).find_one({
|
|
"type":design_progress.type-1,"project_id":design_progress.project_id
|
|
})
|
|
if design_progress_before and "complete_at" in design_progress_before:
|
|
start_at = design_progress_before["complete_at"]
|
|
await use_collection(COLLECTION).update_one({"_id": _id}, {"$set": {
|
|
"start_at": start_at, "complete_at": _inp.complete_at
|
|
}})
|
|
if design_progress.type == 1:
|
|
await use_collection('project').update_one({"_id": design_progress.project_id}, {"$set": {"design.sts": 2}})
|
|
elif design_progress.type == 7:
|
|
await use_collection('project').update_one({"_id": design_progress.project_id}, {"$set": {"design.sts": 4}})
|
|
get_record = GetRecord(
|
|
project_id=design_progress.project_id,
|
|
type=design_progress.type,
|
|
sub_type=design_progress.sub_type
|
|
)
|
|
return await self.get(get_record)
|
|
|
|
async def check_complete(self, get_record: GetRecord):
|
|
result = {
|
|
"exist": False
|
|
}
|
|
if get_record.type:
|
|
if get_record.type > 1:
|
|
progress = await use_collection(COLLECTION).find_one(
|
|
{"project_id": get_record.project_id, "type": int(get_record.type) - 1,
|
|
"complete_at": {"$exists": 1},"sub_type":{"$exists":0}})
|
|
if not progress:
|
|
return {"exist": True, "detail": 'before type not complete'}
|
|
if get_record.type < 7:
|
|
progress = await use_collection(COLLECTION).find(
|
|
{"project_id": get_record.project_id, "type": int(get_record.type) + 1}).to_list(None)
|
|
if progress:
|
|
ids = [i["_id"] for i in progress]
|
|
records = await use_collection('design_progress_record').find_one(
|
|
{"design_progress_id": {"$in": ids}})
|
|
if records:
|
|
return {"exist": True, "detail": "next record is exist"}
|
|
|
|
return result
|
|
|
|
class DesignProgressRecordRepository(BaseRepository):
|
|
def __init__(self):
|
|
super().__init__(collection='design_progress_record', model=RecordModel)
|
|
|