from rest_framework import serializers

from .models import *

from apps.system.serializers import DictSerializer, UserListSerializer
from apps.certset.serializers import ImplementRuleSerializer

class EmployeeSerializer(serializers.ModelSerializer):
    """
    普通序列化
    """
    user = UserListSerializer()
    class Meta:
        model = Employee
        fields = '__all__'

    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('user')
        return queryset

class EmployeeUpdateSerializer(serializers.ModelSerializer):
    """
    update序列化
    """
    class Meta:
        model = Employee
        exclude = ['user']

class QualificationSerializer(serializers.ModelSerializer):
    """
    普通序列化
    """
    cert_field_ = DictSerializer(source='cert_field', read_only=True)
    auditor_type_ = DictSerializer(source='auditor_type', 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('cert_field', 'auditor_type')
        return queryset

class AbilitySerializer(serializers.ModelSerializer):
    """
    普通序列化
    """
    cert_field_ = DictSerializer(source='cert_field', read_only=True)
    major_ = DictSerializer(source='major', read_only=True)
    major_rule_code = serializers.StringRelatedField(source='major_rule')
    afrom_ = DictSerializer(source='afrom', read_only=True)
    auditor_abilitys_ = DictSerializer(source='auditor_abilitys', read_only=True, many = True)
    class Meta:
        model = Ability
        fields = '__all__'
    
    # def get_auditor_abilitys_(self, obj):
    #     return obj.auditor_abilitys.order_by('sort').values_list('name', flat=True)
    
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('cert_field', 'major', 'afrom', 'major_rule')
        queryset = queryset.prefetch_related('auditor_abilitys',)
        return queryset


    def save(self, **kwargs):
        validated_data = dict(
            list(self.validated_data.items()) +
            list(kwargs.items())
        )
        if validated_data['cert_field'] and validated_data['cert_field'].code == 'CCC':
            validated_data['major'] = None
        else:
            validated_data['major_rule'] = None
        if self.instance is not None:
            self.instance = self.update(self.instance, validated_data)
        else:
            self.instance = self.create(validated_data)
        return self.instance

class EducationSerializer(serializers.ModelSerializer):
    """
    普通序列化
    """
    level_ = DictSerializer(source='level', read_only=True)
    class Meta:
        model = Education
        fields = '__all__'

class TrainSerializer(serializers.ModelSerializer):
    """
    普通序列化
    """
    class Meta:
        model = Train
        fields = '__all__'