from datetime import datetime

import axipy
from axipy import provider_manager, Feature, data_manager


def createCopyTable(table_source,path_dest,dectimate):
    schema=table_source.schema


    # table_dest=provider_manager.create_open(path_out,definition)
    table_dest = provider_manager.tab.create_open(path_dest,  schema)
    return table_dest
class CopyTable:
    def __init__(self,filter_field,cache_size=100):
        self.__filter_field=filter_field
        self.__cach_size=cache_size
    def createDestTable(self,table_source,path_dest_tab):
        self.__cach_feature=[]
        schema = table_source.schema

        # table_dest=provider_manager.create_open(path_out,definition)
        self.__table_dest = provider_manager.tab.create_open(path_dest_tab, schema)
        if self.__table_dest is not None:
            return True
        return False
    def __copyFeature(self,ft_source):
        new_atts= {}
        for item in ft_source.items():
            if self.__filter_field == item[0]:
                continue
            new_atts[item[0]]=item[1]
        new_ft=Feature(new_atts,ft_source.geometry,ft_source.style)
        return new_ft

    def insetFeature(self,ft):
        if ft is None:
            return
        new_ft=self.__copyFeature(ft)
        self.__cach_feature.append(new_ft)
        if len(self.__cach_feature)>self.__cach_size:
            self.__table_dest.insert(self.__cach_feature)
            self.__cach_feature.clear()
        return
    def lastInsert(self):
        if len(self.__cach_feature)>0:
            self.__table_dest.insert(self.__cach_feature)
            self.__cach_feature.clear()
time_start=datetime.now()
name_tab=r"E:\Support\AxiomaTesting\DubleRecord\Вода_итоговая_2.tab"
path_dest_table=r"E:\Support\AxiomaTesting\DubleRecord\Вода_итоговая_2_del.tab"

table = provider_manager.openfile(name_tab)
copyDelTab=CopyTable("id___num")
copyDelTab.createDestTable(table,path_dest_table)
sql="Select rowid as id___num,* from "+table.name
query = data_manager.query(sql)
list_name_by_group=['obj']
name_by_group=','.join(list_name_by_group)
sql_2="Select *,Count(*) as cnt From "+query.name+" group by "+name_by_group+" order by id___num ASC "
query_2=data_manager.query(sql_2)
#sql_order="Select * from "+query_2p.name+" order by id___num DESC"
#sql_order="Select * from "+query_2p.name+" order by id___num "
#query_2=data_manager.query(sql_order)
#sql_3="Select * from "+query_2.name+" where cnt>1 order by id___num"
#query_3=data_manager.query(sql_3)
()
for i,item in enumerate(query_2.items()):
    print(i)
    cur_id=item['id___num']
    print(str(i)+" id="+str(cur_id))
    sql_temp_query="Select * from "+query.name+" where id___num="+str(cur_id)
    #temp_query=data_manager.query(sql_temp_query)
    #sql_temp_query="("+sql_temp_query+")"
    #cur_sql="select "+query_2.name+".* from "+query_2.name+","+temp_query.name +" where "+query_2.name+".id___num<>"+str(cur_id)+" and "+query_2.name+".obj="+temp_query.name+".obj"
    '''
    cur_sql = "select " + query.name + ".* from " + query.name + "," + sql_temp_query+" as Query_z " + " where " + query.name + ".id___num<>" + str(
        cur_id) + " and " + query.name + ".obj=" + "Query_z.obj"
    ()
    '''
    #sql_delete="delete From ("+cur_sql+")"
    query_cur=data_manager.query_hidden(sql_temp_query,axipy.da.TypeSqlDialect.axioma)
    if query_cur.count() > 0:
        features=list(query_cur.items())

        copyDelTab.insetFeature(features[0])
    query_cur.close()
    #break
copyDelTab.lastInsert()
time_end=datetime.now()
delta_time=time_end-time_start
#delta_time_str=delta_time.strftime("%H:%M:%S")
print("Time elapsed:"+str(delta_time))
print("End End")


