from apps.utils.serializers import CustomModelSerializer
from apps.mtm.models import Shift, Material, Mgroup, Team, Goal, Process, Route, TeamMember, RoutePack, Srule
from apps.utils.constants import EXCLUDE_FIELDS, EXCLUDE_FIELDS_BASE, EXCLUDE_FIELDS_DEPT
from rest_framework import serializers
from rest_framework.exceptions import ValidationError, ParseError
from apps.system.models import Dept, UserPost
from django.db import transaction
from apps.wf.serializers import TicketSimpleSerializer


class ShiftSerializer(CustomModelSerializer):
    class Meta:
        model = Shift
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS


class MaterialSimpleSerializer(CustomModelSerializer):
    full_name = serializers.SerializerMethodField()
    process_name = serializers.CharField(source='process.name', read_only=True)

    class Meta:
        model = Material
        fields = ['id', 'name', 'number', 'model',
                  'specification', 'type', 'cate', 'brothers', 'process_name', 'full_name', "tracking"]

    def get_full_name(self, obj):
        return f'{obj.name}|{obj.specification if obj.specification else ""}|{obj.model if obj.model else ""}|{obj.process.name if obj.process else ""}'


class MaterialSerializer(CustomModelSerializer):
    process_name = serializers.CharField(source='process.name', read_only=True)
    full_name = serializers.SerializerMethodField()

    class Meta:
        model = Material
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS + ['count']

    def get_full_name(self, obj):
        return f'{obj.name}|{obj.specification if obj.specification else ""}|{obj.model if obj.model else ""}|{obj.process.name if obj.process else ""}'

    def create(self, validated_data):
        if Material.objects.filter(name=validated_data['name'], specification=validated_data.get('specification', None), model=validated_data.get('model', None), process=validated_data.get('process', None)).exists():
            raise serializers.ValidationError('物料已存在')
        return super().create(validated_data)
    
    def validate(self, attrs):
        type = attrs['type']
        if type in [Material.MA_TYPE_GOOD, Material.MA_TYPE_HALFGOOD]:
            if attrs.get('process', None) is None:
                raise ValidationError('半成品/成品必须指定所到工序')
        return super().validate(attrs)


class MgroupSerializer(CustomModelSerializer):
    belong_dept = serializers.PrimaryKeyRelatedField(
        label="所属部门", queryset=Dept.objects.all(), required=True)
    belong_dept_name = serializers.CharField(
        source='belong_dept.name', read_only=True)
    process_name = serializers.CharField(source='process.name', read_only=True)
    process_type = serializers.CharField(source='process.type', read_only=True)
    process_cate = serializers.CharField(source='process.cate', read_only=True)

    class Meta:
        model = Mgroup
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS


class TeamMemberSerializer(CustomModelSerializer):
    user_name = serializers.CharField(source='user.name', read_only=True)
    mgroup_name = serializers.CharField(source='mgroup.name', read_only=True)
    post_name = serializers.CharField(source='post.name', read_only=True)

    class Meta:
        model = TeamMember
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS_BASE

    def validate(self, attrs):
        user = attrs['user']
        post = attrs.get('post', None)
        if post is None:
            userpost = UserPost.objects.filter(
                dept=attrs['mgroup'].belong_dept, user=user).first()
            if userpost:
                attrs['post'] = userpost.post
            else:
                raise ParseError(f'{user.name}未配置岗位')
        return super().validate(attrs)


class TeamSerializer(CustomModelSerializer):
    belong_dept = serializers.PrimaryKeyRelatedField(
        label="所属部门", queryset=Dept.objects.all(), required=True)
    leader_name = serializers.CharField(source='leader.name', read_only=True)
    belong_dept_name = serializers.CharField(
        source='belong_dept.name', read_only=True)

    class Meta:
        model = Team
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS


class GoalSerializer(CustomModelSerializer):
    goal_cate_name = serializers.CharField(
        source='goal_cate.name', read_only=True)
    goal_cate_code = serializers.CharField(source='goal_cate.code', read_only=True)
    mgroup_name = serializers.CharField(source='mgroup.name', read_only=True)

    class Meta:
        model = Goal
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS


class MgroupGoalYearSerializer(serializers.Serializer):
    mgroup = serializers.CharField(label='工段ID')
    year = serializers.IntegerField(label='年')


class ProcessSerializer(CustomModelSerializer):
    belong_dept_name = serializers.CharField(
        source='belong_dept.name', read_only=True)

    class Meta:
        model = Process
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS


class RoutePackSerializer(CustomModelSerializer):
    material_name = serializers.StringRelatedField(
        source='material', read_only=True)
    create_by_name = serializers.CharField(source='create_by.name', read_only=True)
    ticket_ = TicketSimpleSerializer(source='ticket', read_only=True)
    class Meta:
        model = RoutePack
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS + ['state', 'ticket']
    
    def update(self, instance, validated_data):
        if validated_data['material'] != instance.material:
            raise ParseError('不可变更产品')
        return super().update(instance, validated_data)


class RouteSerializer(CustomModelSerializer):
    material_ = MaterialSerializer(source='material', read_only=True)
    routepack_name = serializers.StringRelatedField(source='routepack.name', read_only=True)
    process_name = serializers.CharField(source='process.name', read_only=True)
    process_cate = serializers.CharField(source='process.cate', read_only=True)
    material_in_name = serializers.StringRelatedField(
        source='material_in', read_only=True)
    material_out_name = serializers.StringRelatedField(
        source='material_out', read_only=True)
    material_out_type = serializers.CharField(
        source='material_out.type', read_only=True)
    material_out_is_hidden = serializers.BooleanField(
        source='material_out.is_hidden', read_only=True)

    class Meta:
        model = Route
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS

    def validate(self, attrs):
        if attrs.get('routepack', None):
            attrs['material'] = attrs['routepack'].material
        if 'mgroup' in attrs and attrs['mgroup']:
            attrs['process'] = attrs['mgroup'].process
        process: Process = attrs.get('process', None)
        if process is None:
            raise ParseError('未提供操作工序')
        if process.mtype == Process.PRO_DIV and attrs.get('div_number', 1) <= 1:
            raise ParseError('切分数量必须大于1') 
        return super().validate(attrs)

    def gen_material_out(self, instance: Route):
        """
        自动形成物料
        """
        material = instance.material
        process = instance.process
        material_out: Material = Material.objects.get_queryset(all=True).filter(type=Material.MA_TYPE_HALFGOOD, parent=material, process=process).first()
        if material_out:
            material_out.is_deleted = False
            if material_out.parent == material:
                material_out.name = material.name
                material_out.model = material.model
                material_out.specification = material.specification
                material_out.cate = material.cate
                material_out.tracking = material.tracking
            material_out.save()
            instance.material_out = material_out
            instance.save()
            return
        material_out = Material.objects.get_queryset(all=True).filter(name=material.name, model=material.model, process=process, specification=material.specification).first()
        if material_out:
            material_out.is_deleted = False
            if material_out.parent is None:
                material_out.parent = material
                material_out.cate = material.cate
                material_out.tracking = material.tracking
            material_out.save()
            instance.material_out = material_out
            instance.save()
            return
        material_out = Material.objects.create(**{'parent': instance.material, 'process': instance.process,
                                                                                          'is_hidden': True, 'name': material.name,
                                                                                          'number': material.number,
                                                                                          'specification': material.specification,
                                                                                          'model': material.model,
                                                                                          'type': Material.MA_TYPE_HALFGOOD,
                                                                                          'cate': material.cate,
                                                                                          'tracking': material.tracking,
                                                                                          'create_by': self.request.user,
                                                                                          'update_by': self.request.user,
                                                                                          })
        instance.material_out = material_out
        instance.save()
        return

    def create(self, validated_data):
        process = validated_data['process']
        routepack = validated_data.get('routepack', None)
        if routepack:
            if Route.objects.filter(routepack=routepack, process=process).exists():
                raise ValidationError('已选择该工序!')
        else:
            material = validated_data.get('material', None)
            if material and process and Route.objects.filter(material=material, process=process).exists():
                raise ValidationError('已选择该工序!!')
        with transaction.atomic():
            instance = super().create(validated_data)
            material_out = instance.material_out
            if material_out:
                if material_out.process is None:
                    material_out.process = process
                    if instance.material:
                        material_out.parent = instance.material
                    material_out.save()
                elif material_out.process != process:
                    raise ParseError('物料工序错误!请重新选择')
            else:
                if instance.material:
                    self.gen_material_out(instance)
            return instance

    def update(self, instance, validated_data):
        validated_data.pop('material', None)
        process = validated_data.pop('process', None)
        with transaction.atomic():
            instance = super().update(instance, validated_data)
            material_out = instance.material_out
            if material_out:
                if material_out.process is None:
                    material_out.process = process
                    if instance.material:
                        material_out.parent = instance.material
                    material_out.save()
                elif material_out.process != process:
                    raise ParseError('物料工序错误!请重新选择')
            else:
                if instance.material:
                    self.gen_material_out(instance)
            return instance


class SruleSerializer(CustomModelSerializer):
    belong_dept_name = serializers.CharField(source='belong_dept.name', read_only=True)
    class Meta:
        model = Srule
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS


class MgroupDaysSerializer(serializers.Serializer):
    mgroup = serializers.CharField(label="工段ID")
    now = serializers.DateTimeField(label="当前时间", required=False)