188 lines
7.4 KiB
Python
188 lines
7.4 KiB
Python
from apps.utils.serializers import CustomModelSerializer
|
|
from apps.enm.models import Mpoint, MpointStat, EnStat, EnStat2, MpLogx
|
|
from apps.utils.constants import EXCLUDE_FIELDS
|
|
from rest_framework import serializers
|
|
from apps.mtm.models import Mgroup
|
|
from rest_framework.exceptions import ParseError
|
|
from django.core.cache import cache
|
|
|
|
|
|
class MpointSerializer(CustomModelSerializer):
|
|
mgroup = serializers.PrimaryKeyRelatedField(label="测点集", queryset=Mgroup.objects.all(), required=False, allow_null=True)
|
|
mgroup_name = serializers.CharField(source="mgroup.name", read_only=True)
|
|
belong_dept_name = serializers.CharField(source="belong_dept.name", read_only=True)
|
|
ep_monitored_name = serializers.CharField(source="ep_monitored.name", read_only=True)
|
|
ep_monitored_power_kw = serializers.CharField(source="ep_monitored.power_kw", read_only=True)
|
|
ep_belong_name = serializers.CharField(source="ep_belong.name", read_only=True)
|
|
material_name = serializers.CharField(source="material.name", read_only=True)
|
|
formula = serializers.CharField(allow_blank=True, required=False)
|
|
last_data = serializers.SerializerMethodField()
|
|
gather_state = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Mpoint
|
|
fields = "__all__"
|
|
read_only_fields = EXCLUDE_FIELDS + ["belong_dept", "cate"]
|
|
|
|
def get_last_data(self, obj):
|
|
cache_mp = cache.get(Mpoint.cache_key(obj.code))
|
|
if isinstance(cache_mp, dict):
|
|
last_data = cache_mp.get('last_data', {})
|
|
else:
|
|
last_data = {}
|
|
return last_data
|
|
|
|
def get_gather_state(self, obj):
|
|
cache_mp = cache.get(Mpoint.cache_key(obj.code))
|
|
if isinstance(cache_mp, dict):
|
|
gather_state = cache_mp.get('gather_state', -2)
|
|
else:
|
|
gather_state = -2
|
|
return gather_state
|
|
|
|
def validate(self, attrs):
|
|
if "material" in attrs and attrs["material"]:
|
|
attrs["cate"] = "material"
|
|
if "mgroup" in attrs and attrs["mgroup"]:
|
|
attrs["belong_dept"] = attrs["mgroup"].belong_dept
|
|
formula = attrs.get("formula", "")
|
|
if formula:
|
|
pass
|
|
return attrs
|
|
|
|
|
|
# class MpLogSerializer(CustomModelSerializer):
|
|
# mpoint_name = serializers.CharField(source='mpoint.name', read_only=True)
|
|
# class Meta:
|
|
# model = MpLog
|
|
# fields = '__all__'
|
|
# read_only_fields = EXCLUDE_FIELDS + ['mpoint_name']
|
|
|
|
|
|
class MpLogxSerializer(CustomModelSerializer):
|
|
"""Serializer for EnvData model"""
|
|
|
|
class Meta:
|
|
model = MpLogx
|
|
fields = "__all__"
|
|
|
|
|
|
class MpointStatSerializer(CustomModelSerializer):
|
|
mpoint_name = serializers.CharField(source="mpoint.name", read_only=True)
|
|
mpoint_nickname = serializers.CharField(source="mpoint.nickname", read_only=True)
|
|
ep_monitored_name = serializers.CharField(source="mpoint.ep_monitored.name", read_only=True)
|
|
ep_monitored_number = serializers.CharField(source="mpoint.ep_monitored.number", read_only=True)
|
|
ep_monitored_power_kw = serializers.CharField(source="mpoint.ep_monitored.power_kw", read_only=True)
|
|
ep_belong_name = serializers.CharField(source="mpoint.ep_belong.name", read_only=True)
|
|
mgroup_name = serializers.CharField(source="mgroup.name", read_only=True)
|
|
belong_dept_name = serializers.CharField(source="mgroup.belong_dept.name", read_only=True)
|
|
|
|
class Meta:
|
|
model = MpointStat
|
|
fields = "__all__"
|
|
read_only_fields = EXCLUDE_FIELDS + ["mpoint_name", "type", "year", "month", "day"]
|
|
|
|
def check_required_keys(dictionary, keys):
|
|
for key in keys:
|
|
if key not in dictionary or not dictionary[key]:
|
|
return False
|
|
return True
|
|
|
|
def create(self, validated_data):
|
|
if MpointStat.objects.filter(mpoint=validated_data["mpoint"], sflog=validated_data["sflog"]).exists():
|
|
raise ParseError("该数据已录入")
|
|
return super().create(validated_data)
|
|
|
|
def validate(self, attrs):
|
|
mpoint = attrs["mpoint"]
|
|
if 'mgroup' not in attrs:
|
|
raise ParseError("请选择工段")
|
|
if mpoint.material and mpoint.type is Mpoint.MT_MANUAL and "sflog" in attrs and attrs["sflog"]:
|
|
attrs["type"] = "sflog"
|
|
sflog = attrs["sflog"]
|
|
attrs["year_s"], attrs["month_s"], attrs["day_s"] = sflog.get_ymd
|
|
attrs["mgroup"] = sflog.mgroup
|
|
else:
|
|
raise ParseError("该数据不支持手工录入")
|
|
# if 'sflog' in attrs and attrs['sflog']:
|
|
|
|
# else:
|
|
# keys = ['hour', 'day_s', 'month_s', 'year_s']
|
|
# for ind, key in enumerate(keys):
|
|
# if key in attrs and attrs['key']:
|
|
# if not self.check_required_keys(attrs, keys[ind+1:]):
|
|
# raise ParseError('缺少数据')
|
|
# attrs['type'] = key
|
|
return super().validate(attrs)
|
|
|
|
def to_representation(self, instance):
|
|
ret = super().to_representation(instance)
|
|
my_dic_keys = list(ret.keys())
|
|
for key in my_dic_keys:
|
|
ret_one_val = ret[key]
|
|
if isinstance(ret_one_val, float):
|
|
ret[key] = "{:.2f}".format(round(ret_one_val, 2))
|
|
return ret
|
|
|
|
|
|
class EnStatSerializer(CustomModelSerializer):
|
|
mgroup_name = serializers.CharField(source="mgroup.name", read_only=True)
|
|
team_name = serializers.CharField(source="team.name", read_only=True)
|
|
belong_dept_name = serializers.CharField(source="mgroup.belong_dept.name", read_only=True)
|
|
|
|
class Meta:
|
|
model = EnStat
|
|
fields = "__all__"
|
|
|
|
def to_representation(self, instance):
|
|
ret = super().to_representation(instance)
|
|
if 'run_sec' in ret:
|
|
ret['run_hour'] = ret['run_sec'] / 3600
|
|
my_dic_keys = list(ret.keys())
|
|
for key in my_dic_keys:
|
|
ret_one_val = ret[key]
|
|
if isinstance(ret_one_val, float):
|
|
ret[key] = "{:.2f}".format(round(ret_one_val, 2))
|
|
qua_data = ret.get("qua_data", {})
|
|
equip_elec_data = ret.get("equip_elec_data", {})
|
|
if qua_data:
|
|
for item in qua_data:
|
|
ret[f'{item["material_name"]}_{item["testitem_name"]}_rate_pass'] = "{:.2f}".format(round(item["rate_pass"], 4))
|
|
if equip_elec_data:
|
|
for item in equip_elec_data:
|
|
val = item.get("consume_unit", None)
|
|
if val:
|
|
val = "{:.2f}".format(round(val, 2))
|
|
ret[f'{item["equipment_name"]}_consume_unit'] = val
|
|
return ret
|
|
|
|
|
|
class EnStat2Serializer(CustomModelSerializer):
|
|
class Meta:
|
|
model = EnStat2
|
|
fields = "__all__"
|
|
|
|
def to_representation(self, instance):
|
|
ret = super().to_representation(instance)
|
|
my_dic_keys = list(ret.keys())
|
|
for key in my_dic_keys:
|
|
ret_one_val = ret[key]
|
|
if isinstance(ret_one_val, float):
|
|
ret[key] = "{:.2f}".format(round(ret_one_val, 2))
|
|
return ret
|
|
|
|
|
|
class ReCalSerializer(serializers.Serializer):
|
|
start_time = serializers.DateTimeField(label="开始时间")
|
|
end_time = serializers.DateTimeField(label="结束时间")
|
|
|
|
|
|
class MpointStatCorrectSerializer(CustomModelSerializer):
|
|
class Meta:
|
|
model = MpointStat
|
|
fields = ['val_correct', 'id']
|
|
|
|
class EnStatAnaSerializer(serializers.Serializer):
|
|
start_date = serializers.DateField(label="开始日期")
|
|
end_date = serializers.DateField(label="结束日期")
|