feat: 修复自动化代码的bug
This commit is contained in:
@@ -2,16 +2,7 @@
|
|||||||
// 在结构体中新增如下字段
|
// 在结构体中新增如下字段
|
||||||
{{- range .Fields}}
|
{{- range .Fields}}
|
||||||
{{- if ne .FieldSearchType ""}}
|
{{- if ne .FieldSearchType ""}}
|
||||||
{{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}}
|
{{ GenerateSearchField . }}
|
||||||
Start{{.FieldName}} *{{.FieldType}} `json:"start{{.FieldName}}" form:"start{{.FieldName}}"`
|
|
||||||
End{{.FieldName}} *{{.FieldType}} `json:"end{{.FieldName}}" form:"end{{.FieldName}}"`
|
|
||||||
{{- else }}
|
|
||||||
{{- if or (eq .FieldType "enum") (eq .FieldType "picture") (eq .FieldType "pictures") (eq .FieldType "video") (eq .FieldType "json") }}
|
|
||||||
{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" `
|
|
||||||
{{- else }}
|
|
||||||
{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" `
|
|
||||||
{{- end }}
|
|
||||||
{{- end }}
|
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- if .NeedSort}}
|
{{- if .NeedSort}}
|
||||||
@@ -36,16 +27,7 @@ type {{.StructName}}Search struct{
|
|||||||
{{- end }}
|
{{- end }}
|
||||||
{{- range .Fields}}
|
{{- range .Fields}}
|
||||||
{{- if ne .FieldSearchType ""}}
|
{{- if ne .FieldSearchType ""}}
|
||||||
{{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}}
|
{{ GenerateSearchField . }}
|
||||||
Start{{.FieldName}} *{{.FieldType}} `json:"start{{.FieldName}}" form:"start{{.FieldName}}"`
|
|
||||||
End{{.FieldName}} *{{.FieldType}} `json:"end{{.FieldName}}" form:"end{{.FieldName}}"`
|
|
||||||
{{- else }}
|
|
||||||
{{- if or (eq .FieldType "enum") (eq .FieldType "picture") (eq .FieldType "pictures") (eq .FieldType "video") (eq .FieldType "json") }}
|
|
||||||
{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" `
|
|
||||||
{{- else }}
|
|
||||||
{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" `
|
|
||||||
{{- end }}
|
|
||||||
{{- end }}
|
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
request.PageInfo
|
request.PageInfo
|
||||||
|
@@ -2,16 +2,7 @@
|
|||||||
// 在结构体中新增如下字段
|
// 在结构体中新增如下字段
|
||||||
{{- range .Fields}}
|
{{- range .Fields}}
|
||||||
{{- if ne .FieldSearchType ""}}
|
{{- if ne .FieldSearchType ""}}
|
||||||
{{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}}
|
{{ GenerateSearchField . }}
|
||||||
Start{{.FieldName}} *{{.FieldType}} `json:"start{{.FieldName}}" form:"start{{.FieldName}}"`
|
|
||||||
End{{.FieldName}} *{{.FieldType}} `json:"end{{.FieldName}}" form:"end{{.FieldName}}"`
|
|
||||||
{{- else }}
|
|
||||||
{{- if or (eq .FieldType "enum") (eq .FieldType "picture") (eq .FieldType "pictures") (eq .FieldType "video") (eq .FieldType "json") }}
|
|
||||||
{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" `
|
|
||||||
{{- else }}
|
|
||||||
{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" `
|
|
||||||
{{- end }}
|
|
||||||
{{- end }}
|
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- if .NeedSort}}
|
{{- if .NeedSort}}
|
||||||
@@ -35,16 +26,7 @@ type {{.StructName}}Search struct{
|
|||||||
{{- end }}
|
{{- end }}
|
||||||
{{- range .Fields}}
|
{{- range .Fields}}
|
||||||
{{- if ne .FieldSearchType ""}}
|
{{- if ne .FieldSearchType ""}}
|
||||||
{{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}}
|
{{ GenerateSearchField . }}
|
||||||
Start{{.FieldName}} *{{.FieldType}} `json:"start{{.FieldName}}" form:"start{{.FieldName}}"`
|
|
||||||
End{{.FieldName}} *{{.FieldType}} `json:"end{{.FieldName}}" form:"end{{.FieldName}}"`
|
|
||||||
{{- else }}
|
|
||||||
{{- if or (eq .FieldType "enum") (eq .FieldType "picture") (eq .FieldType "pictures") (eq .FieldType "video") (eq .FieldType "json") }}
|
|
||||||
{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" `
|
|
||||||
{{- else }}
|
|
||||||
{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" `
|
|
||||||
{{- end }}
|
|
||||||
{{- end }}
|
|
||||||
{{- end}}
|
{{- end}}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
request.PageInfo
|
request.PageInfo
|
||||||
|
@@ -12,6 +12,7 @@ import (
|
|||||||
func GetTemplateFuncMap() template.FuncMap {
|
func GetTemplateFuncMap() template.FuncMap {
|
||||||
return template.FuncMap{
|
return template.FuncMap{
|
||||||
"GenerateField": GenerateField,
|
"GenerateField": GenerateField,
|
||||||
|
"GenerateSearchField": GenerateSearchField,
|
||||||
"GenerateSearchConditions": GenerateSearchConditions,
|
"GenerateSearchConditions": GenerateSearchConditions,
|
||||||
"GenerateSearchFormItem": GenerateSearchFormItem,
|
"GenerateSearchFormItem": GenerateSearchFormItem,
|
||||||
"GenerateTableColumn": GenerateTableColumn,
|
"GenerateTableColumn": GenerateTableColumn,
|
||||||
@@ -44,6 +45,10 @@ func GenerateField(field systemReq.AutoCodeField) string {
|
|||||||
|
|
||||||
gormTag += "column:" + field.ColumnName + ";"
|
gormTag += "column:" + field.ColumnName + ";"
|
||||||
|
|
||||||
|
if field.DataTypeLong != "" && field.FieldType != "enum" {
|
||||||
|
gormTag += fmt.Sprintf("size:%s;", field.DataTypeLong)
|
||||||
|
}
|
||||||
|
|
||||||
requireTag := ` binding:"required"` + "`"
|
requireTag := ` binding:"required"` + "`"
|
||||||
|
|
||||||
// 根据字段类型构建不同的字段定义
|
// 根据字段类型构建不同的字段定义
|
||||||
@@ -55,40 +60,30 @@ func GenerateField(field systemReq.AutoCodeField) string {
|
|||||||
case "picture", "video":
|
case "picture", "video":
|
||||||
tagContent := fmt.Sprintf(`json:"%s" form:"%s" gorm:"%s"`,
|
tagContent := fmt.Sprintf(`json:"%s" form:"%s" gorm:"%s"`,
|
||||||
field.FieldJson, field.FieldJson, gormTag)
|
field.FieldJson, field.FieldJson, gormTag)
|
||||||
if field.DataTypeLong != "" {
|
|
||||||
tagContent += fmt.Sprintf("size:%s;", field.DataTypeLong)
|
|
||||||
}
|
|
||||||
result = fmt.Sprintf(`%s string `+"`"+`%s`+"`"+``, field.FieldName, tagContent)
|
result = fmt.Sprintf(`%s string `+"`"+`%s`+"`"+``, field.FieldName, tagContent)
|
||||||
case "file", "pictures", "array":
|
case "file", "pictures", "array":
|
||||||
tagContent := fmt.Sprintf(`json:"%s" form:"%s" gorm:"%s"`,
|
tagContent := fmt.Sprintf(`json:"%s" form:"%s" gorm:"%s"`,
|
||||||
field.FieldJson, field.FieldJson, gormTag)
|
field.FieldJson, field.FieldJson, gormTag)
|
||||||
if field.DataTypeLong != "" {
|
|
||||||
tagContent += fmt.Sprintf("size:%s;", field.DataTypeLong)
|
|
||||||
}
|
|
||||||
result = fmt.Sprintf(`%s datatypes.JSON `+"`"+`%s swaggertype:"array,object"`+"`"+``,
|
result = fmt.Sprintf(`%s datatypes.JSON `+"`"+`%s swaggertype:"array,object"`+"`"+``,
|
||||||
field.FieldName, tagContent)
|
field.FieldName, tagContent)
|
||||||
case "richtext":
|
case "richtext":
|
||||||
tagContent := fmt.Sprintf(`json:"%s" form:"%s" gorm:"%s"`,
|
tagContent := fmt.Sprintf(`json:"%s" form:"%s" gorm:"%s"`,
|
||||||
field.FieldJson, field.FieldJson, gormTag)
|
field.FieldJson, field.FieldJson, gormTag)
|
||||||
if field.DataTypeLong != "" {
|
|
||||||
tagContent += fmt.Sprintf("size:%s;", field.DataTypeLong)
|
|
||||||
}
|
|
||||||
result = fmt.Sprintf(`%s *string `+"`"+`%stype:text;"`+"`"+``,
|
result = fmt.Sprintf(`%s *string `+"`"+`%stype:text;"`+"`"+``,
|
||||||
field.FieldName, tagContent)
|
field.FieldName, tagContent)
|
||||||
case "json":
|
case "json":
|
||||||
tagContent := fmt.Sprintf(`json:"%s" form:"%s" gorm:"%s"`,
|
tagContent := fmt.Sprintf(`json:"%s" form:"%s" gorm:"%s"`,
|
||||||
field.FieldJson, field.FieldJson, gormTag)
|
field.FieldJson, field.FieldJson, gormTag)
|
||||||
if field.DataTypeLong != "" {
|
|
||||||
tagContent += fmt.Sprintf("size:%s;", field.DataTypeLong)
|
|
||||||
}
|
|
||||||
result = fmt.Sprintf(`%s datatypes.JSON `+"`"+`%s swaggertype:"object"`+"`"+``,
|
result = fmt.Sprintf(`%s datatypes.JSON `+"`"+`%s swaggertype:"object"`+"`"+``,
|
||||||
field.FieldName, tagContent)
|
field.FieldName, tagContent)
|
||||||
default:
|
default:
|
||||||
tagContent := fmt.Sprintf(`json:"%s" form:"%s" gorm:"%s"`,
|
tagContent := fmt.Sprintf(`json:"%s" form:"%s" gorm:"%s"`,
|
||||||
field.FieldJson, field.FieldJson, gormTag)
|
field.FieldJson, field.FieldJson, gormTag)
|
||||||
if field.DataTypeLong != "" {
|
|
||||||
tagContent += fmt.Sprintf("size:%s;", field.DataTypeLong)
|
|
||||||
}
|
|
||||||
result = fmt.Sprintf(`%s *%s `+"`"+`%s`+"`"+``,
|
result = fmt.Sprintf(`%s *%s `+"`"+`%s`+"`"+``,
|
||||||
field.FieldName, field.FieldType, tagContent)
|
field.FieldName, field.FieldType, tagContent)
|
||||||
}
|
}
|
||||||
@@ -672,3 +667,34 @@ func GenerateDefaultFormValue(field systemReq.AutoCodeField) string {
|
|||||||
// 返回格式化后的默认值字符串
|
// 返回格式化后的默认值字符串
|
||||||
return fmt.Sprintf(`%s: %s,`, field.FieldJson, defaultValue)
|
return fmt.Sprintf(`%s: %s,`, field.FieldJson, defaultValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GenerateSearchField 根据字段属性生成搜索结构体中的字段定义
|
||||||
|
func GenerateSearchField(field systemReq.AutoCodeField) string {
|
||||||
|
var result string
|
||||||
|
|
||||||
|
if field.FieldSearchType == "" {
|
||||||
|
return "" // 如果没有搜索类型,返回空字符串
|
||||||
|
}
|
||||||
|
|
||||||
|
if field.FieldSearchType == "BETWEEN" || field.FieldSearchType == "NOT BETWEEN" {
|
||||||
|
// 生成范围搜索字段
|
||||||
|
startField := fmt.Sprintf("Start%s *%s `json:\"start%s\" form:\"start%s\"`",
|
||||||
|
field.FieldName, field.FieldType, field.FieldName, field.FieldName)
|
||||||
|
endField := fmt.Sprintf("End%s *%s `json:\"end%s\" form:\"end%s\"`",
|
||||||
|
field.FieldName, field.FieldType, field.FieldName, field.FieldName)
|
||||||
|
result = startField + "\n" + endField
|
||||||
|
} else {
|
||||||
|
// 生成普通搜索字段
|
||||||
|
if field.FieldType == "enum" || field.FieldType == "picture" ||
|
||||||
|
field.FieldType == "pictures" || field.FieldType == "video" ||
|
||||||
|
field.FieldType == "json" || field.FieldType == "richtext" {
|
||||||
|
result = fmt.Sprintf("%s string `json:\"%s\" form:\"%s\"` ",
|
||||||
|
field.FieldName, field.FieldJson, field.FieldJson)
|
||||||
|
} else {
|
||||||
|
result = fmt.Sprintf("%s *%s `json:\"%s\" form:\"%s\"` ",
|
||||||
|
field.FieldName, field.FieldType, field.FieldJson, field.FieldJson)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user