117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
"""演示 / 初始化数据播种。
|
|
|
|
整个播种流程是**幂等**的:每个子步骤先判断自己是否已经落库,已存在则跳过,
|
|
因此可以反复执行(包括 ``AUTO_SEED=true`` 时的每次启动)。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
import random
|
|
from datetime import date, datetime, timedelta
|
|
|
|
from sqlalchemy import func, select
|
|
|
|
from ..db.session import SessionFactory
|
|
from ..models.rbac import Permission
|
|
|
|
log = logging.getLogger("hrms.seed")
|
|
|
|
RNG = random.Random(20261009)
|
|
|
|
# 播种账号(生产环境请务必修改)
|
|
ACCOUNTS = [
|
|
("admin", "admin123", "超级管理员", "super_admin", None),
|
|
("hr", "hr123456", "张人事", "hr_admin", None),
|
|
("recruiter", "rec123456", "刘招聘", "recruit_manager", None),
|
|
("payroll", "pay123456", "陈薪酬", "payroll_admin", None),
|
|
("manager", "mgr123456", "王志强", "manager", None),
|
|
("emp", "emp123456", "普通员工", "employee", None),
|
|
]
|
|
|
|
|
|
def mk(model, **kw):
|
|
"""构造模型实例,自动忽略不属于该表的字段(播种时更宽容)。"""
|
|
cols = model.__table__.columns.keys()
|
|
return model(**{k: v for k, v in kw.items() if k in cols})
|
|
|
|
|
|
async def count(session, model) -> int:
|
|
return int((await session.execute(select(func.count()).select_from(model))).scalar() or 0)
|
|
|
|
|
|
async def already(session, model) -> bool:
|
|
return (await count(session, model)) > 0
|
|
|
|
|
|
async def add(session, obj):
|
|
session.add(obj)
|
|
await session.flush()
|
|
return obj
|
|
|
|
|
|
def months_back(n: int, base: date | None = None) -> date:
|
|
today = base or date.today()
|
|
year, month = today.year, today.month - n
|
|
while month <= 0:
|
|
month += 12
|
|
year -= 1
|
|
return date(year, min(month, 12), min(today.day, 28))
|
|
|
|
|
|
def days_ago(n: int, base: date | None = None) -> date:
|
|
return (base or date.today()) - timedelta(days=n)
|
|
|
|
|
|
async def seed_all(force: bool = False) -> dict:
|
|
"""执行全部播种步骤,返回每一步的结果摘要。"""
|
|
from . import seed_benefits, seed_content, seed_employee, seed_org
|
|
from . import seed_payroll, seed_recruitment, seed_rbac
|
|
|
|
report: dict[str, object] = {}
|
|
async with SessionFactory() as session:
|
|
report["rbac"] = await seed_rbac.run(session, force=force)
|
|
report["org"] = await seed_org.run(session, force=force)
|
|
await session.commit()
|
|
|
|
async with SessionFactory() as session:
|
|
report["employees"] = await seed_employee.run(session, force=force)
|
|
await session.commit()
|
|
|
|
async with SessionFactory() as session:
|
|
report["accounts"] = await seed_rbac.seed_users(session, force=force)
|
|
await session.commit()
|
|
|
|
# 参保登记必须先于算薪,否则工资条里五险一金为 0
|
|
async with SessionFactory() as session:
|
|
report["benefits"] = await seed_benefits.run(session, force=force)
|
|
await session.commit()
|
|
|
|
async with SessionFactory() as session:
|
|
report["payroll"] = await seed_payroll.run(session, force=force)
|
|
await session.commit()
|
|
|
|
async with SessionFactory() as session:
|
|
report["recruitment"] = await seed_recruitment.run(session, force=force)
|
|
await session.commit()
|
|
|
|
async with SessionFactory() as session:
|
|
report["content"] = await seed_content.run(session, force=force)
|
|
await session.commit()
|
|
|
|
return report
|
|
|
|
|
|
def run_sync() -> dict:
|
|
return asyncio.run(seed_all())
|
|
|
|
|
|
if __name__ == "__main__": # pragma: no cover
|
|
import json
|
|
import sys
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
|
print(json.dumps(run_sync(), ensure_ascii=False, indent=2))
|
|
sys.exit(0)
|