feat: certificate添加state字段及其处理

This commit is contained in:
caoqianming 2025-02-05 14:41:32 +08:00
parent 821b88eb90
commit 6cd55f8460
5 changed files with 55 additions and 3 deletions

View File

@ -71,5 +71,6 @@ class CertificateFilterSet(filters.FilterSet):
'employee__name': ['exact', 'contains'], 'employee__name': ['exact', 'contains'],
'employee__user': ['exact'], 'employee__user': ['exact'],
'employee__type': ['exact', 'in'], 'employee__type': ['exact', 'in'],
'type': ['exact'] 'type': ['exact'],
"state": ['exact'],
} }

View File

@ -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='证书状态'),
),
]

View File

@ -2,6 +2,8 @@ from django.db import models
from apps.system.models import Post, User from apps.system.models import Post, User
from apps.utils.models import BaseModel, CommonADModel, CommonAModel, CommonBModel from apps.utils.models import BaseModel, CommonADModel, CommonAModel, CommonBModel
from django.utils import timezone
from datetime import timedelta
class Employee(CommonBModel): class Employee(CommonBModel):
@ -157,6 +159,10 @@ class Certificate(CommonAModel):
""" """
证书 证书
""" """
CERT_OK = 10
CERT_EXPIRING = 20
CERT_EXPIRED = 30
CERTIFICATE_TYPE_CHOICES = ( CERTIFICATE_TYPE_CHOICES = (
(10, '特种作业证书'), (10, '特种作业证书'),
(20, '特种设备操作证书'), (20, '特种设备操作证书'),
@ -169,5 +175,18 @@ class Certificate(CommonAModel):
type = models.PositiveSmallIntegerField('证书类型', default=10) type = models.PositiveSmallIntegerField('证书类型', default=10)
issue_date = models.DateField('发证日期') issue_date = models.DateField('发证日期')
expiration_date = models.DateField('有效期') expiration_date = models.DateField('有效期')
state = models.PositiveSmallIntegerField('证书状态', default=CERT_OK, choices=((CERT_OK, '有效'), (CERT_EXPIRING, '即将过期'), (CERT_EXPIRED, '已过期')))
review_date = models.DateField('下一次复审日期') review_date = models.DateField('下一次复审日期')
file = models.TextField('文件地址', null=True, blank=True) 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

View File

@ -7,7 +7,7 @@ from celery import shared_task
from dateutil import tz from dateutil import tz
from django.core.cache import cache 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.hrm.services import HrmService
from apps.third.dahua import dhClient from apps.third.dahua import dhClient
from apps.third.tapis import dhapis from apps.third.tapis import dhapis
@ -129,4 +129,10 @@ def delete_face_pkl(epId):
def update_global_face(): def update_global_face():
facedata = Employee.objects.filter(face_data__isnull=False, facedata = Employee.objects.filter(face_data__isnull=False,
user__is_active=True).values_list('id', 'face_data') user__is_active=True).values_list('id', 'face_data')
cache.set('global_face', list(facedata), timeout=None) 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)

View File

@ -382,3 +382,11 @@ class CertificateViewSet(CustomModelViewSet):
serializer_class = CertificateSerializer serializer_class = CertificateSerializer
filterset_class = CertificateFilterSet filterset_class = CertificateFilterSet
search_fields = ['name', 'number', 'employee__name'] 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)