from django.db import models
from django.utils.timezone import datetime
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')
    org = models.ForeignKey('Org', verbose_name='所在组织', on_delete='PROTECT')
    total = models.PositiveIntegerField(verbose_name='总数')
    increment = models.IntegerField(blank=True, null=True, verbose_name='增量')
    date = models.DateField(auto_now_add=False, verbose_name='日期', unique=False)
    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, blank=True, null=True, verbose_name='更新时间')

    class Meta:
        unique_together = ('device_id', 'date')
        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, unique=True, verbose_name='设备ID')
    device_name = models.CharField(max_length=100, blank=True, null=True, verbose_name='设备名称')
    longitude = models.FloatField(blank=True, null=True, verbose_name='经度')
    latitude = models.FloatField(blank=True, null=True, verbose_name='维度')
    org = models.ForeignKey(Org, verbose_name='所在组织', on_delete='PROTECT')

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

    def __str__(self):
        return self.device_id


class DeviceTempLog(models.Model):
    device_id = models.CharField(max_length=100, verbose_name='设备ID')
    temperature = models.DecimalField(max_digits=6, decimal_places=2, verbose_name='温度')
    humidity = models.DecimalField(max_digits=6, decimal_places=2, verbose_name='湿度')
    last_time = models.DateTimeField(null=True, blank=True, db_index=True, verbose_name='日志时间')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        verbose_name = '设备温度湿度日志'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.device_id


class WeatherLog(models.Model):
    data_time = models.DateTimeField(blank=True, null=True, verbose_name='时间')
    device_id = models.CharField(max_length=100, verbose_name='台站编号')
    device_name = models.CharField(max_length=255, blank=True, null=True, verbose_name='台站名称')
    env_temp = models.CharField(max_length=255, blank=True, null=True, verbose_name='环温')
    temperature1 = models.CharField(max_length=255, blank=True, null=True, verbose_name='温度1')
    temperature2 = models.CharField(max_length=255, blank=True, null=True, verbose_name='温度2')
    temperature3 = models.CharField(max_length=255, blank=True, null=True, verbose_name='温度3')
    temperature4 = models.CharField(max_length=255, blank=True, null=True, verbose_name='温度4')
    temperature5 = models.CharField(max_length=255, blank=True, null=True, verbose_name='温度5')
    dew_point_temp = models.CharField(max_length=255, blank=True, null=True, verbose_name='露点温度')
    env_humi = models.CharField(max_length=255, blank=True, null=True, verbose_name='环湿')
    soil_humi1 = models.CharField(max_length=255, blank=True, null=True, verbose_name='土湿1')
    soil_humi2 = models.CharField(max_length=255, blank=True, null=True, verbose_name='土湿2')
    soil_humi3 = models.CharField(max_length=255, blank=True, null=True, verbose_name='土湿3')
    co2 = models.CharField(max_length=255, blank=True, null=True, verbose_name='CO2')
    evaporation = models.CharField(max_length=255, blank=True, null=True, verbose_name='蒸发')
    air_pressure = models.CharField(max_length=255, blank=True, null=True, verbose_name='气压')
    total_radiation_1_ins = models.CharField(max_length=255, blank=True, null=True, verbose_name='总辐射1瞬时')
    scat_radiation_ins = models.CharField(max_length=255, blank=True, null=True, verbose_name='散辐射瞬时')
    direct_radiation_ins = models.CharField(max_length=255, blank=True, null=True, verbose_name='直接辐射瞬时')
    total_radiation_2_ins = models.CharField(max_length=255, blank=True, null=True, verbose_name='总辐射2瞬时')
    net_radiation_ins = models.CharField(max_length=255, blank=True, null=True, verbose_name='净辐射瞬时')
    pho_radiation_ins = models.CharField(max_length=255, blank=True, null=True, verbose_name='光合辐射瞬时')
    uv_radiation_ins = models.CharField(max_length=255, blank=True, null=True, verbose_name='紫外辐射瞬时')
    wind_direction = models.CharField(max_length=255, blank=True, null=True, verbose_name='风向')
    wind_speed_ins = models.CharField(max_length=255, blank=True, null=True, verbose_name='瞬时风速')
    wind_speed_2mins = models.CharField(max_length=255, blank=True, null=True, verbose_name='2分钟风速')
    wind_speed_10mins = models.CharField(max_length=255, blank=True, null=True, verbose_name='10分钟风速')
    rainfall_interval_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='雨量间隔累计')
    sunshine_time_interval_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='日照时间隔累计')
    total_radiation_1_interval_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='总辐射1间隔累计')
    scat_radiation_interval_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='散辐射间隔累计')
    direct_radiation_interval_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='直接辐射间隔累计')
    total_radiation_2_interval_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='总辐射2间隔累计')
    net_radiation_interval_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='净辐射间隔累计')
    pho_radiation_interval_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='光合辐射间隔累计')
    uv_radiation_interval_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='紫外辐射间隔累计')
    rainfall_daily_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='雨量日累计')
    sunshine_time_daily_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='日照时日累计')
    total_radiation_1_daily_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='总辐射1日累计')
    scat_radiation_daily_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='散辐射日累计')
    direct_radiation_daily_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='直接辐射日累计')
    total_radiation_2_daily_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='总辐射2日累计')
    net_radiation_daily_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='净辐射日累计')
    pho_radiation_daily_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='光合辐射日累计')
    uv_radiation_daily_cum = models.CharField(max_length=255, blank=True, null=True, verbose_name='紫外辐射日累计')
    light_intensity = models.CharField(max_length=255, blank=True, null=True, verbose_name='光照度')
    power = models.CharField(max_length=255, blank=True, null=True, verbose_name='电量')
    pm25 = models.CharField(max_length=255, blank=True, null=True, verbose_name='PM2.5')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        managed = False
        db_table = 'weather_log'
        verbose_name = '气象台数据日志'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.device_id
