1
This commit is contained in:
@@ -5,6 +5,7 @@ from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.roles import ROLE_AGENT, ROLE_OEM
|
||||
from app.core.card_key_types import CARD_TYPE_DURATION, CARD_TYPE_POINTS
|
||||
from app.models.card_key import CardKey
|
||||
from app.models.user import User
|
||||
from app.schemas.admin_card_key import CardKeyCreate, CardKeyUpdate
|
||||
@@ -129,7 +130,9 @@ async def create_card_keys(db: AsyncSession, body: CardKeyCreate) -> list[CardKe
|
||||
)
|
||||
card = CardKey(
|
||||
serial_number=serial,
|
||||
duration_days=body.duration_days,
|
||||
card_type=body.card_type,
|
||||
duration_days=body.duration_days if body.card_type == CARD_TYPE_DURATION else 0,
|
||||
points_amount=body.points_amount if body.card_type == CARD_TYPE_POINTS else None,
|
||||
remark=body.remark,
|
||||
oem_id=body.oem_id,
|
||||
agent_id=body.agent_id,
|
||||
@@ -153,10 +156,30 @@ async def update_card_key(
|
||||
raise AdminCardKeyError("卡密不存在")
|
||||
|
||||
if card.activated_at is not None:
|
||||
if body.duration_days is not None:
|
||||
raise AdminCardKeyError("已激活卡密不能修改时长")
|
||||
elif body.duration_days is not None:
|
||||
card.duration_days = body.duration_days
|
||||
if (
|
||||
body.duration_days is not None
|
||||
or body.points_amount is not None
|
||||
or body.card_type is not None
|
||||
):
|
||||
raise AdminCardKeyError("已激活卡密不能修改类型或面值")
|
||||
else:
|
||||
if body.card_type is not None:
|
||||
card.card_type = body.card_type
|
||||
if body.card_type == CARD_TYPE_DURATION:
|
||||
card.points_amount = None
|
||||
if body.duration_days is None and card.duration_days <= 0:
|
||||
raise AdminCardKeyError("时长卡密须填写有效天数")
|
||||
elif body.card_type == CARD_TYPE_POINTS:
|
||||
card.duration_days = 0
|
||||
if body.points_amount is None and card.points_amount is None:
|
||||
raise AdminCardKeyError("点数卡密须填写点数")
|
||||
|
||||
effective_type = card.card_type
|
||||
if effective_type == CARD_TYPE_DURATION:
|
||||
if body.duration_days is not None:
|
||||
card.duration_days = body.duration_days
|
||||
elif body.points_amount is not None:
|
||||
card.points_amount = body.points_amount
|
||||
|
||||
if body.remark is not None:
|
||||
card.remark = body.remark
|
||||
|
||||
Reference in New Issue
Block a user