import json, datetime
from common.base import Config, my_log, MyGuid
from mongoengine import *
from common.base import *

class writer_meta_variable_tables:
    def __init__(self, libname="readInfo"):
        self.libname = libname
        self.res = Pymongoengine(self.libname)

    def find(self, PlatID, ProjID, dataSetId):
        self.res.connect_db()
        pipeline = []
        ret = meta_variable.objects(platId=PlatID, projId=ProjID, dataSetId=dataSetId).aggregate([])
        return ret

    def delete(self, PlatID, ProjID, dataSetId):
        self.res.connect_db()
        meta_variable.objects.filter(platId=PlatID, projId=ProjID, dataSetId=dataSetId).delete()

    def insert(self, data):
        self.res.connect_db()
        meta_insert = meta_variable(**data)
        meta_insert.save()
        return True

    def close(self):
        self.res.connect().close()

class meta_variable(DynamicDocument):
    platId=StringField(required=True,max_length=100)
    projId=StringField(required=True,max_length=100)
    dataSetId=StringField(required=True,max_length=100)
    version = IntField(required=True)


class writer_data_case_tables:
    def __init__(self, libname="readInfo"):
        self.libname = libname
        self.res = Pymongoengine(self.libname)

    def find(self, PlatID, ProjID, dataSetId):
        self.res.connect_db()
        pipeline = []
        dataSetIdSave = dataSetId
        class data_case_set(DynamicDocument):
            meta = {'collection': 'data_case_' + dataSetIdSave}
            dataSetId = StringField(required=True, max_length=100)
            version = IntField(required=True)
        ret = data_case_set.objects(platId=PlatID, projId=ProjID, dataSetId=dataSetId).aggregate([])
        return ret

    def delete(self, PlatID, ProjID, dataSetId):
        self.res.connect_db()
        dataSetIdSave = dataSetId

        class data_case_set(DynamicDocument):
            meta = {'collection': 'data_case_' + dataSetIdSave}
            dataSetId = StringField(required=True, max_length=100)
            version = IntField(required=True)
        data_case_set.objects.filter(platId=PlatID, projId=ProjID, dataSetId=dataSetId).delete()

    def insert(self, data, dataSetId):
        self.res.connect_db()
        dataSetIdSave = dataSetId

        class data_case_set(DynamicDocument):
            meta = {'collection': 'data_case_' + dataSetIdSave}
            dataSetId = StringField(required=True, max_length=100)
            version = IntField(required=True)
        meta_insert = data_case_set(**data)
        meta_insert.save()
        return True

    def close(self):
        self.res.connect().close()

class data_case(DynamicDocument):
    platId = StringField(required=True, max_length=100)
    projId = StringField(required=True, max_length=100)
    dataSetId = StringField(required=True, max_length=100)
    version = IntField(required=True)


class writer_report_template:
    def __init__(self, libname="readInfo"):
        self.libname = libname
        self.res = Pymongoengine(self.libname)

    def find(self, PlatID, ProjID, temID):
        self.res.connect_db()
        ret = report_template_config.objects(platId=PlatID, projId=ProjID, tempId=temID).aggregate([])
        return ret

    def delete(self, PlatID, ProjID, temID):
        self.res.connect_db()
        report_template_config.objects.filter(platId=PlatID, projId=ProjID, tempId=temID).delete()

    def insert(self, data):
        self.res.connect_db()
        temp_insert = report_template_config(**data)
        temp_insert.save()
        return True

    def close(self):
        self.res.connect().close()


class report_template_config(DynamicDocument):
    platId = StringField(required=True, max_length=100)
    projId = StringField(required=True, max_length=100)
    tempId = StringField(required=True, max_length=100)


class writer_analysis_data:
    def __init__(self, libname="readInfo"):
        self.libname = libname
        self.res = Pymongoengine(self.libname)

    def SelectAnalysis(self, PlatID, ProjID, TempID):
        self.res.connect_db()
        pipeline = [
            {"$match": {"tempId": TempID}}
        ]
        # ret = client._get_collection("report_template_config").aggregate(*pipeline)
        retAnalysis = data_analysis.objects(tempId=TempID).aggregate(
            *pipeline, allowDiskUse=True)
        return retAnalysis
    def SelectAnalysisReport(self, PlatID, ProjID, TempID, RepID):
        self.res.connect_db()
        pipeline = [
            {"$match": {"tempId": TempID}}
        ]
        ret_data_analysis = data_analysis.objects(tempId=TempID, reportId=RepID).aggregate(
            *pipeline, allowDiskUse=True)
        return ret_data_analysis
    def delete(self, PlatID, ProjID, TempID):
        self.res.connect_db()
        data_analysis.objects.filter(tempId=TempID).delete()
    def deleteReport(self, PlatID, ProjID, TempID, RepID):
        self.res.connect_db()
        data_analysis.objects.filter(tempId=TempID, reportId=RepID).delete()
    def insert(self, data):
        self.res.connect_db()
        analysis_insert = data_analysis(**data)
        analysis_insert.save()
        return True
    def close(self):
        self.res.connect().close()


class data_analysis(DynamicDocument):
    platId=StringField(required=True,max_length=100)
    projId=StringField(required=True,max_length=100)
    tempId=StringField(required=True,max_length=100)
    reportId = StringField(required=True, max_length=100)