from rest_framework import serializers
from .models import *
from apps.system.serializers import OrganizationSerializer

class QueryRecordSerializer(serializers.ModelSerializer):
    user_ = serializers.SerializerMethodField()
    class Meta:
        model = QueryRecord
        fields = '__all__'
    
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('user')
        return queryset
    
    def get_user_(self, obj):
        data = {'name':'', 'dept':''}
        data['name'] = obj.user.name if obj.user.name else obj.user.username
        data['dept'] = obj.user.dept.name if obj.user.dept else ""
        return data

class CMASerializer(serializers.ModelSerializer):
    """
    CMA能力表序列化
    """
    class Meta:
        model = CMA
        fields = '__all__'

class CNASSerializer(serializers.ModelSerializer):
    """
    CNAS能力表序列化
    """
    class Meta:
        model = CNAS
        fields = '__all__'
class InspectionSerializer(serializers.ModelSerializer):
    """
    检验能力反馈啊
    """
    class Meta:
        model = Inspection
        fields = '__all__'

class QualificationSerializer(serializers.ModelSerializer):
    """
    资质能力序列化
    """
    bm_=OrganizationSerializer(source = 'ssbm', read_only=True)
    class Meta:
        model = Qualification
        fields = '__all__'
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('ssbm')
        return queryset
class QualificationotherSerializer(serializers.ModelSerializer):
    qualification_ = QualificationSerializer(source = 'qualification', read_only=True)
    
    class Meta:
        model = Qualificationother
        fields = '__all__'
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('qualification')
        return queryset

class CorrectSerializer(serializers.ModelSerializer):
    """
    校验能力序列化
    """
    bm_ =OrganizationSerializer(source = 'ssbm', read_only=True)
    class Meta:
        model = Correct
        fields = '__all__'
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('ssbm')
        return queryset

