#!D:/IDE/anaconda3/envs python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName：GripperConfigModel.py\n
 @DateTime: 2025/7/2 17:52\n
 @Author  ：xsh
"""
from PyQt5 import Qt
from datetime import datetime, date
import copy
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 GripperConfigModel(CoreModel):
    __tablename__ = 't_GripperConfigModel'
    ID                  : Mapped[int    ] = mapped_column(primary_key=True      , nullable=False, doc='数据序号', comment='数据序号')
    gripperType         : Mapped[int    ] = mapped_column(default=20            , nullable=True , doc='夹爪类型', comment='夹爪类型')
    gripperON           : Mapped[float  ] = mapped_column(default=0.0           , nullable=True , doc='打开行程', comment='打开行程')
    gripperMinLength    : Mapped[float  ] = mapped_column(default=8.9           , nullable=True , doc='最小行程', comment='最小行程')
    gripperMaxLength    : Mapped[float  ] = mapped_column(default=20.0          , nullable=True , doc='最大行程', comment='最大行程')
    gripperMiddle       : Mapped[float  ] = mapped_column(default=8.0           , nullable=True , doc='中间行程', comment='中间行程')
    gripperOFF          : Mapped[float  ] = mapped_column(default=10.0          , nullable=True , doc='关闭行程', comment='关闭行程')
    grabMinLength       : Mapped[float  ] = mapped_column(default=8.00          , nullable=True , doc='最小行程', comment='最小行程')
    grabMaxLength       : Mapped[float  ] = mapped_column(default=11.5          , nullable=True , doc='最大行程', comment='最大行程')
    grabOFF             : Mapped[float  ] = mapped_column(default=12.5          , nullable=True , doc='抓取状态', comment='抓取状态')
    grabON              : Mapped[float  ] = mapped_column(default=10.0          , nullable=True , doc='抓取状态', comment='抓取状态')
    beginOFF            : Mapped[float  ] = mapped_column(default=5.0           , nullable=True , doc='中间行程', comment='中间行程')
    def __init__(self, **args):
        super(GripperConfigModel, self).__init__()
        self.initialize()
        self.initParameter(**args)


    def initValue(self):
        super(GripperConfigModel, self).initValue()
        self.ID                   = 1
        self.gripperType          = 20
        self.gripperON            = 0.0
        self.gripperMinLength     = 8.9
        self.gripperMaxLength     = 20.0
        self.gripperMiddle        = 8.0
        self.gripperOFF           = 10.0
        self.grabMinLength        = 8.00
        self.grabMaxLength        = 11.5
        self.grabOFF              = 12.5
        self.grabON               = 10.0
        self.beginOFF             = 5.0











    @classmethod
    def isGripperON(cls) -> (float, float):
        model: cls = cls.Gripper()
        return model.gripperON, model.gripperMiddle
    @classmethod
    def isGripperOFF(cls) -> (float, float):
        model: cls = cls.Gripper()
        return model.gripperMinLength, model.gripperMaxLength

    @classmethod
    def isBeginOFF(cls) -> (float, float):
        model: cls = cls.Gripper()
        return model.BeginOFF, model.GripperMinLength

    @classmethod
    def isGrab(cls) -> (float, float):
        model: cls = cls.Gripper()
        return model.gripperMiddle, model.grabMaxLength

    @classmethod
    def Switch(cls) -> (float, float):
        model: cls = cls.Gripper()
        return model.gripperON, model.gripperOFF

    @classmethod
    def Gripper(cls):
        model: cls
        try:
            dbController.initEngine()
            with Session(dbController.Engine) as session:
                model = session.query(cls).first()
                session.close()
        except Exception as e:
            AppUtils.print_Exception(e)
            model = cls.instance(ID=1)
        return model

    @classmethod
    def initModels(cls):
        return [cls.instance(ID=1)]

    @classmethod
    def GripperSwitch(cls) -> (float, float):
        item: cls = cls.Gripper()
        return item.gripperON, item.gripperOFF

    @classmethod
    def GripperType         (cls) -> int    :
        return cls.Gripper().gripperType

    @classmethod
    def GripperON           (cls) -> float  :
        return cls.Gripper().gripperON

    @classmethod
    def GripperMinLength    (cls) -> float  :
        return cls.Gripper().gripperMinLength

    @classmethod
    def GripperMaxLength    (cls) -> float  :
        return cls.Gripper().gripperMaxLength

    @classmethod
    def GripperMiddle       (cls) -> float  :
        return cls.Gripper().gripperMiddle

    @classmethod
    def GripperOFF          (cls) -> float  :
        return cls.Gripper().gripperOFF

    @classmethod
    def GrabMinLength       (cls) -> float  :
        return cls.Gripper().grabMinLength

    @classmethod
    def GrabMaxLength       (cls) -> float  :
        return cls.Gripper().grabMaxLength

    @classmethod
    def GrabOFF             (cls) -> float  :
        return cls.Gripper().grabOFF

    @classmethod
    def GrabON              (cls) -> float  :
        return cls.Gripper().grabON

    @classmethod
    def GripperMoving       (cls) -> float  :
        return cls.Gripper().gripperMoving

    @classmethod
    def BeginOFF            (cls) -> float  :
        return cls.Gripper().beginOFF













    __field__ = (
        'ID'                 ,
        'gripperType'        ,
        'gripperON'          ,
        'gripperMinLength'   ,
        'gripperMaxLength'   ,
        'gripperMiddle'      ,
        'gripperOFF'         ,
        'grabMinLength'      ,
        'grabMaxLength'      ,
        'grabOFF'            ,
        'grabON'             ,
        'beginOFF'           ,
    )
    __types__ = (
        int    ,
        int    ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
        float  ,
    )