Optimize dict create and update logic (#691)

* Optimize dict create and update logic

* Update version
This commit is contained in:
Wu Clan
2025-06-25 10:41:09 +08:00
committed by GitHub
parent 0602c6144d
commit d47375ae41
4 changed files with 15 additions and 8 deletions
+11 -4
View File
@@ -70,26 +70,33 @@ class CRUDDictData(CRUDPlus[DictData]):
"""
return await self.select_model_by_column(db, label=label)
async def create(self, db: AsyncSession, obj: CreateDictDataParam) -> None:
async def create(self, db: AsyncSession, obj: CreateDictDataParam, type_code: str) -> None:
"""
创建字典数据
:param db: 数据库会话
:param obj: 创建字典数据参数
:param type_code: 字典类型编码
:return:
"""
await self.create_model(db, obj)
dict_obj = obj.model_dump()
dict_obj.update({'type_code': type_code})
new_data = self.model(**dict_obj)
db.add(new_data)
async def update(self, db: AsyncSession, pk: int, obj: UpdateDictDataParam) -> int:
async def update(self, db: AsyncSession, pk: int, obj: UpdateDictDataParam, type_code: str) -> int:
"""
更新字典数据
:param db: 数据库会话
:param pk: 字典数据 ID
:param obj: 更新字典数据参数
:param type_code: 字典类型编码
:return:
"""
return await self.update_model(db, pk, obj)
dict_obj = obj.model_dump()
dict_obj.update({'type_code': type_code})
return await self.update_model(db, pk, dict_obj)
async def delete(self, db: AsyncSession, pks: list[int]) -> int:
"""
+1 -1
View File
@@ -1,6 +1,6 @@
[plugin]
summary = '数据字典'
version = '0.0.4'
version = '0.0.5'
description = '通常用于约束前端工程数据展示'
author = 'wu-clan'
+1 -1
View File
@@ -12,7 +12,6 @@ class DictDataSchemaBase(SchemaBase):
"""字典数据基础模型"""
type_id: int = Field(description='字典类型 ID')
type_code: str = Field(description='字典类型编码')
label: str = Field(description='字典标签')
value: str = Field(description='字典值')
sort: int = Field(description='排序')
@@ -40,5 +39,6 @@ class GetDictDataDetail(DictDataSchemaBase):
model_config = ConfigDict(from_attributes=True)
id: int = Field(description='字典数据 ID')
type_code: str = Field(description='字典类型编码')
created_time: datetime = Field(description='创建时间')
updated_time: datetime | None = Field(None, description='更新时间')
@@ -68,7 +68,7 @@ class DictDataService:
dict_type = await dict_type_dao.get(db, obj.type_id)
if not dict_type:
raise errors.NotFoundError(msg='字典类型不存在')
await dict_data_dao.create(db, obj)
await dict_data_dao.create(db, obj, dict_type.code)
@staticmethod
async def update(*, pk: int, obj: UpdateDictDataParam) -> int:
@@ -89,7 +89,7 @@ class DictDataService:
dict_type = await dict_type_dao.get(db, obj.type_id)
if not dict_type:
raise errors.NotFoundError(msg='字典类型不存在')
count = await dict_data_dao.update(db, pk, obj)
count = await dict_data_dao.update(db, pk, obj, dict_type.code)
return count
@staticmethod