Skip to content

Optimize dict create and update logic #691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions backend/plugin/dict/crud/crud_dict_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,33 @@ async def get_by_label(self, db: AsyncSession, label: str) -> DictData | None:
"""
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:
"""
Expand Down
2 changes: 1 addition & 1 deletion backend/plugin/dict/plugin.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[plugin]
summary = '数据字典'
version = '0.0.4'
version = '0.0.5'
description = '通常用于约束前端工程数据展示'
author = 'wu-clan'

Expand Down
2 changes: 1 addition & 1 deletion backend/plugin/dict/schema/dict_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='排序')
Expand Down Expand Up @@ -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='更新时间')
4 changes: 2 additions & 2 deletions backend/plugin/dict/service/dict_data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def create(*, obj: CreateDictDataParam) -> None:
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:
Expand All @@ -89,7 +89,7 @@ async def update(*, pk: int, obj: UpdateDictDataParam) -> int:
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
Expand Down