from django.db import models
from django.contrib.contenttypes.fields import GenericRelation
from smart.models import SmartModule, SmartPush


class Mosquito(models.Model):
    name = models.CharField(max_length=100, verbose_name='设备名称')
    device_id = models.CharField(max_length=100, verbose_name='设备ID')
    region = models.CharField(max_length=50, verbose_name='区域')
    smart_module = GenericRelation(SmartModule)

    class Meta:
        verbose_name = '灭蚊灯'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        pass

    def get_api_url(self):
        pass


class MosqPost(models.Model):
    smart_push = models.ForeignKey(SmartPush, on_delete='PROTECT')
    mosq = models.ForeignKey(Mosquito, verbose_name='灭蚊灯', on_delete='PROTECT')
    led = models.PositiveIntegerField(verbose_name='灭蚊数')
    energy = models.PositiveIntegerField(verbose_name='电量')
    # signal = models.IntegerField(verbose_name='信号')
    # coordinate = models.CharField(max_length=100, verbose_name='坐标')
    time = models.DateTimeField(auto_now=False, auto_now_add=True)

    class Meta:
        verbose_name = '灭蚊数据'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '{0}: {1}'.format(self.mosq.name, self.led)

    def get_api_url(self):
        pass


class MosqPostStatistic(models.Model):
    total = models.PositiveIntegerField(verbose_name='总数')
    increment = models.IntegerField(verbose_name='增量')
    date = models.DateField(auto_now_add=False, verbose_name='日期', unique=True)

    class Meta:
        verbose_name = '统计报表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '{0}: {1}'.format(self.date, self.increment)


class DevicePostStatistic(models.Model):
    device_id = models.CharField(max_length=100, verbose_name='设备ID')
    total = models.PositiveIntegerField(verbose_name='总数')
    date = models.DateField(auto_now_add=False, verbose_name='日期', unique=False)

    class Meta:
        verbose_name = '设备日志统计表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '{0}: {1}'.format(self.device_id, self.total)


class Org(models.Model):
    name = models.CharField(max_length=100, verbose_name='组织名称')
    code = models.CharField(max_length=10, verbose_name='组织代码')
    desc = models.CharField(max_length=100, blank='True', verbose_name='描述')

    class Meta:
        verbose_name = '客户组织'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class DeviceInfo(models.Model):
    device_id = models.CharField(max_length=100, verbose_name='设备ID')
    org = models.ForeignKey(Org, verbose_name='所在组织', on_delete='PROTECT')
