28 lines
1.1 KiB
Python
28 lines
1.1 KiB
Python
import uuid
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from app.config import TaskEnv
|
|
from app.models.program_audit import ProgramModel, CheckLast
|
|
from core.repositories.base import BaseRepository
|
|
from core.service.mongo_db import use_collection
|
|
|
|
|
|
class ProgramRepository(BaseRepository):
|
|
def __init__(self):
|
|
super().__init__(collection=TaskEnv.CLN_AUDIT_PROGRAM,model=ProgramModel)
|
|
|
|
async def is_last(self,_inp:CheckLast):
|
|
last = await use_collection(TaskEnv.CLN_AUDIT_PROGRAM).find({"project_id":_inp.project_id}).sort('$natural', -1).to_list(1)
|
|
if last[0]["_id"] == _inp.audit_program_id:
|
|
return True
|
|
else:
|
|
raise HTTPException(status_code=422,detail='امکان ویرایش برای این آیتم وجود ندارد')
|
|
|
|
async def cal_sum(self, project_id: uuid.UUID):
|
|
sum = await use_collection(TaskEnv.CLN_AUDIT_PROGRAM).aggregate([
|
|
{"$match": {"project_id": project_id}},
|
|
{"$group": {"_id": None, "sum_add": {"$sum": "$add_price"},"sum_sub": {"$sum": "$sub_price"},"sum_diff": {"$sum": "$diff_price"}}}
|
|
]).to_list(None)
|
|
return sum
|