from utils import serializer
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(read_only=True)
    
    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 TeamMemberSerializer(serializers.ModelSerializer):
    """
    选人序列化
    """
    user_ = UserListSerializer(source='user', read_only=True)
    majors = serializers.SerializerMethodField()
    
    class Meta:
        model = Employee
        fields = ['code', 'user', 'user_', 'photo', 'fields', 'majors']
    
    def get_majors(self, obj):
        majors = obj.ability_employee.all()
        m = []
        for i in majors:
            n = {'major':'', 'abilitys':''}
            if not i.is_paused:
                if i.major:
                    n['major'] = i.major.code
                    n['abilitys'] = i.auditor_abilitys.values_list('name', flat=True)
                    m.append(n)
                elif i.major_rule:
                    n['major'] = i.major_rule.code
                    n['abilitys'] = i.auditor_abilitys.values_list('name', flat=True)
                    m.append(n)
        return m
    
    @staticmethod
    def setup_eager_loading(queryset):
        """ Perform necessary eager loading of data. """
        queryset = queryset.select_related('user')
        return queryset


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

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


class TrainSerializer(serializers.ModelSerializer):
    """
    普通序列化
    """
    
    employees_user_name = serializers.StringRelatedField(source='employees',read_only=True, many=True)
    class Meta:
        model = Train
        fields = ['id', 'name', 'start_time', 'end_time', 'hours',
                  'train_org', 'teacher', 'content', 'rain_address', 'kaohe_type', 'remark', 'employees', 'employees_user_name']