from model_data_api.model.base_model import BaseManager
from model_data_api.model.tab_model import TabDataSet,TabDataSetDatail,TabFeatureData,TabModelsData

class DataSetDetailManager(BaseManager):

    def find_single_FeatureData(self, model_name):
        query = self.session.query(TabDataSet.model_name,TabModelsData.id.label('model_id'),TabDataSet.user_count,
                                   TabFeatureData.id,TabFeatureData.feature_name,
                                   TabFeatureData.feature_type,TabFeatureData.feature_value,
                                   TabFeatureData.feature_gdp,
                                   TabFeatureData.data_type,
                                   TabFeatureData.data_round
                                   ).join(TabFeatureData,
                                          TabDataSet.feature_name==TabFeatureData.feature_name
                                   ).join(TabModelsData,TabModelsData.name ==TabDataSet.model_name
                                          ).filter(TabDataSet.model_name == model_name,TabFeatureData.is_active==1)
        #query = self.session.execute(query).fetchall()
        result = {}
        #print('query==>',query)
        temp=['model_name', 'model_id', 'user_count']
        temp_3=['id', 'feature_name', 'feature_type', 'feature_value', 'feature_gdp', 'data_type', 'data_round']
        for i ,res in enumerate(query):
            #print('datasetDetail==',i,'---',res,type(res))
            if not result:
                # print('keys==',res,res.keys())
                # print('keys==',res,list(res[0:3]))
                result=dict(result,**dict(zip(temp,list(res[0:3]))))
                result['data'] = []
            result['data'].append(dict(zip(temp_3,list(res[3:]))))

        # result = {}
        # data = {}
        # _len = len(query.all())-1
        # for i,res in enumerate(query):
        #     if not result:
        #         result=dict(result,**dict(zip(res.keys()[0:2],list(res[0:2]))))
        #         result['data'] = []
        #     if not data or res[2] != data['feature_name']:
        #         try:
        #             if res[2] != data['feature_name']:
        #                 result['data'].append(data)
        #                 data = {}
        #         except:
        #             pass
        #         data = dict(data,**{res.keys()[2]:res[2],'detail':[]})
        #     data['detail'].append(dict(zip(res.keys()[3:],list(res[3:]))))
        #     if i >= _len:
        #         result['data'].append(data)
        return result

    def find_single(self, **kwargs):
        return self.get_json(self.session.query(TabDataSetDatail).filter_by(**kwargs))

    def find_all(self):
        return self.get_json(self.session.query(TabDataSetDatail).all())


    def find_condition(self, **kwargs):
        return self.session.query(TabDataSetDatail).filter(**kwargs)

    def create_obj_single(self,**kwargs):#(self,model_id,model_name,user_count,feature_name,deta_path):#,model_id,feature_name,set_name,set_data,set_number):
        self.create_obj(TabDataSetDatail,**kwargs)

    def create_obj_all(self,cls,**kw):
        print()
