#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：AppendScanTemplateBusiness.py
@Description：
@Author：XSH
@Time：2025/12/2 17:24
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5 import Qt
from abc import abstractmethod, abstractclassmethod, abstractstaticmethod
from AppInterfaces.utils                    import *
from AppSettings.utils                      import *
from AppCore.Requests                       import *
from AppInterfaces.Interfaces               import *
from AppCore.Levels                         import *
from AppUtils.utils                         import *
from AppBackEnd.WebBusinessInterface import WebBusinessInterface

class AppendScanTemplateBusiness(Qt.QObject, WebBusinessInterface):

    def __init__(self, manager: WebReaderInterface = None, parent: Qt.QObject = None):
        super(AppendScanTemplateBusiness, self).__init__(parent=parent)
        self.Manager = manager
        self.Initialization("AppendScanTemplateBusiness", self, parent)


    def initWEBAPILevel(self):
        self.Level = WebAPILevel.pushTemplate


    def Done(self, handler: WebClientInterface, **args):
        from AppCore.DBModels import ScanTemplate
        try:
            self.Handler = handler
            item: ScanTemplate
            items: list[ScanTemplate] = ScanTemplate.all()
            if not isinstance(items, (list, tuple)):
                return
            data: list[dict[str, object]] = [item.serialize() for item in items]
            request: Response = Response.APIInstance(self.Level)
            request.data = data
            return self.WebSendFrame(request.toJson())
        except Exception as e:
            AppUtils.print_Exception(e)
        return



    def WaitDone(self, handler: WebClientInterface, **args) -> int:
        isWrong: int = RECODE_FAILURE
        try:
            self.Handler = handler
            item: ScanTemplate
            request: Response = Response.APIInstance(self.Level)
            items: list[ScanTemplate] = ScanTemplate.all()
            if items is None:
                request.data = []
            else:
                request.data = [item.serialize() for item in items]
            isWrong = self.WebSendFrame(request.toJson())
        except Exception as e:
            AppUtils.print_Exception(e)
        return isWrong

