from django.db import models
from apps.hrm.models import Employee
from apps.system.models import Dept, Post, User
from apps.utils.models import CommonADModel, CommonBModel
# Create your models here.


class Area(CommonBModel):
    """
    TN: 地图区域
    """
    AREA_1 = 10
    AREA_2 = 20
    AREA_3 = 30
    AREA_4 = 40
    AREA_LEVEL_CHOICES = (
        (AREA_1, '办公'),
        (AREA_2, '生产一般'),
        (AREA_3, '生产重点'),
        (AREA_4, '四级')
    )
    AREA_TYPE_FIX = 10
    AREA_TYPE_TEMP = 20
    AREA_TYPE_CHOICES = (
        (10, '固定'),
        (20, '临时作业')
    )
    name = models.CharField('名称', max_length=20)
    type = models.PositiveSmallIntegerField('区域类型', default=10, choices=AREA_TYPE_CHOICES)
    level = models.PositiveSmallIntegerField('区域风险等级')
    cate = models.PositiveSmallIntegerField('区域分类')
    number = models.CharField('编号', max_length=20, null=True, blank=True)
    access_list = models.JSONField('准入人员类型', default=list, null=False, blank=True, help_text='employee/remployee/visitor/driver')
    count_people_min = models.PositiveIntegerField('最小人员数', default=0)
    count_people_max = models.PositiveIntegerField('最大人员数', default=1000)
    count_people = models.PositiveIntegerField('当前人数', default=0)
    stay_minute_min = models.PositiveSmallIntegerField('最短停留时间', default=0, help_text='0代表未配置')
    stay_minute_max = models.PositiveSmallIntegerField('最长停留时间', default=0, help_text='0代表未配置')
    is_hidden = models.BooleanField('隐藏围栏用', default=False)
    manager = models.ForeignKey(User, on_delete=models.SET_NULL, verbose_name='区域负责人', null=True, blank=True)
    third_info = models.JSONField('三方信息', default=dict,
                                  null=False, blank=True)

    def __str__(self) -> str:
        return self.name


class Access(CommonADModel):
    """
    TN: 准入禁入权限(动态变化)
    """
    ACCESS_IN_YES = 10
    ACCESS_IN_NO = 20
    ACCESS_CHOICE = (
        (ACCESS_IN_YES, '准入'),
        (ACCESS_IN_NO, '禁入')
    )

    type = models.PositiveSmallIntegerField('准入类型', choices=ACCESS_CHOICE)
    area = models.ForeignKey(Area, verbose_name='关联区域',
                             on_delete=models.CASCADE)
    obj_cate = models.CharField('对象类型', max_length=20, help_text='post/org/people/visit')
    post = models.ForeignKey(Post, verbose_name='关联岗位', on_delete=models.SET_NULL, null=True, blank=True)
    dept = models.ForeignKey(Dept, verbose_name='关联部门', on_delete=models.SET_NULL, null=True, blank=True)
    employee = models.ForeignKey(Employee, verbose_name='关联人员', on_delete=models.SET_NULL, null=True, blank=True)
    stay_minute_min = models.PositiveSmallIntegerField('最短停留时间', default=0)
    stay_minute_max = models.PositiveSmallIntegerField('最长停留时间', default=0)
    sort = models.PositiveSmallIntegerField('排序', default=1)
