import copy import uuid from typing import List from uuid import UUID from fastapi import HTTPException from app.models.design_progress import DesignProgressModel, CreateDesignProgress, RecordModel, GetRecord 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[CreateDesignProgress]): def __int__(self): super().__init__(collection=COLLECTION, model=CreateDesignProgress) def create_progress_model(self,_inp:CreateDesignProgress, type_create='create')->DesignProgressModel: if _inp.complete_at and _inp.records: _inp.start_at = _inp.records[0].send.date_at progress = DesignProgressModel( project_id=_inp.project_id, type=_inp.type, parent_id=_inp.parent_id, sub_type=_inp.sub_type, start_at= _inp.start_at, complete_at= _inp.complete_at ) if type_create == 'create': progress.init_id().cu_at() else: progress.u_at() return progress def create_record(self,_inp:CreateDesignProgress,_id:uuid.UUID=None): records = [] for record in _inp.records: save_record = RecordModel( design_progress_id=_id, title=record.title, send=record.send, receive=record.receive ).init_id().cu_at() records.append(save_record) return records async def create(self,create:CreateDesignProgress): progress = self.create_progress_model(create) await use_collection(COLLECTION).insert_one(progress.mongo_dict()) if create.records: list_records = [] records = self.create_record(create, progress.id) for record in records: list_records.append(record.mongo_dict()) await use_collection('design_progress_record').insert_many(list_records) get_record = GetRecord( project_id=create.project_id, type=create.type, sub_type=create.sub_type ) return await self.get(get_record) async def get(self,get_record:GetRecord): print("2222222") if get_record.type in [2,3] and not get_record.sub_type: raise HTTPException(status_code=422,detail='لطفا نوع زیرمجموعه را ارد کنید') 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, update: CreateDesignProgress): progress = self.create_progress_model(update,'update') print(progress) print(_id) await use_collection(COLLECTION).update_one({"_id":_id},{"$set":progress.mongo_dict()}) if update.records: before_records = await use_collection('design_progress_record').distinct("_id",{"design_progress_id":_id}) update_records = [] update_record_ids = [] create_records = [] delete_records = list(before_records).copy() for record in update.records: if record.id: save_record = { "_id":record.id, "title":record.title, "design_progress_id":_id, "send":record.send.dict(), "receive":record.receive.dict() } update_records.append(save_record) update_record_ids.append(record.id) delete_records.remove(record.id) else: save_record = RecordModel( design_progress_id=_id, title=record.title, send=record.send, receive=record.receive ).init_id().cu_at() create_records.append(save_record.mongo_dict()) if update_records: print("111111111",update_record_ids) await use_collection('design_progress_record').update_many({"_id": {"$in":update_record_ids}},{"$set":update_records}) if create_records: await use_collection('design_progress_record').insert_many(create_records) if delete_records: await mongo_soft_deleter({"_id":{"$in":delete_records}},'design_progress_record') return await self.get(progress.id)