import copy
import re,json,os,sys
import pandas as pd
import copy


class prepareESdata():
    # 整理非AI资源sop中的数据，方便保存进es数据库
    def __init__(self):
        self.head2key = []
        pass

    def clean(self, text):
        text = text.strip()
        text = re.sub("“", " ", text)
        text = re.sub("”", " ", text) # “”
        text = re.sub('"', " ", text) # ""'
        text = re.sub("'", " ", text)

        return text

    def add_message(self, message):
        add_info = []
        if "中考" in message:
            add_info.append('初三')
        if "幼小衔接" in message:
            add_info.append('一年级')
        if "五六年级" in message:
            add_info.append('五年级')
            add_info.append('六年级')
        if "中小学" in message:
            add_info.append('小学')
            add_info.append('初中')
        if "初升高" in message:
            add_info.append('初三')
        if "中小学" in message:
            add_info.append('小学')
            add_info.append('初中')
        if "1年级" in message:
            add_info.append('一年级')
        if "2年级" in message:
            add_info.append('二年级')
        if "3年级" in message:
            add_info.append('三年级')
        if "1-2年级" in message:
            add_info.append('一年级')
            add_info.append('二年级')
        if "3-6年级" in message:
            add_info.append('三年级')
            add_info.append('四年级')
            add_info.append('五年级')
            add_info.append('六年级')
        if "初升高" in message:
            add_info.append('初三')

        add_info = list(set(add_info))
        add_info = "；".join(add_info)

        return add_info


    def study_tool(self, excel_path, sheet_name):
        # 1、伴学工具
        head2key1 = {"raysAppId":"raysAppId", "typeCode":"typeCode", "typeName":"resourceName",
                    "subjectType":"subjectCategory", "title":"resourceDescribe"}
        self.head2key.append(head2key1)
        key_value = {"bookCategory":"K12", "tableName":"伴学工具"}

        data = pd.read_excel(excel_path, sheet_name=sheet_name)
        data.fillna("")
        data = data.to_dict(orient='records')

        save_data = []
        for item in data:
            temp = copy.deepcopy(key_value)
            for k, v in item.items():
                v = str(v)
                if v == "NaT":
                    v = ""
                if k in head2key1.keys() :
                    temp[head2key1[k]] = self.clean( v)
            message = [temp['bookCategory'], temp['subjectCategory'], temp['resourceName'], temp['resourceDescribe']]
            message = [item for item in message if item]
            message = list(set(message))
            message = "；".join(message)
            temp['message'] = message + "；" + self.add_message(message)
            save_data.append(temp)

        return save_data

    def third_party_own_resource(self, excel_path, sheet_name):
        # 2、第三方自有资源评级详表
        head2key2 = {"资源编号": "typeCode", "创建时间": "createTime", "资源名称": "resourceName", "作品类型": "workType",
                   "作者名称": "authorName", "资源简介": "resourceDescribe", "图书类型/受众": "bookCategory",
                   "学段": "schoolStage", "年级": "studentGrade", "学科": "subjectCategory", "内容": "resourceContent",
                   "版本": "bookVersion","科目/标签":"subjectORlabel", "年级/深度":"gradeDeep",  "目的": "purpose",
                    "学期":"studentSemester", "版本.1":"textBookEdition", "销售价": "bookPrice"
                   }
        self.head2key.append(head2key2)
        data = pd.read_excel(excel_path, sheet_name).fillna("")
        data = data.to_dict(orient='records')

        save_data = []
        key_value = {"tableName":"第三方自有资源评级详表"}
        for item in data:
            temp = copy.deepcopy(key_value)
            if "缺" in item['资源简介']:
                continue
            for k, v in item.items():
                v = str(v)
                if v == "NaT":
                    v = ""
                if k=='创建时间':
                    v = str(v)[:10]
                if k in head2key2.keys() :
                    temp[head2key2[k]] = self.clean( v)

            message = [temp['bookCategory'], temp['resourceName'], temp['workType'], temp['resourceDescribe'], temp['schoolStage'],
                       temp['studentGrade'], temp['subjectCategory'], temp['resourceContent'], temp['bookVersion'], temp['subjectORlabel'],
                        temp['gradeDeep'], temp['purpose'],temp['studentSemester'], temp['textBookEdition'],
                       ]
            message = [item for item in message if item]
            message = list(set(message))
            message = "；".join(message)
            temp['message'] = message+ "；" + self.add_message(message)
            save_data.append(temp)

        return save_data

    def brand_resource(self, excel_path, sheet_name):
        # 3 品牌资源（爱奇艺+慕课）
        head2key3 = {"资源编号": "typeCode", "创建时间": "createTime", "作品名称": "resourceName", "作品类型": "workType",
                   "作者名称": "authorName",  "图书类型/受众": "bookCategory", "科目/标签":"subjectORlabel", "年级/深度":"gradeDeep",
                     "外链销售价": "bookPrice"
                   }
        self.head2key.append(head2key3)
        data = pd.read_excel(excel_path, sheet_name).fillna("")
        data = data.to_dict(orient='records')

        save_data = []
        key_value = {"tableName":"品牌资源（爱奇艺+慕课）"}
        for item in data:
            temp = copy.deepcopy(key_value)
            for k, v in item.items():
                v = str(v)
                if v == "NaT":
                    v = ""
                if k == '创建时间':
                    v = str(v)[:10]

                if k in head2key3.keys() :
                    temp[head2key3[k]] = self.clean( v)

            message = [
                temp['resourceName'], temp['workType'], temp['bookCategory'], temp['subjectORlabel'], temp['gradeDeep']
                       ]
            message = [item for item in message if item]
            message = list(set(message))
            message = "；".join(message)
            temp['message'] = message+ "；" + self.add_message(message)
            save_data.append(temp)

        return save_data

    def xiaorui_information(self, excel_path, sheet_name):
        # 4、小睿资讯服务
        head2key4 = {"资讯编号": "typeCode",  "创建时间": "createTime", "资讯标题（26字内）": "resourceName", "资讯来源":"resourceFrom",
                     "受众": "audience", "科目/标签": "subjectORlabel", "目的": "purpose", "bookCategory":"bookCategory"
                    }
        self.head2key.append(head2key4)
        data = pd.read_excel(excel_path, sheet_name).fillna("")
        data = data.to_dict(orient='records')

        save_data = []
        key_value = {"tableName": "小睿资讯服务"}
        for item in data:
            temp = copy.deepcopy(key_value)
            for k, v in item.items():
                v=str(v)
                if v == "NaT":
                    v = ""
                if k == '创建时间':
                    v = str(v)[:10]
                    if v=="NaT":
                        v= ""
                if k == "bookCategory" and v not in ["K12", "童书"]:
                    v = "其他图书"

                if k in head2key4.keys() :
                    temp[head2key4[k]] = self.clean( v)

            message = [temp['bookCategory'], temp['resourceName'],  temp['resourceFrom'], temp['subjectORlabel'],
                       temp['audience'],  temp['purpose'],
                       ]
            message = [item for item in message if item]
            message = list(set(message))
            message = "；".join(message)
            temp['message'] = message+ "；" + self.add_message(message)
            save_data.append(temp)

        return save_data

    def evaluation_database(self, excel_path, sheet_name):
        # 5、测评库资源
        head2key5 = {"资源编号": "typeCode",  "创建时间": "createTime",
                     "作品名称": "resourceName", "资源简介": "resourceDescribe", "科目/标签": "subjectCategory", "年级/深度":"gradeDeep",
                    "图书类型/受众":"bookCategory"
                    }
        self.head2key.append(head2key5)
        data = pd.read_excel(excel_path, sheet_name).fillna("")
        data = data.to_dict(orient='records')

        save_data = []
        key_value = {"tableName": "测评库资源"}
        for item in data:
            temp = copy.deepcopy(key_value)
            for k, v in item.items():
                v=str(v)
                if v == "NaT":
                    v = ""
                if k == '创建时间':
                    v = str(v)[:10]

                if k == "图书类型/受众":
                    bookCategory = []
                    if "K12" in v:
                        bookCategory.append('K12')
                    if "童书" in v:
                        bookCategory.append('童书')
                    if "大众生活" in v:
                        bookCategory.append('其他图书')
                    if "不限" in v:
                        bookCategory.append('K12')
                        bookCategory.append('童书')
                        bookCategory.append('其他图书')

                    if not bookCategory:
                        bookCategory.append('其他图书')
                    bookCategory = list(set(bookCategory))
                    bookCategory = "；".join(bookCategory)
                    v = bookCategory

                if k in head2key5.keys() :
                    temp[head2key5[k]] = self.clean( v)

            message = [temp['bookCategory'], temp['resourceName'], temp['resourceDescribe'],
                       temp['subjectCategory'],  temp['gradeDeep'],
                       ]
            message = [item for item in message if item]
            message = list(set(message))
            message = "；".join(message)
            temp['message'] = message+ "；" + self.add_message(message)
            save_data.append(temp)

        return save_data

    def existing_tool(self, excel_path, sheet_name):
        # 6、已有工具（数独闯关不算）
        # 大多是AI类工具 （不考虑写进es数据库）
        head2key6 = {"资源编号": "typeCode",  "工具名称": "resourceName", "介绍": "resourceDescribe", "科目/标签": "subjectCategory",
                    "年级/深度": "gradeDeep",
                    "图书类型/受众": "bookCategory"
                    }
        # # self.head2key.append(head2key6)
        data = pd.read_excel(excel_path, sheet_name).fillna("")
        data = data.to_dict(orient='records')

        save_data = []
        key_value = {"tableName": "已有工具（数独闯关不算）"}
        for item in data:
            temp = copy.deepcopy(key_value)
            for k, v in item.items():
                v = str(v)
                if v == "NaT":
                    v = ""
                if k == '创建时间':
                    v = str(v)[:10]

                if k == "bookCategory":
                    bookCategory = []
                    if "K12" in v:
                        bookCategory.append('K12')
                    if "童书" in v:
                        bookCategory.append('童书')
                    if "大总生活" in v:
                        bookCategory.append('其他图书')
                    if "不限" in v:
                        bookCategory.append('其他图书')

                    if not bookCategory:
                        bookCategory.append('其他图书')
                    bookCategory = "；".join(bookCategory)
                    v = bookCategory

                if k in head2key6.keys():
                    temp[head2key6[k]] = self.clean( v)

            message = [temp['bookCategory'], temp['resourceName'], temp['resourceDescribe'],
                       temp['subjectCategory'], temp['gradeDeep'],
                       ]
            message = [item for item in message if item]
            message = list(set(message))
            message = "；".join(message)
            temp['message'] = message+ "；" + self.add_message(message)
            save_data.append(temp)

        return save_data

    def result(self):
        save_sheets = []
        save_sheets_name = []
        path = r"/home/liuxin/work/AI_planner/data/非AI工具"
        # 1、伴学工具
        excel_path = f"/home/liuxin/work/AI_planner/data/非AI工具/伴学工具-2025-02-14.xlsx"
        sheet_name = 'Sheet1'

        study_tool = self.study_tool( excel_path, sheet_name)  # 伴学工具
        data = pd.DataFrame(study_tool)
        save_sheets.append(copy.deepcopy(data))
        save_sheets_name.append("伴学工具")
        study_tool_demp = study_tool[:4]
        print(json.dumps(study_tool_demp, ensure_ascii=False, indent=4))

        # 2、第三方自有资源评级详表
        excel_path = f"/home/liuxin/work/AI_planner/data/非AI工具/商务资源SOP管理表read.xlsx"
        sheet_name = '第三方自有资源评级详表'
        third_party = self.third_party_own_resource( excel_path, sheet_name)
        data = pd.DataFrame(third_party)
        save_sheets.append(copy.deepcopy(data))
        save_sheets_name.append(sheet_name)

        third_party_demp = third_party[:4]
        print(json.dumps(third_party_demp, ensure_ascii=False, indent=4))

        # 3、品牌资源（爱奇艺+慕课）
        excel_path = f"/home/liuxin/work/AI_planner/data/非AI工具/商务资源SOP管理表read.xlsx"
        sheet_name = '品牌资源（爱奇艺+慕课）'
        brand_resource = self.brand_resource( excel_path, sheet_name)
        data = pd.DataFrame(brand_resource)
        save_sheets.append(copy.deepcopy(data))
        save_sheets_name.append(sheet_name)

        brand_resource_demp = brand_resource[:4]
        print(json.dumps(brand_resource_demp, ensure_ascii=False, indent=4))

        # 4、小睿资讯服务
        excel_path = f"/home/liuxin/work/AI_planner/data/非AI工具/商务资源SOP管理表read.xlsx"
        sheet_name = '小睿资讯服务'
        xiaorui_information = self.xiaorui_information(excel_path, sheet_name)
        data = pd.DataFrame(xiaorui_information)
        save_sheets.append(copy.deepcopy(data))
        save_sheets_name.append(sheet_name)

        xiaorui_information_demp = xiaorui_information[:4]
        print(json.dumps(xiaorui_information_demp, ensure_ascii=False, indent=4))

        # 5、测评库资源
        excel_path = f"/home/liuxin/work/AI_planner/data/非AI工具/商务资源SOP管理表read.xlsx"
        sheet_name = '测评库资源'
        evaluation_database = self.evaluation_database(excel_path, sheet_name)
        data = pd.DataFrame(evaluation_database)
        save_sheets.append(copy.deepcopy(data))
        save_sheets_name.append(sheet_name)

        evaluation_database_demp = evaluation_database[:4]
        print(json.dumps(evaluation_database_demp, ensure_ascii=False, indent=4))

        # 保存所欲 非AI资源进 json字典中
        json_data = study_tool + third_party + brand_resource + xiaorui_information + evaluation_database
        print(f"准备了{len(json_data)} 条数据写入es数据库.")  # 准备了18137 条数据写入es数据库.
        file = os.path.join(path, "商务资源SOP管理表es.json")
        with open(file, "w", encoding='utf-8') as f:
            for id, item in enumerate(json_data):
                item['id'] = id

            f.write(json.dumps(json_data, ensure_ascii=False, indent=4))

        # 将数据保存进excel的多个表中
        with pd.ExcelWriter(excel_path[:-5] + "_es"  + ".xlsx") as writer:
            for sheet_name, data in zip(save_sheets_name, save_sheets):
                data.to_excel(writer, sheet_name=sheet_name, index=False)

        # 所有的字段名称
        keys_all = []
        for source in [study_tool[0], third_party[0], brand_resource[0], xiaorui_information[0], evaluation_database[0]]:
            for k,v in source.items():
                keys_all.append(k)
        keys_all = list(set(keys_all))
        print(keys_all)  # ['id', 'studentSemester', 'audience', 'studentGrade', 'bookPrice', 'resourceDescribe', 'resourceContent', 'subjectORlabel', 'typeCode', 'tableName', 'gradeDeep', 'bookCategory', 'resourceFrom', 'createTime', 'raysAppId', 'resourceName', 'workType', 'schoolStage', 'bookVersion', 'purpose', 'message', 'authorName', 'subjectCategory', 'textBookEdition']
        print(len(keys_all))  # 24


if __name__ == "__main__":
    prepare_es_data = prepareESdata()
    prepare_es_data.result()

    print("\nfinished.")


