import uuid from app.config import TaskEnv from app.models.register_status import RegisterStatusModel from core.repositories.base import BaseRepository from core.service.mongo_db import use_collection class RegisterStatusRepository(BaseRepository): def __init__(self): super().__init__(collection=TaskEnv.CLN_AUDIT_STATUS, model=RegisterStatusModel) async def get_last(self,project_id:uuid.UUID): last = await use_collection(TaskEnv.CLN_AUDIT_STATUS).find({"project_id":project_id}).sort('$natural', -1).to_list(1) if last and len(last) > 0 and 'number' in last[0]: return last[0]['number'] else: return None async def get_list(self,project_id:uuid.UUID): list_status = await use_collection(TaskEnv.CLN_AUDIT_STATUS).find({"project_id":project_id}).to_list(None) return list_status async def create(self, coding: RegisterStatusModel): result = await super().create(coding) price = await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).find_one({'project_id': coding.project_id}) if price and coding.amount_ّfinancial: if coding.sts ==1: await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one( {'project_id': coding.project_id}, {"$inc": {'amount_status': coding.amount_ّfinancial}} ) elif coding.sts == 2: await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one( {'project_id': coding.project_id}, {"$inc": {'amount_adjust': coding.amount_ّfinancial}} ) return result async def update(self, _id: uuid.UUID, _update: RegisterStatusModel): _old_dic = await use_collection(TaskEnv.CLN_AUDIT_STATUS).find_one({'_id':_id}) change = 0 result = await super().update(_id, _update) price = await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).find_one({'project_id': _update.project_id}) if price: _old = RegisterStatusModel(**_old_dic) if 'amount_ّfinancial' not in _old_dic and _update.amount_ّfinancial: change = _update.amount_ّfinancial if _old.amount_ّfinancial != _update.amount_ّfinancial: change = _update.amount_ّfinancial - _old.amount_ّfinancial if change != 0 : if _old.sts == 1: await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one( {'project_id': _update.project_id}, {"$inc": {'amount_status': change}} ) elif _old.sts == 2: await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one( {'project_id': _update.project_id}, {"$inc": {'amount_adjust': change}} ) return result async def delete(self, _id: uuid.UUID): _old_dic = await use_collection(TaskEnv.CLN_AUDIT_STATUS).find_one({'_id': _id}) await super().delete(_id) price = await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).find_one({'project_id': _old_dic['project_id']}) if price and _old_dic['sts'] == 1 and 'amount_status' in price: await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one( {'project_id': _old_dic['project_id']}, {"$inc": {'amount_status': -(_old_dic['amount_ّfinancial'])}} ) if price and _old_dic['sts'] == 2 and 'amount_adjust' in price: await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one( {'project_id': _old_dic['project_id']}, {"$inc": {'amount_adjust': -(_old_dic['amount_ّfinancial'])}} ) return True