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):
    """TN:排口表"""
    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="", blank=True)
    pm_limit = models.FloatField("PM超低排放限值", default=10, help_text="单位:mg/m³")
    measure = models.CharField("治理措施", max_length=20, default="", blank=True)
    # {"x": 1.0, "y": 2.0, "z": 3.0}
    coordinates = models.JSONField("坐标", default=dict, blank=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):
    """
    TN:排口/设备关系表
    """

    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, blank=True)
    dust_alarm = models.FloatField("颗粒物报警值", null=True, blank=True)
    pm10_alarm = models.FloatField("PM10报警值", null=True, blank=True)
    pm25_alarm = models.FloatField("PM2.5报警值", null=True, blank=True)


class EnvData(models.Model):
    """
    TN:环保监测数据
    """

    enp_fields = [
        "running_state",
        "dust_rtd",
        "dust_zs",
        "temperature",
        "pressure",
        "speed",
        "humdity",
        "flux",
        "pm25",
        "pm10",
        "tsp",
        "wind_direction",
        "wind_speed",
        "so2_rtd",
        "so2_zs",
        "nox_rtd",
        "nox_zs",
        "o2",
        "ammonia_e"
    ]

    RUNING_STATE_CHOICES = ((10, "运行"), (20, "待机"), (30, "停机"), (40, "故障"), (50, "未知"))
    equipment = models.ForeignKey(Equipment, verbose_name="关联设备", on_delete=models.CASCADE)
    timex = models.DateTimeField("采集时间", primary_key=True)
    running_state = models.PositiveSmallIntegerField("运行状态", default=10)
    dust_rtd = models.FloatField("颗粒物实测(mg/m3)", null=True, blank=True)
    dust_zs = models.FloatField("颗粒物折算(mg/m3)", null=True, blank=True)
    temperature = models.FloatField("温度(℃)", null=True, blank=True)
    pressure = models.FloatField("压力(kPa)", null=True, blank=True)
    speed = models.FloatField("流速(m/s)", null=True, blank=True)
    humidity = models.FloatField("湿度(%)", null=True, blank=True)
    flux = models.FloatField("流量(m3/h)", null=True, blank=True)
    pm25 = models.FloatField("PM2.5(ug/m3)", null=True, blank=True)
    pm10 = models.FloatField("PM10(ug/m3)", null=True, blank=True)
    tsp = models.FloatField("TSP(ug/m3)", null=True, blank=True)
    wind_direction = models.PositiveSmallIntegerField("风向", null=True, blank=True)
    wind_speed = models.FloatField("风速(m/s)", null=True, blank=True)
    so2_rtd = models.FloatField("二氧化硫实测(mg/m3)", null=True, blank=True)
    so2_zs = models.FloatField("二氧化硫折算(mg/m3)", null=True, blank=True)
    nox_rtd = models.FloatField("氮氧化物实测(mg/m3)", null=True, blank=True)
    nox_zs = models.FloatField("氮氧化物折算(mg/m3)", null=True, blank=True)
    ammonia_e = models.FloatField("氨逃逸(mg/m3)", null=True, blank=True)
    o2 = models.FloatField("含氧量(%)", null=True, blank=True)

    class Meta:
        db_table = "enp_envdata"
        managed = False
        unique_together = (("equipment", "timex"),)


class VehicleAccess(BaseModel):
    """TN:车辆出入厂记录"""
    type = models.PositiveSmallIntegerField("出入类型", default=1, help_text="1: 进厂, 2: 出厂")
    vehicle_number = models.CharField("车牌号", max_length=10)
    access_time = models.DateTimeField("出入时间", null=True, blank=True)
    emission_standard = models.CharField("排放标准", max_length=10, null=True, blank=True)
    is_new_energy = models.BooleanField("是否新能源", default=False)
    door_name = models.CharField("门禁名称", max_length=10, null=True, blank=True)


class CarWash(BaseModel):
    """TN:洗车记录"""
    station = models.ForeignKey(Equipment, verbose_name="洗车台", on_delete=models.CASCADE)
    vehicle_number = models.CharField("车牌号", max_length=10, default="")
    start_time = models.DateTimeField("洗车时间", null=True, blank=True)
    end_time = models.DateTimeField("洗车完成时间", null=True, blank=True)
    duration = models.PositiveIntegerField("洗车时长(s)", null=True, blank=True)
    pressure = models.DecimalField("洗车压力(Mpa)", max_digits=10, decimal_places=4, null=True, blank=True)
    flux = models.DecimalField("洗车流量(L/min)", max_digits=10, decimal_places=4, null=True, blank=True)
