# from UI.matching_photos.matching_photos import Ui_Form as matching_photos_Ui_Form
import json
import os

from UI.matching_photos.matching_and_cutout_photos import (
    Ui_Form as matching_photos_Ui_Form,
)

from import_qt_mode import *

import settings
from collections import defaultdict

from module.view_control.match_and_cutout_mode_control.base_deal_image_v2 import BaseDealImage
from module.view_control.generate_goods_art_no_table.module_generate_goods_art_no_table import GenerateGoodsArtNoTable
from module.view_control.matching_photos.data import DataModeMatchPhoto
from module.view_control.manual_image_matching.m_image_matching_cotrol import MainMatchingWindow

from PySide6.QtGui import QPixmap
from PIL import Image
import io
from functools import partial
import threading
import time
from module.base_mode.pic_deal import PicDeal
from module.base_mode.base import *
from module.view_control.MineQWidget import MineQWidget, DialogShow, WorkerOneThread
from threading import Event
from UI.matching_photos.temp_item import Ui_Form as UI_temp_item
from concurrent.futures import ThreadPoolExecutor
import concurrent.futures

"""
照片自动货号匹配 将图片放置在指定文件夹下，并自动对应不同的货号进行整理
"""


class Communicate(QObject):
    # 定义一个信号用于请求显示对话框
    show_dialog_signal = Signal()
    # 定义一个信号用于返回对话框的结果
    dialog_result_signal = Signal(str)


class MatchingPhotos(MineQWidget):
    progress_sign = Signal(dict)
    info_sign = Signal(str)
    text_show = Signal(str)
    show_dialog_sign = Signal(dict)
    change_state_sign = Signal(int)

    def __init__(self):
        super().__init__()
        self.image_dir = settings.PhotoImageDir
        self.ui = matching_photos_Ui_Form()
        self.ui.setupUi(self)
        # 0禁用  1进行中  2已结束
        self.state = 2
        self.event = Event()
        self.goods_images_count_dict = defaultdict(int)
        self.data_match_photo = DataModeMatchPhoto()
        self.excel_path = ""
        # 上次使用的详情模板
        self.last_temp = settings.MATCHING_LAST_TEMP
        # 指定单独处理的颜色文件夹
        self.special_goods_art_no_folder = []

        self.communicator = Communicate()
        self.dialog_result = ""
        self.communicator.dialog_result_signal.connect(self.on_dialog_result)
        self.event_dialog = Event()
        self.show_dialog_sign.connect(self.show_dialog)

        # 改变状态
        self.change_state_sign.connect(self.set_state)

        self.init()
        self.show()

    def init(self):
        # 文本
        self.ui.textBrowser.clear()
        text = """请将capture One 加工后的图片放在指定目录下（目录为当前文件夹下的{}/data文件夹），务必确保导出的图片有拍摄时间信息。""".format(os.getcwd())
        self.ui.textBrowser.append(text)
        self.ui.tabWidget.setCurrentIndex(0)
        # 页面视图1
        self.ui.stackedWidget.setCurrentIndex(0)
        self.text_show.connect(self.append_text_to_browser)
        self.show_img_dir()
        self.ui.select_path.mousePressEvent = self.change_img_dir
        self.ui.progressBar.setValue(0)
        self.ui.progressBar.setMaximum(100)
        self.ui.progressBar.setValue(0)
        self.ui.progressBar.hide()
        self.ui.line_image_pos.setText(settings.cutimage_dict['imageorder'])
        self.ui.need_resize.setText(settings.cutimage_dict['resize_image_view'])

        self.ui.check_image_total.setChecked(bool(settings.cutimage_dict['is_check_number']))
        self.ui.check_is_pass.setChecked(bool(settings.cutimage_dict['is_filter']))
        print(settings.cutimage_dict)
        if settings.DEFAULT_CUTOUT_MODE == '普通抠图':
            self.ui.cutout_nomal.setChecked(True)
        elif settings.DEFAULT_CUTOUT_MODE == ' 精细化抠图':
            self.ui.cutout_exquisite.setChecked(True)

        if not bool(settings.IsExquisiteMode):
            self.ui.cutout_exquisite.setEnabled(False)

        # 展示是否指定内容抠图
        self.ui.is_do_other.toggled.connect(self.change_do_special_treatment)
        self.change_do_special_treatment()

        self.progress_sign.connect(self.show_progress)
        self.ui.run.clicked.connect(self.run_image_cut)  # 抠图与主图加工
        self.ui.run_2.clicked.connect(self.run_matching)  # 匹配图片
        self.ui.back.clicked.connect(self.back)
        self.ui.stop.clicked.connect(self.to_stop)

        # 页面视图2
        self.ui.back.hide()
        self.set_logo_selected()

        # 手动匹配图片
        self.ui.manual_matching_pic.mousePressEvent = self.manual_matching_pic_mousePressEvent

    # 暂停
    def to_stop(self, *args):
        if self.state == 1:
            self.set_state(state_value=99)

    # 切换是否特殊处理的开关
    def change_do_special_treatment(self, *args):
        if self.ui.is_do_other.isChecked():
            self.ui.show_deal_condition.show()
        else:
            self.ui.show_deal_condition.hide()

    # 设置选择logo模板
    def set_logo_selected(self):
        logo_root_path = ""
        if settings.PROJECT == "红蜻蜓":
            logo_root_path = r"{}\resources\LOGO\HQT".format(os.getcwd())
        elif settings.PROJECT == "惠利玛":
            if "小苏" in settings.Company:
                logo_root_path = r"{}\resources\LOGO\xiaosushuoxie".format(os.getcwd())
            if "惠利玛" in settings.Company:
                logo_root_path = r"{}\resources\LOGO\HLM".format(os.getcwd())

        self.logo_path_dict = {"请选择": "", "无logo": ""}

        if logo_root_path:
            if os.path.exists(logo_root_path):
                image_list = get_images(logo_root_path)
                for image_data in image_list:
                    self.logo_path_dict[image_data["file_name"]] = image_data["file_path"]

        self.ui.logo_select_ui.addItems([x for x in self.logo_path_dict])

        self.ui.logo_select_ui.setCurrentText("请选择")
        if settings.MATCHING_LAST_LOGO_TEMP:
            if settings.MATCHING_LAST_LOGO_TEMP in [x for x in self.logo_path_dict]:
                self.ui.logo_select_ui.setCurrentText(settings.MATCHING_LAST_LOGO_TEMP)

    def manual_matching_pic_mousePressEvent(self, event):
        if not self.image_dir:
            a = QMessageBox.question(
                self,
                "确认",
                "请先选择目标文件夹",
                QMessageBox.Yes,
            )
            return
        self.manual = MainMatchingWindow(root_path=self.image_dir)

    def get_config(self, *args):
        if not settings.IsLogin:
            QMessageBox.question(
                self, "确认", "请先登录", QMessageBox.Yes | QMessageBox.No
            )
            return

        logo_name = self.ui.logo_select_ui.currentText()
        if logo_name == "请选择":
            QMessageBox.question(
                self, "确认", "请先选择logo模板", QMessageBox.Yes | QMessageBox.No
            )
            return

        if self.ui.cutout_exquisite.isChecked() and not bool(settings.IsExquisiteMode):
            QMessageBox.question(
                self, "确认", "暂无 精细化抠图权限", QMessageBox.Yes | QMessageBox.No
            )
            return

        image_dir = self.ui.path_name.text()
        image_order = self.ui.line_image_pos.text()
        is_check_number = self.ui.check_image_total.isChecked()
        is_filter = self.ui.check_is_pass.isChecked()
        resize_image_view = self.ui.need_resize.text()
        logo_name = self.ui.logo_select_ui.currentText()
        settings.MATCHING_LAST_LOGO_TEMP = logo_name

        logo_path = self.logo_path_dict[logo_name]

        cutout_mode = '1'
        if self.ui.cutout_exquisite.isChecked():
            cutout_mode = '2'

        configData = {
            'imageorder': image_order,
            'resize_image_view': resize_image_view,
            'is_check_number': str(is_check_number),
            'is_filter': str(is_filter),
            'cutout_mode': str(cutout_mode),
            'matching_mode_last': settings.MATCHING_MODE_LAST,
            "matching_last_temp": self.last_temp,
            "matching_last_logo_temp": logo_name,
        }

        settings.cutimage_dict['imageorder'] = image_order
        settings.cutimage_dict['resize_image_view'] = resize_image_view
        settings.cutimage_dict['is_check_number'] = str(is_check_number)
        settings.cutimage_dict['is_filter'] = str(is_filter)
        settings.cutimage_dict['cutout_mode'] = str(cutout_mode)
        settings.set_config(data_dict=configData, section='cutimage')

        config_data = {}
        config_data["image_dir"] = image_dir
        config_data["image_order"] = image_order
        config_data["is_check_number"] = is_check_number
        config_data["is_filter"] = is_filter
        config_data["resize_image_view"] = resize_image_view
        config_data["cutout_mode"] = cutout_mode
        config_data["logo_path"] = logo_path
        return config_data

    @Slot()
    def on_stop_clicked(self):
        self.event.set()
        pass

    def change_img_dir(self, *args):
        folder = QFileDialog.getExistingDirectory(self, "选取文件夹", "./")
        print(folder)
        settings.set_config(data_dict={"photo_image_dir": folder})

        # 清空已特殊选择等处理
        self.ui.is_do_other.setChecked(False)

        self.show_img_dir(image_dir=folder)

    def show_img_dir(self, image_dir=None):
        if image_dir:
            self.image_dir = image_dir
        else:
            self.image_dir = settings.PhotoImageDir

        if self.image_dir:
            self.ui.path_name.show()
            self.ui.path_name.setText(self.image_dir)
        else:
            self.ui.path_name.hide()

    def show_progress(self, data):
        progress_bar_value = data["progress_bar_value"]
        self.ui.progressBar.setValue(progress_bar_value)

    def check(self):
        _path = self.image_dir + "/历史"
        if not os.path.exists(_path):
            os.mkdir(_path)
        return True

    def set_state(self, state_value: int):
        # 0禁用  1进行中  2已结束
        if state_value not in [0, 1, 2, 99]:
            return
        self.state = state_value

        if self.state == 0:
            self.ui.stackedWidget.setCurrentIndex(0)
        if self.state == 1:
            self.ui.stackedWidget.setCurrentIndex(1)
            self.ui.back.hide()
            self.ui.stop.show()
            self.ui.stop.setEnabled(True)
            self.ui.textBrowser_4.clear()
            self.ui.textBrowser_4.show()
            self.event.clear()

        if self.state == 2:
            self.ui.stackedWidget.setCurrentIndex(1)
            self.ui.back.show()
            self.ui.stop.hide()

        if self.state == 99:
            self.ui.stop.setEnabled(False)
        print("设置state：{}".format(state_value))

    def back(self):
        self.ui.stackedWidget.setCurrentIndex(0)
        self.ui.back.hide()
        self.ui.run.show()
        self.ui.stop.hide()
        self.ui.textBrowser_4.clear()
        self.ui.textBrowser_4.hide()

    def run_matching(self, *args):
        if settings.PROJECT == "红蜻蜓":
            if not settings.IsLogin:
                a = QMessageBox.question(
                    self,
                    "确认",
                    "请先进行登录红蜻蜓账号",
                    QMessageBox.Yes | QMessageBox.No,
                )
                return

        self.set_state(state_value=1)
        self.goods_images_count_dict = {}
        self.goods_images_count_dict = defaultdict(int)
        self.do_thread_run(func=self.run_by_thread, call_back=self.matchingCallBack, time_out=60)

        # self.t = threading.Thread(target=self.run_by_thread, args=())
        # self.t.start()

    def matchingCallBack(self, data: dict):
        self.set_state(2)
        if data['code'] == 0:
            self.show_progress_detail('处理完成')
            button_1, button_2, button_3 = None, None, None
            button_1 = "去抠图"
            text = '移动完成'
            my_dialog = DialogShow(self, text=text, button_1=button_1, button_2=button_2)
            ret = my_dialog.exec()
            if "去抠图" in my_dialog.flag_name:
                self.back()
                self.ui.tabWidget.setCurrentIndex(1)
                path = os.getcwd() + "/" + data['target_path']
                self.ui.path_name.setText(path)
        else:
            self.show_progress_detail(data['msg'])
            text = data['msg']
            my_dialog = DialogShow(self, text=text)
            ret = my_dialog.exec()

    def run_image_cut(self, *args):
        if not settings.IsLogin:
            QMessageBox.question(
                self, "确认", "请先登录", QMessageBox.Yes | QMessageBox.No
            )
            return

        logo_name = self.ui.logo_select_ui.currentText()
        if logo_name == "请选择":
            QMessageBox.question(
                self, "确认", "请先选择logo模板", QMessageBox.Yes | QMessageBox.No
            )
            return

        if self.ui.cutout_exquisite.isChecked() and not bool(settings.IsExquisiteMode):
            QMessageBox.question(
                self, "确认", "暂无 精细化抠图权限", QMessageBox.Yes | QMessageBox.No
            )
            return

        # 先做整体校验
        self.do_thread_run(func=self.check_for_cutout_image_first,
                           call_back=self.check_for_cutout_image_first_call_back, time_out=30)

    # 抠图前先做数据规整处理；类似详情图生成逻辑
    def check_for_cutout_image_first(self, ):
        return_data = {
            "code": 99,
            "message": "",
            "data": {
                "all_goods_art_no_folder_data": [],
            },
        }
        image_dir = self.ui.path_name.text()
        image_order = self.ui.line_image_pos.text()
        is_check_number = self.ui.check_image_total.isChecked()
        is_filter = self.ui.check_is_pass.isChecked()
        resize_image_view = self.ui.need_resize.text()
        logo_name = self.ui.logo_select_ui.currentText()
        settings.MATCHING_LAST_LOGO_TEMP = logo_name
        logo_path = self.logo_path_dict[logo_name]

        # 是否精细化抠图，默认为普通抠图
        cutout_mode = '1'
        if self.ui.cutout_exquisite.isChecked():
            cutout_mode = '2'

        configData = {
            'imageorder': image_order,
            'resize_image_view': resize_image_view,
            'is_check_number': str(is_check_number),
            'is_filter': str(is_filter),
            'cutout_mode': str(cutout_mode),
            'matching_mode_last': settings.MATCHING_MODE_LAST,
            "matching_last_temp": self.last_temp,
            "matching_last_logo_temp": logo_name,
        }

        settings.cutimage_dict['imageorder'] = image_order
        settings.cutimage_dict['resize_image_view'] = resize_image_view
        settings.cutimage_dict['is_check_number'] = str(is_check_number)
        settings.cutimage_dict['is_filter'] = str(is_filter)
        settings.cutimage_dict['cutout_mode'] = str(cutout_mode)
        settings.set_config(data_dict=configData, section='cutimage')

        # =================整理要处理的货号文件夹数据=================
        # all_goods_art_no_folder_data 为数据列表，格式如下：
        # {"folder_path": file_path,
        #                  "folder_name": _file,
        #                  "root_path": path,
        #                  "to_do": True,  # 是否需要继续处理;默认为需要处理
        #                  }
        # 自动处理红蜻蜓货号，进行重命名
        if settings.PROJECT == "红蜻蜓":
            # 规整红蜻蜓货号图
            all_goods_art_no_folder_data = get_all_goods_art_no_folder(path=image_dir)
            BaseDealImage().rename_folder_for_hqt(all_goods_art_no_folder_data=all_goods_art_no_folder_data)

        # 重新获取文件夹信息
        all_goods_art_no_folder_data = get_all_goods_art_no_folder(path=image_dir)

        f = True
        if self.ui.is_do_other.isChecked():
            for i in all_goods_art_no_folder_data:
                i["label"] = "不处理"
            is_filter = False
            specified_goods_art_no_folder = self.ui.special_goods_art_no_folder_line.text()
            specified_goods_art_no_folder = specified_goods_art_no_folder.strip()
            specified_goods_art_no_folder = specified_goods_art_no_folder.replace("，", ",")
            specified_goods_art_no_folder_list = specified_goods_art_no_folder.split(",")
            specified_goods_art_no_folder_list = [x for x in specified_goods_art_no_folder_list if x]
            if not specified_goods_art_no_folder_list:
                return_data["message"] += '请手动输入文件夹名称（多选），或关闭指定文件夹模式\n'
            else:
                for i in all_goods_art_no_folder_data:
                    if i["folder_path"] in specified_goods_art_no_folder_list:
                        i["label"] = "待处理"
            # 哪些数据不合规
            all_folder_name_list = [x["folder_name"] for x in all_goods_art_no_folder_data]
            for goods_art_no_folder_name in specified_goods_art_no_folder_list:
                if goods_art_no_folder_name not in all_folder_name_list:
                    return_data["message"] += '文件夹：{}，在您选的目录下不存在\n'.format(goods_art_no_folder_name)
                    f = False

        if not f:
            self.set_state(state_value=2)
            return

        # 清空指定文件夹的已抠图文件
        if self.ui.is_do_other.isChecked():
            if self.ui.is_clean_cutout_image.isChecked():
                for folder_data in all_goods_art_no_folder_data:
                    goods_art_no_folder_path = "{}/原始图_已抠图".format(folder_data["folder_path"])
                    if os.path.exists(goods_art_no_folder_path):
                        remove_all_file(goods_art_no_folder_path)

        return_data["data"]["succeed_folder_list"] = 1

        # ==================检查填写的图片视角是否符合要求
        res = BaseDealImage().getImageOrder(image_order=image_order, resize_image_view=resize_image_view)
        if res['code'] != 0:
            return_data["message"] += "{}\n".format(res['msg'])
            return return_data
        else:
            # 图片命名顺序
            image_order_list = res['imageOrderList']
            for goods_art_no_folder_data in all_goods_art_no_folder_data:
                if goods_art_no_folder_data["label"] != "待处理":
                    continue
                goods_art_no_folder_data["image_order_list"] = image_order_list

        # ================是否过滤已有生成的文件夹
        if is_filter:
            for goods_art_no_folder_data in all_goods_art_no_folder_data:
                if goods_art_no_folder_data["label"] != "待处理":
                    continue
                folder_path = goods_art_no_folder_data["folder_path"]
                _p = "{}/800x800".format(folder_path)
                if os.path.exists(_p):
                    if len(os.listdir(_p)):
                        goods_art_no_folder_data["label"] = "不处理"

        # ================检查每个货号文件夹图片数量是否符合要求
        all_goods_art_no_folder_data, message = BaseDealImage().check_folders_image_amount(all_goods_art_no_folder_data,
                                                                                           image_order_list)
        if message:
            return_data["message"] += "{}\n".format(message)
        return_data["code"] = 0
        return_data["data"]["all_goods_art_no_folder_data"] = all_goods_art_no_folder_data
        return_data["data"]["image_dir"] = image_dir
        return_data["data"]["resize_image_view"] = resize_image_view
        return_data["data"]["cutout_mode"] = cutout_mode
        return_data["data"]["image_order_list"] = image_order_list
        return_data["data"]["logo_path"] = logo_path
        return return_data

    # 抠图校验后的回调函数处理
    def check_for_cutout_image_first_call_back(self, return_data):
        # return_data = {
        #     "code": 99,
        #     "message": "",
        #     "data": {
        #         "all_goods_art_no_folder_data": [],
        #     },
        # }
        code = return_data["code"]
        print("===================return_data=================")
        print(json.dumps(return_data))
        if code != 0:
            # self.show_message(return_data["message"])
            _dialog_dict = {"text": return_data["message"],
                            "windows": self,
                            }
            self.show_dialog_sign.emit(_dialog_dict)
            self.event_dialog.wait()
            return

        do_next = False
        text = "check_for_cutout_image_first_call_back\n"
        all_goods_art_no_folder_data = return_data["data"]["all_goods_art_no_folder_data"]
        button_1, button_2, button_3 = None, None, None
        text += return_data["message"]
        # 存在错误文件夹
        error_folder = [x for x in all_goods_art_no_folder_data if x["label"] == "错误"]
        todo_folder = [x for x in all_goods_art_no_folder_data if x["label"] == "待处理"]
        if error_folder:
            button_2 = "移除错误文件"
        if error_folder and todo_folder:
            button_1 = "移除并继续"
            button_3 = "继续(忽略其他)"
        if not error_folder and todo_folder:
            button_1 = "继续"
            # do_next = True
        text += "\n==================\n错误数据：{}个，校验无误数据：{}个".format(len(error_folder), len(todo_folder))
        # my_dialog = DialogShow(
        #     self, text=text, button_1=button_1, button_2=button_2, button_3=button_3
        # )
        # ret = my_dialog.exec()

        _dialog_dict = {"text": text,
                        "button_1": button_1,
                        "button_2": button_2,
                        "button_3": button_3,
                        "windows": self,
                        }
        self.show_dialog_sign.emit(_dialog_dict)
        # self.exec_()
        # 等待事件被设置
        self.event_dialog.wait()
        print("self.dialog_result", self.dialog_result)

        print("520  ===============my_dialog.flag_name===============")

        if "移除" in self.dialog_result:
            for error_folder_data in [x for x in all_goods_art_no_folder_data if x["label"] == "错误"]:
                self.move_error_folders(
                    one_path=error_folder_data["folder_path"],
                    target_folder="{}/软件-处理失败".format(self.image_dir),
                )
        if "继续" in self.dialog_result:
            do_next = True

        if do_next:
            all_goods_art_no_folder_data = [x for x in all_goods_art_no_folder_data if x["label"] == "待处理"]
            # self.set_state(state_value=1)
            self.change_state_sign.emit(1)
            time.sleep(1.5)
            # 强制刷新界面命令
            # QApplication.processEvents()
            kwargs = {"all_goods_art_no_folder_data": all_goods_art_no_folder_data,
                      "callback_func": self.show_progress_detail,
                      "image_order_list": return_data["data"]["image_order_list"],
                      "cutout_mode": return_data["data"]["cutout_mode"],
                      "resize_image_view": return_data["data"]["resize_image_view"],
                      "windows": self,
                      "logo_path": return_data["data"]["logo_path"], }
            # self.t = threading.Thread(target=self.do_run2,
            #                           kwargs={"all_goods_art_no_folder_data": all_goods_art_no_folder_data,
            #                                   "callback_func": self.show_progress_detail,
            #                                   "image_order_list": return_data["data"]["image_order_list"],
            #                                   "cutout_mode": return_data["data"]["cutout_mode"],
            #                                   "resize_image_view": return_data["data"]["resize_image_view"],
            #                                   "windows": self,
            #                                   "logo_path": return_data["data"]["logo_path"],
            #                                   })
            # self.t.start()
            # new_func = partial(self.do_run2,
            #                    all_goods_art_no_folder_data=kwargs["all_goods_art_no_folder_data"],
            #                    callback_func=kwargs["callback_func"],
            #                    image_order_list=kwargs["image_order_list"],
            #                    cutout_mode=kwargs["cutout_mode"],
            #                    resize_image_view=kwargs["resize_image_view"],
            #                    windows=kwargs["windows"],
            #                    logo_path=kwargs["logo_path"],
            #                    )
            # self._w_3 = WorkerOneThread(func=new_func, name="_w_3")
            # self._w_3.start()
            self.do_run2(all_goods_art_no_folder_data=kwargs["all_goods_art_no_folder_data"],
                         callback_func=kwargs["callback_func"],
                         image_order_list=kwargs["image_order_list"],
                         cutout_mode=kwargs["cutout_mode"],
                         resize_image_view=kwargs["resize_image_view"],
                         windows=kwargs["windows"],
                         logo_path=kwargs["logo_path"], )

    def do_run2(self, all_goods_art_no_folder_data,
                callback_func,
                image_order_list,
                cutout_mode,
                resize_image_view,
                windows,
                logo_path):
        try:
            print("610 BaseDealImage")
            print(all_goods_art_no_folder_data)
        except BaseException as e:
            print("615", e)

        BaseDealImage().run_main(
            all_goods_art_no_folder_data=all_goods_art_no_folder_data,
            callback_func=callback_func,
            image_order_list=image_order_list,
            cutout_mode=cutout_mode,
            resize_image_view=resize_image_view,
            windows=windows,
            logo_path=logo_path)

        # max_workers = 1
        # with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
        #     futures = []
        #     futures.append(executor.submit(
        #         BaseDealImage().run_main,
        #         all_goods_art_no_folder_data=all_goods_art_no_folder_data,
        #         callback_func=callback_func,
        #         image_order_list=image_order_list,
        #         cutout_mode=cutout_mode,
        #         resize_image_view=resize_image_view,
        #         windows=windows,
        #         logo_path=logo_path,
        #     ))
        #
        #     # 使用 wait 方法等待所有任务完成
        #     done, not_done = concurrent.futures.wait(futures)
        #     # 处理完成的任务
        #     for future in done:
        #         if settings.IS_TEST:
        #             result = future.result()

        # ==============完成处理==============
        self.change_state_sign.emit(2)
        # self.set_state(state_value=2)
        callback_func("已结束")

    def move_error_folders(self, one_path, target_folder, message=""):
        if os.path.exists(one_path):
            check_path(target_folder)
            move_folders(path_list=[one_path], target_folder=target_folder)

    def threadImageCut(self, ):
        image_dir = self.ui.path_name.text()
        baseDealImage = BaseDealImage(image_dir=image_dir)
        image_order = self.ui.line_image_pos.text()
        is_check_number = self.ui.check_image_total.isChecked()
        is_filter = self.ui.check_is_pass.isChecked()
        resize_image_view = self.ui.need_resize.text()
        logo_name = self.ui.logo_select_ui.currentText()
        settings.MATCHING_LAST_LOGO_TEMP = logo_name

        logo_path = self.logo_path_dict[logo_name]

        cutout_mode = '1'
        if self.ui.cutout_exquisite.isChecked():
            cutout_mode = '2'

        configData = {
            'imageorder': image_order,
            'resize_image_view': resize_image_view,
            'is_check_number': str(is_check_number),
            'is_filter': str(is_filter),
            'cutout_mode': str(cutout_mode),
            'matching_mode_last': settings.MATCHING_MODE_LAST,
            "matching_last_temp": self.last_temp,
            "matching_last_logo_temp": logo_name,
        }

        settings.cutimage_dict['imageorder'] = image_order
        settings.cutimage_dict['resize_image_view'] = resize_image_view
        settings.cutimage_dict['is_check_number'] = str(is_check_number)
        settings.cutimage_dict['is_filter'] = str(is_filter)
        settings.cutimage_dict['cutout_mode'] = str(cutout_mode)
        settings.set_config(data_dict=configData, section='cutimage')

        # =================整理要处理的货号文件夹数据=================
        all_goods_art_no_folder_data = get_all_goods_art_no_folder(path=image_dir)
        f = True
        if self.ui.is_do_other.isChecked():
            for i in all_goods_art_no_folder_data:
                i["todo"] = False

            is_filter = False
            specified_goods_art_no_folder = self.ui.special_goods_art_no_folder_line.text()
            specified_goods_art_no_folder = specified_goods_art_no_folder.strip()
            specified_goods_art_no_folder = specified_goods_art_no_folder.replace("，", ",")
            specified_goods_art_no_folder_list = specified_goods_art_no_folder.split(",")
            specified_goods_art_no_folder_list = [x for x in specified_goods_art_no_folder_list if x]
            if not specified_goods_art_no_folder_list:
                self.show_progress_detail('请手动输入文件夹名称（多选），或关闭指定文件夹模式')
            else:
                for i in all_goods_art_no_folder_data:
                    if i["folder_path"] in specified_goods_art_no_folder_list:
                        i["todo"] = True
            # 哪些数据不合规
            all_folder_name_list = [x["folder_name"] for x in all_goods_art_no_folder_data]
            for goods_art_no_folder_name in specified_goods_art_no_folder_list:
                if goods_art_no_folder_name not in all_folder_name_list:
                    self.show_progress_detail('文件夹：{}，在您选的目录下不存在'.format(goods_art_no_folder_name))
                    f = False

        if not f:
            self.set_state(state_value=2)
            return

        # 清空指定文件夹的已抠图文件
        # print("todo_goods_art_no_folder_name_list",todo_goods_art_no_folder_name_list)
        if self.ui.is_do_other.isChecked():
            if self.ui.is_clean_cutout_image.isChecked():
                for folder_data in all_goods_art_no_folder_data:
                    goods_art_no_folder_path = "{}/原始图_已抠图".format(folder_data["folder_path"])
                    if os.path.exists(goods_art_no_folder_path):
                        remove_all_file(goods_art_no_folder_path)

        if settings.IS_TEST:
            result = baseDealImage.cutoutImage(
                image_dir=image_dir,
                all_goods_art_no_folder_data=all_goods_art_no_folder_data,
                image_order=image_order,
                is_check_number=is_check_number,
                is_filter=is_filter,
                resize_image_view=resize_image_view,
                callback_func=self.show_progress_detail,
                event=self.event,
                cutout_mode=cutout_mode,
                logo_path=logo_path,
            )
            if "code" in result and result['code'] != 0:
                self.show_progress_detail(result['msg'])
            else:
                self.show_progress_detail('处理完成')
        else:
            try:
                result = baseDealImage.cutoutImage(
                    image_dir=image_dir,
                    image_order=image_order,
                    all_goods_art_no_folder_data=all_goods_art_no_folder_data,
                    is_check_number=is_check_number,
                    is_filter=is_filter,
                    resize_image_view=resize_image_view,
                    callback_func=self.show_progress_detail,
                    event=self.event,
                    cutout_mode=cutout_mode,
                    logo_path=logo_path,
                )

                if "code" in result and result['code'] != 0:
                    self.show_progress_detail(result['msg'])
                else:
                    self.show_progress_detail('处理完成')

            except BaseException as e:
                print(e)
                self.show_progress_detail('程序异常')

        self.set_state(state_value=2)

    def show_progress_detail(self, text):
        self.text_show.emit(text)

    def append_text_to_browser(self, text):
        self.ui.textBrowser_4.append(text)

    def run_by_thread(self):
        image_dir = "{}/data".format(os.getcwd())
        baseDealImage = BaseDealImage(image_dir=image_dir)
        # result = baseDealImage.dealMoveImage(
        #     image_dir=settings.PhotoOutputDir, callback_func=self.show_progress_detail
        # )
        result = baseDealImage.dealMoveImage(
            image_dir=image_dir, callback_func=self.show_progress_detail
        )
        return result

    def run_by_thread_backup(self):
        if not self.check():
            return

        # 遍历目标文件夹，获取有拍摄信息的图片，并按拍摄时间排序
        files = os.listdir(self.image_dir)
        _Type = [".png", ".PNG", ".jpg", ".JPG", ".gif", ".GIF", ".jpge", ".JPGE"]
        original_photo_list = []  # 原始图片列表
        for file in files:
            # -----图片清洗
            file_path = self.image_dir + "/" + file
            if os.path.isdir(file_path):  # 忽略文件夹
                continue
            file_name, e = os.path.splitext(file)
            if e not in _Type:  # 非图片进行移除
                shutil.move(file_path, self.image_dir + "/历史/" + file)
                continue

            date_time_original = self.get_date_time_original(
                file_path
            )  # 获取照片拍照时间
            if date_time_original:
                # 基于照片的时间，与数据库匹配goods_art_no
                _data = self.data_match_photo.get_goods_art_no(date_time_original)

                if _data:
                    # 能匹配上数据库
                    goods_art_no, image_index, image_deal_mode = _data
                    print(
                        "与数据库匹配goods_art_no",
                        file_name,
                        date_time_original,
                        goods_art_no,
                    )
                    original_photo_list.append(
                        {
                            "file_path": file_path,
                            "file": file,
                            "date_time_original": date_time_original,
                            "goods_art_no": goods_art_no,
                            "image_index": image_index,
                            "real_goods_art_no": "",
                            "real_goods_number": "",
                        }
                    )
                else:
                    # 匹配不上报错
                    self.show_progress_detail(
                        "图片：{} 无法对应货号，不做处理".format(file)
                    )
                    # shutil.move(photo_dict["file_path"], self.image_dir + "/历史/" + photo_dict["file"])
                    continue
            else:
                shutil.move(file_path, self.image_dir + "/历史/" + file)

        if not original_photo_list:
            self.show_progress_detail("没有任何匹配的图片~")
            self.set_state(state_value=2)
            return

        if settings.PROJECT == "红蜻蜓":
            # 批量请求货号图信息
            goods_art_no_list = [x["goods_art_no"] for x in original_photo_list]
            goods_art_no_list = list(set(goods_art_no_list))
            goods_art_no_list = [x for x in goods_art_no_list if "NUM" not in x]

            if goods_art_no_list:
                goods_art_no_dict = (
                    self.data_match_photo.get_data_from_hqt_with_goods_art_no(
                        goods_art_no_list=goods_art_no_list
                    )
                )

                for i in original_photo_list:
                    if i["goods_art_no"] in goods_art_no_dict:
                        i["real_goods_art_no"] = i["goods_art_no"]
                        i["real_goods_number"] = "NUM{}".format(
                            goods_art_no_dict[i["goods_art_no"]]["编号"]
                        )

            # 批量请求编号对应信息
            goods_number_list = [x["goods_art_no"] for x in original_photo_list]
            goods_number_list = list(set(goods_number_list))
            goods_number_list = [x for x in goods_number_list if "NUM" in x]

            if goods_number_list:
                goods_number_dict = self.data_match_photo.get_data_from_hqt(
                    goods_number_list=goods_number_list
                )

                for i in original_photo_list:
                    if i["goods_art_no"] in goods_number_dict:
                        i["real_goods_number"] = i["goods_art_no"]
                        i["real_goods_art_no"] = goods_number_dict[i["goods_art_no"]][
                            "商品货号"
                        ]

        # 排序需要基于拍照的文件序号进行处理
        original_photo_list.sort(
            key=lambda x: "{}-{}-{}".format(
                x["goods_art_no"], x["image_index"], x["file"]
            )
        )
        print(original_photo_list)

        # 对有拍摄信息的图片进行数据库比对，如有比对上，则移动至货号文件夹，否则移入历史文件夹
        total_num = len(original_photo_list)
        # 当天日期作为文件夹
        seconds = time.time()
        output_path = "output/{f_name}".format(
            f_name=time.strftime("%Y-%m-%d", time.localtime(seconds))
        )

        # 遍历每个匹配好的数据进行处理
        n = 0
        for photo_dict in original_photo_list:
            n += 1
            # 进度条
            goods_art_no = photo_dict["goods_art_no"]
            original_image_path = photo_dict["file_path"]
            # 输出货号文件夹
            if photo_dict["real_goods_art_no"]:
                goods_art_no = "{}@{}".format(
                    photo_dict["real_goods_art_no"], photo_dict["real_goods_number"]
                )

            goods_art_no_path = "{output_path}/{goods_art_no}".format(
                output_path=output_path, goods_art_no=goods_art_no
            )

            # 创建货号下的一系列文件夹
            self.create_folder(goods_art_no_path)

            # 重命名并进行移动
            self.move_images(
                goods_art_no, goods_art_no_path, original_image_path
            )  # 货号、货号文件路径、原始图路径

            self.progress_sign.emit(
                {"type": "移动原始图片", "progress_bar_value": int(n / total_num * 100)}
            )
            self.show_progress_detail(
                "货号{} 相关文件夹创建完成,已移动原图~".format(goods_art_no)
            )

        if n != 0:
            # if settings.MattingPics:
            #     # 检查所有未处理的货号文件夹，查看是否有完成图片加工处理
            #     self.deal_images()

            # 自动生成一个货号表
            print("output_path", output_path)
            GenerateGoodsArtNoTable.deal(output_path)

        # 完成处理
        self.set_state(state_value=2)

    def move_images(self, goods_art_no, goods_art_no_path, old_image_path):
        """
        步骤：
        1、移动到原始图
        Args:
            goods_art_no:
            goods_art_no_path:
            old_image_path:

        Returns:

        """
        # 移动到原始图
        file = os.path.split(old_image_path)[1]
        # 扩展名
        e = os.path.splitext(file)[1]
        # 获取图片序列
        self.goods_images_count_dict[goods_art_no] += 1
        # A9999（1）.jpg
        new_file_name = "{}（{}）".format(
            goods_art_no, self.goods_images_count_dict[goods_art_no]
        )
        original_image_path = "{}/原始图/{}{}".format(
            goods_art_no_path, new_file_name, e
        )
        # 移动图片
        shutil.move(old_image_path, original_image_path)

    def deal_images(self):
        """
        2、压缩并上传平台获取抠图
        3、抠图处理成白底图
        4、做成800*800/200*200
        :return:
        """
        output_path = "output"
        """
        扫描文档，检查有哪些需要进行抠图等处理
        """
        to_do_images_total = 0
        for _date_folder in os.listdir(output_path):
            if not os.path.isdir("{}/{}".format(output_path, _date_folder)):
                continue
            for goods_art_no_folder in os.listdir(
                    "{}/{}".format(output_path, _date_folder)
            ):
                # print(goods_art_no_folder)
                all_original_images = os.listdir(
                    "{}/{}/{}/原始图".format(
                        output_path, _date_folder, goods_art_no_folder
                    )
                )
                all_moved_images = [
                    os.path.splitext(x)[0]
                    for x in os.listdir(
                        "{}/{}/{}/原始图_已抠图".format(
                            output_path, _date_folder, goods_art_no_folder
                        )
                    )
                ]
                for file in all_original_images:
                    if os.path.splitext(file)[0] not in all_moved_images:
                        print("----------》", goods_art_no_folder, file)
                        to_do_images_total += 1

        if to_do_images_total > 0:
            self.progress_sign.emit(
                {"type": "处理图片 抠图、加工等", "progress_bar_value": 0}
            )
        else:
            return True

        """开始处理抠图等处理"""
        n = 0
        for _date_folder in os.listdir(output_path):
            # 过滤非文件夹的文件
            if not os.path.isdir("{}/{}".format(output_path, _date_folder)):
                continue

            # 扫描每个货号
            for goods_art_no_folder in os.listdir(
                    "{}/{}".format(output_path, _date_folder)
            ):
                all_original_images = os.listdir(
                    "{}/{}/{}/原始图".format(
                        output_path, _date_folder, goods_art_no_folder
                    )
                )
                all_moved_images = [
                    os.path.splitext(x)[0]
                    for x in os.listdir(
                        "{}/{}/{}/原始图_已抠图".format(
                            output_path, _date_folder, goods_art_no_folder
                        )
                    )
                ]
                for file in all_original_images:
                    original_image_path = "{}/{}/{}/原始图/{}".format(
                        output_path, _date_folder, goods_art_no_folder, file
                    )
                    file_name = os.path.splitext(file)[0]
                    if file_name not in all_moved_images:
                        print("上传平台获取抠图结果----------》", file)
                        goods_art_no_folder_path = "{}/{}/{}".format(
                            output_path, _date_folder, goods_art_no_folder
                        )
                        pic_deal = PicDeal()
                        # 上传平台获取抠图结果
                        original_move_bg_image_path = "{}/原始图_已抠图/{}{}".format(
                            goods_art_no_folder_path, file_name, ".png"
                        )
                        if pic_deal.remove_bg(
                                in_path=original_image_path,
                                out_path=original_move_bg_image_path,
                        ):
                            self.show_progress_detail(
                                "货号图{} 已完成抠图处理~".format(file_name)
                            )
                            # 抠图处理成白底图 800*800
                            out_800_path = "{}/800x800/{}{}".format(
                                goods_art_no_folder_path, file_name, ".jpg"
                            )
                            pic_deal.create_800image(
                                image_path=original_move_bg_image_path,
                                out_path=out_800_path,
                            )
                            # 抠图处理成白底图 200*200
                            out_200_path = "{}/200images/{}{}".format(
                                goods_art_no_folder_path, file_name, ".jpg"
                            )
                            pic_deal.resize_and_save(
                                image_path=out_800_path,
                                out_path=out_200_path,
                                width=200,
                            )
                            self.show_progress_detail(
                                "货号图{} 已完成800*800图片和200*200图片制作~".format(
                                    file_name
                                )
                            )
                        else:
                            self.show_progress_detail(
                                "货号图{} 抠图处理失败~".format(file_name)
                            )
                        # 完成处理的图片进度
                        n += 1
                        self.progress_sign.emit(
                            {
                                "type": "处理图片 抠图、加工等",
                                "progress_bar_value": int(n / to_do_images_total * 100),
                            }
                        )

    def create_folder(self, path):
        def check_folder(__path):
            if not os.path.exists(__path):
                os.makedirs(__path)
                return False
            return True

        # 文件夹不存在，创建货号子集文件夹
        if not check_folder(path):
            for name in ["原始图", "原始图_已抠图", "800x800", "200images"]:
                other_path = path + "/" + name
                check_folder(other_path)

    def get_date_time_original(self, file_path):
        with open(file_path, "rb") as file_data:
            tags = exifread.process_file(file_data)
            if "EXIF DateTimeOriginal" in tags:
                return str(tags["EXIF DateTimeOriginal"])
            else:
                return False

    def on_dialog_result(self, result):
        """处理对话框结果"""
        self.dialog_result = result
        print("1077  处理对话框结果:{}".format(result))
        # self.quit()  # 结束事件循
        self.event_dialog.set()

    # 弹出对话框
    def show_dialog(self, data):
        windows = data["windows"]
        windows.dialog_result = ""

        my_dialog = DialogShow(self, text=data["text"],
                               button_1=data["button_1"] if "button_1" in data else None,
                               button_2=data["button_2"] if "button_2" in data else None,
                               button_3=data["button_3"] if "button_3" in data else None, )
        ret = my_dialog.exec()
        print("632  my_dialog.flag_name", my_dialog.flag_name)
        # 根据用户的选择发出相应的信号
        windows.communicator.dialog_result_signal.emit(my_dialog.flag_name)


class TempItem(QWidget):
    select_sign = Signal(str)

    def __init__(self, parent, _id, image_path):
        super().__init__(parent)
        self.ui = UI_temp_item()
        self.ui.setupUi(self)
        self.image_path = image_path
        self.id = _id
        self.is_select = False
        self.init()

    def init(self):
        self.ui.label.mousePressEvent = self.pic_show
        self.ui.radioButton.clicked.connect(self.select)
        self.set_label_image()

    def cancel_select(self, *args):
        self.is_select = False
        self.ui.radioButton.setChecked(False)

    def select(self, *args):
        self.is_select = True
        self.ui.radioButton.setChecked(True)
        self.select_sign.emit(self.id)

    def pic_show(self, *args, **kwargs):
        if os.path.exists(self.image_path):
            im = Image.open(self.image_path)
            threading.Thread(target=im.show, args=()).start()

    def set_label_image(self):
        if not os.path.exists(self.image_path):
            return
        try:
            bytes_io = io.BytesIO()
            w, h = self.ui.image_show.width(), self.ui.image_show.height()
            _img_raw = Image.open(self.image_path)
            _img_raw = self.to_resize(_img_raw, width=w * 2)
            _img_raw = _img_raw.crop(box=(0, 0, w * 2, h * 2))
            # _img_raw.thumbnail((w * 2, int(_img_raw.height * w * 2 / _img_raw.width)))
            # img_raw = _img_raw.resize(size=(w, h))
            _img_raw.save(bytes_io, "JPEG")
            icon = QPixmap()
            # icon.loadFromData(img)
            icon.loadFromData(bytes_io.getvalue())
            icon = icon.scaled(
                self.ui.image_show.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation
            )
            self.ui.image_show.setPixmap(icon)
            # self.mousePressEvent = lambda x: self.show_one_data(row)
        except:
            pass

    def to_resize(self, _im, width=None, high=None):
        _im_x, _im_y = _im.size
        if width and high:
            if _im_x >= _im_y:
                high = None
            else:
                width = None
        if width:
            re_x = int(width)
            re_y = int(_im_y * re_x / _im_x)
        else:
            re_y = int(high)
            re_x = int(_im_x * re_y / _im_y)
        _im = _im.resize((re_x, re_y))
        return _im
