feat: 支持按权益名称导入用户
This commit is contained in:
@@ -312,6 +312,41 @@ class EntitlementService:
|
||||
)
|
||||
return entitlement
|
||||
|
||||
@staticmethod
|
||||
def clear_user_plan(
|
||||
db: Session,
|
||||
*,
|
||||
user: User,
|
||||
operated_by: int | None,
|
||||
reason: str | None = None,
|
||||
) -> None:
|
||||
"""Remove explicit entitlements so the user returns to the unassigned/default state."""
|
||||
current_rows = list(
|
||||
db.scalars(
|
||||
select(UserEntitlement)
|
||||
.where(UserEntitlement.user_id == user.id, UserEntitlement.status == "active")
|
||||
.order_by(UserEntitlement.created_at.desc(), UserEntitlement.id.desc())
|
||||
)
|
||||
)
|
||||
if not current_rows:
|
||||
return
|
||||
for current in current_rows:
|
||||
current.status = "replaced"
|
||||
db.add(current)
|
||||
latest = current_rows[0]
|
||||
db.add(
|
||||
UserEntitlementLog(
|
||||
user_id=user.id,
|
||||
entitlement_id=latest.id,
|
||||
from_plan_id=latest.plan_id,
|
||||
to_plan_id=None,
|
||||
action="clear",
|
||||
detail_json=json.dumps({"reason": reason}, ensure_ascii=False),
|
||||
operated_by=operated_by,
|
||||
created_at=_now(),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def plan_dict(plan: EntitlementPlan) -> dict:
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user