feat(points): 新增积分赠送
This commit is contained in:
39
core/routers/card/actions.py
Normal file
39
core/routers/card/actions.py
Normal file
@@ -0,0 +1,39 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Project : X25020501_card_book
|
||||
# @File : actions.py.py
|
||||
# @Date : 2025/5/7 20:38
|
||||
# @Software : PyCharm
|
||||
# @Author : xingc
|
||||
# @Desc :
|
||||
import logging
|
||||
|
||||
from core.base.schema import ProductCategory
|
||||
from core.db.crud import CRUD
|
||||
from core.db.models import User, Product
|
||||
from core.routers.auth.schema import LoginUser
|
||||
from core.routers.pay.actions import actions_handler
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def reward_points_after_uploading_pictures(
|
||||
db_crud: CRUD,
|
||||
gift_product_id: int,
|
||||
login_user: LoginUser
|
||||
):
|
||||
product: Product = await db_crud.get(
|
||||
model=Product, filters={
|
||||
'id': gift_product_id,
|
||||
'category': ProductCategory.GIFT
|
||||
}
|
||||
)
|
||||
if not product:
|
||||
logger.warning(f'Product not found -> {gift_product_id}')
|
||||
return
|
||||
|
||||
user: User = await db_crud.get(model=User, filters={'user_id': login_user.user_id})
|
||||
|
||||
try:
|
||||
await actions_handler(db_crud, product, user)
|
||||
except Exception as e:
|
||||
logger.error(f'Reward Points After Uploading Pictures -> {e}')
|
||||
@@ -23,6 +23,7 @@ from core.routers.auth.services import get_current_user_form_http
|
||||
from core.utils.helper import generate_keyname, check_picture_for_empty
|
||||
|
||||
from . import schema
|
||||
from .actions import reward_points_after_uploading_pictures
|
||||
from .scraper import card_scraper
|
||||
from .services import check_user_card_created_permission
|
||||
|
||||
@@ -190,17 +191,18 @@ async def created_card(
|
||||
login_user: LoginUser = Depends(get_current_user_form_http),
|
||||
db_session: AsyncSession = Depends(get_async_session)
|
||||
):
|
||||
card_crud = CRUD(db_session, Card)
|
||||
db_crud = CRUD(db_session, Card)
|
||||
# 确认用户存卡权限
|
||||
if await check_user_card_created_permission(
|
||||
db_session, login_user=login_user
|
||||
) is False:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST, detail=f'非会员仅支持存入{settings.USER_MAX_CREATED_CARD_NUM}张卡片'
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f'非会员仅支持存入{settings.USER_MAX_CREATED_CARD_NUM}张卡片'
|
||||
)
|
||||
|
||||
# 卡片库查询
|
||||
card: Card = await card_crud.get(filters={'cert_number': payload.cert_number})
|
||||
card: Card = await db_crud.get(filters={'cert_number': payload.cert_number})
|
||||
if payload.source.value != 'other':
|
||||
if card is None:
|
||||
# 卡片缓存库转正至卡片库, 重新查询保障数据准确性
|
||||
@@ -208,7 +210,7 @@ async def created_card(
|
||||
cache_card = await cache_card_crud.get(
|
||||
filters={'cert_number': payload.cert_number, 'source': payload.source.value}
|
||||
)
|
||||
card = await card_crud.create(
|
||||
card = await db_crud.create(
|
||||
data={
|
||||
**cache_card.data,
|
||||
'category': payload.category,
|
||||
@@ -232,17 +234,23 @@ async def created_card(
|
||||
if card_picture.get(key) is None:
|
||||
card_picture[key] = value
|
||||
|
||||
await card_crud.update(
|
||||
await db_crud.update(
|
||||
data={
|
||||
'card_picture': card_picture,
|
||||
'perfect_user_id': login_user.user_id,
|
||||
},
|
||||
filters={'id': card.id}
|
||||
)
|
||||
# 上传图片奖励
|
||||
if payload.reward and settings.UPLOAD_REWARD_POINTS_GIFT_ID > 0:
|
||||
await reward_points_after_uploading_pictures(
|
||||
db_crud, gift_product_id=settings.UPLOAD_REWARD_POINTS_GIFT_ID, login_user=login_user
|
||||
)
|
||||
|
||||
|
||||
else:
|
||||
if card is None:
|
||||
card: Card = await card_crud.create(
|
||||
card: Card = await db_crud.create(
|
||||
data={
|
||||
**payload.dict(exclude_none=True, exclude={'reward'}),
|
||||
'create_user_id': login_user.user_id,
|
||||
@@ -251,12 +259,12 @@ async def created_card(
|
||||
filters={'cert_number': payload.cert_number, 'source': payload.source.value}
|
||||
)
|
||||
|
||||
collection_crud = CRUD(db_session, UserCollection)
|
||||
record = {
|
||||
'user_id': login_user.user_id,
|
||||
'card_id': card.id
|
||||
}
|
||||
result = await collection_crud.create(
|
||||
await db_crud.create(
|
||||
model=UserCollection,
|
||||
data=record,
|
||||
filters=record
|
||||
)
|
||||
@@ -299,7 +307,8 @@ async def display_card_status(
|
||||
db_session, login_user=login_user, other_conditions={'displayed': True}
|
||||
) is False and action == 'displayed':
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST, detail=f'非会员仅支持展示{settings.USER_MAX_CREATED_CARD_NUM}张卡片'
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f'非会员仅支持展示{settings.USER_MAX_CREATED_CARD_NUM}张卡片'
|
||||
)
|
||||
|
||||
db_crud = CRUD(db_session, UserCollection)
|
||||
|
||||
Reference in New Issue
Block a user