from django.db import models
from apps.utils.models import CommonBModel, BaseModel
from apps.mtm.models import Mgroup
from apps.em.models import Equipment
# Create your models here.


class Drain(CommonBModel):
    DR_TYPE_CHOICES = (
        (10, '排放口'),
        (20, '污染源'),
    )
    DR_LEVLE_CHOICES = (
        (10, '主要排放口'),
        (20, '次要排放口')
    )

    DR_CATE_MTRAN = 'mtrans'
    DR_CATE_PRODUCT = 'product'
    DR_CATE_MSTORE = 'mstore'
    DR_CATE_CHOICES = (
        (DR_CATE_PRODUCT, '生产工艺'),
        (DR_CATE_MTRAN, '物料输送'),
        (DR_CATE_MSTORE, '物料储存'),
    )
    type = models.PositiveSmallIntegerField(
        '类型', choices=DR_TYPE_CHOICES, help_text=str(DR_TYPE_CHOICES))
    number = models.CharField('编号', max_length=20)
    name = models.CharField('名称', max_length=20)
    level = models.PositiveSmallIntegerField(
        '排口等级', default=20, choices=DR_LEVLE_CHOICES, help_text=str(DR_LEVLE_CHOICES))
    cate = models.CharField('分类', max_length=10,
                            choices=DR_CATE_CHOICES, null=True, blank=True, help_text=str(DR_CATE_CHOICES))
    height = models.PositiveSmallIntegerField('排气筒高度', default=0)
    treatment_capacity = models.CharField('处理量', max_length=20, default='')
    pm_limit = models.FloatField('PM超低排放限值', default=10, help_text='单位:mg/m³')
    measure = models.CharField('治理措施', max_length=20, default='')
    # {"x": 1.0, "y": 2.0, "z": 3.0}
    coordinates = models.JSONField('坐标', default=dict, null=True)
    mgroup = models.ForeignKey(
        Mgroup, verbose_name='所属工段', on_delete=models.CASCADE)
    equipments = models.ManyToManyField(
        Equipment, verbose_name='关联设备', through='DrainEquip', related_name='drain_equipments', blank=True)


class DrainEquip(BaseModel):
    """
    排口/设备关系表
    """
    drain = models.ForeignKey(
        Drain, verbose_name='排口', related_name='drainequip_drain', on_delete=models.CASCADE)
    equipment = models.ForeignKey(
        Equipment, verbose_name='关联设备', related_name='drainequip_equipment', on_delete=models.CASCADE)
    params_list = models.JSONField('监测参数列表', default=list, null=True)


class EnvData(models.Model):
    """
    环保监测数据
    """
    equipment = models.ForeignKey(
        Equipment, verbose_name='关联设备', on_delete=models.CASCADE)
    time = models.DateTimeField('采集时间', primary_key=True)
    is_online = models.BooleanField('是否在线', default=True)
    is_runing = models.BooleanField('是否运行', default=True)
    dust_rtd = models.DecimalField(
        '颗粒物实测(mg/m3)', max_digits=10, decimal_places=4, null=True, blank=True)
    dust_zs = models.DecimalField(
        '颗粒物折算(mg/m3)', max_digits=10, decimal_places=4, null=True, blank=True)
    temperature = models.DecimalField(
        '温度(℃)', max_digits=10, decimal_places=4, null=True, blank=True)
    pressure = models.DecimalField(
        '压力(kPa)', max_digits=10, decimal_places=4, null=True, blank=True)
    speed = models.DecimalField(
        '流速(m/s)', max_digits=10, decimal_places=4, null=True, blank=True)
    humidity = models.DecimalField(
        '湿度(%)', max_digits=10, decimal_places=4, null=True, blank=True)
    flux = models.DecimalField(
        '流量(m3/h)', max_digits=10, decimal_places=4, null=True, blank=True)
    pm25 = models.DecimalField(
        'PM2.5(ug/m3)', max_digits=10, decimal_places=4, null=True, blank=True)
    pm10 = models.DecimalField(
        'PM10(ug/m3)', max_digits=10, decimal_places=4, null=True, blank=True)
    tsp = models.DecimalField(
        'TSP(ug/m3)', max_digits=10, decimal_places=4, null=True, blank=True)
    wind_speed = models.DecimalField(
        '风速(m/s)', max_digits=10, decimal_places=4, null=True, blank=True)
    so2_rtd = models.DecimalField(
        '二氧化硫实测(mg/m3)', max_digits=10, decimal_places=4, null=True, blank=True)
    so2_zs = models.DecimalField(
        '二氧化硫折算(mg/m3)', max_digits=10, decimal_places=4, null=True, blank=True)
    nox_rtd = models.DecimalField(
        '氮氧化物实测(mg/m3)', max_digits=10, decimal_places=4, null=True, blank=True)
    nox_zs = models.DecimalField(
        '氮氧化物折算(mg/m3)', max_digits=10, decimal_places=4, null=True, blank=True)
    o2 = models.DecimalField('含氧量(%)', max_digits=10,
                             decimal_places=4, null=True, blank=True)

    class Meta:
        db_table = 'enp_envdata'
        managed = False
        unique_together = (('equipment', 'time'), )
