Update unique judgment logic for dict data (#859)

This commit is contained in:
Wu Clan
2025-10-17 13:04:35 +08:00
committed by GitHub
parent a93973af06
commit 89e5d02fe2
3 changed files with 12 additions and 10 deletions
+4 -3
View File
@@ -1,6 +1,6 @@
from collections.abc import Sequence
from sqlalchemy import Select
from sqlalchemy import Select, and_
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus
@@ -79,15 +79,16 @@ class CRUDDictData(CRUDPlus[DictData]):
return await self.select_order('id', 'desc', load_strategies={'type': 'noload'}, **filters)
async def get_by_label(self, db: AsyncSession, label: str) -> DictData | None:
async def get_by_label_and_type_code(self, db: AsyncSession, label: str, type_code: str) -> DictData | None:
"""
通过标签获取字典数据
:param db: 数据库会话
:param label: 字典标签
:param type_code: 字典类型编码
:return:
"""
return await self.select_model_by_column(db, label=label)
return await self.select_model_by_column(db, and_(self.model.label == label, self.model.type_code == type_code))
async def create(self, db: AsyncSession, obj: CreateDictDataParam, type_code: str) -> None:
"""
+1 -1
View File
@@ -1,6 +1,6 @@
[plugin]
summary = '数据字典'
version = '0.0.6'
version = '0.0.7'
description = '通常用于约束前端工程数据展示'
author = 'wu-clan'
@@ -94,13 +94,12 @@ class DictDataService:
:param obj: 字典数据创建参数
:return:
"""
dict_data = await dict_data_dao.get_by_label(db, obj.label)
if dict_data:
raise errors.ConflictError(msg='字典数据已存在')
dict_type = await dict_type_dao.get(db, obj.type_id)
if not dict_type:
raise errors.NotFoundError(msg='字典类型不存在')
dict_data = await dict_data_dao.get_by_label_and_type_code(db, obj.label, dict_type.code)
if dict_data:
raise errors.ConflictError(msg='字典数据已存在')
await dict_data_dao.create(db, obj, dict_type.code)
@staticmethod
@@ -117,11 +116,13 @@ class DictDataService:
dict_data = await dict_data_dao.get(db, pk)
if not dict_data:
raise errors.NotFoundError(msg='字典数据不存在')
if dict_data.label != obj.label and await dict_data_dao.get_by_label(db, obj.label):
raise errors.ConflictError(msg='字典数据已存在')
dict_type = await dict_type_dao.get(db, obj.type_id)
if not dict_type:
raise errors.NotFoundError(msg='字典类型不存在')
if dict_data.label != obj.label and await dict_data_dao.get_by_label_and_type_code(
db, obj.label, dict_type.code
):
raise errors.ConflictError(msg='字典数据已存在')
count = await dict_data_dao.update(db, pk, obj, dict_type.code)
return count