from model_data_api.model.base_model import BaseManager
from model_data_api.model.tab_model import TabUserTest,TabDataSetDatail

class UserTestDataManager(BaseManager):

    def find_single_model(self,path):
        # query= self.session.query(TabUserTest.data_set_datail_id,
        #                           TabUserTest.model_name
        #                           ).join(TabDataSetDatail,
        #                            TabUserTest.data_set_datail_id==TabDataSetDatail.id
        #                            ).filter(TabDataSetDatail.data_path==path).limit(1)
        query = self.session.query(TabDataSetDatail.id,
                                   TabDataSetDatail.model_name
                                   ).filter(TabDataSetDatail.data_path==path).limit(1)

        result = {}
        for i ,res in enumerate(query):
            result = dict(zip(res.keys(),list(res)))
        return result

    # def find_single_features(self,model_name):


    def find_single(self,batch_uuid):
        query = self.session.query(TabUserTest.batch_uuid,TabUserTest.data_detail).filter_by(batch_uuid = batch_uuid)
        result = {}
        for i ,res in enumerate(query):
            result = dict(zip(res.keys(),list(res)))
            result['data_detail'] = eval(result['data_detail'])
        return result

    def find_single_data_set_datail_id(self,model_name,data_set_datail_id):
        query = self.session.query(TabUserTest.batch_uuid,TabUserTest.data_detail).filter_by(model_name=model_name,data_set_datail_id = data_set_datail_id)
        result = []
        for i ,res in enumerate(query):
            _r = dict(zip(res.keys(),list(res)))
            _r['data_detail'] = eval(_r['data_detail'])
            result.append(_r)
        return result

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

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

    def create_obj_all(self,set_datail_id,model_name,batch_uuid,raw_data):#(self,model_id,model_name,user_count,feature_name,deta_path):#,model_id,feature_name,set_name,set_data,set_number):

        p = []
        for id ,data in zip(batch_uuid,raw_data):
            p.append(TabUserTest(
                    data_set_datail_id = int(set_datail_id),
                    model_name =model_name,
                    batch_uuid=id,
                    data_detail = str(data)
                                ))

        self.session.add_all(p)
        self.session.commit()

