from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.base import Model
import django.utils.timezone as timezone
from django.db.models.query import QuerySet
from apps.system.models import CommonAModel, CommonBModel, Organization, User, Dict, File
#from apps.mtm.models import Process
from utils.model import SoftModel, BaseModel
from simple_history.models import HistoricalRecords



class Equipment(CommonBModel):
    """
    设备台账信息
    """
    state_choices = (
         (0, '完好'),
        (1, '限用'),
        (2, '在修'),
         (3, '禁用')
    )
    statedm_choices = (
         (0, '合格'),
        (1, '准用'),
        (2, '限用'),
        (3, '禁用'),
        (4, '停用'),
        (5, '封存')
    )
   
    type_choices = (
        (1, '生产设备'),
        (2, '检验工具')
    )
    mgmtype_choices = (
         (1, 'A'),
         (2, 'B'),
         (3, 'C')
    )
    way_choices = (
         (1, '外检'),
         (2, '自检'),
        
    )
    usetype_choices = (
         (1, '专用'),
         (2, '公用'),
    )
    type = models.IntegerField('类型', choices=type_choices, default=1)
    name = models.CharField('设备名称', max_length=50)
    number = models.CharField('设备编号', max_length=50, unique=True)
    model = models.CharField('规格型号', max_length=60, null=True, blank=True)
    factory = models.CharField('生产厂', max_length=50, null=True, blank=True)
    production_date = models.DateField('生产日期', null=True, blank=True)
    buy_date = models.DateField('购置日期', null=True, blank=True)
    state = models.CharField('设备状态', max_length=11, choices=state_choices, default=0)
    parameter = models.TextField('技术参数', null=True, blank=True)
    place = models.CharField('存放位置', max_length=50, null=True, blank=True)
    count = models.IntegerField('数量', default=0)
    keeper = models.ForeignKey(User, verbose_name='保管人', on_delete=models.CASCADE, null=True, blank=True)
    description = models.CharField('描述', max_length=200, blank=True, null=True)
    #process = models.ForeignKey(Process, verbose_name='工序', on_delete=models.CASCADE, null=True, blank=True)
    mgmtype  = models.IntegerField('管理类别', choices=mgmtype_choices, default=1)#监视，测量设备
    way  = models.IntegerField('校准或检定方式', choices=way_choices, default=1)#监视，测量设备
    standard = models.CharField('溯源标准或依据', max_length=200, blank=True, null=True)#监视，测量设备
    cycle = models.IntegerField('校准或检定周期', default=0)#监视，测量设备
    usetype  = models.IntegerField('使用类别', choices=usetype_choices, default=1)#监视，测量设备
    statedm = models.IntegerField('设备状态', choices=statedm_choices, default=0)#监视，测量设备
    
    class Meta:
        verbose_name = '设备信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.number + '-' + self.name
    
class Equipmentrecord(CommonBModel):
    equipment = models.ForeignKey(Equipment, verbose_name='校准检定设备', on_delete=models.CASCADE, null=True, blank=True)
    recentlydate = models.DateField('最近一次校准/检定日期',blank=True, null=True)
    nextdate = models.DateField('下次应校准或检定日期',blank=True, null=True)
    description = models.CharField('描述', max_length=200, blank=True, null=True)