#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：TrayConfigModel.py
@Description：
@Author：XSH
@Time：2025/11/11 10:00
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5 import Qt
from datetime import datetime, date
import copy
from AppSettings.Settings import *
from AppCore.AppDBModels.CoreModel import (CoreModel, DBManager, dbController)
from sqlalchemy.orm import declarative_base, mapper, sessionmaker, Session
from sqlalchemy.orm import (declarative_base, Mapped, mapped_column, relationship, mapper, sessionmaker)
from sqlalchemy import Table, Engine, create_engine, Column, ForeignKey, and_, or_, insert, delete, update, select, func, ColumnElement,distinct
from sqlalchemy.types import (Text, VARCHAR, REAL, Float, Integer, String, SmallInteger, DateTime, Date, ARRAY,Boolean, JSON)
from sqlalchemy.orm.decl_api import DeclarativeBase, MetaData
from AppUtils.utils import *
from AppCore.Levels import *


class TrayConfigModel(CoreModel):
    __tablename__ = 't_TrayConfigModel'
    Information                     : Mapped[str    ] = mapped_column(Text, default=''      , nullable=True , doc='　　说明信息　　　　', comment='　　说明信息　　　　')
    ID                              : Mapped[int    ] = mapped_column(primary_key=True      , nullable=False, doc='　　数据ＩＤ　　　　', comment='　　数据ＩＤ　　　　')
    Position_X                      : Mapped[float  ] = mapped_column(default=147.8         , nullable=False, doc='　　放片Ｘ位置　　　', comment='　　放片Ｘ位置　　　')
    Position_Y                      : Mapped[float  ] = mapped_column(default=16.0          , nullable=False, doc='　　放片Ｙ位置　　　', comment='　　放片Ｙ位置　　　')
    Position_Z                      : Mapped[float  ] = mapped_column(default=10000000.0    , nullable=False, doc='　　托盘Ｚ位置　　　', comment='　　托盘Ｚ位置　　　')
    SlideSpacing                    : Mapped[float  ] = mapped_column(default=30.0          , nullable=False, doc='　　托盘玻片间距　　', comment='　　托盘玻片间距　　')
    SlideWidth                      : Mapped[float  ] = mapped_column(default=23.0          , nullable=False, doc='　　玻片宽度　　　　', comment='　　玻片宽度　　　　')
    SlideHeight                     : Mapped[float  ] = mapped_column(default=41.00         , nullable=False, doc='　　玻片高度　　　　', comment='　　玻片高度　　　　')
    Step_X                          : Mapped[float  ] = mapped_column(default=1.0           , nullable=False, doc='Ｘ轴移动步长１０Ｘ　', comment='Ｘ轴移动步长１０Ｘ　')
    Step_Y                          : Mapped[float  ] = mapped_column(default=1.0           , nullable=False, doc='Ｙ轴移动步长１０Ｘ　', comment='Ｙ轴移动步长１０Ｘ　')
    Step_X_20X                      : Mapped[float  ] = mapped_column(default=0.7           , nullable=False, doc='Ｘ轴移动步长２０Ｘ　', comment='Ｘ轴移动步长２０Ｘ　')
    Step_Y_20X                      : Mapped[float  ] = mapped_column(default=0.7           , nullable=False, doc='Ｙ轴移动步长２０Ｘ　', comment='Ｙ轴移动步长２０Ｘ　')
    Step_X_40X                      : Mapped[float  ] = mapped_column(default=0.7           , nullable=False, doc='Ｘ轴移动步长４０Ｘ　', comment='Ｘ轴移动步长４０Ｘ　')
    Step_Y_40X                      : Mapped[float  ] = mapped_column(default=0.7           , nullable=False, doc='Ｙ轴移动步长４０Ｘ　', comment='Ｙ轴移动步长４０Ｘ　')
    Step_X_100X                     : Mapped[float  ] = mapped_column(default=0.7           , nullable=False, doc='Ｘ轴移动步长１００Ｘ', comment='Ｘ轴移动步长１００Ｘ')
    Step_Y_100X                     : Mapped[float  ] = mapped_column(default=0.7           , nullable=False, doc='Ｙ轴移动步长１００Ｘ', comment='Ｙ轴移动步长１００Ｘ')

    def __init__(self, **args):
        super(TrayConfigModel, self).__init__()
        self.initialize()
        self.initParameter(**args)

    def initParameter(self, **args):
        super(TrayConfigModel, self).initParameter(**args)


    def initValue(self):
        self.Information                      = '托盘参数'       #
        self.ID                               = 1               #
        self.Position_X                       = 147.8           #
        self.Position_Y                       = 16.0            #
        self.Position_Z                       = 10000000.0      #
        self.SlideSpacing                     = 30.0            #
        self.SlideWidth                       = 23.0            #
        self.SlideHeight                      = 41.00           #
        self.Step_X                           = 1.0             #
        self.Step_Y                           = 1.0             #
        self.Step_X_20X                       = 0.7             #
        self.Step_Y_20X                       = 0.7             #
        self.Step_X_40X                       = 0.7             #
        self.Step_Y_40X                       = 0.7             #
        self.Step_X_100X                      = 0.7             #
        self.Step_Y_100X                      = 0.7             #




    @classmethod
    def initModels(cls):
        return [
            cls.instance(Information                      = '托盘参数' ,ID                               = 1          ,Position_X                       = 147.8      ,Position_Y                       = 16.0       ,Position_Z                       = 10000000.0 ,SlideSpacing                     = 30.0       ,SlideWidth                       = 23.0       ,SlideHeight                      = 41.00      ,Step_X                           = 1.0        ,Step_Y                           = 1.0        ,Step_X_20X                       = 0.7        ,Step_Y_20X                       = 0.7        ,Step_X_40X                       = 0.7        ,Step_Y_40X                       = 0.7        ,Step_X_100X                      = 0.7        ,Step_Y_100X                      = 0.7        )
        ]
    @classmethod
    def Instance(cls, level: int, **args):
        result: cls = cls.__new__(cls)
        result.__init__()
        result.setLevel(level)
        result.initParameter(**args)
        return result

    @classmethod
    def setPosition_xyz(cls, x, y, z):
        if isinstance(x, (int, float)) and isinstance(y, (int, float)) and isinstance(z, (int, float)):
            cls.changed({
                cls.Position_X : float(x),
                cls.Position_Y : float(y),
                cls.Position_Z : float(z),
            })

    @classmethod
    def getPosition_xyz(cls) -> (float, float, float):
        item: cls = cls.first()
        return item.GetPosition_X, item.GetPosition_Y, item.GetPosition_Z

    @classmethod
    def getPosition_xy(cls) -> (float, float):
        item: cls = cls.first()
        return item.GetPosition_X, item.GetPosition_Y

    @classmethod
    def getSlideSize(cls) -> (float, float):
        item: cls = cls.first()
        return item.GetSlideWidth, item.GetSlideHeight


    @classmethod
    def getStep(cls, level: int) -> (float, float):
        item: cls = cls.first()
        x: float
        y: float

        if level == ObjectiveLevel.Objective1:
            x, y = item.GetStep_X, item.GetStep_Y
        elif level == ObjectiveLevel.Objective10:
            x, y = item.GetStep_X, item.GetStep_Y
        elif level == ObjectiveLevel.Objective20:
            x, y = item.GetStep_X_20X, item.GetStep_Y_20X
        elif level == ObjectiveLevel.Objective40:
            x, y = item.GetStep_X_40X, item.GetStep_Y_40X
        elif level == ObjectiveLevel.Objective63:
            x, y = item.GetStep_X_100X, item.GetStep_Y_100X
        elif level == ObjectiveLevel.Objective100:
            x, y = item.GetStep_X_100X, item.GetStep_Y_100X
        else:
            x, y = item.GetStep_X, item.GetStep_Y
        return x, y


    @classmethod
    def setPosition_X      (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Position_X   : float(value)})

    @classmethod
    def setPosition_Y      (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Position_Y   : float(value)})

    @classmethod
    def setPosition_Z      (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Position_Z   : float(value)})

    @classmethod
    def setSlideSpacing    (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.SlideSpacing : float(value)})

    @classmethod
    def setSlideWidth      (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.SlideWidth   : float(value)})

    @classmethod
    def setSlideHeight     (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.SlideHeight  : float(value)})

    @classmethod
    def setStep_X          (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Step_X       : float(value)})

    @classmethod
    def setStep_Y          (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Step_Y       : float(value)})

    @classmethod
    def setStep_X_20X      (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Step_X_20X   : float(value)})

    @classmethod
    def setStep_Y_20X      (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Step_Y_20X   : float(value)})

    @classmethod
    def setStep_X_40X      (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Step_X_40X   : float(value)})

    @classmethod
    def setStep_Y_40X      (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Step_Y_40X   : float(value)})

    @classmethod
    def setStep_X_100X    (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Step_X_100X : float(value)})

    @classmethod
    def setStep_Y_100X    (cls, value: (int, float)):
        if isinstance(value, (int, float)):
            cls.changed({cls.Step_Y_100X : float(value)})


    @classmethod
    def getPosition_X   (cls) -> float:
        item: cls = cls.first()
        return item.GetPosition_X         #

    @classmethod
    def getPosition_Y   (cls) -> float:
        item: cls = cls.first()
        return item.GetPosition_Y         #

    @classmethod
    def getPosition_Z   (cls) -> float:
        item: cls = cls.first()
        return item.GetPosition_Z         #

    @classmethod
    def getSlideSpacing (cls) -> float:
        item: cls = cls.first()
        return item.GetSlideSpacing       #

    @classmethod
    def getSlideWidth   (cls) -> float:
        item: cls = cls.first()
        return item.GetSlideWidth         #

    @classmethod
    def getSlideHeight  (cls) -> float:
        item: cls = cls.first()
        return item.GetSlideHeight        #

    @classmethod
    def getStep_X       (cls) -> float:
        item: cls = cls.first()
        return item.GetStep_X             #

    @classmethod
    def getStep_Y       (cls) -> float:
        item: cls = cls.first()
        return item.GetStep_Y             #

    @classmethod
    def getStep_X_20X   (cls) -> float:
        item: cls = cls.first()
        return item.GetStep_X_20X         #

    @classmethod
    def getStep_Y_20X   (cls) -> float:
        item: cls = cls.first()
        return item.GetStep_Y_20X         #

    @classmethod
    def getStep_X_40X   (cls) -> float:
        item: cls = cls.first()
        return item.GetStep_X_40X         #

    @classmethod
    def getStep_Y_40X   (cls) -> float:
        item: cls = cls.first()
        return item.GetStep_Y_40X         #

    @classmethod
    def getStep_X_100X (cls) -> float:
        item: cls = cls.first()
        return item.GetStep_X_100X       #

    @classmethod
    def getStep_Y_100X (cls) -> float:
        item: cls = cls.first()
        return item.GetStep_Y_100X       #



    def SetInformation                      (self, value: str         ):
        if isinstance(value, str         ):
            self.Information                      = f'{value}'

    def SetID                               (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.ID                               = int(value)

    def SetPosition_X                       (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Position_X                       = float(value)

    def SetPosition_Y                       (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Position_Y                       = float(value)

    def SetPosition_Z                       (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Position_Z                       = float(value)

    def SetSlideSpacing                     (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.SlideSpacing                     = float(value)

    def SetSlideWidth                       (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.SlideWidth                       = float(value)

    def SetSlideHeight                      (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.SlideHeight                      = float(value)

    def SetStep_X                           (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Step_X                           = float(value)

    def SetStep_Y                           (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Step_Y                           = float(value)

    def SetStep_X_20X                       (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Step_X_20X                       = float(value)

    def SetStep_Y_20X                       (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Step_Y_20X                       = float(value)

    def SetStep_X_40X                       (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Step_X_40X                       = float(value)

    def SetStep_Y_40X                       (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Step_Y_40X                       = float(value)

    def SetStep_X_100X                     (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Step_X_100X                     = float(value)

    def SetStep_Y_100X                     (self, value: (int, float)):
        if isinstance(value, (int, float)):
            self.Step_Y_100X                     = float(value)


    @property
    def GetInformation                    (self) -> str    :
        return None if not self.hasInformation                     else self.Information

    @property
    def GetID                             (self) -> int    :
        return None if not self.hasID                              else self.ID

    @property
    def GetPosition_X                     (self) -> float  :
        return None if not self.hasPosition_X                      else self.Position_X

    @property
    def GetPosition_Y                     (self) -> float  :
        return None if not self.hasPosition_Y                      else self.Position_Y

    @property
    def GetPosition_Z                     (self) -> float  :
        return None if not self.hasPosition_Z                      else self.Position_Z

    @property
    def GetSlideSpacing                   (self) -> float  :
        return None if not self.hasSlideSpacing                    else self.SlideSpacing

    @property
    def GetSlideWidth                     (self) -> float  :
        return None if not self.hasSlideWidth                      else self.SlideWidth

    @property
    def GetSlideHeight                    (self) -> float  :
        return None if not self.hasSlideHeight                     else self.SlideHeight

    @property
    def GetStep_X                         (self) -> float  :
        return None if not self.hasStep_X                          else self.Step_X

    @property
    def GetStep_Y                         (self) -> float  :
        return None if not self.hasStep_Y                          else self.Step_Y

    @property
    def GetStep_X_20X                     (self) -> float  :
        return None if not self.hasStep_X_20X                      else self.Step_X_20X

    @property
    def GetStep_Y_20X                     (self) -> float  :
        return None if not self.hasStep_Y_20X                      else self.Step_Y_20X

    @property
    def GetStep_X_40X                     (self) -> float  :
        return None if not self.hasStep_X_40X                      else self.Step_X_40X

    @property
    def GetStep_Y_40X                     (self) -> float  :
        return None if not self.hasStep_Y_40X                      else self.Step_Y_40X

    @property
    def GetStep_X_100X                   (self) -> float  :
        return None if not self.hasStep_X_100X                    else self.Step_X_100X

    @property
    def GetStep_Y_100X                   (self) -> float  :
        return None if not self.hasStep_Y_100X                    else self.Step_Y_100X


    __field__ = (
        'Information'  ,
        'ID'           ,
        'Position_X'   ,
        'Position_Y'   ,
        'Position_Z'   ,
        'SlideSpacing' ,
        'SlideWidth'   ,
        'SlideHeight'  ,
        'Step_X'       ,
        'Step_Y'       ,
        'Step_X_20X'   ,
        'Step_Y_20X'   ,
        'Step_X_40X'   ,
        'Step_Y_40X'   ,
        'Step_X_100X'  ,
        'Step_Y_100X'  ,
    )
    __types__ = (
        str    ,
        int    ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
    )
    @property
    def hasInformation    (self) -> bool:
        return hasattr(self, 'Information'  ) and isinstance(self.Information     , str    )

    @property
    def hasID             (self) -> bool:
        return hasattr(self, 'ID'           ) and isinstance(self.ID              , int    )

    @property
    def hasPosition_X     (self) -> bool:
        return hasattr(self, 'Position_X'   ) and isinstance(self.Position_X      , (int, float))

    @property
    def hasPosition_Y     (self) -> bool:
        return hasattr(self, 'Position_Y'   ) and isinstance(self.Position_Y      , (int, float))

    @property
    def hasPosition_Z     (self) -> bool:
        return hasattr(self, 'Position_Z'   ) and isinstance(self.Position_Z      , (int, float))

    @property
    def hasSlideSpacing   (self) -> bool:
        return hasattr(self, 'SlideSpacing' ) and isinstance(self.SlideSpacing    , (int, float))

    @property
    def hasSlideWidth     (self) -> bool:
        return hasattr(self, 'SlideWidth'   ) and isinstance(self.SlideWidth      , (int, float))

    @property
    def hasSlideHeight    (self) -> bool:
        return hasattr(self, 'SlideHeight'  ) and isinstance(self.SlideHeight     , (int, float))

    @property
    def hasStep_X         (self) -> bool:
        return hasattr(self, 'Step_X'       ) and isinstance(self.Step_X          , (int, float))

    @property
    def hasStep_Y         (self) -> bool:
        return hasattr(self, 'Step_Y'       ) and isinstance(self.Step_Y          , (int, float))

    @property
    def hasStep_X_20X     (self) -> bool:
        return hasattr(self, 'Step_X_20X'   ) and isinstance(self.Step_X_20X      , (int, float))

    @property
    def hasStep_Y_20X     (self) -> bool:
        return hasattr(self, 'Step_Y_20X'   ) and isinstance(self.Step_Y_20X      , (int, float))

    @property
    def hasStep_X_40X     (self) -> bool:
        return hasattr(self, 'Step_X_40X'   ) and isinstance(self.Step_X_40X      , (int, float))

    @property
    def hasStep_Y_40X     (self) -> bool:
        return hasattr(self, 'Step_Y_40X'   ) and isinstance(self.Step_Y_40X      , (int, float))

    @property
    def hasStep_X_100X   (self) -> bool:
        return hasattr(self, 'Step_X_100X' ) and isinstance(self.Step_X_100X    , (int, float))

    @property
    def hasStep_Y_100X   (self) -> bool:
        return hasattr(self, 'Step_Y_100X' ) and isinstance(self.Step_Y_100X    , (int, float))
