#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：DataBaseDeploy.py\n
 @DateTime: 2025/6/24 9:38\n
 @Author  ：xsh
"""
from PyQt5 import Qt
from decimal import Decimal
from datetime import datetime, date
from AppCore.Levels import *
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 AppCore.AppDBModels.DBModels import *



class DataBaseDeploy:


    @classmethod
    def init(cls):
        cls.initUsers                   ()
        cls.initClassifiers             ()
        cls.initConfig                  ()
        cls.initTray                    ()
        cls.initCamera                  ()
        cls.initScanningTemplate        ()
        cls.initScanningStatus          ()
        from AppSettings.SystemSetting import SystemConfig
        SystemConfig.UpdateMaxLocation()
        cls.initSlide                   ()

        # cls.initHitBot                  ()

    @classmethod
    def initUsers(cls):
        cls.initGroupModel                                      ()
        cls.initRoleModel                                       ()
        cls.initUserModel                                       ()

    @classmethod
    def initClassifiers(cls):
        cls.initPreviewClassifierModel                          ()
        cls.initPreferredClassifierModel                        ()
        cls.initCollectClassifierModel                          ()

    @classmethod
    def initConfig(cls):
        cls.initAnointingModel                                  ()
        cls.initAxisVelocityModel                               ()
        cls.initFocusingConfigModel                             ()
        cls.initGripperConfigModel                              ()
        cls.initImageAnalysisModel                              ()
        cls.initImageConfigModel                                ()
        cls.initIntegrationExposureTimes                        ()
        cls.initObjectiveModel                                  ()
        cls.initSystemConfigModel                               ()
        cls.initChamberConfigModel                              ()
        cls.initObjectiveOffsetModel                            ()
        # cls.initHigObjectiveCellModel                           ()
        # cls.initHitBotConfigModel                               ()


    @classmethod
    def initScanningTemplate(cls):
        cls.initCollectsettings                                 ()
        cls.initPreviewsettings                                 ()
        cls.initScanTemplate                                    ()

    @classmethod
    def initSlideTable(cls):
        cls.initTrayModel                                       ()

    @classmethod
    def initTray(cls):
        cls.initTrayConfigModel()
        cls.initTrayModel()

    @classmethod
    def initScanningStatus(cls):
        cls.initOperatingStatusModel                            ()
        cls.initScanStatusModel                                 ()
        cls.initCustomScanningModel                             ()

    @classmethod
    def initSlide(cls):
        cls.initAllCellModel                                    ()
        cls.initAlreadyScannedModel                             ()
        cls.initCellModel                                       ()
        cls.initChamberStatusModel                              ()
        cls.initSlideCaseModel                                  ()
        cls.initSlideFocusingModel                              ()
        cls.initSlideLoggerModel                                ()
        cls.initSlideModel                                      ()
        cls.initTraySlideModel                                  ()
        cls.initSpecimenCountModel                              ()

    @classmethod
    def initHitBot(cls):
        pass
        """##################################################机械臂动作表######################################################"""
        cls.initHitBotBrokenDropAction      ()
        cls.initHitBotPickErrorAction       ()
        cls.initHitBotChamberDropAction     ()
        cls.initHitBotChamberPickAction     ()
        cls.initHitBotCheckChamberAction    ()
        cls.initHitBotCheckSlideLAction     ()
        cls.initHitBotCheckSlideRAction     ()
        cls.initHitBotCodeScanAction        ()
        cls.initHitBotDropSlideAction       ()
        cls.initHitBotLoggerModel           ()
        cls.initHitBotModel                 ()
        cls.initHitBotSafeAction            ()
        cls.initHitBotTrayDropAction        ()
        cls.initHitBotTrayPickAction        ()


    @classmethod
    def initCamera(cls):
        cls.initCameraModel                     ()
        cls.initCameraColorModel                ()
        cls.initColorAdjustmentSelector         ()


    """##################################################账号管理表######################################################"""
    @classmethod
    def initUserModel                           (cls):
        if UserModel.has_limit(3):
            return
        UserModel.create_table()
        UserModel.clear()
        UserModel.append_all(UserModel.initModels())
    @classmethod
    def initGroupModel                          (cls):
        if GroupModel.has_limit(3):
            return
        GroupModel.create_table()
        GroupModel.clear()
        GroupModel.append_all(GroupModel.initModels())
    @classmethod
    def initRoleModel                           (cls):
        if RoleModel.has_limit(3):
            return
        RoleModel.create_table()
        RoleModel.clear()
        RoleModel.append_all(RoleModel.initModels())

    """##################################################AI模型参数表######################################################"""
    @classmethod
    def initPreviewClassifierModel              (cls):
        if PreviewClassifierModel.has_limit(3):
            return
        PreviewClassifierModel.create_table()
        PreviewClassifierModel.clear()
        PreviewClassifierModel.append_all(PreviewClassifierModel.initModels())
    @classmethod
    def initPreferredClassifierModel            (cls):
        if PreferredClassifierModel.has_limit(3):
            return
        PreferredClassifierModel.create_table()
        PreferredClassifierModel.clear()
        PreferredClassifierModel.append_all(PreviewClassifierModel.initModels())
    @classmethod
    def initCollectClassifierModel              (cls):
        if CollectClassifierModel.has_limit(3):
            return
        CollectClassifierModel.create_table()
        CollectClassifierModel.clear()
        CollectClassifierModel.append_all(CollectClassifierModel.initModels())

    """##################################################扫描状态表######################################################"""
    @classmethod
    def initOperatingStatusModel                (cls):
        if OperatingStatusModel .has_limit(1):
            return
        OperatingStatusModel .create_table()
        OperatingStatusModel .clear()
        OperatingStatusModel .append_all(OperatingStatusModel .initModels())
    @classmethod
    def initScanStatusModel                     (cls):
        if ScanStatusModel.has_limit(1):
            return
        ScanStatusModel.create_table()
        ScanStatusModel.clear()
        ScanStatusModel.append_all(ScanStatusModel.initModels())

    """##################################################自定义模板表######################################################"""
    @classmethod
    def initCustomScanningModel                 (cls):
        if CustomScanningModel.has_limit(120):
            return
        CustomScanningModel.create_table()
        CustomScanningModel.clear()
        CustomScanningModel.append_all(CustomScanningModel.initModels())

    """##################################################扫描模板表######################################################"""
    @classmethod
    def initScanTemplate                                    (cls):
        if ScanTemplate                                    .has_limit(3):
            return

        ScanTemplate                                    .create_table()
        ScanTemplate                                    .clear()
        ScanTemplate                                    .append_all(ScanTemplate                                    .initModels())

    @classmethod
    def initCollectsettings                                 (cls):
        if Collectsettings                                 .has_limit(3):
            return
        Collectsettings                                 .create_table()
        Collectsettings                                 .clear()
        Collectsettings                                 .append_all(Collectsettings                                 .initModels())

    @classmethod
    def initPreviewsettings                                 (cls):
        if Previewsettings                                 .has_limit(3):
            return
        Previewsettings                                 .create_table()
        Previewsettings                                 .clear()
        Previewsettings                                 .append_all(Previewsettings                                 .initModels())

    """##################################################载玻台参数表######################################################"""
    @classmethod
    def initTrayModel                                       (cls):
        if TrayModel                                       .has_limit(5):
            return
        TrayModel                                       .create_table()
        TrayModel                                       .clear()
        TrayModel                                       .append_all(TrayModel                                       .initModels())

    @classmethod
    def initTrayConfigModel(cls):
        if TrayConfigModel.has_limit(1):
            return
        TrayConfigModel.create_table()
        TrayConfigModel.clear()
        TrayConfigModel.append_all(TrayConfigModel.initModels())


    """##################################################滴油参数表######################################################"""
    @classmethod
    def initAnointingModel                      (cls):
        if AnointingModel.has_limit(7):
            return
        AnointingModel.create_table()
        AnointingModel.clear()
        AnointingModel.append_all(AnointingModel.initModels())

    """##################################################载玻台XY轴速度表######################################################"""

    @classmethod
    def initAxisVelocityModel                   (cls):
        if AxisVelocityModel.has_limit(8):
            return
        AxisVelocityModel.create_table()
        AxisVelocityModel.clear()
        AxisVelocityModel.append_all(AxisVelocityModel.initModels())

    """##################################################片仓参数表######################################################"""


    """##################################################定焦参数表######################################################"""
    @classmethod
    def initFocusingConfigModel                 (cls):
        if FocusingConfigModel.has_limit(28):
            return
        FocusingConfigModel.create_table()
        FocusingConfigModel.clear()
        FocusingConfigModel.append_all(FocusingConfigModel.initModels())

    """##################################################夹抓参数表######################################################"""
    @classmethod
    def initGripperConfigModel                  (cls):
        if GripperConfigModel.has_limit(1):
            return
        GripperConfigModel.create_table()
        GripperConfigModel.clear()
        GripperConfigModel.append_all(GripperConfigModel.initModels())

    """##################################################图像预处理参数表######################################################"""
    @classmethod
    def initImageAnalysisModel                  (cls):
        if ImageAnalysisModel.has_limit(3):
            return
        ImageAnalysisModel.create_table()
        ImageAnalysisModel.clear()
        ImageAnalysisModel.append_all(ImageAnalysisModel.initModels())

    """##################################################图像尺寸表######################################################"""
    @classmethod
    def initImageConfigModel                    (cls):
        if ImageConfigModel.has_limit(1):
            return
        ImageConfigModel.create_table()
        ImageConfigModel.clear()
        ImageConfigModel.append_all(ImageConfigModel.initModels())

    """##################################################多曝光时间表######################################################"""
    @classmethod
    def initIntegrationExposureTimes            (cls):
        if IntegrationExposureTimes.has_limit(6):
            return
        IntegrationExposureTimes.create_table()
        IntegrationExposureTimes.clear()
        IntegrationExposureTimes.append_all(IntegrationExposureTimes.initModels())

    """##################################################物镜参数######################################################"""
    @classmethod
    def initObjectiveModel                      (cls):
        if ObjectiveModel.has_limit(7):
            return
        ObjectiveModel.create_table()
        ObjectiveModel.clear()
        ObjectiveModel.append_all(ObjectiveModel.initModels())
    @classmethod
    def initObjectiveOffsetModel(cls):
        if ObjectiveOffsetModel.has_limit(7):
            return
        ObjectiveOffsetModel.create_table()
        ObjectiveOffsetModel.clear()
        ObjectiveOffsetModel.append_all(ObjectiveOffsetModel.initModels())
    """##################################################系统参数表######################################################"""
    @classmethod
    def initSystemConfigModel                   (cls):
        if SystemConfigModel.has_limit(1):
            return
        SystemConfigModel.create_table()
        SystemConfigModel.clear()
        SystemConfigModel.append_all(SystemConfigModel.initModels())

    @classmethod
    def initChamberConfigModel(cls):
        if ChamberConfigModel.has_limit(1):
            return
        ChamberConfigModel.create_table()
        ChamberConfigModel.clear()
        ChamberConfigModel.append_all(ChamberConfigModel.initModels())

    @classmethod
    def initSlideSplitModel                     (cls):
        if SlideSplitModel.has_limit(10):
            return
        SlideSplitModel.create_table()
        SlideSplitModel.clear()
        SlideSplitModel.append_all(SlideSplitModel.initModels())

    @classmethod
    def initHitBotConfigModel                   (cls):
        if HitBotConfigModel.has_limit(1):
            return
        HitBotConfigModel.create_table()
        HitBotConfigModel.clear()
        HitBotConfigModel.append_all(HitBotConfigModel.initModels())

    """#######################################################相机参数###################################################"""

    @classmethod
    def initCameraModel(cls):
        if CameraModel.has_limit(7):
            return
        CameraModel.create_table()
        CameraModel.clear()
        CameraModel.append_all(CameraModel.initModels())




    @classmethod
    def initCameraColorModel(cls):
        if CameraColorModel.has_limit(7):
            return
        CameraColorModel.create_table()
        CameraColorModel.clear()
        CameraColorModel.append_all(CameraColorModel.initModels())

    @classmethod
    def initColorAdjustmentSelector(cls):
        modules: list[ColorAdjustmentSelector] = ColorAdjustmentSelector.initModels()
        if ColorAdjustmentSelector.has_limit(len(modules)):
            return
        ColorAdjustmentSelector.create_table()
        ColorAdjustmentSelector.clear()
        ColorAdjustmentSelector.append_all(modules)








    """##################################################玻片数据表######################################################"""
    @classmethod
    def initAllCellModel         (cls):
        if AllCellModel         .has_limit(1):
            return
        AllCellModel         .create_table()
        AllCellModel         .clear()

    @classmethod
    def initAlreadyScannedModel  (cls):
        if AlreadyScannedModel  .has_limit(1):
            return
        AlreadyScannedModel  .create_table()
        AlreadyScannedModel  .clear()

    @classmethod
    def initCellModel            (cls):
        if CellModel            .has_limit(1):
            return
        CellModel            .create_table()
        CellModel            .clear()

    @classmethod
    def initChamberStatusModel   (cls):
        if ChamberStatusModel   .has_limit(120):
            return
        ChamberStatusModel   .create_table()
        ChamberStatusModel   .clear()
        ChamberStatusModel  .append_all(ChamberStatusModel.initModels())
    @classmethod
    def initSlideCaseModel       (cls):
        if SlideCaseModel       .has_limit(1):
            return
        SlideCaseModel       .create_table()
        SlideCaseModel       .clear()

    @classmethod
    def initSlideFocusingModel   (cls):
        if SlideFocusingModel   .has_limit(1):
            return
        SlideFocusingModel   .create_table()
        SlideFocusingModel   .clear()

    @classmethod
    def initSlideScanningModel   (cls):
        if SlideScanningModel   .has_limit(1):
            return
        SlideScanningModel   .create_table()
        SlideScanningModel   .clear()

    @classmethod
    def initSlideLoggerModel     (cls):
        if SlideLoggerModel     .has_limit(1):
            return
        SlideLoggerModel     .create_table()
        SlideLoggerModel     .clear()

    @classmethod
    def initSlideModel           (cls):
        if SlideModel           .has_limit(1):
            return
        SlideModel           .create_table()
        SlideModel           .clear()

    @classmethod
    def initTraySlideModel       (cls):
        if TraySlideModel       .has_limit(5):
            return
        TraySlideModel       .create_table()
        TraySlideModel       .clear()
        TraySlideModel.append_all(TraySlideModel.initModels())

    @classmethod
    def initCellFocusingModel(cls):
        if CellFocusingModel.has_limit(1):
            CellFocusingModel.clear()
            return
        CellFocusingModel.create_table()
        CellFocusingModel.clear()
    @classmethod
    def initSpecimenCountModel(cls):
        if SpecimenCountModel.has_limit(1):
            return
        SpecimenCountModel.create_table()
        SpecimenCountModel.clear()

    """##################################################相机参数表######################################################"""
    @classmethod
    def BayerCameraModel (cls):
        if BayerCameraModel.has_limit(7):
            return
        BayerCameraModel.create_table()
        BayerCameraModel.clear()
        BayerCameraModel.append_all(BayerCameraModel.initModels())

    @classmethod
    def MoonsCameraModel (cls):
        if MonosCameraModel.has_limit(7):
            return
        MonosCameraModel.create_table()
        MonosCameraModel.clear()
        MonosCameraModel.append_all(MonosCameraModel.initModels())

    @classmethod
    def OtherCameraModel (cls):
        if OtherCameraModel.has_limit(7):
            return
        OtherCameraModel.create_table()
        OtherCameraModel.clear()
        OtherCameraModel.append_all(OtherCameraModel.initModels())

    """##################################################机械臂动作表######################################################"""
    @classmethod
    def initHitBotBrokenDropAction      (cls):
        if HitBotBrokenDropAction      .has_limit(20):
            return
        HitBotBrokenDropAction      .create_table()
        HitBotBrokenDropAction      .clear()
        HitBotBrokenDropAction      .append_all(HitBotBrokenDropAction      .initModels())

    @classmethod
    def initHitBotChamberDropAction     (cls):
        if HitBotChamberDropAction     .has_limit(40):
            return
        HitBotChamberDropAction     .create_table()
        HitBotChamberDropAction     .clear()
        HitBotChamberDropAction     .append_all(HitBotChamberDropAction     .initModels())

    @classmethod
    def initHitBotChamberPickAction     (cls):
        if HitBotChamberPickAction     .has_limit(40):
            return
        HitBotChamberPickAction     .create_table()
        HitBotChamberPickAction     .clear()
        HitBotChamberPickAction     .append_all(HitBotChamberPickAction     .initModels())

    @classmethod
    def initHitBotCheckChamberAction    (cls):
        if HitBotCheckChamberAction    .has_limit(10):
            return
        HitBotCheckChamberAction    .create_table()
        HitBotCheckChamberAction    .clear()
        HitBotCheckChamberAction    .append_all(HitBotCheckChamberAction    .initModels())

    @classmethod
    def initHitBotCheckSlideLAction     (cls):
        if HitBotCheckSlideLAction     .has_limit(10):
            return
        HitBotCheckSlideLAction     .create_table()
        HitBotCheckSlideLAction     .clear()
        HitBotCheckSlideLAction     .append_all(HitBotCheckSlideLAction     .initModels())

    @classmethod
    def initHitBotCheckSlideRAction     (cls):
        if HitBotCheckSlideRAction     .has_limit(120):
            return
        HitBotCheckSlideRAction     .create_table()
        HitBotCheckSlideRAction     .clear()
        HitBotCheckSlideRAction     .append_all(HitBotCheckSlideRAction     .initModels())

    @classmethod
    def initHitBotCodeScanAction        (cls):
        if HitBotCodeScanAction        .has_limit(4):
            return
        HitBotCodeScanAction        .create_table()
        HitBotCodeScanAction        .clear()
        HitBotCodeScanAction        .append_all(HitBotCodeScanAction        .initModels())

    @classmethod
    def initHitBotDropSlideAction       (cls):
        if HitBotDropSlideAction       .has_limit(1):
            return
        HitBotDropSlideAction       .create_table()
        HitBotDropSlideAction       .clear()
        HitBotDropSlideAction       .append_all(HitBotDropSlideAction       .initModels())

    @classmethod
    def initHitBotLoggerModel           (cls):
        if HitBotLoggerModel           .has_limit(1):
            return
        HitBotLoggerModel           .create_table()
        HitBotLoggerModel           .clear()

    @classmethod
    def initHitBotModel                 (cls):
        if HitBotModel                 .has_limit(1):
            return
        HitBotModel                 .create_table()
        HitBotModel                 .clear()

    @classmethod
    def initHitBotSafeAction            (cls):
        if HitBotSafeAction            .has_limit(4):
            return
        HitBotSafeAction            .create_table()
        HitBotSafeAction            .clear()
        HitBotSafeAction            .append_all(HitBotSafeAction            .initModels())

    @classmethod
    def initHitBotTrayDropAction        (cls):
        if HitBotTrayDropAction        .has_limit(22):
            return
        HitBotTrayDropAction        .create_table()
        HitBotTrayDropAction        .clear()
        HitBotTrayDropAction        .append_all(HitBotTrayDropAction        .initModels())

    @classmethod
    def initHitBotTrayPickAction        (cls):
        if HitBotTrayPickAction        .has_limit(22):
            return
        HitBotTrayPickAction        .create_table()
        HitBotTrayPickAction        .clear()
        HitBotTrayPickAction        .append_all(HitBotTrayPickAction        .initModels())

    @classmethod
    def initHitBotPickErrorAction(cls):
        if HitBotPickErrorAction.has_limit(1):
            return
        HitBotPickErrorAction.create_table()
        HitBotPickErrorAction.clear()
        HitBotPickErrorAction.append_all(HitBotPickErrorAction.initModels())