# coding=utf-8

from django.db import models
from django.conf import settings
from apps.account.models import User
from apps.tenant.models import Tenant
from apps.tenant.area.models import Department
from apps.tenant.option.models import Option
from utils.exceptions import CustomError
from django.db import transaction

class Employee(models.Model):
    ENABLE = 1
    WORKING = 2
    DAY_OFF = 3
    DISABLE = 4
    STATUS_CHOICES = (
        (ENABLE, u'在用'),
        (WORKING, u'工作中'),
        (DAY_OFF, u'调休'),
        (DISABLE, u'禁用'),
    )

    SUPER = 1
    ADMIN = 2
    EMPLOYEE = 3
    REPAIR = 4
    TYPE_CHOICES = (
        (SUPER, u'平台管理员'),
        (ADMIN, u'管理者'),
        (EMPLOYEE, u'检修人'),
        (REPAIR, u'报修人'),
    )
    name = models.CharField(max_length=100, verbose_name=u"姓名")
    tel = models.CharField(max_length=50, verbose_name=u'电话')
    gender = models.PositiveSmallIntegerField(choices=settings.GENDER_CHOICES, verbose_name=u'性别', default=settings.MALE)
    status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, verbose_name=u'状态',default=ENABLE)
    user = models.OneToOneField(settings.AUTH_USER_MODEL, editable=False, related_name='employee',
                                on_delete=models.PROTECT, verbose_name=u'添加人', null=True)
    position = models.CharField(max_length=20, verbose_name=u'职位', null=True)
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u"创建人", on_delete=models.PROTECT,
                                    editable=False, related_name='create_user',null=True)
    face = models.CharField(max_length=200, verbose_name=u'头像', null=True)
    baoxiu_count = models.IntegerField(verbose_name='报修次数', default=0)
    tenant = models.ForeignKey(Tenant, verbose_name=u'租户', editable=False, on_delete=models.PROTECT,null=True)
    type = models.PositiveSmallIntegerField(verbose_name=u'人员类型', choices=TYPE_CHOICES, default=REPAIR)

    class Meta:
        db_table = "employee"
        verbose_name = u"用户管理"
        ordering = ['-id']
        index_together = (
            'name',
            'tel',
        )
        default_permissions = ()

    @staticmethod
    def create_super_admin(tenant,name, username, password):
        # 租户添加管理超级管理员
        user = User.objects.create_superuser_tenant(username, password)
        instance = Employee.objects.create(
            name=name,
            tel=username,
            gender=settings.MALE,
            user=user,
            tenant=tenant,
            type=Employee.SUPER,
        )
        return instance

    @staticmethod
    def get_instance_by_id(id, tenant):
        try:
            id = int(id)
        except:
            raise CustomError('无效的employee_id')
        instance = Employee.objects.filter(pk=id, tenant=tenant).first()
        if not instance:
            raise CustomError('未找到对应的员工实例')
        return instance

    @staticmethod
    def getOrRegister(user):
        # 小程序登录后，自动注册报修人
        customer = Employee.objects.filter(user=user).first()
        if customer:
            return customer
        with transaction.atomic():
            customer = Employee.objects.create(user=user,type=Employee.REPAIR, tel=user.username, name=user.username)
        return customer

    @staticmethod
    def checkSuper(tenant, id=None):
        #  检查超级管理员数量
        rows = Employee.objects.filter(type=Employee.SUPER, tenant=tenant)
        if id:
            rows = rows.exclude(id=id)
        return rows.count()
