1. 规范订单号
2. 账户明细中,关联对应的订单号。
This commit is contained in:
parent
9b44fdf36b
commit
1c38cf825b
@ -89,6 +89,7 @@ class AccountDetailItem(BaseModel):
|
|||||||
amount: float
|
amount: float
|
||||||
type: AccountDetailType
|
type: AccountDetailType
|
||||||
description: str
|
description: str
|
||||||
|
transaction_id: Optional[str] = None
|
||||||
create_time: datetime
|
create_time: datetime
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
@ -136,6 +137,7 @@ async def list_account_details(
|
|||||||
"amount": float(detail.amount),
|
"amount": float(detail.amount),
|
||||||
"type": detail.type,
|
"type": detail.type,
|
||||||
"description": detail.description,
|
"description": detail.description,
|
||||||
|
"transaction_id": detail.transaction_id,
|
||||||
"create_time": detail.create_time
|
"create_time": detail.create_time
|
||||||
}
|
}
|
||||||
for detail in details
|
for detail in details
|
||||||
|
|||||||
@ -184,7 +184,8 @@ async def verify_order(
|
|||||||
account_manager.change_balance(
|
account_manager.change_balance(
|
||||||
user_id=order.MerchantDB.user_id,
|
user_id=order.MerchantDB.user_id,
|
||||||
amount=settlement_amount,
|
amount=settlement_amount,
|
||||||
description=f"{order.MerchantProductDB.name} 订单核销"
|
description=f"{order.MerchantProductDB.name} 订单核销",
|
||||||
|
transaction_id=order.MerchantOrderDB.order_id
|
||||||
)
|
)
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from app.models.user_account import UserAccountDB, AccountDetailDB, AccountDetailType
|
from app.models.user_account import UserAccountDB, AccountDetailDB, AccountDetailType
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
class AccountManager:
|
class AccountManager:
|
||||||
"""用户账户管理类"""
|
"""用户账户管理类"""
|
||||||
@ -24,7 +25,13 @@ class AccountManager:
|
|||||||
|
|
||||||
return account
|
return account
|
||||||
|
|
||||||
def change_balance(self, user_id: int, amount: float, description: str) -> UserAccountDB:
|
def change_balance(
|
||||||
|
self,
|
||||||
|
user_id: int,
|
||||||
|
amount: float,
|
||||||
|
description: str,
|
||||||
|
transaction_id: Optional[str] = None
|
||||||
|
) -> UserAccountDB:
|
||||||
"""
|
"""
|
||||||
变更用户余额
|
变更用户余额
|
||||||
|
|
||||||
@ -32,6 +39,7 @@ class AccountManager:
|
|||||||
user_id: 用户ID
|
user_id: 用户ID
|
||||||
amount: 变更金额(正数为收入,负数为支出)
|
amount: 变更金额(正数为收入,负数为支出)
|
||||||
description: 变更说明
|
description: 变更说明
|
||||||
|
transaction_id: 关联订单号
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
更新后的账户信息
|
更新后的账户信息
|
||||||
@ -57,7 +65,8 @@ class AccountManager:
|
|||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
amount=decimal_amount,
|
amount=decimal_amount,
|
||||||
type=AccountDetailType.INCOME if decimal_amount > 0 else AccountDetailType.EXPENSE,
|
type=AccountDetailType.INCOME if decimal_amount > 0 else AccountDetailType.EXPENSE,
|
||||||
description=description
|
description=description,
|
||||||
|
transaction_id=transaction_id
|
||||||
)
|
)
|
||||||
self.db.add(detail)
|
self.db.add(detail)
|
||||||
|
|
||||||
|
|||||||
@ -67,7 +67,7 @@ def generate_order_id() -> str:
|
|||||||
date_str = now.strftime('%Y%m%d')
|
date_str = now.strftime('%Y%m%d')
|
||||||
# 取时间戳后7位
|
# 取时间戳后7位
|
||||||
timestamp = str(int(time.time() * 1000))[-7:]
|
timestamp = str(int(time.time() * 1000))[-7:]
|
||||||
return f"{date_str}{timestamp}"
|
return f"M{date_str}{timestamp}"
|
||||||
|
|
||||||
def generate_verify_code() -> str:
|
def generate_verify_code() -> str:
|
||||||
"""生成21位数字核销码"""
|
"""生成21位数字核销码"""
|
||||||
|
|||||||
@ -116,7 +116,7 @@ def generate_order_id() -> str:
|
|||||||
date_str = now.strftime('%Y%m%d') # 8位日期
|
date_str = now.strftime('%Y%m%d') # 8位日期
|
||||||
# 生成6位序号(毫秒级时间戳后6位)
|
# 生成6位序号(毫秒级时间戳后6位)
|
||||||
timestamp = str(int(now.timestamp() * 1000))[-6:]
|
timestamp = str(int(now.timestamp() * 1000))[-6:]
|
||||||
return f"{date_str}{timestamp}"
|
return f"D{date_str}{timestamp}"
|
||||||
|
|
||||||
class OrderPriceInfo(BaseModel):
|
class OrderPriceInfo(BaseModel):
|
||||||
package_count: int
|
package_count: int
|
||||||
|
|||||||
@ -29,6 +29,7 @@ class AccountDetailDB(Base):
|
|||||||
amount = Column(DECIMAL(10, 2), nullable=False)
|
amount = Column(DECIMAL(10, 2), nullable=False)
|
||||||
type = Column(Enum(AccountDetailType), nullable=False)
|
type = Column(Enum(AccountDetailType), nullable=False)
|
||||||
description = Column(String(200), nullable=False)
|
description = Column(String(200), nullable=False)
|
||||||
|
transaction_id = Column(String(64), nullable=True) # 关联订单号
|
||||||
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
||||||
|
|
||||||
# Pydantic 模型
|
# Pydantic 模型
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user