#!/usr/bin/env python
# -*- coding:utf-8 -*-

from common.base import FormatMyStr


def condition(CasesCondition):
    if CasesCondition == [{}]:
        subSelectCases = "1=1"
    elif CasesCondition == [{'SampleVariable': '', 'WhereSubVarName': '1', 'WhereSubValue': '1', 'WhereSubSymbol': '=',
          'WhereSubType': 'INT'}]:
        subSelectCases = "1=1"
    else:
        if CasesCondition:
            """
            CasesCondition : [{"WhereSubVarName": ID, "WhereSubSymbol": '>', "WhereSubValue": "", "WhereSubType": "INT"}]
            "WhereSubType": ("FLOAT", "VARCHAR", "DATETIME", "INT", "DATE")
            """
            subSelectCases = "( "

            for subCondition in CasesCondition:
                # print(subCondition)
                if subCondition:
                    WhereSubVarName = FormatMyStr(subCondition["WhereSubVarName"])
                    WhereSubSymbol = FormatMyStr(subCondition["WhereSubSymbol"])
                    WhereSubValue = FormatMyStr(subCondition["WhereSubValue"]) # 如果是多值， 用逗号分割就好
                    WhereSubType = FormatMyStr(subCondition["WhereSubType"])
                    subSelectCases += " " + WhereSubVarName + ' '
                    if WhereSubSymbol in ['<', '>', ">=", "<=", '=', "!="]:
                        if WhereSubType == "INT" or WhereSubType == "BIGINT":
                            subSelectCases += WhereSubSymbol + " " + WhereSubValue + " and "
                        elif WhereSubType == "FLOAT" or WhereSubType == "DOUBLE":
                            subSelectCases += WhereSubSymbol + " " + WhereSubValue + " and "
                        if WhereSubType == "VARCHAR" or WhereSubType == "TEXT":
                            subSelectCases += WhereSubSymbol + ' "' + WhereSubValue + '" and '
                        if WhereSubType == "DATETIME":
                            subSelectCases += WhereSubSymbol + ' "' + WhereSubValue + '" and '
                        if WhereSubType == "DATE":
                            subSelectCases += WhereSubSymbol + ' "' + WhereSubValue + '" and '
                    elif  WhereSubSymbol == "in":
                        if WhereSubType == "INT" or WhereSubType == "BIGINT":
                            WhereSubValue = FormatMyStr(WhereSubValue, int)

                        if WhereSubType == "FLOAT" or WhereSubType == "DOUBLE":
                            WhereSubValue = FormatMyStr(WhereSubValue, float)
                        if WhereSubType == "VARCHAR" or WhereSubType == "TEXT":
                            WhereSubValue = FormatMyStr(WhereSubValue, str)
                        if WhereSubType == "DATETIME":
                            WhereSubValue = FormatMyStr(WhereSubValue, str)
                        if WhereSubType == "DATE":
                            WhereSubValue = FormatMyStr(WhereSubValue, str)
                        if '(' not in WhereSubValue:
                            if WhereSubType in ["INT", "BIGINT", "FLOAT", "DOUBLE"]:
                                subSelectCases += WhereSubSymbol + " (" + WhereSubValue + ") and "
                            else:
                                WhereSubValue = WhereSubValue.replace("，", ",").replace(',', "','")
                                subSelectCases += WhereSubSymbol + " ('" + WhereSubValue + "') and "

                        else:
                            subSelectCases += WhereSubSymbol + " " + WhereSubValue + " and "

                else:
                    subSelectCases += " 1=1 and "
            subSelectCases = subSelectCases.rstrip("and ") + " )"
        else:
            subSelectCases = "1=1"
    return subSelectCases


def down_excel_condition(Condition):
    if Condition == [{'WhereSubType': 'INT', 'WhereSubVarName': '1', 'SampleVariable': '', 'WhereSubSymbol': '=',
                         'WhereSubValue': '1'}]:
        Condition = [{'WhereSubType': 'INT', 'WhereSubVarName': '1', 'SampleVariable': '1', 'WhereSubSymbol': '=',
                         'WhereSubValue': '1'}]

    if Condition == [{}]:
        subSelectCases = "1=1"
        subSelectSample = "1=1"
        # continue

    elif Condition:
        """
        CasesCondition : [{"WhereSubVarName": ID, "WhereSubSymbol": '>', "WhereSubValue": "", "WhereSubType": "INT"}]
        "WhereSubType": ("FLOAT", "VARCHAR", "DATETIME", "INT", "DATE")
        """
        subSelectCases = "( "
        subSelectSample = "( "

        for subCondition in Condition:
            if subCondition:
                WhereSubVarName = FormatMyStr(subCondition["WhereSubVarName"])
                WhereSubSampleVarName = FormatMyStr(subCondition["SampleVariable"])
                WhereSubSymbol = FormatMyStr(subCondition["WhereSubSymbol"])
                WhereSubValue = subCondition["WhereSubValue"]
                WhereSubType = FormatMyStr(subCondition["WhereSubType"])

                subSelectCases += " " + WhereSubVarName + ' ' + WhereSubSymbol
                subSelectSample += " " + WhereSubSampleVarName + ' ' + WhereSubSymbol
                if WhereSubSymbol in ['<', '>', ">=", "<=", '=', "!="]:

                    if WhereSubType == "INT" or WhereSubType == "BIGINT":
                        subSelectCases += " " + WhereSubValue + " and "
                        subSelectSample += " " + WhereSubValue + " and "
                    elif WhereSubType == "FLOAT" or WhereSubType == "DOUBLE":
                        subSelectCases += " " + WhereSubValue + " and "
                        subSelectSample += " " + WhereSubValue + " and "
                    if WhereSubType == "VARCHAR" or WhereSubType == "TEXT":
                        subSelectCases += ' "' + WhereSubValue + '" and '
                        subSelectSample += ' "' + WhereSubValue + '" and '
                    if WhereSubType == "DATETIME":
                        subSelectCases += ' "' + WhereSubValue + '" and '
                        subSelectSample += ' "' + WhereSubValue + '" and '
                    if WhereSubType == "DATE":
                        subSelectCases += ' "' + WhereSubValue + '" and '
                        subSelectSample += ' "' + WhereSubValue + '" and '

                elif WhereSubSymbol == "in":
                    if WhereSubType == "INT" or WhereSubType == "BIGINT":
                        WhereSubValue = FormatMyStr(WhereSubValue, int)
                    if WhereSubType == "FLOAT" or WhereSubType == "DOUBLE":
                        WhereSubValue = FormatMyStr(WhereSubValue, float)
                    if WhereSubType == "VARCHAR" or WhereSubType == "TEXT":
                        WhereSubValue = FormatMyStr(WhereSubValue, str)
                    if WhereSubType == "DATETIME":
                        WhereSubValue = FormatMyStr(WhereSubValue, str)
                    if WhereSubType == "DATE":
                        WhereSubValue = FormatMyStr(WhereSubValue, str)

                    if '(' not in WhereSubValue:
                        if WhereSubType in ["INT", "BIGINT", "FLOAT", "DOUBLE"]:
                            subSelectCases += " (" + WhereSubValue + ") and "
                            subSelectSample += " (" + WhereSubValue + ") and "
                        else:
                            WhereSubValue = WhereSubValue.replace("，", ",").replace(',', "','")
                            subSelectCases += " ('" + WhereSubValue + "') and "
                            subSelectSample += " ('" + WhereSubValue + "') and "
                    else:
                        subSelectCases += " " + WhereSubValue + " and "
                        subSelectSample += " " + WhereSubValue + " and "
            else:
                subSelectCases += " 1=1 and "
                subSelectSample += " 1=1 and "
        subSelectCases = subSelectCases.rstrip("and ") + " )"
        subSelectSample = subSelectSample.rstrip("and ") + " )"
    else:
        subSelectCases = "1=1"
        subSelectSample = "1=1"
    return subSelectCases, subSelectSample


def show_workprogress_message_returndata(Condition):
    if Condition:
        """
        CasesCondition : [{"WhereSubVarName": ID, "WhereSubSymbol": '>', "WhereSubValue": "", "WhereSubType": "INT"}]
        "WhereSubType": ("FLOAT", "VARCHAR", "DATETIME", "INT", "DATE")
        """
        subSelectCases = "( "
        subSelectSample = "( "

        for subCondition in Condition:
            if subCondition:
                WhereSubVarName = FormatMyStr(subCondition["WhereSubVarName"])
                WhereSubSampleVarName = FormatMyStr(subCondition["SampleVariable"])
                WhereSubSymbol = FormatMyStr(subCondition["WhereSubSymbol"])
                WhereSubValue = subCondition["WhereSubValue"]
                WhereSubType = FormatMyStr(subCondition["WhereSubType"])

                subSelectCases += " " + WhereSubVarName + ' ' + WhereSubSymbol
                subSelectSample += " " + WhereSubSampleVarName + ' ' + WhereSubSymbol
                if WhereSubSymbol in ['<', '>', ">=", "<=", '=', "!="]:

                    if WhereSubType == "INT" or WhereSubType == "BIGINT":
                        subSelectCases += " " + WhereSubValue + " and "
                        subSelectSample += " " + WhereSubValue + " and "
                    elif WhereSubType == "FLOAT" or WhereSubType == "DOUBLE":
                        subSelectCases += " " + WhereSubValue + " and "
                        subSelectSample += " " + WhereSubValue + " and "
                    if WhereSubType == "VARCHAR" or WhereSubType == "TEXT":
                        subSelectCases += ' "' + WhereSubValue + '" and '
                        subSelectSample += ' "' + WhereSubValue + '" and '
                    if WhereSubType == "DATETIME":
                        subSelectCases += ' "' + WhereSubValue + '" and '
                        subSelectSample += ' "' + WhereSubValue + '" and '
                    if WhereSubType == "DATE":
                        subSelectCases += ' "' + WhereSubValue + '" and '
                        subSelectSample += ' "' + WhereSubValue + '" and '

                elif WhereSubSymbol == "in":
                    if WhereSubType == "INT" or WhereSubType == "BIGINT":
                        WhereSubValue = FormatMyStr(WhereSubValue, int)
                    if WhereSubType == "FLOAT" or WhereSubType == "DOUBLE":
                        WhereSubValue = FormatMyStr(WhereSubValue, float)
                    if WhereSubType == "VARCHAR" or WhereSubType == "TEXT":
                        WhereSubValue = FormatMyStr(WhereSubValue, str)
                    if WhereSubType == "DATETIME":
                        WhereSubValue = FormatMyStr(WhereSubValue, str)
                    if WhereSubType == "DATE":
                        WhereSubValue = FormatMyStr(WhereSubValue, str)

                    if '(' not in WhereSubValue:
                        if WhereSubType in ["INT", "BIGINT", "FLOAT", "DOUBLE"]:
                            subSelectCases += " (" + WhereSubValue + ") and "
                            subSelectSample += " (" + WhereSubValue + ") and "
                        else:
                            WhereSubValue = WhereSubValue.replace("，", ",").replace(',', "','")
                            subSelectCases += " ('" + WhereSubValue + "') and "
                            subSelectSample += " ('" + WhereSubValue + "') and "
                    else:
                        subSelectCases += " " + WhereSubValue + " and "
                        subSelectSample += " " + WhereSubValue + " and "
            else:
                subSelectCases += " 1=1 and "
                subSelectSample += " 1=1 and "
        subSelectCases = subSelectCases.rstrip("and ") + " )"
        subSelectSample = subSelectSample.rstrip("and ") + " )"

    else:
        subSelectCases = "1=1"
        subSelectSample = "1=1"
    return subSelectCases, subSelectSample



###
# -----<下载文件公共类>----- #
###
def downfile(obj, filePathName):
    FileName = filePathName.split("/")[-1]
    obj.set_header("Content-Type", "application/x-zip-compressed")
    obj.set_header("Content-Disposition", "attachment; filename=%s" % FileName)
    # print()
    f = open(filePathName, 'rb')
    while True:
        b = f.read(8096)
        if not b:
            break
        else:
            obj.write(b)
        obj.flush()
    f.close()

if __name__ == '__main__':
    # aaa=[{"WhereSubVarName":"Q1R3","WhereSubSymbol":"<","WhereSubValue":"1","WhereSubType":"INT"},{"WhereSubVarName":"Q1R5","WhereSubSymbol":"in","WhereSubValue":[2],"WhereSubType":"INT"}]
    aaa = [{"WhereSubVarName":"WJ_YXMC","WhereSubSymbol":"in","WhereSubValue":"北京交通大学, 北京交通大学","WhereSubType":"TEXT","SampleVariable":""}]
    data = condition(aaa)
    print(data)