From 6cd55f84605cc8c829bc610b4bc9136dc2833d1c Mon Sep 17 00:00:00 2001 From: caoqianming Date: Wed, 5 Feb 2025 14:41:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20certificate=E6=B7=BB=E5=8A=A0state?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=8F=8A=E5=85=B6=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/hrm/filters.py | 3 ++- apps/hrm/migrations/0019_certificate_state.py | 18 ++++++++++++++++++ apps/hrm/models.py | 19 +++++++++++++++++++ apps/hrm/tasks.py | 10 ++++++++-- apps/hrm/views.py | 8 ++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 apps/hrm/migrations/0019_certificate_state.py diff --git a/apps/hrm/filters.py b/apps/hrm/filters.py index c4f28b68..0a3329c4 100755 --- a/apps/hrm/filters.py +++ b/apps/hrm/filters.py @@ -71,5 +71,6 @@ class CertificateFilterSet(filters.FilterSet): 'employee__name': ['exact', 'contains'], 'employee__user': ['exact'], 'employee__type': ['exact', 'in'], - 'type': ['exact'] + 'type': ['exact'], + "state": ['exact'], } diff --git a/apps/hrm/migrations/0019_certificate_state.py b/apps/hrm/migrations/0019_certificate_state.py new file mode 100644 index 00000000..06024fab --- /dev/null +++ b/apps/hrm/migrations/0019_certificate_state.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.12 on 2025-02-05 06:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('hrm', '0018_alter_attendance_unique_together'), + ] + + operations = [ + migrations.AddField( + model_name='certificate', + name='state', + field=models.PositiveSmallIntegerField(choices=[(10, '有效'), (20, '即将过期'), (30, '已过期')], default=10, verbose_name='证书状态'), + ), + ] diff --git a/apps/hrm/models.py b/apps/hrm/models.py index 9ff67858..9b9c48a5 100755 --- a/apps/hrm/models.py +++ b/apps/hrm/models.py @@ -2,6 +2,8 @@ from django.db import models from apps.system.models import Post, User from apps.utils.models import BaseModel, CommonADModel, CommonAModel, CommonBModel +from django.utils import timezone +from datetime import timedelta class Employee(CommonBModel): @@ -157,6 +159,10 @@ class Certificate(CommonAModel): """ 证书 """ + CERT_OK = 10 + CERT_EXPIRING = 20 + CERT_EXPIRED = 30 + CERTIFICATE_TYPE_CHOICES = ( (10, '特种作业证书'), (20, '特种设备操作证书'), @@ -169,5 +175,18 @@ class Certificate(CommonAModel): type = models.PositiveSmallIntegerField('证书类型', default=10) issue_date = models.DateField('发证日期') expiration_date = models.DateField('有效期') + state = models.PositiveSmallIntegerField('证书状态', default=CERT_OK, choices=((CERT_OK, '有效'), (CERT_EXPIRING, '即将过期'), (CERT_EXPIRED, '已过期'))) review_date = models.DateField('下一次复审日期') file = models.TextField('文件地址', null=True, blank=True) + + def get_state(self, need_update=False): + now = timezone.now().date() + new_state = self.CERT_EXPIRED + if self.expiration_date - timedelta(days=30) <= now < self.expiration_date: + new_state = self.CERT_EXPIRING + elif now < self.expiration_date - timedelta(days=30): + new_state = self.CERT_OK + if need_update and new_state != self.state: + self.state = new_state + self.save(update_fields=['state']) + return new_state \ No newline at end of file diff --git a/apps/hrm/tasks.py b/apps/hrm/tasks.py index 2f1bcfc9..cbc14d35 100755 --- a/apps/hrm/tasks.py +++ b/apps/hrm/tasks.py @@ -7,7 +7,7 @@ from celery import shared_task from dateutil import tz from django.core.cache import cache -from apps.hrm.models import Employee +from apps.hrm.models import Employee, Certificate from apps.hrm.services import HrmService from apps.third.dahua import dhClient from apps.third.tapis import dhapis @@ -129,4 +129,10 @@ def delete_face_pkl(epId): def update_global_face(): facedata = Employee.objects.filter(face_data__isnull=False, user__is_active=True).values_list('id', 'face_data') - cache.set('global_face', list(facedata), timeout=None) \ No newline at end of file + cache.set('global_face', list(facedata), timeout=None) + +@shared_task(base=CustomTask) +def check_cert_state(): + cert_qs = Certificate.objects.filter(state=Certificate.CERT_OK) + for cert in cert_qs: + cert.get_state(need_update=True) \ No newline at end of file diff --git a/apps/hrm/views.py b/apps/hrm/views.py index 8fb5bfaf..68a60cac 100755 --- a/apps/hrm/views.py +++ b/apps/hrm/views.py @@ -382,3 +382,11 @@ class CertificateViewSet(CustomModelViewSet): serializer_class = CertificateSerializer filterset_class = CertificateFilterSet search_fields = ['name', 'number', 'employee__name'] + + def perform_create(self, serializer): + ins: Certificate = serializer.save() + ins.get_state(need_update=True) + + def perform_update(self, serializer): + ins: Certificate = serializer.save() + ins.get_state(need_update=True) \ No newline at end of file