From bc6e6b6fd3bae9d218c7f8732d57d81f06a291ac Mon Sep 17 00:00:00 2001 From: shahzeydyfah Date: Sat, 23 Nov 2024 00:37:23 +0330 Subject: [PATCH] feat(monitor physical):add update --- app/repositories/monitor_physical_progress.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/app/repositories/monitor_physical_progress.py b/app/repositories/monitor_physical_progress.py index 8e99e04..6fab3f6 100644 --- a/app/repositories/monitor_physical_progress.py +++ b/app/repositories/monitor_physical_progress.py @@ -1,6 +1,8 @@ import uuid import datetime +from pymongo import ReturnDocument + from app.config import TaskEnv from app.models.monito_physical_progress import PhysicalProgressModel, LastRecord from core.repositories.base import BaseRepository @@ -56,3 +58,41 @@ class PhysicalProgressRepository(BaseRepository): async def list_by_project(self,_id:uuid.UUID): _get = await use_collection(COLLECTION).find({"project_id":_id}).to_list(None) return _get + + async def update(self, _id: uuid.UUID, update: MODEL): + _get = await use_collection(COLLECTION).find_one({"_id": _id}) + res = await use_collection(COLLECTION).find_one_and_update({"_id":_id},{"$set":update.mongo_dict()},return_document=ReturnDocument.AFTER) + _get_model = MODEL(**_get) + if _get_model.real != update.real: + _list = await use_collection(COLLECTION).find({"project_id":_get_model.project_id, "date_at":{"$gt":_get_model.date_at}}).to_list(None) + if _list: + for i in _list: + _i_model = MODEL(**i) + _avg = await use_collection(TaskEnv.CLN_MONITOR_PYSICAL).aggregate([ + {'$match': {'project_id': update.project_id, "date_at": {"$lt": _i_model.date_at}}}, + {'$group': {'_id': None, 'avg': {'$avg': "$real"}}} + ]).to_list(None) + _model = LastRecord( + project_id= _i_model.project_id, + date_at= _i_model.date_at + ) + _last = await self.last_record(_model) + unset = {} + _update = {'medium_progress': _avg[0]['avg']} + if _last['month']: + _update.update({'last_month':_last['month']['real']}) + else: + unset.update({'last_month': None}) + if _last['three_month']: + _update.update({'last_three_month': _last['three_month']['real']}) + else: + unset.update({'last_three_month': None}) + if unset: + await use_collection(COLLECTION).update_one({"_id": _i_model.id}, {"$set":_update, "$unset":unset}) + else: + await use_collection(COLLECTION).update_one({"_id": _i_model.id},{"$set": _update}) + return res + + + +