import json

from ..data_metaclass import DataBaseModel
import copy
import pandas as pd
import settings
import numpy as np


class DataModeGenerateDetail(DataBaseModel):
    def __init__(self):
        super().__init__()
        self.get_online_data.refresh_headers()

    def get_real_color_name(self, color_name: str):
        _ = ["薄棉", "加厚", "厚棉"]
        for i in _:
            color_name = color_name.replace(i, "")
        return color_name

    def get_goods_art_no_info(self, numbers_list=None, goods_art_list=None):
        if goods_art_list:
            data_list = copy.deepcopy(goods_art_list)
            flag = "Goods_Art"
        else:
            data_list = copy.deepcopy(numbers_list)
            flag = "NUM"
        _list = []
        # 单次请求数少于20个
        goods_dict = {}
        while data_list:
            one_data = data_list.pop()
            _list.append(one_data)

            if len(_list) == 20 or len(data_list) == 0:
                if flag == "NUM":
                    # 注意，红蜻蜓和惠利玛函数不同
                    online_goods_art_data = self.get_online_data.get_goods_art_no_info(numbers_list=_list)
                else:
                    online_goods_art_data = self.get_online_data.get_goods_art_no_info(goods_art_list=_list)
                if online_goods_art_data:
                    for key in online_goods_art_data:
                        # 映射为真实的文件夹名称
                        goods_dict[key] = online_goods_art_data[key]
                _list = []
        return goods_dict

    def get_basic_goods_art_data_by_hqt_and_hlm(self, folder_name_list):
        """
        查询红蜻蜓货号的对应数据，输入写入为编码，可能带有@，可能为货号等 NUN为编码
        """
        original_folder_name_list = copy.deepcopy(folder_name_list)

        # 编号与文件名映射表
        return_dict = {}
        for folder_name in original_folder_name_list:
            folder_name: str
            if "NUM" not in folder_name:
                # 即为货号
                return_dict[folder_name] = {"type": "goods_art_no",
                                            "name": folder_name.upper(),
                                            "文件夹名称": folder_name,
                                            "data": {}
                                            }
            else:
                if "@" not in folder_name:
                    return_dict[folder_name] = {"type": "goods_number",
                                                "name": folder_name.upper().replace("NUM", ""),
                                                "文件夹名称": folder_name,
                                                "data": {}
                                                }
                else:
                    return_dict[folder_name] = {"type": "goods_number",
                                                "name": folder_name.upper().replace("NUM", "").split("@")[1],
                                                "文件夹名称": folder_name,
                                                "data": {}
                                                }

        # -----------请求货号数据处理------------
        goods_art_no_list = [v["name"] for k, v in return_dict.items() if v["type"] == "goods_art_no"]
        goods_art_no_all_data = self.get_goods_art_no_info(goods_art_list=goods_art_no_list)
        for folder_name, value in return_dict.items():
            if value["type"] == "goods_art_no":
                if value["name"] in goods_art_no_all_data:
                    return_dict[folder_name]["data"] = goods_art_no_all_data[value["name"]]

        # ------------请求编码数据----------------------
        goods_number_list = [v["name"] for k, v in return_dict.items() if v["type"] == "goods_number"]
        goods_number_all_data = self.get_goods_art_no_info(numbers_list=goods_number_list)
        for folder_name, value in return_dict.items():
            if value["type"] == "goods_number":
                if value["name"] in goods_number_all_data:
                    return_dict[folder_name]["data"] = goods_number_all_data[value["name"]]

        # 清空没有值的数据
        error_key = []
        for folder_name, value in return_dict.items():
            if not value["data"]:
                error_key.append(folder_name)

        if error_key:
            for folder_name in error_key:
                return_dict.pop(folder_name)

        return {"code": 0, "message": "", "data": return_dict}

    def get_basic_goods_art_data_form_excel(self, folder_name_list, excel_path, keys):

        # =====创建虚拟表格并进行连表处理
        need_df = pd.DataFrame(columns=["文件夹名称"])
        for folder_name in folder_name_list:
            new_row = {"文件夹名称": str(folder_name),
                       }
            need_df = need_df._append(new_row, ignore_index=True)

        need_df = need_df.fillna(value='')

        # 打开表格并进行匹配
        _df = pd.read_excel(excel_path, sheet_name=0, header=0)
        # 去重数据
        duplicates = _df.duplicated(subset=['文件夹名称'], keep="first")
        _df = _df.loc[~duplicates]
        _df = _df.fillna(value='')
        _df = _df.astype(str)

        # 数据匹配关联，左关联
        need_df = pd.merge(need_df, _df, on=["文件夹名称"], how="left", indicator=False, )
        # 补全字段
        header_list = need_df.columns.values.tolist()
        for key in keys:
            if key not in header_list:
                need_df[key] = ""

        need_df = need_df.fillna(value='')
        need_df = need_df.astype(str)
        # 数据转字典
        return_dict = {}
        message = ""
        for index, row in need_df.iterrows():
            if settings.PROJECT == "红蜻蜓":
                if row["商品货号"] and row["款号"] and row["编号"]:
                    return_dict[row["文件夹名称"]] = {
                        "type": "goods_art_no",
                        "name": row["文件夹名称"].upper(),
                        "文件夹名称": row["文件夹名称"],
                        "template_name": row["模板名称"],
                        "data": row.to_dict(),
                    }
                else:
                    message = "商品货号、款号、编号必须有值"
            else:
                if row["商品货号"] and row["款号"]:
                    return_dict[row["文件夹名称"]] = {
                        "type": "goods_art_no",
                        "name": row["文件夹名称"].upper(),
                        "文件夹名称": row["文件夹名称"],
                        "template_name": row["模板名称"],
                        "data": row.to_dict(),
                    }

        print("return_dict", return_dict)

        if not return_dict:
            message += "\n没有找到任何匹配的数据"
            return {"code": 99, "message": message, "data": return_dict}
        else:
            return {"code": 0, "message": message, "data": return_dict}

    def check_goods_is_not_deficiency(self, goods_no_dict: dict):
        # 数据返回结果为款号列表
        error_data = {}
        for key, value in goods_no_dict.items():
            number = value["货号资料"][0]["编号"].replace("NUM", "")
            number_list = [x["编号"].replace("NUM", "") for x in value["货号资料"]]
            all_color_name = [self.get_real_color_name(x["颜色名称"]) for x in value["货号资料"]]  # 本地数据
            goods_no = key

            _t_goods_number, all_goods_number = self.get_online_data.get_on_goods_all_art(number=number)
            # all_goods_number------》 [{'number': '14250232', 'goods_art_no': 'AC52001173', 'color': '杏色'}]

            for i in all_goods_number:
                if i["number"] not in number_list:
                    # 检查款号+颜色名称是否已经存在
                    real_color = self.get_real_color_name(i["color"])
                    if real_color not in all_color_name:
                        if goods_no not in error_data:
                            error_data[goods_no] = {"message": "颜色："}
                        error_data[goods_no]["message"] += "{}缺失 ".format(real_color)

        return error_data

    def get_basic_template_information(self, _goods_no_dict, excel_path: str):
        # 本地的文件夹数据形成一个虚拟表格
        original_df = pd.DataFrame(columns=["款号"])
        for goods_no in _goods_no_dict:
            new_row = {"款号": str(goods_no),
                       "flag": "是"
                       }
            original_df = original_df._append(new_row, ignore_index=True)
        original_df = original_df.fillna(value='')

        # EXCEL表格数据
        local_df = pd.read_excel(excel_path, sheet_name=0, header=0)
        values = {'模板名称': "任意", }
        # 确保所有模板都有名称
        local_df.fillna(value=values, inplace=True)
        # 分组后取第一个有效值（即不是NaN的值）；此处用于获取某个款号的所有基础信息
        group_local_df = local_df.groupby(['模板名称', '款号']).agg(
            lambda x: x.dropna().iloc[0] if not x.dropna().empty else np.nan)
        group_local_df = group_local_df.reset_index()

        # -----------数据匹配关联，左关联
        group_local_df = pd.merge(group_local_df, original_df, on=["款号"], how="left", indicator=False, )
        # -----------只取有标记的数据
        group_local_df = group_local_df.loc[~group_local_df['flag'].isnull(), :]
        group_local_df = group_local_df.loc[group_local_df['模板名称'] != "任意", :]
        # -----------数据规整
        group_local_df = group_local_df.fillna(value='')
        group_local_df = group_local_df.astype(str)

        # 组装成字典
        temp_goods_no_dict = {}
        for index, row in group_local_df.iterrows():
            goods_no = row["款号"]
            if goods_no not in temp_goods_no_dict:
                temp_goods_no_dict[goods_no] = {}
            temp_goods_no_dict[goods_no][row["模板名称"]] = row.to_dict()

        return temp_goods_no_dict

    def check_goods_is_not_deficiency_form_excel(self, goods_no_dict: dict, excel_path: str):
        # 检查颜色是否齐全，基于Excel处理
        """
        步骤：
        1、goods_no_dict款号生成一个表格（款号+颜色）
        2、excel表格数据进行款号+颜色去重
        3、excel表格关联 上述的款号 进行标记，其他删除
        4、excel表格关联 上述款号与颜色，进行标记
        5、上述未标记的就是没有的颜色（有对应款号）。
        6、对对应的款号进行设置为错误数据
        返回数据结构

        {
        “A00001--款号”：{“message”：“这个款的错误原因”}，
        “A00002--款号”：{“message”：“这个款的错误原因”}，
        }

        """

        def change_to_int(x):
            if pd.isna(x):
                return 0
            else:
                try:
                    x = int(x)
                    return str(x)
                except:
                    return x

        error_data = {}
        # -----------------goods_no_dict款号生成一个表格（款号+颜色）
        new_df = pd.DataFrame(columns=['款号', '颜色名称', '款色标记'])
        for key, value in goods_no_dict.items():
            goods_no = value["款号"]
            for color_name in [x["颜色名称"] for x in value["货号资料"]]:
                new_row = {"款号": goods_no,
                           "颜色名称": self.get_real_color_name(color_name),
                           "款色标记": 1,
                           }

                new_df = new_df._append(new_row, ignore_index=True)
        new_df.drop_duplicates(subset=['款号', "颜色名称"], keep='first', inplace=True)

        # -------------参考的本地excel表格数据进行款号+颜色去重------------
        local_df = pd.read_excel(excel_path, sheet_name=0, header=0)
        local_df.fillna(value={'颜色名称': "", }, inplace=True)
        local_df = local_df.astype({"颜色名称": str})

        header_list = local_df.columns.values.tolist()

        for key in ["帮高", "鞋宽", "跟高"]:
            if key in header_list:
                local_df[key] = local_df[key].apply(change_to_int)

        #     颜色名称规整
        local_df["颜色名称"] = local_df["颜色名称"].apply(self.get_real_color_name)
        local_df.drop_duplicates(subset=['款号', "颜色名称"], keep='first', inplace=True)

        # ------------excel表格关联 上述的款号 进行标记，其他删除
        _df = new_df.drop_duplicates(subset=['款号'], keep='first', inplace=False)
        _df = _df[["款号"]]
        _df["对应款号"] = 1
        local_df = pd.merge(local_df, _df, on=["款号"], how="left", indicator=False, )
        local_df = local_df.loc[local_df['对应款号'] == 1, :]

        # ------------excel表格关联 上述款号与颜色，进行标记;取没有关联的颜色数据---------
        local_df = pd.merge(local_df, new_df, on=['款号', "颜色名称"], how="left", indicator=False, )
        local_df.fillna(value={'款色标记': 0, }, inplace=True)
        local_df = local_df.loc[local_df['款色标记'] == 0, :]
        print("local_df")
        print(local_df)

        # ------------数据分组，对对应的款号进行设置为错误数据---------------
        grouped_df = local_df.groupby('款号')['颜色名称'].apply(lambda x: '、'.join(x)).reset_index()
        for index, row in grouped_df.iterrows():
            error_data[row["款号"]] = {"message": "颜色：{}缺失".format(row["颜色名称"])}

        print("======error_data")
        print(json.dumps(error_data))

        return error_data


class DataModeUploadPic(DataBaseModel):

    def __init__(self, token):
        super().__init__(token)
        self.token = token
        self.get_online_data.refresh_headers(token)
        self.is_deal_goods_no = []

    def get_goods_art_no_info(self, numbers_list=None, goods_art_list=None):
        return self.get_online_data.get_goods_art_no_info(numbers_list=numbers_list, goods_art_list=goods_art_list,token=self.token)

    def upload_pic_list_data(self, data):
        return self.get_online_data.upload_pic_list_data(data, token=self.token)

    def upload_pic(self,goods_data):
        return self.get_online_data.upload_pic(goods_data, token=self.token)

    def check_is_uploaded(self,goods_art_no=None):
        return self.get_online_data.check_detail_image(goods_art_no, token=self.token)
