🎨 将文本内容同一更新

🐛 修复偷菜无法正常计算的BUG
This commit is contained in:
2025-06-06 10:56:22 +08:00
parent ba2ecf19fc
commit ac13ab9280
20 changed files with 1284 additions and 699 deletions
+20 -25
View File
@@ -1,5 +1,3 @@
from typing import Dict, Optional
from zhenxun.services.log import logger
from .database import CSqlManager
@@ -9,10 +7,10 @@ class CUserPlantDB(CSqlManager):
@classmethod
async def initDB(cls):
userPlant = {
"uid": "TEXT NOT NULL", #用户Uid
"plant": "TEXT NOT NULL", #作物名称
"count": "INTEGER NOT NULL DEFAULT 0", #数量
"PRIMARY KEY": "(uid, plant)"
"uid": "TEXT NOT NULL", # 用户Uid
"plant": "TEXT NOT NULL", # 作物名称
"count": "INTEGER NOT NULL DEFAULT 0", # 数量
"PRIMARY KEY": "(uid, plant)",
}
await cls.ensureTableSchema("userPlant", userPlant)
@@ -31,33 +29,33 @@ class CUserPlantDB(CSqlManager):
"""
try:
async with cls._transaction():
#检查是否已存在该作物
# 检查是否已存在该作物
async with cls.m_pDB.execute(
"SELECT count FROM userPlant WHERE uid = ? AND plant = ?",
(uid, plant)
(uid, plant),
) as cursor:
row = await cursor.fetchone()
if row:
#如果作物已存在,则更新数量
# 如果作物已存在,则更新数量
new_count = row[0] + count
await cls.m_pDB.execute(
"UPDATE userPlant SET count = ? WHERE uid = ? AND plant = ?",
(new_count, uid, plant)
(new_count, uid, plant),
)
else:
#如果作物不存在,则插入新记录
# 如果作物不存在,则插入新记录
await cls.m_pDB.execute(
"INSERT INTO userPlant (uid, plant, count) VALUES (?, ?, ?)",
(uid, plant, count)
(uid, plant, count),
)
return True
except Exception as e:
logger.warning(f"addUserPlantByUid 失败!", e=e)
logger.warning("addUserPlantByUid 失败!", e=e)
return False
@classmethod
async def getUserPlantByUid(cls, uid: str) -> Dict[str, int]:
async def getUserPlantByUid(cls, uid: str) -> dict[str, int]:
"""根据用户uid获取全部作物信息
Args:
@@ -67,14 +65,13 @@ class CUserPlantDB(CSqlManager):
Dict[str, int]: 作物名称和数量
"""
cursor = await cls.m_pDB.execute(
"SELECT plant, count FROM userPlant WHERE uid=?",
(uid,)
"SELECT plant, count FROM userPlant WHERE uid=?", (uid,)
)
rows = await cursor.fetchall()
return {row["plant"]: row["count"] for row in rows}
@classmethod
async def getUserPlantByName(cls, uid: str, plant: str) -> Optional[int]:
async def getUserPlantByName(cls, uid: str, plant: str) -> int | None:
"""根据作物名称获取用户的作物数量
Args:
@@ -86,13 +83,12 @@ class CUserPlantDB(CSqlManager):
"""
try:
async with cls.m_pDB.execute(
"SELECT count FROM userPlant WHERE uid = ? AND plant = ?",
(uid, plant)
"SELECT count FROM userPlant WHERE uid = ? AND plant = ?", (uid, plant)
) as cursor:
row = await cursor.fetchone()
return row[0] if row else None
except Exception as e:
logger.warning(f"getUserPlantByName 查询失败!", e=e)
logger.warning("getUserPlantByName 查询失败!", e=e)
return None
@classmethod
@@ -114,11 +110,11 @@ class CUserPlantDB(CSqlManager):
async with cls._transaction():
await cls.m_pDB.execute(
"UPDATE userPlant SET count = ? WHERE uid = ? AND plant = ?",
(count, uid, plant)
(count, uid, plant),
)
return True
except Exception as e:
logger.warning(f"updateUserPlantByName失败!", e=e)
logger.warning("updateUserPlantByName失败!", e=e)
return False
@classmethod
@@ -135,10 +131,9 @@ class CUserPlantDB(CSqlManager):
try:
async with cls._transaction():
await cls.m_pDB.execute(
"DELETE FROM userPlant WHERE uid = ? AND plant = ?",
(uid, plant)
"DELETE FROM userPlant WHERE uid = ? AND plant = ?", (uid, plant)
)
return True
except Exception as e:
logger.warning(f"deleteUserPlantByName 失败!", e=e)
logger.warning("deleteUserPlantByName 失败!", e=e)
return False