factory/apps/hrm/services.py

169 lines
6.4 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='', start_time=None, end_time=None):
"""同步大华信息
Args:
ep (Employee): 人员实例
old_photo (str, optional): 原照片地址. Defaults to ''.
start_time (_type_, optional): 开人脸卡起始时间. Defaults to None.
end_time (_type_, optional): 开人脸卡结束时间. Defaults to None.
Returns:
_type_: _description_
"""
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]:
dh_face_card = cls.open_face_card(
ep=ep, dh_id=dh_id, departmentId=departmentId, start_time=start_time, end_time=end_time)
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]:
dh_face_card = cls.open_face_card(
ep=ep, dh_id=dh_id, departmentId=departmentId, start_time=start_time, end_time=end_time)
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, start_time, end_time):
"""开人脸卡/有卡就更新卡时间
"""
if not ep.third_info.get('dh_face_card', None):
cardNumber = ep.third_info.get('dh_face_card')
# 如果有人脸卡就执行更新操作
if start_time is None: # 如果时间段未提供,跳过更新操作
pass
else:
startDate = start_time.strftime("%Y-%m-%d %H:%M:%S")
endDate = end_time.strftime("%Y-%m-%d %H:%M:%S")
json_data = {
"cardNumber": cardNumber,
"startDate": startDate,
"endDate": endDate,
}
_, res = dhClient.request(**dhapis['card_update'])
return cardNumber
else:
_, res = dhClient.request(**dhapis['card_gen_id'])
cardId = res['id']
cardNumber = str(ep.id)[3:8] + rannum(5)
now = datetime.now()
if start_time is None: # 如果未规定时间
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")
else:
startDate = start_time.strftime("%Y-%m-%d %H:%M:%S")
endDate = end_time.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 cardNumber
@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