refactor(gerp): 种子/清理命令裸 ORM 收敛进 SDK Manager
CleanAuditLogs 改用 AuditManager.QueryBefore/PurgeBefore;SeedPositions、SeedPositionCategories 改用 TenantManager.ListAllTenants 与 PositionManager 的 ListPositionCodes/CreatePosition/GetCategoriesForTenant/CreateCategory。 SeedPositionCategories 建通用部门时由「逐个 get 已存在编码」改为一次性取回按 CategoryCode 建索引,减少 N 次查询;顺带移除两处已失效的 import uuid。
This commit is contained in:
1 parent
1f25d1373b
commit
a7c70401c2
3 files changed
+15
-37
No files matched your search
@@ -11,7 +11,7 @@ import logging
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.utils import timezone
|
||||
|
||||
from gvsdsdk.models import AuditLog
|
||||
from gvsdsdk.auth.managers import AuditManager
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -43,8 +43,7 @@ class Command(BaseCommand):
|
||||
|
||||
# 按 CreateTime 字段过滤(AuditLog.CreateTime 为 UTC 时间)
|
||||
threshold = timezone.now() - datetime.timedelta(days=days)
|
||||
qs = AuditLog.objects.filter(CreateTime__lt=threshold)
|
||||
count = qs.count()
|
||||
count = AuditManager.QueryBefore(threshold).count()
|
||||
|
||||
if dry_run:
|
||||
self.stdout.write(self.style.WARNING(
|
||||
@@ -58,7 +57,7 @@ class Command(BaseCommand):
|
||||
))
|
||||
return
|
||||
|
||||
deleted_count, _ = qs.delete()
|
||||
deleted_count = AuditManager.PurgeBefore(threshold)
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
f'清理完成:已删除 {deleted_count} 条早于 {days} 天的审计日志(阈值: {threshold.isoformat()})'
|
||||
))
|
||||
|
||||
@@ -9,11 +9,9 @@
|
||||
- 职务绑定规则:PositionCode 前 3 位匹配通用部门编码(如 FIN-001 → 财务)
|
||||
- 已存在的通用部门(同租户同编码)跳过;已绑定通用部门的职务跳过,不会覆盖。
|
||||
"""
|
||||
import uuid
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from gvsdsdk.models import Position, PositionCategory, Tenant
|
||||
from gvsdsdk.auth.managers import PositionManager, TenantManager
|
||||
from gvsdsdk.model_utils import UUIDObj
|
||||
|
||||
|
||||
@@ -38,33 +36,24 @@ class Command(BaseCommand):
|
||||
bound_pos = 0
|
||||
skipped_pos = 0
|
||||
|
||||
for tenant in Tenant.objects.all():
|
||||
for tenant in TenantManager.ListAllTenants():
|
||||
tenant_bytes = UUIDObj(tenant.TenantUUID).bytes
|
||||
|
||||
# 1. 创建通用部门(同租户同编码已存在则跳过)
|
||||
cat_by_code = {}
|
||||
existing_codes = set(
|
||||
PositionCategory.objects.filter(TenantUUID=tenant_bytes).values_list('CategoryCode', flat=True)
|
||||
)
|
||||
existing = {c.CategoryCode: c for c in PositionManager.GetCategoriesForTenant(tenant_bytes)}
|
||||
for code, name, sort in self.BUILTIN_CATEGORIES:
|
||||
if code in existing_codes:
|
||||
cat = PositionCategory.objects.get(TenantUUID=tenant_bytes, CategoryCode=code)
|
||||
if code in existing:
|
||||
cat = existing[code]
|
||||
skipped_cat += 1
|
||||
else:
|
||||
cat = PositionCategory.objects.create(
|
||||
PositionCategoryUUID=uuid.uuid4().bytes,
|
||||
TenantUUID=tenant_bytes,
|
||||
CategoryName=name,
|
||||
CategoryCode=code,
|
||||
CategorySort=sort,
|
||||
CategoryStatus=1,
|
||||
)
|
||||
cat = PositionManager.CreateCategory(name, tenant_bytes, code, Sort=sort)
|
||||
created_cat += 1
|
||||
cat_by_code[code] = cat.PositionCategoryUUID
|
||||
|
||||
# 2. 按职务编码前缀绑定通用部门(已绑定则跳过)
|
||||
# 前缀取 "-" 前一段:FIN-001 → FIN;注意不能按 [:3](HR-001 会截成 "HR-")
|
||||
for pos in Position.objects.filter(TenantUUID=tenant_bytes):
|
||||
for pos in PositionManager.ListPositions(tenant_bytes):
|
||||
if pos.CategoryUUID:
|
||||
skipped_pos += 1
|
||||
continue
|
||||
|
||||
@@ -9,11 +9,9 @@
|
||||
- 后续业务逻辑(调岗/升职/分类统计等)基于这些编码扩展。
|
||||
"""
|
||||
|
||||
import uuid
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from gvsdsdk.models import Position, Tenant
|
||||
from gvsdsdk.auth.managers import PositionManager, TenantManager
|
||||
from gvsdsdk.model_utils import UUIDObj
|
||||
|
||||
|
||||
@@ -82,23 +80,15 @@ class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
created_total = 0
|
||||
skipped_total = 0
|
||||
for tenant in Tenant.objects.all():
|
||||
for tenant in TenantManager.ListAllTenants():
|
||||
tenant_bytes = UUIDObj(tenant.TenantUUID).bytes
|
||||
existing_codes = set(
|
||||
Position.objects.filter(TenantUUID=tenant_bytes).values_list('PositionCode', flat=True)
|
||||
)
|
||||
existing_codes = set(PositionManager.ListPositionCodes(tenant_bytes))
|
||||
for name, code, level, desc in self.BUILTIN_POSITIONS:
|
||||
if code in existing_codes:
|
||||
skipped_total += 1
|
||||
continue
|
||||
Position.objects.create(
|
||||
PositionUUID=uuid.uuid4().bytes,
|
||||
TenantUUID=tenant_bytes,
|
||||
PositionName=name,
|
||||
PositionCode=code,
|
||||
PositionLevel=level,
|
||||
PositionStatus=1,
|
||||
PositionDesc=desc,
|
||||
PositionManager.CreatePosition(
|
||||
name, tenant_bytes, code, level, PositionDesc=desc,
|
||||
)
|
||||
created_total += 1
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
|
||||
Reference in new issue
Block a user