From 7f56c5e39e465faf6018c58e345f26249d3a16e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E5=89=8D=E6=98=8E?= <909355014@qq.com> Date: Thu, 23 Jun 2022 17:03:58 +0800 Subject: [PATCH] dahua face hard --- apps/hrm/serializers.py | 133 +++------------------------------------ apps/hrm/services.py | 134 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 123 deletions(-) diff --git a/apps/hrm/serializers.py b/apps/hrm/serializers.py index a2d0be79..340b4720 100755 --- a/apps/hrm/serializers.py +++ b/apps/hrm/serializers.py @@ -1,6 +1,7 @@ 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 @@ -49,62 +50,10 @@ class EmployeeCreateUpdateSerializer(EmployeeBaseSerializer): def create(self, validated_data): instance = super().create(validated_data) if settings.DAHUA_ENABLED and dhClient: - # 创建人员 - _, res = dhClient.request(**dhapis['person_gen_id']) - personId = res['id'] - departmentId = 1 - if instance.belong_dept: - try: - departmentId = instance.belong_dept.third_info['dh_id'] - except Exception: - pass - json_data = { - "service": "ehs", - "id": personId, - "name": instance.name, - "code": instance.number, - "paperType": 111, - "paperNumber": instance.id_number, - "paperAddress": "default", - "departmentId": departmentId, - "phone": instance.phone, - "email": instance.email, - "sex": 1 if instance.gender == '男' else 2 - } - _, res = dhClient.request(**dhapis['person_img_upload'], file_path_rela=instance.photo) - dh_photo = res["fileUrl"] - json_data.update( - { - "biosignatureTypeList": [3], - "personBiosignatures": [{ - "type": 3, - "index": 1, - "path": dh_photo - }] - } - ) - _, res = dhClient.request(**dhapis['person_add'], json=json_data) - # 开人脸卡 - _, res = dhClient.request(**dhapis['card_gen_id']) - cardId = res['id'] - cardNumber = instance.id[:8] + rannum(2) - now = datetime.now() - startDate = now.strftime("%Y-%m-%d %H:%M:%S") - endDate = (datetime(year=now.year+50, - month=now.month, day=1)).strftime("%Y-%m-%d %H:%M:%S") - json_data = { - "id": cardId, - "cardNumber": cardNumber, - "category": 0, - "cardType": 0, - "personId": personId, - "departmentId": departmentId, - "startDate": startDate, - "endDate": endDate - } - _, res = dhClient.request(**dhapis['card_add'], json=json_data) - instance.third_info = {'dh_id': personId, - 'dh_photo': dh_photo, 'dh_face_card': res['id']} + 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 @@ -112,73 +61,11 @@ class EmployeeCreateUpdateSerializer(EmployeeBaseSerializer): def update(self, instance, validated_data): old_photo = instance.photo instance = super().update(instance, validated_data) - departmentId = 1 - if instance.belong_dept: - try: - departmentId = instance.belong_dept.third_info['dh_id'] - except Exception: - pass - if settings.DAHUA_ENABLED and dhClient: - third_info = instance.third_info - dh_id = instance.third_info['dh_id'] - dh_photo = third_info['dh_photo'] - json_data = { - "service": "ehs", - "id": dh_id, - "name": instance.name, - "code": instance.number, - "paperType": 111, - "paperNumber": instance.id_number, - "paperAddress": "default", - "departmentId": departmentId, - "phone": instance.phone, - "email": instance.email, - "sex": 1 if instance.gender == '男' else 2, - "biosignatureTypeList": [3], - "personBiosignatures": [{ - "type": 3, - "index": 1, - "path": third_info['dh_photo'] - }] - } - if instance.photo != old_photo: - _, res = dhClient.request(**dhapis['person_img_upload'], file_path_rela=instance.photo) - dh_photo = res["fileUrl"] - json_data.update( - { - "biosignatureTypeList": [3], - "personBiosignatures": [{ - "type": 3, - "index": 1, - "path": dh_photo - }] - } - ) - third_info['dh_photo'] = dh_photo - dhClient.request(**dhapis['person_update'], json=json_data) - # 开人脸卡 - if instance.job_state in [Employee.JOB_ON]: - if not third_info.get('dh_face_card', None): - _, res = dhClient.request(**dhapis['card_gen_id']) - cardId = res['id'] - cardNumber = instance.id[3:8] + rannum(5) - now = datetime.now() - startDate = now.strftime("%Y-%m-%d %H:%M:%S") - endDate = (datetime(year=now.year+50, - month=now.month, day=1)).strftime("%Y-%m-%d %H:%M:%S") - json_data = { - "id": cardId, - "cardNumber": cardNumber, - "category": 0, - "cardType": 0, - "personId": third_info['dh_id'], - "departmentId": departmentId, - "startDate": startDate, - "endDate": endDate - } - _, res = dhClient.request(**dhapis['card_add'], json=json_data) - third_info['dh_face_card'] = cardNumber - instance.save() + 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 diff --git a/apps/hrm/services.py b/apps/hrm/services.py index e69de29b..07af774d 100755 --- a/apps/hrm/services.py +++ b/apps/hrm/services.py @@ -0,0 +1,134 @@ +from apps.hrm.models import Employee +from apps.third.tapis import dhapis +from apps.third.clients import dhClient +from apps.utils.tools import rannum +from datetime import datetime + + +class HrmService: + + def sync_dahua_employee(cls, ep: Employee, old_photo=''): + dh_id = None + dh_photo = None + dh_face_card = None + departmentId = 1 + if ep.belong_dept: + try: + departmentId = ep.belong_dept.third_info['dh_id'] + except Exception: + pass + if ep.third_info.get('dh_id', None): # 如果有大华信息 + dh_id = ep.third_info['dh_id'] + dh_photo = ep.third_info['dh_photo'] + json_data = { + "service": "ehs", + "id": dh_id, + "name": ep.name, + "code": ep.number, + "paperType": 111, + "paperNumber": ep.id_number, + "paperAddress": "default", + "departmentId": departmentId, + "phone": ep.phone, + "email": ep.email, + "sex": 1 if ep.gender == '男' else 2, + "biosignatureTypeList": [3], + "personBiosignatures": [{ + "type": 3, + "index": 1, + "path": dh_photo + }] + } + if ep.photo != old_photo: + _, res = dhClient.request(**dhapis['person_img_upload'], file_path_rela=ep.photo) + dh_photo = res["fileUrl"] + json_data.update( + { + "biosignatureTypeList": [3], + "personBiosignatures": [{ + "type": 3, + "index": 1, + "path": dh_photo + }] + } + ) + dhClient.request(**dhapis['person_update'], json=json_data) + # 开人脸卡 长时间 + if ep.job_state in [Employee.JOB_ON] and ep.type == 'employee': + if not ep.third_info.get('dh_face_card', None): + _, res = dhClient.request(**dhapis['card_gen_id']) + cardId = res['id'] + cardNumber = ep.id[3:8] + rannum(5) + now = datetime.now() + startDate = now.strftime("%Y-%m-%d %H:%M:%S") + endDate = (datetime(year=now.year+50, + month=now.month, day=1)).strftime("%Y-%m-%d %H:%M:%S") + json_data = { + "id": cardId, + "cardNumber": cardNumber, + "category": 0, + "cardType": 0, + "personId": dh_id, + "departmentId": departmentId, + "startDate": startDate, + "endDate": endDate + } + _, res = dhClient.request(**dhapis['card_add'], json=json_data) + dh_face_card = cardNumber + else: + _, res = dhClient.request(**dhapis['person_gen_id']) + personId = res['id'] + departmentId = 1 + if ep.belong_dept: + try: + departmentId = ep.belong_dept.third_info['dh_id'] + except Exception: + pass + json_data = { + "service": "ehs", + "id": personId, + "name": ep.name, + "code": ep.number, + "paperType": 111, + "paperNumber": ep.id_number, + "paperAddress": "default", + "departmentId": departmentId, + "phone": ep.phone, + "email": ep.email, + "sex": 1 if ep.gender == '男' else 2 + } + _, res = dhClient.request(**dhapis['person_img_upload'], file_path_rela=ep.photo) + dh_photo = res["fileUrl"] + json_data.update( + { + "biosignatureTypeList": [3], + "personBiosignatures": [{ + "type": 3, + "index": 1, + "path": dh_photo + }] + } + ) + _, res = dhClient.request(**dhapis['person_add'], json=json_data) + # 开人脸卡 + if ep.type == 'employee': + _, res = dhClient.request(**dhapis['card_gen_id']) + cardId = res['id'] + cardNumber = ep.id[:8] + rannum(2) + now = datetime.now() + startDate = now.strftime("%Y-%m-%d %H:%M:%S") + endDate = (datetime(year=now.year+50, + month=now.month, day=1)).strftime("%Y-%m-%d %H:%M:%S") + json_data = { + "id": cardId, + "cardNumber": cardNumber, + "category": 0, + "cardType": 0, + "personId": personId, + "departmentId": departmentId, + "startDate": startDate, + "endDate": endDate + } + _, res = dhClient.request(**dhapis['card_add'], json=json_data) + dh_face_card = res['id'] + return {'dh_id': personId, 'dh_photo': dh_photo, 'dh_face_card': dh_face_card}