import datetime import uuid import pandas as pd import os from app.config import TaskEnv from app.models.design_progress import DesignProgressModel, RecordModel, SubRecord, Record, UpdateDesignProgress from app.models.monito_physical_progress import LastRecord, PhysicalProgressModel from app.models.monitor_rial_progress import RialProgressModel from app.models.project import ProjectModel, ProjectSts from core.service.mongo_db import use_collection import jdatetime from app.repositories.project import ProjectRepository from app.repositories.design_progress import DesignProgressRepository from app.repositories.monitor_physical_progress import PhysicalProgressRepository from app.repositories.monitor_rial_progress import RialProgressRepository class ExcelRepository(): async def addDesignSheet(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # این یعنی مسیر فایل جاری (مثلاً main.py) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # رفتن به ریشه پروژه # excel_path = os.path.join(project_root, "projects1.xlsx") # print("Excel file path:", excel_path) # df = pd.read_excel(excel_path, engine="openpyxl", sheet_name="طراحی", dtype={'code': str,'date_send':str}) # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code",my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects',p) # if my_row['letter'] and pd.notna(row['letter']): # p['design']["letter"] = my_row["letter"] # if my_row['date_send'] and pd.notna(row['date_send']): # year = int(my_row['date_send'][:4]) # month = int(my_row['date_send'][4:6]) # day = int(my_row['date_send'][6:8]) # jalali_date = jdatetime.date(year, month, day) # gregorian_date = jalali_date.togregorian() # created_at = datetime.datetime.combine(gregorian_date, datetime.time()) # p['design']['date_send'] = created_at # if my_row['land_area'] and pd.notna(row['land_area']): # p['design']["land_area"] = my_row["land_area"] # if my_row['building_infrastructure'] and pd.notna(row['building_infrastructure']): # p['design']["building_infrastructure"] = my_row["building_infrastructure"] # if my_row['site_area'] and pd.notna(row['site_area']): # p['design']["site_area"] = my_row["site_area"] # if my_row['designer'] and pd.notna(row['designer']): # p['design']["designer"] = [my_row["designer"]] # p['design']["sts"] = 2 # # await use_collection(TaskEnv.CLN_PROJECT).update_one({'counter':int(my_row["code"])},{"$set":{'design':p['design']}}) return async def addMonitorSheet(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # excel_path = os.path.join(project_root, "projects1.xlsx") # print("Excel file path:", excel_path) # df = pd.read_excel(excel_path, engine="openpyxl", sheet_name="نظارت", dtype={'code': str,'date_create_project':str,'date_estimate_complete':str}) # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code",my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects',p) # p['monitor'] = {} # p['monitor']['base_information'] = {"budget" : []} # p['monitor']['detail'] = {"add_25_percent" : [],"adding" : []} # p['monitor']['agent'] = {} # p['monitor']['status_branch'] = {'electricity': False, 'water': False, 'wastewater': False, 'gas': False, 'phone': False} # if my_row['building_infrastructure'] and pd.notna(row['building_infrastructure']): # p['design']["building_infrastructure"] = my_row["building_infrastructure"] # print('a') # if my_row['land_area'] and pd.notna(row['land_area']): # p['design']["land_area"] = my_row["land_area"] # print('a1',my_row['monitor_type']) # if my_row['monitor_type'] == 'عالیه' : # print('b1') # p['monitor']['base_information']['type'] = 39 # elif my_row['monitor_type'] == 'عالیه و کارگاهی' : # print('b2') # p['monitor']['base_information']['type'] = 40 # else: # p['monitor']['base_information']['type'] = 42 # print('a2') # if my_row['date_create_project'] and pd.notna(row['date_create_project']): # year = int(my_row['date_create_project'][:4]) # month = int(my_row['date_create_project'][4:6]) # day = int(my_row['date_create_project'][6:8]) # jalali_date = jdatetime.date(year, month, day) # gregorian_date = jalali_date.togregorian() # created_at = datetime.datetime.combine(gregorian_date, datetime.time()) # p['monitor']['detail']['date_create_project'] = created_at # print('a3') # if my_row['first_time'] and pd.notna(row['first_time']): # p['monitor']['detail']["first_time"] = int(my_row["first_time"]) # print('a4') # if my_row['extend_time'] and pd.notna(row['extend_time']): # p['monitor']['detail']["extend_time"] = my_row["extend_time"] # print('a5') # # if my_row['date_estimate_complete'] and pd.notna(row['date_estimate_complete']): # # print('my_row[date_estimate_complete]',my_row['date_estimate_complete']) # # year_date = int(my_row['date_estimate_complete'][:4]) # # month_date = int(my_row['date_estimate_complete'][4:6]) # # day_date = int(my_row['date_estimate_complete'][6:8]) # # jalali_date_date = jdatetime.date(year_date, month_date, day_date) # # gregorian_date_date = jalali_date_date.togregorian() # # estimate = datetime.datetime.combine(gregorian_date_date, datetime.time()) # # p['monitor']['detail']['date_estimate_complete'] = estimate # # print('a6') # if my_row['first_amount'] and pd.notna(row['first_amount']): # p['monitor']['detail']["first_amount"] = my_row["first_amount"] # # price = { # # '_id': uuid.uuid4(), # # 'project_id': p['_id'], # # 'first_amount': my_row["first_amount"] # # } # # await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).insert_one(price) # print('a7') # if my_row['add_25_percent'] and pd.notna(row['add_25_percent']): # p['monitor']['detail']['add_25_percent'].append({"amount": my_row["add_25_percent"]}) # print('a8') # if my_row['adding_amount'] and pd.notna(row['adding_amount']): # p['monitor']['detail']['adding'].append({"amount":my_row["adding_amount"]}) # print('a9') # if my_row['workshop_consultant'] and pd.notna(row['workshop_consultant']): # p['monitor']['agent']['workshop_consultant'] = my_row["workshop_consultant"] # print('a0') # if my_row['constractor'] and pd.notna(row['constractor']): # p['monitor']['agent']['constractor'] = my_row["constractor"] # print('a00') # p['has_monitor'] = True # p['design']['sts'] = 2 # p['monitor']['sts'] = 2 # p['monitor']['has_design'] = True # create_code = ProjectRepository().create_code(ProjectModel(**p)) # p['monitor']['code'] = 'S'+ create_code # print('code',p['monitor']['code']) # if 'date_create_project' in p['monitor']['detail'] and "first_time" in p['monitor']['detail']: # p['date_delay'] = ProjectRepository().creat_date_delay(ProjectModel(**p)) # print('date_delay',p['date_delay']) # await use_collection(TaskEnv.CLN_PROJECT).update_one({'counter':int(my_row["code"])},{"$set":p}) return async def addProgress1(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # excel_path = os.path.join(project_root, "takmil_madarek.xlsx") # print("Excel file path:", excel_path) # df = pd.read_excel(excel_path, engine="openpyxl", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code",my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress_id = None # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = DesignProgressModel( # project_id=my_project['_id'], # type=1, # state=my_row['state'] # ) # progress.init_id().cu_at() # await use_collection("design_progress").insert_one(progress.mongo_dict()) # progress_id = progress.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time',progress_id) # if 'second_time/send/date_at' in my_row and my_row['second_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه دوم', 'second_time', progress_id) # if 'third_time/send/date_at' in my_row and my_row['third_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه سوم', 'third_time', progress_id) # if 'forth_time/send/date_at' in my_row and my_row['forth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه جهارم', 'forth_time', progress_id) # if 'fifth_time/send/date_at' in my_row and my_row['fifth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه پنجم', 'fifth_time', progress_id) # if 'sixth_time/send/date_at' in my_row and my_row['sixth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه ششم', 'sixth_time', progress_id) # if 'seventh_time/send/date_at' in my_row and my_row['seventh_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه هفتم', 'seventh_time', progress_id) # break # print("code", my_row['code']) return True # faz zero------------------------------------------------------------------------ async def addProgressPhysical(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # excel_path = os.path.join(project_root, "faz_zero.xlsx") # df = pd.read_excel(excel_path, engine="openpyxl", sheet_name="physical", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code",my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress_id = None # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = DesignProgressModel( # project_id=my_project['_id'], # type=2, # state=my_row['state'] # ) # progress.init_id().cu_at() # await use_collection("design_progress").insert_one(progress.mongo_dict()) # progress_type = DesignProgressModel( # project_id=my_project['_id'], # type=2, # state=my_row['state'], # parent_id= progress.id, # sub_type= 1, # ) # progress_type.init_id().cu_at() # await use_collection("design_progress").insert_one(progress_type.mongo_dict()) # progress_id = progress_type.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time',progress_id) # if 'second_time/send/date_at' in my_row and my_row['second_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه دوم', 'second_time', progress_id) # if 'third_time/send/date_at' in my_row and my_row['third_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه سوم', 'third_time', progress_id) # if 'forth_time/send/date_at' in my_row and my_row['forth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه جهارم', 'forth_time', progress_id) # if 'fifth_time/send/date_at' in my_row and my_row['fifth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه پنجم', 'fifth_time', progress_id) # if 'sixth_time/send/date_at' in my_row and my_row['sixth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه ششم', 'sixth_time', progress_id) # break # print("code", my_row['code']) return True async def addProgressStudy(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # excel_path = os.path.join(project_root, "faz_zero.xlsx") # df = pd.read_excel(excel_path, engine="openpyxl", sheet_name="study", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code", my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress_id = None # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = await use_collection("design_progress").find_one({'project_id':my_project['_id'],'type':2}) # if not progress: # progress = DesignProgressModel( # project_id=my_project['_id'], # type=2, # state=my_row['state'] # ) # progress.init_id().cu_at() # progress = progress.mongo_dict() # await use_collection("design_progress").insert_one(progress) # progress_type = DesignProgressModel( # project_id=my_project['_id'], # type=2, # state=my_row['state'], # parent_id=progress['_id'], # sub_type=2, # ) # progress_type.init_id().cu_at() # await use_collection("design_progress").insert_one(progress_type.mongo_dict()) # progress_id = progress_type.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time', progress_id) # if 'second_time/send/date_at' in my_row and my_row[ # 'second_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه دوم', 'second_time', progress_id) # if 'third_time/send/date_at' in my_row and my_row[ # 'third_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه سوم', 'third_time', progress_id) # break # print("code", my_row['code']) return True async def addProgressAlternativeSite(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # excel_path = os.path.join(project_root, "faz_zero.xlsx") # df = pd.read_excel(excel_path, engine="openpyxl", sheet_name="alternative_site", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code", my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress_id = None # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = await use_collection("design_progress").find_one({'project_id':my_project['_id'],'type':2}) # if not progress: # progress = DesignProgressModel( # project_id=my_project['_id'], # type=2, # state=my_row['state'] # ) # progress.init_id().cu_at() # progress = progress.mongo_dict() # await use_collection("design_progress").insert_one(progress) # progress_type = DesignProgressModel( # project_id=my_project['_id'], # type=2, # state=my_row['state'], # parent_id=progress['_id'], # sub_type=3, # ) # progress_type.init_id().cu_at() # await use_collection("design_progress").insert_one(progress_type.mongo_dict()) # progress_id = progress_type.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time', progress_id) # if 'second_time/send/date_at' in my_row and my_row[ # 'second_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه دوم', 'second_time', progress_id) # if 'third_time/send/date_at' in my_row and my_row[ # 'third_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه سوم', 'third_time', progress_id) # if 'forth_time/send/date_at' in my_row and my_row[ # 'forth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه جهارم', 'forth_time', progress_id) # break # print("code", my_row['code']) return True async def addProgressAlternativeArchitecture(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # excel_path = os.path.join(project_root, "faz_zero.xlsx") # df = pd.read_excel(excel_path, engine="openpyxl", sheet_name="alternative_architecture", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code", my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress_id = None # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = await use_collection("design_progress").find_one({'project_id':my_project['_id'],'type':2}) # if not progress: # progress = DesignProgressModel( # project_id=my_project['_id'], # type=2, # state=my_row['state'] # ) # progress.init_id().cu_at() # progress = progress.mongo_dict() # await use_collection("design_progress").insert_one(progress) # progress_type = DesignProgressModel( # project_id=my_project['_id'], # type=2, # state=my_row['state'], # parent_id=progress['_id'], # sub_type=4, # ) # progress_type.init_id().cu_at() # await use_collection("design_progress").insert_one(progress_type.mongo_dict()) # progress_id = progress_type.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time', progress_id) # if 'second_time/send/date_at' in my_row and my_row[ # 'second_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه دوم', 'second_time', progress_id) # if 'third_time/send/date_at' in my_row and my_row[ # 'third_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه سوم', 'third_time', progress_id) # if 'forth_time/send/date_at' in my_row and my_row[ # 'forth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه جهارم', 'forth_time', progress_id) # if 'fifth_time/send/date_at' in my_row and my_row[ # 'fifth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه پنجم', 'fifth_time', progress_id) # break # print("code", my_row['code']) return True async def completeProgressZero(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # excel_path = os.path.join(project_root, "faz_zero.xlsx") # df = pd.read_excel(excel_path, engine="openpyxl", sheet_name="alternative_architecture", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code", my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress = await use_collection("design_progress").find_one( # {'project_id': my_project['_id'], 'type': 2}) # if progress and 'complete_at' in my_row and my_row['complete_at'] != '': # print('jjjjjjjjjjjjjjjjjjjj',progress["_id"]) # c = UpdateDesignProgress(complete_at=my_row['complete_at']) # await DesignProgressRepository().update(progress["_id"],c) # break # print("code", my_row['code']) return True #---------------------------------------------------------------------------------------- # faz one----------------------------------------------------------------- async def addProgressFazOneArt(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # excel_path = os.path.join(project_root, "faz_one.xlsx") # df = pd.read_excel(excel_path, engine="openpyxl", sheet_name="architecture", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code",my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress_id = None # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = DesignProgressModel( # project_id=my_project['_id'], # type=3, # state=my_row['state'] # ) # progress.init_id().cu_at() # await use_collection("design_progress").insert_one(progress.mongo_dict()) # progress_type = DesignProgressModel( # project_id=my_project['_id'], # type=3, # state=my_row['state'], # parent_id= progress.id, # sub_type= 5, # ) # progress_type.init_id().cu_at() # await use_collection("design_progress").insert_one(progress_type.mongo_dict()) # progress_id = progress_type.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time',progress_id) # if 'second_time/send/date_at' in my_row and my_row['second_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه دوم', 'second_time', progress_id) # if 'third_time/send/date_at' in my_row and my_row['third_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه سوم', 'third_time', progress_id) # if 'forth_time/send/date_at' in my_row and my_row['forth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه جهارم', 'forth_time', progress_id) # if 'fifth_time/send/date_at' in my_row and my_row['fifth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه پنجم', 'fifth_time', progress_id) # if 'sixth_time/send/date_at' in my_row and my_row['sixth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه ششم', 'sixth_time', progress_id) # if 'seventh_time/send/date_at' in my_row and my_row['seventh_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه هفتم', 'seventh_time', progress_id) # if 'eighth_time/send/date_at' in my_row and my_row['eighth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه هشتم', 'eighth_time', progress_id) # break # print("code", my_row['code']) return True async def addProgressFazOneUniversity(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # excel_path = os.path.join(project_root, "faz_one.xlsx") # df = pd.read_excel(excel_path, engine="openpyxl", sheet_name="university", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code", my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress_id = None # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = await use_collection("design_progress").find_one({'project_id':my_project['_id'],'type':3}) # if not progress: # progress = DesignProgressModel( # project_id=my_project['_id'], # type=3, # state=my_row['state'] # ) # progress.init_id().cu_at() # progress = progress.mongo_dict() # await use_collection("design_progress").insert_one(progress) # progress_type = DesignProgressModel( # project_id=my_project['_id'], # type=3, # state=my_row['state'], # parent_id=progress['_id'], # sub_type=6, # ) # progress_type.init_id().cu_at() # await use_collection("design_progress").insert_one(progress_type.mongo_dict()) # progress_id = progress_type.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time', progress_id) # if 'second_time/send/date_at' in my_row and my_row[ # 'second_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه دوم', 'second_time', progress_id) # if 'third_time/send/date_at' in my_row and my_row[ # 'third_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه سوم', 'third_time', progress_id) # if 'forth_time/send/date_at' in my_row and my_row[ # 'forth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه جهارم', 'forth_time', progress_id) # if 'fifth_time/send/date_at' in my_row and my_row[ # 'fifth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه پنجم', 'fifth_time', progress_id) # break # print("code", my_row['code']) return True async def addProgressFazOneAll(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../..")) # excel_path = os.path.join(project_root, "faz_one.xlsx") # df = pd.read_excel(excel_path, engine="openpyxl", sheet_name="university", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code", my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress_id = None # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = await use_collection("design_progress").find_one({'project_id':my_project['_id'],'type':3}) # if not progress: # progress = DesignProgressModel( # project_id=my_project['_id'], # type=3, # state=my_row['state'] # ) # progress.init_id().cu_at() # progress = progress.mongo_dict() # await use_collection("design_progress").insert_one(progress) # progress_type = DesignProgressModel( # project_id=my_project['_id'], # type=3, # state=my_row['state'], # parent_id=progress['_id'], # sub_type=7, # ) # progress_type.init_id().cu_at() # await use_collection("design_progress").insert_one(progress_type.mongo_dict()) # progress_id = progress_type.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time', progress_id) # if 'second_time/send/date_at' in my_row and my_row[ # 'second_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه دوم', 'second_time', progress_id) # if 'third_time/send/date_at' in my_row and my_row[ # 'third_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه سوم', 'third_time', progress_id) # # break # print("code", my_row['code']) return True # طراحی دوربین و شبکه async def addProgressNetworkDesign(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../../excel")) # excel_path = os.path.join(project_root, "network.xlsx") # df = pd.read_excel(excel_path, engine="openpyxl", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code", my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = DesignProgressModel( # project_id=my_project['_id'], # type=4, # state=my_row['state'] # ) # progress.init_id().cu_at() # await use_collection("design_progress").insert_one(progress.mongo_dict()) # progress_id = progress.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time', progress_id) # break # print("code", my_row['code']) return True async def addProgressFazTwo(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../../excel")) # excel_path = os.path.join(project_root, "faz_two.xlsx") # print("Excel file path:", excel_path) # df = pd.read_excel(excel_path, engine="openpyxl", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code",my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress_id = None # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = DesignProgressModel( # project_id=my_project['_id'], # type=5, # state=my_row['state'] # ) # progress.init_id().cu_at() # await use_collection("design_progress").insert_one(progress.mongo_dict()) # progress_id = progress.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time',progress_id) # if 'second_time/send/date_at' in my_row and my_row['second_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه دوم', 'second_time', progress_id) # if 'third_time/send/date_at' in my_row and my_row['third_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه سوم', 'third_time', progress_id) # if 'forth_time/send/date_at' in my_row and my_row['forth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه جهارم', 'forth_time', progress_id) # if 'fifth_time/send/date_at' in my_row and my_row['fifth_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه پنجم', 'fifth_time', progress_id) # break # print("code", my_row['code']) return True async def addProgressMetri(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../../excel")) # excel_path = os.path.join(project_root, "metri.xlsx") # print("Excel file path:", excel_path) # df = pd.read_excel(excel_path, engine="openpyxl", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code",my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # progress_id = None # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = DesignProgressModel( # project_id=my_project['_id'], # type=6, # state=my_row['state'] # ) # progress.init_id().cu_at() # await use_collection("design_progress").insert_one(progress.mongo_dict()) # progress_id = progress.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time',progress_id) # if 'second_time/send/date_at' in my_row and my_row['second_time/send/date_at'] != "" and progress_id: # await self.createRecord(my_row, 'مرتبه دوم', 'second_time', progress_id) # break # print("code", my_row['code']) return True async def addProgressTender(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../../excel")) # excel_path = os.path.join(project_root, "tender.xlsx") # print("Excel file path:", excel_path) # df = pd.read_excel(excel_path, engine="openpyxl", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code",my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # my_project = p # if my_row['first_time/send/date_at'] and my_row['first_time/send/date_at'] != "": # progress = DesignProgressModel( # project_id=my_project['_id'], # type=7, # state=my_row['state'] # ) # progress.init_id().cu_at() # await use_collection("design_progress").insert_one(progress.mongo_dict()) # progress_id = progress.id # await self.createRecord(my_row, 'مرتبه اول', 'first_time',progress_id) # break # print("code", my_row['code']) return True async def changeStatus(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../../excel")) # excel_path = os.path.join(project_root, "status.xlsx") # print("Excel file path:", excel_path) # df = pd.read_excel(excel_path, engine="openpyxl", dtype=str) # df = df.fillna("") # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code",my_row['code']) # for p in projects: # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # print('projects', p) # sts = 2 # if my_row["status"] == 'متوقف': # sts = 3 # elif my_row["status"] == 'خاتمه یافته': # sts=4 # print('sts',sts) # await use_collection('project').update_one({"_id": p["_id"]}, {"$set": { # 'design.sts': sts, 'design.reason_stop': my_row["reason_stop"],'design.description':my_row['description'] # }}) # sts_class = ProjectSts( # project_id=p["_id"], # type='design', # sts=sts, # description=my_row["reason_stop"] # ).init_id().cu_at() # await use_collection(TaskEnv.CLN_PROJECT_STS).insert_one(sts_class.mongo_dict()) # break # print("code", my_row['code']) return True def createDate(self, date): if '/' in date: date = date.replace('/', '') year = int(date[:4]) month = int(date[4:6]) if month == 0: month = 1 print(month,'month') day = int(date[6:8]) if day ==0: day=1 print(day,'day') jalali_date = jdatetime.date(year, month, day) gregorian_date = jalali_date.togregorian() created_at = datetime.datetime.combine(gregorian_date, datetime.time.min) return created_at def findMonth(self,month): match month: case '01': return 'فروردین' case '02': return 'اردیبهشت' case '03': return 'خرداد' case '04': return 'تیر' case '05': return 'مرداد' case '06': return 'شهریور' case '07': return 'مهر' case '08': return 'آبان' case '09': return 'آذر' case '10': return 'دی' case '11': return 'بهمن' case '12': return 'اسفند' async def createRecord(self, my_row, _time_f, _time_e, progress_id): send = SubRecord( date_at=self.createDate(my_row[f'{_time_e}/send/date_at']), number=my_row[f'{_time_e}/send/number'], description=None ) record = RecordModel( design_progress_id=progress_id, title=_time_f, send=send, ) if f'{_time_e}/receive/date_at' in my_row and my_row[f'{_time_e}/receive/date_at'] != "": print(my_row[f'{_time_e}/receive/date_at']) receive = SubRecord( date_at=self.createDate(my_row[f'{_time_e}/receive/date_at']), number=my_row[f'{_time_e}/receive/number'], description=None ) record.receive = receive record.init_id().cu_at() await use_collection("design_progress_record").insert_one(record.mongo_dict()) return record # async def progressMonitor(self): # base_dir = os.path.dirname(os.path.abspath(__file__)) # project_root = os.path.abspath(os.path.join(base_dir, "../../excel")) # excel_path = os.path.join(project_root, "progress_monitor0.xlsx") # print("Excel file path:", excel_path) # df = pd.read_excel(excel_path, engine="openpyxl", dtype={'code': str,'date_create_project':str,'date_estimate_complete':str}) # df = df.fillna("") # value_vars = [col for col in df.columns if "/" in col] # id_vars = [col for col in df.columns if col not in value_vars] # # # تبدیل Wide به Long # df_long = df.melt( # id_vars=id_vars, # value_vars=value_vars, # var_name="metric_date", # value_name="value" # ) # # # جدا کردن تاریخ و متریک از اسم ستون # df_long["date"] = df_long["metric_date"].str.split("/").str[-1] # df_long["metric"] = df_long["metric_date"].str.rsplit("/", n=1).str[0] # projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) # for index, row in df_long.iterrows(): # my_row = row.to_dict() # print(f"سطر {index}: {row.to_dict()}") # print("code",my_row['code']) # # for p in projects: # # if str(my_row["code"]) in str(p['design']["code"]) and p['counter'] == int(str(my_row["code"])): # # print('projects', p) # # return # # # sts = 2 # # if my_row["status"] == 'متوقف': # # sts = 3 # # elif my_row["status"] == 'خاتمه یافته': # # sts=4 # # print('sts',sts) # # await use_collection('project').update_one({"_id": p["_id"]}, {"$set": { # # 'design.sts': sts, 'design.reason_stop': my_row["reason_stop"],'design.description':my_row['description'] # # }}) # # sts_class = ProjectSts( # # project_id=p["_id"], # # type='design', # # sts=sts, # # description=my_row["reason_stop"] # # ).init_id().cu_at() # # await use_collection(TaskEnv.CLN_PROJECT_STS).insert_one(sts_class.mongo_dict()) # # break # # print("code", my_row['code']) # return True # # { # # "project_id": "c1dbe5bb-3175-42dc-a091-fd74e6d10f9e", # # "program": "12", # # "real": "56", # # "medium_progress": 56, # # "deviation": 44, # # "date_at": "2025-03-21T00:00:00", # # "month": "فروردین", # # "year": "1404" # # } # # # { # # "project_id": "c1dbe5bb-3175-42dc-a091-fd74e6d10f9e", # # "program": "10", # # "real": "10", # # "medium_progress": 33, # # "deviation": 0, # # "date_at": "2025-04-21T00:00:00", # # "last_month": -46, # # "month": "اردیبهشت", # # "year": "1404" # # } # # { # # "project_id": "c1dbe5bb-3175-42dc-a091-fd74e6d10f9e", # # "program": "20", # # "real": "15", # # "medium_progress": 27, # # "deviation": -5, # # "date_at": "2025-05-22T00:00:00", # # "last_month": 5, # # "month": "خرداد", # # "year": "1404" # # } # # { # # "project_id": "c1dbe5bb-3175-42dc-a091-fd74e6d10f9e", # # "program": "20", # # "real": "15", # # "medium_progress": 24, # # "deviation": -5, # # "date_at": "2025-06-22T00:00:00", # # "last_month": 0, # # "last_three_month": -41, # # "month": "تیر", # # "year": "1404" # # } async def progressMonitorPhysical(self): base_dir = os.path.dirname(os.path.abspath(__file__)) project_root = os.path.abspath(os.path.join(base_dir, "../../excel")) excel_path = os.path.join(project_root, "progress_monitor.xlsx") print("Excel file path:", excel_path) # خواندن با سه سطر عنوان df = pd.read_excel( excel_path, header=[0, 1], # سه ردیف اول عنوان engine="openpyxl", sheet_name="physical", dtype={'code': str} ).fillna("") # 1) پیدا کردن کلید دقیق ستون‌های شناسه (سطح اول = code/state/title) id_first = {'code', 'state', 'title'} id_map = {} for col in df.columns: # col یک تاپل مثل ('code','Unnamed: 2_level_1') یا ('1401/01/01','program') if col[0] in id_first and col[0] not in id_map: id_map[col[0]] = col # مثلاً id_map['code'] = ('code','Unnamed: 2_level_1') # اگر یکی از شناسه‌ها پیدا نشد، بهتر است خطا/هشدار بدهیم missing_ids = id_first - set(id_map.keys()) if missing_ids: raise ValueError(f"شناسه‌های مورد انتظار در هدر پیدا نشدند: {missing_ids}. هدر اکسل را بررسی کنید.") # 2) تبدیل code به رشته (جلوی تبدیل عددی ناخواسته را می‌گیرد) df[id_map['code']] = df[id_map['code']].astype(str) # 3) ستون‌های متریک: هر ستونی که جزو شناسه‌ها نباشد metric_columns = [col for col in df.columns if col[0] not in id_first] # 4) گروه‌بندی خروجی بر اساس (code, date) با setdefault (بدون KeyError) grouped = {} # {(code,date): {...}} for _, row in df.iterrows(): code_val = str(row[id_map['code']]).strip() state_val = str(row[id_map['state']]).strip() title_val = str(row[id_map['title']]).strip() for (date, metric) in metric_columns: # مقدار سلول value = row[(date, metric)] # رد کردن مقدارهای خالی یا NaN if value == "" or (isinstance(value, float) and pd.isna(value)): continue key = (code_val, str(date)) # تاریخ را هم رشته می‌کنیم تا یکنواخت باشد # اگر قبلاً نساخته‌ایم، بساز obj = grouped.setdefault(key, { "code": code_val, "state": state_val, "title": title_val, "date": str(date), }) print(obj['code']) # گذاشتن متریک (مثلاً program یا real) if value == "-%" or value == "-": value = 0 value = float(value) * 100 value = int(value) obj[metric] = value # 5) خروجی نهایی به صورت لیست results = list(grouped.values()) projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) for res in results: for p in projects: if str(res["code"]) in str(p['design']["code"]) and p['counter'] == int(str(res["code"])): date_at=self.createDate(res['date']) print('date_at',date_at) date = res['date'].replace('/', '') year = str(date[:4]) month = str(date[4:6]) _inp = LastRecord(project_id=p['_id'],date_at=date_at) print('_inp',_inp) last = await PhysicalProgressRepository().last_record(_inp) if 'program' not in res: res['program'] = 0 if 'real' not in res: res['real'] = 0 mydata = PhysicalProgressModel( project_id=p['_id'], program=res['program'], real=res['real'], date_at=date_at, deviation=res['program']-res['real'], year=year, month=self.findMonth(month), ) if 'avg' in last['avg']: mydata.medium_progress = last['avg']['avg'] if 'real' in last['month']: mydata.last_month = res['real'] - last['month']['real'] if 'real' in last['three_month']: mydata.last_three_month = res['real'] - last['three_month']['real'] mydata.init_id().cu_at() _get = await use_collection('monitor_physical').insert_one(mydata.mongo_dict()) # await ProjectRepository().set_sts_two_for_monitor(_inp.project_id) break return results async def progressMonitorRial(self): base_dir = os.path.dirname(os.path.abspath(__file__)) project_root = os.path.abspath(os.path.join(base_dir, "../../excel")) excel_path = os.path.join(project_root, "progress_monitor.xlsx") # خواندن با سه سطر عنوان df = pd.read_excel( excel_path, header=[0, 1], # سه ردیف اول عنوان engine="openpyxl", sheet_name="rial", dtype={'code': str} ).fillna("") # 1) پیدا کردن کلید دقیق ستون‌های شناسه (سطح اول = code/state/title) id_first = {'code', 'state', 'title'} id_map = {} for col in df.columns: # col یک تاپل مثل ('code','Unnamed: 2_level_1') یا ('1401/01/01','program') if col[0] in id_first and col[0] not in id_map: id_map[col[0]] = col # مثلاً id_map['code'] = ('code','Unnamed: 2_level_1') # اگر یکی از شناسه‌ها پیدا نشد، بهتر است خطا/هشدار بدهیم missing_ids = id_first - set(id_map.keys()) if missing_ids: raise ValueError(f"شناسه‌های مورد انتظار در هدر پیدا نشدند: {missing_ids}. هدر اکسل را بررسی کنید.") # 2) تبدیل code به رشته (جلوی تبدیل عددی ناخواسته را می‌گیرد) df[id_map['code']] = df[id_map['code']].astype(str) # 3) ستون‌های متریک: هر ستونی که جزو شناسه‌ها نباشد metric_columns = [col for col in df.columns if col[0] not in id_first] # 4) گروه‌بندی خروجی بر اساس (code, date) با setdefault (بدون KeyError) grouped = {} # {(code,date): {...}} for _, row in df.iterrows(): print('row',row) code_val = str(row[id_map['code']]).strip() state_val = str(row[id_map['state']]).strip() title_val = str(row[id_map['title']]).strip() for (date, metric) in metric_columns: # مقدار سلول value = row[(date, metric)] # رد کردن مقدارهای خالی یا NaN if value == "" or (isinstance(value, float) and pd.isna(value)): continue key = (code_val, str(date)) # تاریخ را هم رشته می‌کنیم تا یکنواخت باشد # اگر قبلاً نساخته‌ایم، بساز obj = grouped.setdefault(key, { "code": code_val, "state": state_val, "title": title_val, "date": str(date), }) # گذاشتن متریک (مثلاً program یا real) if value == "-%" or value == "-": value = 0 value = float(value) * 100 value = int(value) obj[metric] = value print('obj',obj) # 5) خروجی نهایی به صورت لیست results = list(grouped.values()) projects = await use_collection(TaskEnv.CLN_PROJECT).find({}).to_list(None) for res in results: print('res',res) print('code',res["code"]) for p in projects: if str(res["code"]) in str(p['design']["code"]) and p['counter'] == int(str(res["code"])): date_at=self.createDate(res['date']) print('date_at',date_at) date = res['date'].replace('/', '') year = str(date[:4]) month = str(date[4:6]) _inp = LastRecord(project_id=p['_id'],date_at=date_at) print('_inp',_inp) last = await RialProgressRepository().last_record(_inp) # print('last',last) if 'program' not in res: res['program'] = 0 if 'real' not in res: res['real'] = 0 mydata = RialProgressModel( project_id=p['_id'], program=res['program'], real=res['real'], date_at=date_at, ) if 'real' in last: mydata.last_month = res['real'] - last['real'] mydata = mydata.init_id().cu_at().mongo_dict() mydata['month'] = month mydata['year'] = year mydata['date'] = res['date'] print('mydata',mydata) _get = await use_collection('monitor_rial').insert_one(mydata) # await ProjectRepository().set_sts_two_for_monitor(_inp.project_id) break return results async def last_record(self, _inp: LastRecord): send = {'month': {}, 'three_month': {}, 'before':{}} if _inp.id: find_record = await use_collection('monitor_physical_test').find_one({"_id":_inp.id}) if _inp.id: before = await use_collection('monitor_physical_test').find({ 'project_id': _inp.project_id, 'created_at': {'$lt': find_record['created_at']}}).sort("$natural", -1).to_list(None) else: before = await use_collection('monitor_physical_test').find({ 'project_id': _inp.project_id, }).sort("$natural", -1).to_list(None) if before: send['before'] = before[0] last = await PhysicalProgressRepository().search_last('monitor_physical_test', _inp, 35, 25) if last: send['month'] = last three_month = await PhysicalProgressRepository().search_last('monitor_physical_test', _inp, 95, 85) if three_month: send['three_month'] = three_month _avg = await use_collection('monitor_physical_test').aggregate([ {'$match':{'project_id':_inp.project_id, "date_at": {"$lt": _inp.date_at}}}, {'$group': {'_id': None, 'avg': {'$avg': "$real"}, 'sum':{'$sum': "$real"}, 'count':{'$sum': 1}}} ]).to_list(None) if _avg: send['avg'] = _avg[0] else: send['avg'] = {} return send