import json import os from config_base import * from html import escape import json import datetime from config_base import * from dotenv import load_dotenv from general_functions import * from text_normalizer import TextNormalizer from elastic_helper import ElasticHelper from qanon_functions import * # ضرورتا # این برنامه ضرورت دارد اجرا شود # بسیاری از اجزاء قانون دارای وضعیت مثل منسوخه و جایگزین شده بودند که اطلاعات اثرگذ اری شون پر نشده بود # مشخص شد اینها با کلیت یک قانون اثرگذ ار ثبت شده نه بخش مشخص از قانون # لذا این ناقصی با این برنامه تکمیل شد def check_duplicate_ref_state(list): dict_index={} res = [] for item in list : if item["id"] in dict_index : index = dict_index[item["id"]] if res[index]["qanon_id"] == item["qanon_id"] and res[index]["section_id"] == item["section_id"]: continue # duplicate dict_index[item["id"]] = len(res) res.append(item) return res def import_law_section_repair_ref_state(cursor, elastic, index_name_o, file_log): query = ''' SELECT l1.[ID] as sectionId ,l1.F_CMBASETABLEID_SECTIONSTATUS ,l1.[F_LWLAWCHANGEID] as id ,l2.F_LWLAWID_EFFECTED ,l2.F_LWLAWID_EFFECTIVE as effect_qanon_id ,l3.CAPTION as effect_qanon ,l3.APPROVEDATE as effect_Date ,ls1.CAPTION as effect_section_type -- ,l4.ID as id_1 FROM [Qavanin].[dbo].[lwSection] l1 left join [Qavanin].[dbo].[lwLawSectionStatus] ls1 on ls1.ID=l1.[F_CMBASETABLEID_SECTIONSTATUS] left join [Qavanin].[dbo].lwLawChange l2 on l1.F_LWLAWCHANGEID=l2.id left join [Qavanin].[dbo].lwLaw l3 on l2.F_LWLAWID_EFFECTIVE=l3.id left join [Qavanin].[dbo].lwSectionChange l4 on l1.id=l4.F_LWSECTIONID_EFFECTED where l3.ISLAW = 1 and l4.ID is null --یعنی قبلا از طریق دیگر این اطلاعات برایش پر نشده باشد and l2.F_LWLAWID_EFFECTED is not null -- یعنی اطلاعات جدید اثرگذاری وجود داشته باشد ''' cursor.execute(query) text_normal = TextNormalizer() total_count = 0 bulk_data = [] file = open(file_log, 'a+', encoding='utf8') t1= datetime.datetime.now() print(t1) i = 0 while 1: # print(i) i=i+1 row = cursor.fetchone() if not row: break # if i > 3: # break # print(row.sectionId) eid='qs'+ str(row.sectionId) # lawId='qq'+ str(row.lawId) effect_type = '' # effect_type = text_normal.normalize_qanon(row.effect_section_type) if row.effect_section_type == 'معتبر' : effect_type = '' elif row.effect_section_type == 'ساختار الحاقي' or row.effect_section_type == 'ساختار الحاقي مكرر' : effect_type = 'الحاقی' elif row.effect_section_type == 'اصلاحي' or row.effect_section_type == 'نامه اصلاحي': effect_type = 'اصلاحی' elif row.effect_section_type == 'منسوخه' : effect_type = 'نسخی' elif row.effect_section_type == 'احياء' or row.effect_section_type == 'تنفيذ' or row.effect_section_type == 'تمديد': effect_type = 'تمدیدی' elif row.effect_section_type == 'تفسير' : effect_type = 'تفسیری' elif row.effect_section_type == 'ساختار جايگزين' or row.effect_section_type == 'جايگزين شده': effect_type = 'جایگزین' elif row.effect_section_type == 'آزمايشي' : effect_type = '' elif row.effect_section_type == 'موقت' : effect_type = '' elif row.effect_section_type == 'موقوف الاجراء' : effect_type = '' elif row.effect_section_type == 'بااجرامنتفي مي شود' : effect_type = '' # if row.ISIMPLICIT : # effect_type += ' ضمنی' # if i == 1 : # اولین بار # prev_effect_type = effect_type prev_ref_state = [] item = {} try: item = elastic.get_doctument(index_name_o, eid) if 'ref_state' in item['_source'] : if len(item['_source']['ref_state']) > 0: prev_ref_state = item['_source']['ref_state'] prev_ref_state1 = check_duplicate_ref_state(prev_ref_state) if len(prev_ref_state) != len(prev_ref_state1): print(' ==========> check_duplicate_ref_state OK : ', eid) file.write('==========> check_duplicate_ref_state OK : '+ eid + '\n') prev_ref_state = prev_ref_state1 # continue except Exception as e: # print(type(e).__name__) continue ref_state = {} if row.effect_qanon_id : result = [item for item in prev_ref_state if item['id'] == str(row.id)] if result : # این مورد قبلا در الاستیک اضافه شده است continue efect_qanon_id= 'qq'+ str(row.effect_qanon_id) ref_state = { "id" : str(row.id), "qanon_id": efect_qanon_id, "qanon_title" : text_normal.normalize_qanon(row.effect_qanon), "qanon_date": row.effect_Date, "section_id": '', "section_title": "", "ref_key": "qavanin", "effect_type" : effect_type, "new_text" : "", "prev_text" : "", "description" : "" } prev_ref_state.insert(0 , ref_state) data={ '_id' : eid, '_source' : { 'ref_state' : prev_ref_state } } bulk_data.append(data) # print("-"*10, " bulk_data.append ", eid) prev_ref_state = [] if len(bulk_data) >= 100: total_count += len(bulk_data) print('=' * 10, ' law_section_update_ref_state , total='+str(total_count) , str(i)+ ', id=' + eid) elastic.updateBulkList(bulk_data, index_name_o) bulk_data = [] if len(bulk_data) > 0: total_count += len(bulk_data) print('=' * 10, ' law_section_update_ref_state , total='+str(total_count) , str(i)+ ', id=' + eid) elastic.updateBulkList(bulk_data, index_name_o) file.close() print('end success import_law_section_update_ref_state') t2= datetime.datetime.now() print(t2-t1) cursor, cnxn = create_cursor() load_dotenv(dotenv_path='config.env') host=os.getenv('HOST_160') password=os.getenv('HOST_160_PASS') # host=os.getenv('HOST_112') # password=os.getenv('HOST_112_PASS') # host=os.getenv('HOST_113') # password=os.getenv('HOST_113_PASS') # print(password) path_base_map = "G:/Project2/gateway2_main/repo/_other/" elastic = ElasticHelper(host, password, "elastic", path_base_map) import_law_section_repair_ref_state(cursor, elastic, "mj_qa_section", "./output/log_s12.txt") cursor.close() cnxn.close()