137 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
			
		
		
	
	
			137 lines
		
	
	
		
			5.0 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
| from apps.hrm.models import Employee
 | |
| from apps.third.models import TDevice
 | |
| from apps.third.tapis import dhapis
 | |
| from apps.third.clients import dhClient
 | |
| from apps.utils.tools import rannum
 | |
| from datetime import datetime
 | |
| 
 | |
| 
 | |
| class HrmService:
 | |
| 
 | |
|     @classmethod
 | |
|     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):
 | |
|                     dh_face_card = cls.open_face_card(ep=ep, dh_id=dh_id, departmentId=departmentId)
 | |
|         else:
 | |
|             _, res = dhClient.request(**dhapis['person_gen_id'])
 | |
|             dh_id = 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": 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
 | |
|             }
 | |
|             _, 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.job_state in [Employee.JOB_ON] and ep.type == 'employee':
 | |
|                 dh_face_card = cls.open_face_card(ep=ep, dh_id=dh_id, departmentId=departmentId)
 | |
|         return {'dh_id': dh_id, 'dh_photo': dh_photo, 'dh_face_card': dh_face_card}
 | |
| 
 | |
|     @classmethod
 | |
|     def open_face_card(cls, ep, dh_id, departmentId):
 | |
|         """开人脸卡
 | |
|         """
 | |
|         _, res = dhClient.request(**dhapis['card_gen_id'])
 | |
|         cardId = res['id']
 | |
|         cardNumber = str(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)
 | |
|         return cardId
 | |
| 
 | |
|     @classmethod
 | |
|     def swipe(cls, data: dict):
 | |
|         """
 | |
|         刷卡事件/用于记录考勤
 | |
|         """
 | |
|         deviceCode = data['infoArray']['deviceCode']
 | |
|         device = TDevice.objects.filter(code=deviceCode).first()
 | |
|         if not device:
 | |
|             device = TDevice.objects.create(type=TDevice.DEVICE_DCHANNEL, code=deviceCode)
 | |
|         if device.is_clock:
 | |
|             pass
 |