import sqlite3 as sqlite
import json
import os

#检索数据库，将itemlist生成json格式字符串保存到临时文件中
def get_itemlistfromsql():
    db_dir = "/opt/kydoctor/common/db/" #数据库路径
    #db_dir = "./"
    db_dir_path = db_dir+"solution.db"
    if not os.path.exists(db_dir_path):
        print("solution.db not exist!")
        return -1

    con = sqlite.connect(db_dir_path)
    try:
        my_dict = {}
        with con:
            cur = con.cursor()
            cur.execute("select * from checkitemlist")
            rows = cur.fetchall()
            for row in rows:
                table_temp = []
                table_temp.append(row[2])
                table_temp.append(row[3])
                table_temp.append(row[4])
                if row[1] not in my_dict:
                    my_dict[row[1]] = [table_temp]
                else:
                    my_dict[row[1]].append(table_temp)
            print("my_dict = ",my_dict)

        json_table = []
        for key,item in my_dict.items():
            dict_temp = {}
            dict_temp["Category"] = key
            types_table = []
            #types_dict = {}
            for t1_item in item:
                #for t2_num in len(t1_item):
                #print("t1_item = ",t1_item)
                type_dict = {}
                type_dict["Type"] = t1_item[0]
                type_dict["Name"] = t1_item[1]
                type_dict["Describe"] = t1_item[2]
                types_table.append(type_dict)
            dict_temp["Types"] = types_table
            json_table.append(dict_temp)

        print("str === ",str(json_table))
        #print(json.dumps(json_table))
        json_table_str = json.dumps(json_table)

        #开始往临时文件中写数据
        report_result_name = "/tmp/kydoctor"
        result_fd = open(report_result_name,"w")
        result_fd.write(json_table_str)
        result_fd.close()
                            
        #return -1
    except Exception as e:
        print("in get_itemlist,error :",e)
        return -1
    return 0
    pass
def get_itemlistfromtxt():
    db_dir_path = "/tmp/kydoctor_c_sql"
    if not os.path.exists(db_dir_path):
        print("/tmp/kydoctor_c_sql not exist!")
        return -1

    try:
        my_dict = {}
        file1 = open(db_dir_path)
        while True:
            text = file1.readline()
            if len(text) == 0:
                break
            row = text.split("+++")
            table_temp = []
            table_temp.append(row[2])
            table_temp.append(row[3])
            table_temp.append(row[4])
            if row[1] not in my_dict:
                my_dict[row[1]] = [table_temp]
            else:
                my_dict[row[1]].append(table_temp)
        print("my_dict = ",my_dict)

        json_table = []
        for key,item in my_dict.items():
            dict_temp = {}
            dict_temp["Category"] = key
            types_table = []
            #types_dict = {}
            for t1_item in item:
                #for t2_num in len(t1_item):
                #print("t1_item = ",t1_item)
                type_dict = {}
                type_dict["Type"] = t1_item[0]
                type_dict["Name"] = t1_item[1]
                type_dict["Describe"] = t1_item[2]
                types_table.append(type_dict)
            dict_temp["Types"] = types_table
            json_table.append(dict_temp)

        print("str === ",str(json_table))
        #print(json.dumps(json_table))
        json_table_str = json.dumps(json_table)

        #开始往临时文件中写数据
        report_result_name = "/tmp/kydoctor"
        result_fd = open(report_result_name,"w")
        result_fd.write(json_table_str)
        result_fd.close()
        file1.close()                   
        #return -1
    except Exception as e:
        if os.path.exists(db_dir_path):
            os.remove(db_dir_path)
        print("in get_itemlist,error :",e)
        return -1
    if os.path.exists(db_dir_path):
        os.remove(db_dir_path)
    return 0
    pass
if __name__ == "__main__":
    #ret = get_itemlistfromsql() 
    ret = get_itemlistfromtxt()
    if ret == -1:
        exit(-1)
    else:
        exit(0)


