from django.db import models
from apps.utils.models import BaseModel, CommonBModel, CommonADModel, CommonBDModel
from apps.wpm.models import SfLog
from apps.mtm.models import Material, Mgroup, Team


class Mpoint(CommonBDModel):
    """测点
    """
    name = models.CharField('测点名称', max_length=50)
    code = models.CharField('测点编号', max_length=50, unique=True)
    unit = models.CharField('单位', max_length=50)
    cate = models.CharField('分类', max_length=50, default='material')
    material = models.ForeignKey(Material, verbose_name='计量某种物料', on_delete=models.CASCADE, null=True, blank=True)
    ep_monitored = models.ForeignKey('em.equipment', verbose_name='监测哪个设备', related_name='mp_ep_monitored', on_delete=models.SET_NULL, null=True, blank=True)
    ep_belong = models.ForeignKey('em.equipment', verbose_name='属于哪个设备', related_name='mp_ep_belong', on_delete=models.SET_NULL, null=True, blank=True)
    mgroup = models.ForeignKey('mtm.mgroup', verbose_name='所在集合', on_delete=models.SET_NULL, null=True, blank=True)
    mgroups_allocate = models.JSONField('各工段分配', default=list, blank=True, help_text='[{"mgroup":"x", "ratio": 1}]')
    is_auto = models.BooleanField('是否自动采集', default=True)
    is_all = models.BooleanField('是否记录是整个工段', default=False)
    formula = models.TextField('计算公式', default='')
    func_on_change = models.CharField('数据变动时执行方法', max_length=100, default='')


class MpLog(BaseModel):
    """测点原始记录
    """
    mpoint = models.ForeignKey(Mpoint, verbose_name='关联测点', on_delete=models.SET_NULL, null=True, blank=True)
    tag_id = models.BigIntegerField('记录ID', db_index=True)
    tag_code = models.CharField('测点编号', max_length=50)
    tag_update = models.DateTimeField('更新时间')
    tag_val = models.FloatField('当前值')
    

class MpointStat(CommonADModel):
    """测点统计表
    """
    type = models.CharField('统计维度', max_length=50, default='hour', help_text='year/month/day/year_s/month_s/month_st/day_s/sflog/hour_s/hour')
    year = models.PositiveSmallIntegerField('年', null=True, blank=True)
    month = models.PositiveSmallIntegerField('月', null=True, blank=True)
    day = models.PositiveSmallIntegerField('日', null=True, blank=True)

    year_s = models.PositiveSmallIntegerField('班年', null=True, blank=True)
    month_s = models.PositiveSmallIntegerField('班月', null=True, blank=True)
    day_s = models.PositiveSmallIntegerField('班日', null=True, blank=True)

    hour = models.PositiveSmallIntegerField('时', null=True, blank=True)
    sflog = models.ForeignKey(SfLog, verbose_name='关联值班记录', on_delete=models.CASCADE, null=True, blank=True)
    mgroup = models.ForeignKey(Mgroup, verbose_name='关联测点集', on_delete=models.CASCADE, null=True, blank=True)
    mpoint = models.ForeignKey(Mpoint, verbose_name='关联测点', on_delete=models.CASCADE)
    val = models.FloatField('统计值', default=0)
    total_production = models.FloatField('总产量', default=0, help_text='t')
    elec_consume_unit = models.FloatField('单位产品电耗', default=0, help_text='kw·h/t')


class EnStat(BaseModel):
    """
    能源数据统计表
    """
    type = models.CharField('统计维度', max_length=50, default='hour', help_text='year_s/month_s/month_st/day_s/sflog/hour_s')
    sflog = models.ForeignKey(SfLog, verbose_name='关联值班记录', on_delete=models.CASCADE, null=True, blank=True)
    team = models.ForeignKey(Team, verbose_name='关联班组', on_delete=models.CASCADE, null=True, blank=True)
    mgroup = models.ForeignKey(Mgroup, verbose_name='关联工段', on_delete=models.CASCADE)
    year = models.PositiveSmallIntegerField('年', null=True, blank=True)
    month = models.PositiveSmallIntegerField('月', null=True, blank=True)
    day = models.PositiveSmallIntegerField('日', null=True, blank=True)
    hour = models.PositiveSmallIntegerField('小时', null=True, blank=True)
    year_s = models.PositiveSmallIntegerField('班年', null=True, blank=True)
    month_s = models.PositiveSmallIntegerField('班月', null=True, blank=True)
    day_s = models.PositiveSmallIntegerField('班日', null=True, blank=True)
    total_production = models.FloatField('总产量', default=0, help_text='t')
    elec_consume =  models.FloatField('总电耗', default=0, help_text='kw·h')
    elec_coal_consume = models.FloatField('电量折标煤', default=0, help_text='tce')
    pcoal_heat = models.FloatField('煤粉热值', default=0)
    pcoal_consume = models.FloatField('煤粉消耗', default=0, help_text='t')
    pcoal_coal_consume = models.FloatField('煤粉折标煤', default=0, help_text='tce')
    water_consume = models.FloatField('水消耗', default=0, help_text='t')
    cair_consume = models.FloatField('压缩空气', default=0, help_text='m3')
    out_steam = models.FloatField('外送蒸汽', default=0, help_text='t')
    out_steam_coal = models.FloatField('外送蒸汽折标煤', default=0, help_text='tce')
    ccr_consume = models.FloatField('电石渣消耗', default=0, help_text='t')
    kiln_end_heat = models.FloatField('窑尾余热', default=0, help_text='tce')
    imaterial_data = models.JSONField('成本物料数据', default=list, blank=True)
    other_cost_data = models.JSONField('其他成本数据', default=list, blank=True)
    qua_data = models.JSONField('质检数据', default=list, blank=True)
    equip_elec_data = models.JSONField('设备电耗数据', default=list, blank=True)
    production_cost_unit = models.FloatField('单位产品成本', default=0, help_text='y/t')
    elec_consume_unit = models.FloatField('单位产品分布电耗', default=0, help_text='kw·h/t')
    celec_consume_unit = models.FloatField('单位产品综合电耗', default=0, help_text='kw·h/t')
    coal_consume_unit = models.FloatField('单位产品标煤耗', default=0, help_text='kgce/t')
    en_consume_unit = models.FloatField('单位产品能耗', default=0, help_text='tce/t')
    cen_consume_unit = models.FloatField('单位产品综合能耗', default=0, help_text='kgce/t')
    production_hour = models.FloatField('台时产量', default=0, help_text='t/h')
    total_hour_now = models.FloatField('动态总时长', default=0, help_text='h')
    run_hour = models.FloatField('运转时长', default=0, help_text='h')
    shut_hour = models.FloatField('停机时长', default=0, help_text='h')
    run_rate = models.FloatField('运转率', default=0, help_text='%')


class EnStat2(BaseModel):
    """
    能源数据统计表2
    """
    type = models.CharField('统计维度', max_length=50, default='month_s', help_text='month_s/day_s')
    year_s = models.PositiveSmallIntegerField('班年')
    month_s = models.PositiveSmallIntegerField('班月')
    day_s = models.PositiveSmallIntegerField('班日', null=True, blank=True)
    industry_total_val = models.FloatField('工业总产值', default=0, help_text='万元')
    industry_add_val = models.FloatField('工业增加值', default=0, help_text='万元')
    elec_consume =  models.FloatField('总电耗', default=0, help_text='kw·h')
    water_consume = models.FloatField('水耗', default=0, help_text='t')
    cair_consume = models.FloatField('压缩空气', default=0, help_text='m3')
    elec_coal_consume = models.FloatField('电量折标煤', default=0, help_text='tce')
    pcoal_consume = models.FloatField('煤粉消耗', default=0, help_text='t')
    pcoal_coal_consume = models.FloatField('煤粉折标煤', default=0, help_text='tce')
    bulk_cement_val =models.FloatField('散装水泥发运量', default=0)
    bulk_cement_price = models.FloatField('散装水泥价格', default=0)
    bag_cement_val = models.FloatField('袋装水泥发运量', default=0)
    bag_cement_price = models.FloatField('袋装水泥价格', default=0)
    clinker_val = models.FloatField('散装熟料发运量', default=0)
    clinker_price = models.FloatField('散装熟料价格', default=0)
    cement_val = models.FloatField('水泥产量', default=0)
    cement_cost_unit = models.FloatField('水泥单位成本', default=0)
    en_consume = models.FloatField('能源消耗', default=0, help_text='tce')
    en_consume_unit = models.FloatField('单位工业总产值能耗', default=0)
    en_add_consume_unit = models.FloatField('单位工业增加值能耗', default=0)
