feat(monitor physical):add update

This commit is contained in:
shahzeydyfah
2024-11-23 00:37:23 +03:30
parent 7b9dec0117
commit bc6e6b6fd3
@@ -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