代码生成:简化现有 crud 模版的各种 pretty 相关的判断,交给 Java 代码后置统一处理。

This commit is contained in:
YunaiV
2023-11-11 12:56:58 +08:00
parent ebb8f04fb5
commit 055eb350aa
12 changed files with 76 additions and 83 deletions

View File

@@ -6,9 +6,11 @@ import java.util.*;
#foreach ($column in $columns)
#if (${column.javaType} == "BigDecimal")
import java.math.BigDecimal;
#break
#end
#if (${column.javaType} == "LocalDateTime")
import java.time.LocalDateTime;
#break
#end
#end
import javax.validation.constraints.*;

View File

@@ -6,9 +6,11 @@ import java.util.*;
#foreach ($column in $columns)
#if (${column.javaType} == "BigDecimal")
import java.math.BigDecimal;
#break
#end
#if (${column.javaType} == "LocalDateTime")
import java.time.LocalDateTime;
#break
#end
#end

View File

@@ -48,7 +48,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
@Override
## 特殊:主子表专属逻辑
#if ( $subTables.size() > 0)
#if ( $subTables && $subTables.size() > 0 )
@Transactional(rollbackFor = Exception.class)
#end
public ${primaryColumn.javaType} create${simpleClassName}(${sceneEnum.prefixClass}${table.className}CreateReqVO createReqVO) {
@@ -56,7 +56,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
${table.className}DO ${classNameVar} = ${table.className}Convert.INSTANCE.convert(createReqVO);
${classNameVar}Mapper.insert(${classNameVar});
## 特殊:主子表专属逻辑
#if ( $subTables.size() > 0)
#if ( $subTables && $subTables.size() > 0)
// 插入子表($subTable.classComment
#foreach ($subTable in $subTables)
@@ -77,7 +77,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
@Override
## 特殊:主子表专属逻辑
#if ( $subTables.size() > 0)
#if ( $subTables && $subTables.size() > 0)
@Transactional(rollbackFor = Exception.class)
#end
public void update${simpleClassName}(${sceneEnum.prefixClass}${table.className}UpdateReqVO updateReqVO) {
@@ -87,7 +87,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
${table.className}DO updateObj = ${table.className}Convert.INSTANCE.convert(updateReqVO);
${classNameVar}Mapper.updateById(updateObj);
## 特殊:主子表专属逻辑
#if ( $subTables.size() > 0)
#if ( $subTables && $subTables.size() > 0)
// 更新子表
#foreach ($subTable in $subTables)
@@ -106,7 +106,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
@Override
## 特殊:主子表专属逻辑
#if ( $subTables.size() > 0)
#if ( $subTables && $subTables.size() > 0)
@Transactional(rollbackFor = Exception.class)
#end
public void delete${simpleClassName}(${primaryColumn.javaType} id) {
@@ -115,7 +115,7 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
// 删除
${classNameVar}Mapper.deleteById(id);
## 特殊:主子表专属逻辑
#if ( $subTables.size() > 0)
#if ( $subTables && $subTables.size() > 0)
// 删除子表
#foreach ($subTable in $subTables)

View File

@@ -7,7 +7,6 @@
label-width="100px"
v-loading="formLoading"
>
#set ($dictMethods = [])## 使用到的 dict 字典方法
#foreach($column in $columns)
#if ($column.createOperation || $column.updateOperation)
#set ($dictType = $column.dictType)
@@ -45,9 +44,6 @@
<el-form-item label="${comment}" prop="${javaField}">
<el-select v-model="formData.${javaField}" placeholder="请选择${comment}">
#if ("" != $dictType)## 有数据字典
#if (!$dictMethods.contains($dictMethod))## 如果不存在,则添加到 dictMethods 数组中,后续好 import
#set($ignore = $dictMethods.add($dictMethod) )
#end
<el-option
v-for="dict in $dictMethod(DICT_TYPE.$dictType.toUpperCase())"
:key="dict.value"
@@ -63,9 +59,6 @@
<el-form-item label="${comment}" prop="${javaField}">
<el-checkbox-group v-model="formData.${javaField}">
#if ("" != $dictType)## 有数据字典
#if (!$dictMethods.contains($dictMethod))## 如果不存在,则添加到 dictMethods 数组中,后续好 import
#set($ignore = $dictMethods.add($dictMethod) )
#end
<el-checkbox
v-for="dict in $dictMethod(DICT_TYPE.$dictType.toUpperCase())"
:key="dict.value"
@@ -82,9 +75,6 @@
<el-form-item label="${comment}" prop="${javaField}">
<el-radio-group v-model="formData.${javaField}">
#if ("" != $dictType)## 有数据字典
#if (!$dictMethods.contains($dictMethod))## 如果不存在,则添加到 dictMethods 数组中,后续好 import
#set($ignore = $dictMethods.add($dictMethod) )
#end
<el-radio
v-for="dict in $dictMethod(DICT_TYPE.$dictType.toUpperCase())"
:key="dict.value"
@@ -121,9 +111,7 @@
</Dialog>
</template>
<script setup lang="ts">
#if ($dictMethods.size() > 0)
import { DICT_TYPE#foreach ($dictMethod in $dictMethods), ${dictMethod}#end } from '@/utils/dict'
#end
import { getIntDictOptions, getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import * as ${simpleClassName}Api from '@/api/${table.moduleName}/${classNameVar}'
const { t } = useI18n() // 国际化
@@ -134,33 +122,21 @@ const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中1修改时的数据加载2提交的按钮禁用
const formType = ref('') // 表单的类型create - 新增update - 修改
const formData = ref({
#set ($listOperationLastIndex = -1)## 求最后一个需要 , 的地方
#foreach ($column in $columns)
#if ($column.createOperation || $column.updateOperation)
#set ($listOperationLastIndex = $foreach.index)
#end
#end
#foreach ($column in $columns)
#if ($column.createOperation || $column.updateOperation)
#if ($column.htmlType == "checkbox")
$column.javaField: []#if($foreach.index < $listOperationLastIndex),#end
$column.javaField: [],
#else
$column.javaField: undefined#if($foreach.index < $listOperationLastIndex),#end
$column.javaField: undefined,
#end
#end
#end
})
const formRules = reactive({
#set ($listOperationLastIndex = -1)## 求最后一个需要 , 的地方
#foreach ($column in $columns)
#if (($column.createOperation || $column.updateOperation) && !$column.nullable && !${column.primaryKey})## 创建或者更新操作 && 要求非空 && 非主键
#set ($listOperationLastIndex = $foreach.index)
#end
#end
#foreach ($column in $columns)
#if (($column.createOperation || $column.updateOperation) && !$column.nullable && !${column.primaryKey})## 创建或者更新操作 && 要求非空 && 非主键
#set($comment=$column.columnComment)
$column.javaField: [{ required: true, message: '${comment}不能为空', trigger: #if($column.htmlType == 'select')'change'#else'blur'#end }]#if($foreach.index < $listOperationLastIndex),#end
$column.javaField: [{ required: true, message: '${comment}不能为空', trigger: #if($column.htmlType == 'select')'change'#else'blur'#end }],
#end
#end
})
@@ -188,9 +164,7 @@ defineExpose({ open }) // 提供 open 方法,用于打开弹窗
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
const submitForm = async () => {
// 校验表单
if (!formRef) return
const valid = await formRef.value.validate()
if (!valid) return
await formRef.value.validate()
// 提交请求
formLoading.value = true
try {
@@ -213,18 +187,12 @@ const submitForm = async () => {
/** 重置表单 */
const resetForm = () => {
formData.value = {
#set ($listOperationLastIndex = -1)## 求最后一个需要 , 的地方
#foreach ($column in $columns)
#if ($column.createOperation || $column.updateOperation)
#set ($listOperationLastIndex = $foreach.index)
#end
#end
#foreach ($column in $columns)
#if ($column.createOperation || $column.updateOperation)
#if ($column.htmlType == "checkbox")
$column.javaField: []#if($foreach.index < $listOperationLastIndex),#end
$column.javaField: [],
#else
$column.javaField: undefined#if($foreach.index < $listOperationLastIndex),#end
$column.javaField: undefined,
#end
#end
#end

View File

@@ -8,7 +8,6 @@
:inline="true"
label-width="68px"
>
#set ($dictMethods = [])## 使用到的 dict 字典方法
#foreach($column in $columns)
#if ($column.listOperation)
#set ($dictType = $column.dictType)
@@ -36,20 +35,13 @@
</el-form-item>
#elseif ($column.htmlType == "select" || $column.htmlType == "radio")
<el-form-item label="${comment}" prop="${javaField}">
#if ($javaField.length() + $comment.length() > 8)
<el-select
v-model="queryParams.${javaField}"
placeholder="请选择${comment}"
clearable
class="!w-240px"
>
#else
<el-select v-model="queryParams.${javaField}" placeholder="请选择${comment}" clearable class="!w-240px">
#end
#if ("" != $dictType)## 设置了 dictType 数据字典的情况
#if (!$dictMethods.contains($dictMethod))## 如果不存在,则添加到 dictMethods 数组中,后续好 import
#set($ignore = $dictMethods.add($dictMethod) )
#end
<el-option
v-for="dict in $dictMethod(DICT_TYPE.$dictType.toUpperCase())"
:key="dict.value"
@@ -92,16 +84,12 @@
<el-form-item>
<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
<el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
#if ($permissionPrefix.length() <= 12)
<el-button type="primary" @click="openForm('create')" v-hasPermi="['${permissionPrefix}:create']">
#else
<el-button
type="primary"
plain
@click="openForm('create')"
v-hasPermi="['${permissionPrefix}:create']"
>
#end
<Icon icon="ep:plus" class="mr-5px" /> 新增
</el-button>
<el-button
@@ -180,15 +168,8 @@
</template>
<script setup lang="ts">
#if ($dictMethods.size() > 0)
import { DICT_TYPE#foreach ($dictMethod in $dictMethods), ${dictMethod}#end } from '@/utils/dict'
#end
#foreach ($column in $columns)
#if ($column.listOperationResult && $column.htmlType == "datetime")
import { getIntDictOptions, getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
#break
#end
#end
import download from '@/utils/download'
import * as ${simpleClassName}Api from '@/api/${table.moduleName}/${classNameVar}'
import ${simpleClassName}Form from './${simpleClassName}Form.vue'
@@ -204,19 +185,13 @@ const list = ref([]) // 列表的数据
const queryParams = reactive({
pageNo: 1,
pageSize: 10,
#set ($listOperationLastIndex = -1)## 求最后一个需要 , 的地方
#foreach ($column in $columns)
#if ($column.listOperation)
#set ($listOperationLastIndex = $foreach.index)
#end
#end
#foreach ($column in $columns)
#if ($column.listOperation)
#if ($column.listOperationCondition != 'BETWEEN')
$column.javaField: null#if($foreach.index < $listOperationLastIndex),#end
$column.javaField: null,
#end
#if ($column.htmlType == "datetime" || $column.listOperationCondition == "BETWEEN")
$column.javaField: []#if($foreach.index < $listOperationLastIndex),#end
$column.javaField: [],
#end
#end
#end

View File

@@ -1,10 +1,5 @@
import type { CrudSchema } from '@/hooks/web/useCrudSchemas'
#foreach ($column in $columns)
#if ($column.listOperationResult && $column.htmlType == "datetime")
import { dateFormatter } from '@/utils/formatTime'
#break
#end
#end
// 表单校验
export const rules = reactive({

View File

@@ -8,7 +8,7 @@
</Dialog>
</template>
<script setup lang="ts">
import * as ${simpleClassName}Api from '@/api/${table.moduleName}/${classNameVar}'
import * as ${simpleClassName}Api from '@/api/${table.moduleName}/${classNameVar}'
import { rules, allSchemas } from './${classNameVar}.data'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗