feat: 代码生成新增字段唯一性配置
This commit is contained in:
@@ -55,6 +55,7 @@ class GenTableColumn(Base):
|
||||
is_pk = Column(String(1), nullable=True, comment='是否主键(1是)')
|
||||
is_increment = Column(String(1), nullable=True, comment='是否自增(1是)')
|
||||
is_required = Column(String(1), nullable=True, comment='是否必填(1是)')
|
||||
is_unique = Column(String(1), nullable=True, comment='是否唯一(1是)')
|
||||
is_insert = Column(String(1), nullable=True, comment='是否为插入字段(1是)')
|
||||
is_edit = Column(String(1), nullable=True, comment='是否编辑字段(1是)')
|
||||
is_list = Column(String(1), nullable=True, comment='是否列表字段(1是)')
|
||||
|
@@ -172,6 +172,7 @@ class GenTableColumnBaseModel(BaseModel):
|
||||
is_pk: Optional[str] = Field(default=None, description='是否主键(1是)')
|
||||
is_increment: Optional[str] = Field(default=None, description='是否自增(1是)')
|
||||
is_required: Optional[str] = Field(default=None, description='是否必填(1是)')
|
||||
is_unique: Optional[str] = Field(default=None, description='是否唯一(1是)')
|
||||
is_insert: Optional[str] = Field(default=None, description='是否为插入字段(1是)')
|
||||
is_edit: Optional[str] = Field(default=None, description='是否编辑字段(1是)')
|
||||
is_list: Optional[str] = Field(default=None, description='是否列表字段(1是)')
|
||||
@@ -204,6 +205,7 @@ class GenTableColumnModel(GenTableColumnBaseModel):
|
||||
pk: Optional[bool] = Field(default=None, description='是否主键')
|
||||
increment: Optional[bool] = Field(default=None, description='是否自增')
|
||||
required: Optional[bool] = Field(default=None, description='是否必填')
|
||||
unique: Optional[bool] = Field(default=None, description='是否唯一')
|
||||
insert: Optional[bool] = Field(default=None, description='是否为插入字段')
|
||||
edit: Optional[bool] = Field(default=None, description='是否编辑字段')
|
||||
list: Optional[bool] = Field(default=None, description='是否列表字段')
|
||||
@@ -217,6 +219,7 @@ class GenTableColumnModel(GenTableColumnBaseModel):
|
||||
self.pk = True if self.is_pk and self.is_pk == '1' else False
|
||||
self.increment = True if self.is_increment and self.is_increment == '1' else False
|
||||
self.required = True if self.is_required and self.is_required == '1' else False
|
||||
self.unique = True if self.is_unique and self.is_unique == '1' else False
|
||||
self.insert = True if self.is_insert and self.is_insert == '1' else False
|
||||
self.edit = True if self.is_edit and self.is_edit == '1' else False
|
||||
self.list = True if self.is_list and self.is_list == '1' else False
|
||||
|
@@ -71,7 +71,7 @@ class {{ BusinessName }}Dao:
|
||||
select({{ ClassName }}).where(
|
||||
{% for column in columns %}
|
||||
{% if column.required %}
|
||||
{{ ClassName }}.{{ column.python_field | camel_to_snake }} == {{ businessName }}.{{ column.python_field | camel_to_snake }},
|
||||
{{ ClassName }}.{{ column.python_field | camel_to_snake }} == {{ businessName }}.{{ column.python_field | camel_to_snake }} if {{ businessName }}.{{ column.python_field | camel_to_snake }} else True,
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
)
|
||||
@@ -145,7 +145,7 @@ class {{ BusinessName }}Dao:
|
||||
:param {{ businessName }}: {{ functionName }}对象
|
||||
:return:
|
||||
"""
|
||||
db_{{ businessName }} = {{ ClassName }}(**{{ businessName }}.model_dump({%if table.sub %}exclude={'{{ subclassName }}_list'}{% endif %}))
|
||||
db_{{ businessName }} = {{ ClassName }}(**{{ businessName }}.model_dump(exclude={% raw %}{{% endraw %}{% if table.sub %}'{{ subclassName }}_list, '{% endif %}{% for column in columns %}{% if not column.insert %}'{{ column.python_field | camel_to_snake }}'{% if not loop.last %}, {% endif %}{% endif %}{% endfor %}{% raw %}}{% endraw %}))
|
||||
db.add(db_{{ businessName }})
|
||||
await db.flush()
|
||||
|
||||
|
@@ -15,7 +15,7 @@ class {{ ClassName }}(Base):
|
||||
__tablename__ = '{{ tableName }}'
|
||||
|
||||
{% for column in columns %}
|
||||
{{ column.column_name }} = Column({{ column.column_type | get_sqlalchemy_type }}, {% if column.pk %}primary_key=True, {% endif %}{% if column.increment %}autoincrement=True, {% endif %}{% if column.required %}nullable=True{% else %}nullable=False{% endif %}, comment='{{ column.column_comment }}')
|
||||
{{ column.column_name }} = Column({{ column.column_type | get_sqlalchemy_type }}, {% if column.pk %}primary_key=True, {% endif %}{% if column.increment %}autoincrement=True, {% endif %}{% if column.required or column.pk %}nullable=False{% else %}nullable=True{% endif %}, comment='{{ column.column_comment }}')
|
||||
{% endfor %}
|
||||
|
||||
{% if table.sub %}
|
||||
|
@@ -43,7 +43,7 @@ class {{ BusinessName }}Service:
|
||||
{% for column in columns %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
{% if column.required %}
|
||||
{% if column.unique %}
|
||||
@classmethod
|
||||
async def check_{{ column.python_field | camel_to_snake }}_unique_services(cls, query_db: AsyncSession, page_object: {{ BusinessName }}Model):
|
||||
"""
|
||||
@@ -74,7 +74,7 @@ class {{ BusinessName }}Service:
|
||||
{% for column in columns %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
{% if column.required %}
|
||||
{% if column.unique %}
|
||||
if not await cls.check_{{ column.python_field | camel_to_snake }}_unique_services(query_db, page_object):
|
||||
raise ServiceException(message=f'新增{{ functionName }}{page_object.{{ column.python_field | camel_to_snake }}}失败,{{ comment }}已存在')
|
||||
{% endif %}
|
||||
@@ -103,13 +103,13 @@ class {{ BusinessName }}Service:
|
||||
:param page_object: 编辑{{ functionName }}对象
|
||||
:return: 编辑{{ functionName }}校验结果
|
||||
"""
|
||||
edit_{{ businessName }} = page_object.model_dump(exclude_unset=True{% if table.sub %}, exclude={'{{ subclassName }}_list'}{% endif %})
|
||||
edit_{{ businessName }} = page_object.model_dump(exclude_unset=True, exclude={% raw %}{{% endraw %}{% if table.sub %}'{{ subclassName }}_list, '{% endif %}{% for column in columns %}{% if not column.edit and not column.pk %}'{{ column.python_field | camel_to_snake }}'{% if not loop.last %}, {% endif %}{% endif %}{% endfor %}{% raw %}}{% endraw %})
|
||||
{{ businessName }}_info = await cls.{{ businessName }}_detail_services(query_db, page_object.{{ pk_field }})
|
||||
if {{ businessName }}_info.{{ pk_field }}:
|
||||
{% for column in columns %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
{% if column.required %}
|
||||
{% if column.unique %}
|
||||
if not await cls.check_{{ column.python_field | camel_to_snake }}_unique_services(query_db, page_object):
|
||||
raise ServiceException(message=f'修改{{ functionName }}{page_object.{{ column.python_field | camel_to_snake }}}失败,{{ comment }}已存在')
|
||||
{% endif %}
|
||||
|
@@ -162,7 +162,7 @@
|
||||
{% for column in columns %}
|
||||
{% set field = column.python_field %}
|
||||
{% if column.insert and not column.pk %}
|
||||
{% if column.usable_column or column.super_column %}
|
||||
{% if column.usable_column or not column.super_column %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
{% set dictType = column.dict_type %}
|
||||
|
@@ -174,7 +174,7 @@
|
||||
{% for column in columns %}
|
||||
{% set field = column.python_field %}
|
||||
{% if column.insert and not column.pk %}
|
||||
{% if column.usable_column or column.super_column %}
|
||||
{% if column.usable_column or not column.super_column %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
{% set dictType = column.dict_type %}
|
||||
|
@@ -137,7 +137,7 @@
|
||||
{% for column in columns %}
|
||||
{% set field = column.python_field %}
|
||||
{% if column.insert and not column.pk %}
|
||||
{% if column.usable_column or column.super_column %}
|
||||
{% if column.usable_column or not column.super_column %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
{% set dictType = column.dict_type %}
|
||||
|
@@ -160,7 +160,7 @@
|
||||
{% for column in columns %}
|
||||
{% set field = column.python_field %}
|
||||
{% if column.insert and not column.pk %}
|
||||
{% if column.usable_column or column.super_column %}
|
||||
{% if column.usable_column or not column.super_column %}
|
||||
{% set parentheseIndex = column.column_comment.find("(") %}
|
||||
{% set comment = column.column_comment[:parentheseIndex] if parentheseIndex != -1 else column.column_comment %}
|
||||
{% set dictType = column.dict_type %}
|
||||
|
@@ -918,6 +918,7 @@ create table gen_table_column (
|
||||
is_pk char(1),
|
||||
is_increment char(1),
|
||||
is_required char(1),
|
||||
is_unique char(1),
|
||||
is_insert char(1),
|
||||
is_edit char(1),
|
||||
is_list char(1),
|
||||
@@ -942,6 +943,7 @@ comment on column gen_table_column.python_field is 'PYTHON字段名';
|
||||
comment on column gen_table_column.is_pk is '是否主键(1是)';
|
||||
comment on column gen_table_column.is_increment is '是否自增(1是)';
|
||||
comment on column gen_table_column.is_required is '是否必填(1是)';
|
||||
comment on column gen_table_column.is_unique is '是否唯一(1是)';
|
||||
comment on column gen_table_column.is_insert is '是否为插入字段(1是)';
|
||||
comment on column gen_table_column.is_edit is '是否编辑字段(1是)';
|
||||
comment on column gen_table_column.is_list is '是否列表字段(1是)';
|
||||
|
@@ -696,6 +696,7 @@ create table gen_table_column (
|
||||
is_pk char(1) comment '是否主键(1是)',
|
||||
is_increment char(1) comment '是否自增(1是)',
|
||||
is_required char(1) comment '是否必填(1是)',
|
||||
is_unique char(1) comment '是否唯一(1是)',
|
||||
is_insert char(1) comment '是否为插入字段(1是)',
|
||||
is_edit char(1) comment '是否编辑字段(1是)',
|
||||
is_list char(1) comment '是否列表字段(1是)',
|
||||
|
@@ -71,13 +71,13 @@ class GenUtils:
|
||||
column.is_insert = GenConstant.REQUIRE
|
||||
|
||||
# 编辑字段
|
||||
if not cls.arrays_contains(GenConstant.COLUMNNAME_NOT_EDIT, column_name) and not column.is_pk:
|
||||
if not cls.arrays_contains(GenConstant.COLUMNNAME_NOT_EDIT, column_name) and not column.pk:
|
||||
column.is_edit = GenConstant.REQUIRE
|
||||
# 列表字段
|
||||
if not cls.arrays_contains(GenConstant.COLUMNNAME_NOT_LIST, column_name) and not column.is_pk:
|
||||
if not cls.arrays_contains(GenConstant.COLUMNNAME_NOT_LIST, column_name) and not column.pk:
|
||||
column.is_list = GenConstant.REQUIRE
|
||||
# 查询字段
|
||||
if not cls.arrays_contains(GenConstant.COLUMNNAME_NOT_QUERY, column_name) and not column.is_pk:
|
||||
if not cls.arrays_contains(GenConstant.COLUMNNAME_NOT_QUERY, column_name) and not column.pk:
|
||||
column.is_query = GenConstant.REQUIRE
|
||||
|
||||
# 查询字段类型
|
||||
|
@@ -85,6 +85,11 @@
|
||||
<el-checkbox true-label="1" false-label="0" v-model="scope.row.isRequired"></el-checkbox>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="唯一" min-width="5%">
|
||||
<template #default="scope">
|
||||
<el-checkbox true-label="1" false-label="0" v-model="scope.row.isUnique"></el-checkbox>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="显示类型" min-width="12%">
|
||||
<template #default="scope">
|
||||
<el-select v-model="scope.row.htmlType">
|
||||
|
Reference in New Issue
Block a user