#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：SpecimenCountModel.py\n
 @DateTime: 2025/9/19 14:19\n
 @Author  ：xsh
"""
from datetime import datetime
from AppSettings.Settings import *
from AppCore.AppDBModels.CoreModel import (dbController)
from sqlalchemy.orm import Session
from sqlalchemy.orm import (Mapped, mapped_column)
from sqlalchemy.types import (Text, DateTime)
from AppUtils.utils import *
from AppCore.Levels import *
from AppCore.AppDBModels.SlideModels.SlideModelInterface import SlideModelInterface

class SpecimenCountModel(SlideModelInterface):
    __tablename__ = 't_SpecimenCountModel'
    ID                  : Mapped[int            ] = mapped_column(primary_key=True                                      , nullable=False , doc='数据ＩＤ　　', comment='数据ＩＤ　　') # '数据ＩＤ　　'
    ChamberGroupNumber  : Mapped[int            ] = mapped_column(            default=1                                 , nullable=True  , doc='片仓组号　　', comment='片仓组号　　') # '片仓组号　　'
    ChamberRowNumber    : Mapped[int            ] = mapped_column(            default=1                                 , nullable=True  , doc='片仓行号　　', comment='片仓行号　　') # '片仓行号　　'
    CodeString          : Mapped[str            ] = mapped_column(Text      , default=''                                , nullable=True  , doc='二维码编号　', comment='二维码编号　') # '二维码编号　'
    SpecimenNumber      : Mapped[str            ] = mapped_column(Text      , default=''                                , nullable=True  , doc='样本编号　　', comment='样本编号　　') # '样本编号　　'
    SlideNumber         : Mapped[str            ] = mapped_column(Text      , default=''                                , nullable=True  , doc='玻片编号　　', comment='玻片编号　　') # '玻片编号　　'
    TrayNumber          : Mapped[int            ] = mapped_column(            default=ModuleLevel.ModuleTraySlide1      , nullable=True  , doc='托盘编号　　', comment='托盘编号　　') # '托盘编号　　'
    ObjectiveLevel      : Mapped[int            ] = mapped_column(            default=ObjectiveLevel.ObjectiveNone      , nullable=True  , doc='物镜类型　　', comment='物镜类型　　') # '物镜类型　　'
    CollectCount        : Mapped[int            ] = mapped_column(            default=0                                 , nullable=True  , doc='采集图数量　', comment='采集图数量　') # '采集图数量　'
    PreviewCount        : Mapped[int            ] = mapped_column(            default=0                                 , nullable=True  , doc='预览图数量　', comment='预览图数量　') # '采集图数量　'
    def __init__(self, **args):
        super(SpecimenCountModel, self).__init__()
        self.initialize()
        self.initParameter(**args)

    __field__ = (
        'ID'                   ,
        'ChamberGroupNumber'   ,
        'ChamberRowNumber'     ,
        'CodeString'           ,
        'SpecimenNumber'       ,
        'SlideNumber'          ,
        'TrayNumber'           ,
        'ObjectiveLevel'       ,
        'CollectCount'         ,
        'PreviewCount'         ,
    )
    __types__ = (
        int            ,
        int            ,
        int            ,
        str            ,
        str            ,
        str            ,
        int            ,
        int            ,
        int            ,
        int            ,
    )
    __header__ = (
        '数据ＩＤ　　',
        '片仓组号　　',
        '片仓行号　　',
        '二维码编号　',
        '样本编号　　',
        '玻片编号　　',
        '托盘编号　　',
        '物镜类型　　',
        '采集图数量　',
        '预览图数量　',

    )


    @classmethod
    def setCollectCount(cls, chamber_number  : int  = 1    ,row_number    : int  = 1    ,TrayNumber          : str  = 1    , Objective      : int  = 7    ,CollectCount        : int  = 0    ,CodeString          : int  = None ,SpecimenNumber      : str  = None ,SlideNumber         : str  = None ,):
        from AppSettings.SystemSetting import system_config
        ChamberNumber: int
        RowNumber: int
        ChamberNumber, RowNumber = system_config.getChamberNumbers(chamber_number, row_number)
        item: cls
        try:
            dbController.initEngine()
            count: int
            with Session(dbController.Engine) as session:
                count = session.query(cls).filter(cls.ChamberGroupNumber == ChamberNumber, cls.ChamberRowNumber == RowNumber).count()
                if count is None or count <= 0:
                    item = cls.instance()
                    item.ChamberGroupNumber  = ChamberNumber           #
                    item.ChamberRowNumber    = RowNumber               #
                    item.CodeString          = CodeString              #
                    item.SpecimenNumber      = SpecimenNumber          #
                    item.SlideNumber         = SlideNumber             #
                    item.TrayNumber          = TrayNumber              #
                    item.ObjectiveLevel      = Objective               #
                    item.CollectCount        = CollectCount            #
                    session.add(item)
                else:
                    session.query(cls).filter(cls.ChamberGroupNumber == ChamberNumber, cls.ChamberRowNumber == RowNumber).update(
                        {
                            cls.ChamberGroupNumber  : ChamberNumber       ,
                            cls.ChamberRowNumber    : RowNumber           ,
                            cls.CodeString          : CodeString          ,
                            cls.SpecimenNumber      : SpecimenNumber      ,
                            cls.SlideNumber         : SlideNumber         ,
                            cls.TrayNumber          : TrayNumber          ,
                            cls.ObjectiveLevel      : Objective           ,
                            cls.CollectCount        : CollectCount        ,
                    }
                    )
                session.commit()
                session.close()
        except Exception as e:
            from AppLog.logHelper import logDB
            logDB << AppUtils.toString(e)
        return RECODE_SUCCEED

    @classmethod
    def setPreviewCount(cls, chamber_number  : int  = 1    ,row_number    : int  = 1    ,TrayNumber          : str  = 1    , Objective      : int  = 7    ,PreviewCount        : int  = 0    ,CodeString          : int  = None ,SpecimenNumber      : str  = None ,SlideNumber         : str  = None ,):
        from AppSettings.SystemSetting import system_config
        ChamberNumber: int
        RowNumber: int
        ChamberNumber, RowNumber = system_config.getChamberNumbers(chamber_number, row_number)
        item: cls
        try:
            dbController.initEngine()
            count: int
            with Session(dbController.Engine) as session:
                count = session.query(cls).filter(cls.ChamberGroupNumber == ChamberNumber, cls.ChamberRowNumber == RowNumber).count()
                if count is None or count <= 0:
                    item = cls.instance()
                    item.ChamberGroupNumber  = ChamberNumber           #
                    item.ChamberRowNumber    = RowNumber               #
                    item.CodeString          = CodeString              #
                    item.SpecimenNumber      = SpecimenNumber          #
                    item.SlideNumber         = SlideNumber             #
                    item.TrayNumber          = TrayNumber              #
                    item.ObjectiveLevel      = Objective               #
                    item.PreviewCount        = PreviewCount            #
                    session.add(item)
                else:
                    session.query(cls).filter(cls.ChamberGroupNumber == ChamberNumber, cls.ChamberRowNumber == RowNumber).update(
                        {
                            cls.ChamberGroupNumber  : ChamberNumber       ,
                            cls.ChamberRowNumber    : RowNumber           ,
                            cls.CodeString          : CodeString          ,
                            cls.SpecimenNumber      : SpecimenNumber      ,
                            cls.SlideNumber         : SlideNumber         ,
                            cls.TrayNumber          : TrayNumber          ,
                            cls.ObjectiveLevel      : Objective           ,
                            cls.PreviewCount        : PreviewCount        ,
                    }
                    )
                session.commit()
                session.close()
        except Exception as e:
            from AppLog.logHelper import logDB
            logDB << AppUtils.toString(e)
        return RECODE_SUCCEED

    @classmethod
    def getSpecimen(cls, chamber_number: int, row_number: int) :
        from AppSettings.SystemSetting import system_config
        ChamberNumber: int
        RowNumber: int
        ChamberNumber, RowNumber = system_config.getChamberNumbers(chamber_number, row_number)
        item: cls = None
        try:
            dbController.initEngine()
            with Session(dbController.Engine) as session:
                item = session.query(cls).filter(cls.ChamberGroupNumber == ChamberNumber, cls.ChamberRowNumber == RowNumber).first()
                session.close()
        except Exception as e:
            from AppLog.logHelper import logDB
            logDB << AppUtils.toString(e)
        return item