from __future__ import absolute_import, unicode_literals from celery import shared_task from apps.hrm.models import Employee from dateutil import tz from datetime import datetime, timedelta from apps.third.dahua import dhClient from apps.third.tapis import dhapis from apps.hrm.services import HrmService import time @shared_task def update_all_employee_not_atwork(): """ 将所有员工设为非在岗状态 """ Employee.objects.all().update(is_atwork=False, last_check_time=None, not_work_remark=None) @shared_task def correct_swip_task(start_time, end_time): from apps.monitor.models import DrfRequestLog from apps.hrm.services import HrmService lgs = DrfRequestLog.objects.filter(path='/api/third/dahua/c_swip/', requested_at__gte=start_time, requested_at__lte=end_time) for i in lgs: HrmService.swipe(data=eval(i.data)) @shared_task def correct_card_time(): tzinfo = tz.gettz('Asia/Shanghai') s_time_f = datetime.strptime("2022-11-15 14:20:20", "%Y-%m-%d %H:%M:%S").replace(tzinfo=tzinfo) eps = Employee.objects.filter(update_time__lte = s_time_f) print(eps) for ep in eps: dh_face_card = ep.third_info.get('dh_face_card', None) dh_face_card_end = ep.third_info.get('dh_face_card_end', None) if dh_face_card and dh_face_card_end is None: departmentId = 1 if ep.belong_dept: try: departmentId = ep.belong_dept.third_info['dh_id'] except Exception: pass # 获取卡片时间 _, res = dhClient.request(**dhapis['card_detail'], params={'cardNumber': dh_face_card}) time.sleep(1) start_time_str, end_time_str = res['startDate'], res['endDate'] end_time_new = datetime.strptime(end_time_str, "%Y-%m-%d %H:%M:%S") + timedelta(hours=8) end_time_new_str = end_time_new.strftime("%Y-%m-%d %H:%M:%S") json_data = { "cardNumber": dh_face_card, "startDate": start_time_str, "endDate": end_time_new_str, "departmentId": departmentId, } _, res = dhClient.request(**dhapis['card_update'], json=json_data) HrmService.save(ep, {'dh_face_card_start': start_time_str, 'dh_face_card_end': end_time_new_str}) print('已更新-' + ep.name + '-' + dh_face_card + '-' + end_time_new_str) time.sleep(1)