135 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
| from apps.hrm.errors import PHONE_F_WRONG
 | |
| from rest_framework.serializers import ModelSerializer
 | |
| from rest_framework import serializers
 | |
| from apps.hrm.services import HrmService
 | |
| 
 | |
| from apps.utils.serializers import CustomModelSerializer
 | |
| from apps.utils.constants import EXCLUDE_FIELDS, EXCLUDE_FIELDS_BASE
 | |
| from apps.utils.tools import rannum
 | |
| from apps.hrm.models import Certificate, ClockRecord, Employee, NotWorkRemark
 | |
| from apps.system.serializers import DeptSimpleSerializer, UserSimpleSerializer
 | |
| from django.db import transaction
 | |
| from apps.third.clients import dhClient
 | |
| from apps.third.tapis import dhapis
 | |
| import re
 | |
| from datetime import datetime
 | |
| from django.conf import settings
 | |
| 
 | |
| 
 | |
| class EmployeeSimpleSerializer(CustomModelSerializer):
 | |
|     belong_dept_name = serializers.CharField(source='belong_dept.name', read_only=True)
 | |
|     post_name = serializers.CharField(source='post.name', read_only=True)
 | |
| 
 | |
|     class Meta:
 | |
|         model = Employee
 | |
|         fields = ['id', 'type', 'name', 'belong_dept', 'belong_dept_name', 'post', 'post_name', 'photo']
 | |
| 
 | |
| 
 | |
| class EmployeeBaseSerializer(CustomModelSerializer):
 | |
|     def save(self, **kwargs):
 | |
|         if self.validated_data.get('user', None):
 | |
|             user = self.validated_data['user']
 | |
|             self.validated_data['name'] = user.name
 | |
|             self.validated_data['belong_dept'] = user.belong_dept
 | |
|         return super().save(**kwargs)
 | |
| 
 | |
| 
 | |
| def phone_check(phone):
 | |
|     re_phone = r'^1[358]\d{9}$|^147\d{8}$|^176\d{8}$'
 | |
|     if not re.match(re_phone, phone):
 | |
|         raise serializers.ValidationError(**PHONE_F_WRONG)
 | |
|     return phone
 | |
| 
 | |
| 
 | |
| class EmployeeCreateUpdateSerializer(EmployeeBaseSerializer):
 | |
|     phone = serializers.CharField(label="手机号")
 | |
| 
 | |
|     class Meta:
 | |
|         model = Employee
 | |
|         exclude = EXCLUDE_FIELDS + ['is_atwork', 'last_check_time',
 | |
|                                     'not_work_remark', 'third_info', 'type']
 | |
|         extra_kwargs = {
 | |
|             'phone': {'required': True},
 | |
|             'number': {'required': True},
 | |
|             'photo': {'required': True},
 | |
|             'id_number': {'required': True},
 | |
|         }
 | |
| 
 | |
|     @transaction.atomic
 | |
|     def create(self, validated_data):
 | |
|         instance = super().create(validated_data)
 | |
|         if instance.user:  # 同步更新账户里的手机号
 | |
|             user = instance.user
 | |
|             user.phone = instance.phone
 | |
|             user.save()
 | |
|         if settings.DAHUA_ENABLED and dhClient:
 | |
|             dahua_data = HrmService.sync_dahua_employee(ep=instance)
 | |
|             third_info = instance.third_info
 | |
|             third_info.update(dahua_data)
 | |
|             instance.third_info = third_info
 | |
|             instance.save()
 | |
|         return instance
 | |
| 
 | |
|     def update(self, instance, validated_data):
 | |
|         old_photo = instance.photo
 | |
|         instance = super().update(instance, validated_data)
 | |
|         if instance.user:  # 同步更新账户里的手机号
 | |
|             user = instance.user
 | |
|             user.phone = instance.phone
 | |
|             user.save()
 | |
|         if settings.DAHUA_ENABLED and dhClient:
 | |
|             dahua_data = HrmService.sync_dahua_employee(ep=instance, old_photo=old_photo)
 | |
|             third_info = instance.third_info
 | |
|             third_info.update(dahua_data)
 | |
|             instance.third_info = third_info
 | |
|             instance.save()
 | |
|         return instance
 | |
| 
 | |
| 
 | |
| class ChannelAuthoritySerializer(serializers.Serializer):
 | |
|     pks = serializers.ListField(child=serializers.CharField(max_length=20), label="员工ID列表")
 | |
|     channels = serializers.ListField(child=serializers.CharField(max_length=20), label="门通道ID列表")
 | |
| 
 | |
| 
 | |
| class EmployeeSerializer(EmployeeBaseSerializer):
 | |
|     belong_dept_ = DeptSimpleSerializer(source='belong_dept', read_only=True)
 | |
| 
 | |
|     class Meta:
 | |
|         model = Employee
 | |
|         fields = '__all__'
 | |
|         read_only_fields = ['is_atwork', 'last_check_time', 'not_work_remark']
 | |
| 
 | |
| 
 | |
| class EmployeeNotWorkRemarkSerializer(ModelSerializer):
 | |
|     class Meta:
 | |
|         model = Employee
 | |
|         fields = ['not_work_remark']
 | |
| 
 | |
| 
 | |
| class ClockRecordListSerializer(serializers.ModelSerializer):
 | |
|     create_by_ = UserSimpleSerializer(source='create_by', read_only=True)
 | |
| 
 | |
|     class Meta:
 | |
|         model = ClockRecord
 | |
|         fields = '__all__'
 | |
| 
 | |
| 
 | |
| class NotWorkRemarkListSerializer(serializers.ModelSerializer):
 | |
|     class Meta:
 | |
|         model = NotWorkRemark
 | |
|         fields = '__all__'
 | |
| 
 | |
| 
 | |
| class CertificateCreateUpdateSerializer(CustomModelSerializer):
 | |
|     class Meta:
 | |
|         model = Certificate
 | |
|         exclude = EXCLUDE_FIELDS
 | |
| 
 | |
| 
 | |
| class CertificateSerializer(CustomModelSerializer):
 | |
|     employee_name = serializers.CharField(source='employee.name', read_only=True)
 | |
| 
 | |
|     class Meta:
 | |
|         model = Certificate
 | |
|         fields = '__all__'
 |