2025-04-28 19:27:16 +08:00
|
|
|
|
from zhenxun.services.log import logger
|
|
|
|
|
|
|
2025-04-29 18:11:09 +08:00
|
|
|
|
from .database import CSqlManager
|
|
|
|
|
|
|
2025-04-28 19:27:16 +08:00
|
|
|
|
|
|
|
|
|
|
class CUserItemDB(CSqlManager):
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def initDB(cls):
|
|
|
|
|
|
userItem = {
|
2025-06-06 10:56:22 +08:00
|
|
|
|
"uid": "TEXT NOT NULL", # 用户Uid
|
|
|
|
|
|
"item": "TEXT NOT NULL", # 物品名称
|
|
|
|
|
|
"count": "INTEGER NOT NULL DEFAULT 0", # 数量
|
|
|
|
|
|
"PRIMARY KEY": "(uid, item)",
|
2025-04-28 19:27:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await cls.ensureTableSchema("userItem", userItem)
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2025-06-06 10:56:22 +08:00
|
|
|
|
async def getUserItemByName(cls, uid: str, item: str) -> int | None:
|
2025-04-28 19:27:16 +08:00
|
|
|
|
"""根据道具名称查询某一项数量
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户uid
|
|
|
|
|
|
item (str): 道具名称
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
Optional[int]: 数量(不存在返回None)
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not uid or not item:
|
|
|
|
|
|
return None
|
|
|
|
|
|
try:
|
|
|
|
|
|
async with cls.m_pDB.execute(
|
2025-06-06 10:56:22 +08:00
|
|
|
|
"SELECT count FROM userItem WHERE uid = ? AND item = ?", (uid, item)
|
2025-04-28 19:27:16 +08:00
|
|
|
|
) as cursor:
|
|
|
|
|
|
row = await cursor.fetchone()
|
|
|
|
|
|
return row[0] if row else None
|
|
|
|
|
|
except Exception as e:
|
2025-06-06 10:56:22 +08:00
|
|
|
|
logger.warning("getUserItemByName查询失败!", e=e)
|
2025-04-28 19:27:16 +08:00
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def getUserItemByUid(cls, uid: str) -> dict:
|
|
|
|
|
|
"""根据用户Uid获取全部道具信息
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户uid
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
dict: {itemName: count, ...}
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not uid:
|
|
|
|
|
|
return {}
|
|
|
|
|
|
try:
|
|
|
|
|
|
cursor = await cls.m_pDB.execute(
|
2025-06-06 10:56:22 +08:00
|
|
|
|
"SELECT item, count FROM userItem WHERE uid = ?", (uid,)
|
2025-04-28 19:27:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
rows = await cursor.fetchall()
|
|
|
|
|
|
return {row["item"]: row["count"] for row in rows}
|
|
|
|
|
|
except Exception as e:
|
2025-06-06 10:56:22 +08:00
|
|
|
|
logger.warning("getUserItemByUid查询失败!", e=e)
|
2025-04-28 19:27:16 +08:00
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def deleteUserItemByName(cls, uid: str, item: str) -> bool:
|
|
|
|
|
|
"""根据道具名删除道具
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户uid
|
|
|
|
|
|
item (str): 道具名称
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
bool: 是否删除成功
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not uid or not item:
|
|
|
|
|
|
return False
|
|
|
|
|
|
try:
|
|
|
|
|
|
async with cls._transaction():
|
|
|
|
|
|
await cls.m_pDB.execute(
|
2025-06-06 10:56:22 +08:00
|
|
|
|
"DELETE FROM userItem WHERE uid = ? AND item = ?", (uid, item)
|
2025-04-28 19:27:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
return True
|
|
|
|
|
|
except Exception as e:
|
2025-06-06 10:56:22 +08:00
|
|
|
|
logger.warning("deleteUserItemByName失败!", e=e)
|
2025-04-28 19:27:16 +08:00
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def updateUserItemByName(cls, uid: str, item: str, count: int) -> bool:
|
|
|
|
|
|
"""根据道具名直接更新道具数量
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户uid
|
|
|
|
|
|
item (str): 道具名称
|
|
|
|
|
|
count (int): 要更新的新数量
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
bool: 是否更新成功
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not uid or not item:
|
|
|
|
|
|
return False
|
|
|
|
|
|
try:
|
|
|
|
|
|
async with cls._transaction():
|
|
|
|
|
|
if count <= 0:
|
|
|
|
|
|
await cls.m_pDB.execute(
|
2025-06-06 10:56:22 +08:00
|
|
|
|
"DELETE FROM userItem WHERE uid = ? AND item = ?", (uid, item)
|
2025-04-28 19:27:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
await cls.m_pDB.execute(
|
|
|
|
|
|
"UPDATE userItem SET count = ? WHERE uid = ? AND item = ?",
|
2025-06-06 10:56:22 +08:00
|
|
|
|
(count, uid, item),
|
2025-04-28 19:27:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
return True
|
|
|
|
|
|
except Exception as e:
|
2025-06-06 10:56:22 +08:00
|
|
|
|
logger.warning("updateUserItemByName失败!", e=e)
|
2025-04-28 19:27:16 +08:00
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
async def addUserItemByUid(cls, uid: str, item: str, count: int = 1) -> bool:
|
|
|
|
|
|
"""根据用户uid添加道具信息
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
uid (str): 用户uid
|
|
|
|
|
|
item (str): 道具名称
|
|
|
|
|
|
count (int, optional): 数量.Defaults to 1.
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
bool: 是否添加成功
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not uid or not item:
|
|
|
|
|
|
return False
|
|
|
|
|
|
try:
|
|
|
|
|
|
async with cls._transaction():
|
|
|
|
|
|
async with cls.m_pDB.execute(
|
2025-06-06 10:56:22 +08:00
|
|
|
|
"SELECT count FROM userItem WHERE uid = ? AND item = ?", (uid, item)
|
2025-04-28 19:27:16 +08:00
|
|
|
|
) as cursor:
|
|
|
|
|
|
row = await cursor.fetchone()
|
|
|
|
|
|
|
|
|
|
|
|
if row:
|
|
|
|
|
|
newCount = row[0] + count
|
|
|
|
|
|
if newCount <= 0:
|
|
|
|
|
|
await cls.m_pDB.execute(
|
|
|
|
|
|
"DELETE FROM userItem WHERE uid = ? AND item = ?",
|
2025-06-06 10:56:22 +08:00
|
|
|
|
(uid, item),
|
2025-04-28 19:27:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
await cls.m_pDB.execute(
|
|
|
|
|
|
"UPDATE userItem SET count = ? WHERE uid = ? AND item = ?",
|
2025-06-06 10:56:22 +08:00
|
|
|
|
(newCount, uid, item),
|
2025-04-28 19:27:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
if count > 0:
|
|
|
|
|
|
await cls.m_pDB.execute(
|
|
|
|
|
|
"INSERT INTO userItem (uid, item, count) VALUES (?, ?, ?)",
|
2025-06-06 10:56:22 +08:00
|
|
|
|
(uid, item, count),
|
2025-04-28 19:27:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
return True
|
|
|
|
|
|
except Exception as e:
|
2025-06-06 10:56:22 +08:00
|
|
|
|
logger.warning("addUserItemByUid失败!", e=e)
|
2025-04-28 19:27:16 +08:00
|
|
|
|
return False
|