Browse Source

代码生成器支持直接使用字典

修复查询涉及sql关键字时sql语句报错问题
main
QM303176530 4 years ago
parent
commit
323236e756
  1. 1
      server/model/sys_auto_code.go
  2. 25
      server/resource/template/fe/table.vue.tpl
  3. 12
      server/resource/template/te/service.go.tpl

1
server/model/sys_auto_code.go

@ -21,4 +21,5 @@ type Field struct {
Comment string `json:"comment"`
ColumnName string `json:"columnName"`
FieldSearchType string `json:"fieldSearchType"`
DictType string `json:"dictType"`
}

25
server/resource/template/fe/table.vue.tpl

@ -52,7 +52,14 @@
<el-table-column label="日期" width="180">
<template slot-scope="scope">{{ "{{scope.row.CreatedAt|formatDate}}" }}</template>
</el-table-column>
{{range .Fields}} {{- if eq .FieldType "bool" }}
{{range .Fields}}
{{- if .DictType}}
<el-table-column label="{{.FieldDesc}}" prop="{{.FieldJson}}" width="120">
<template slot-scope="scope">
{{"{{"}}filterDict(scope.row.{{.FieldJson}},"{{.DictType}}"){{"}}"}}
</template>
</el-table-column>
{{- else if eq .FieldType "bool" }}
<el-table-column label="{{.FieldDesc}}" prop="{{.FieldJson}}" width="120">
<template slot-scope="scope">{{ "{{scope.row."}}{{.FieldJson}}{{"|formatBoolean}}" }}</template>
</el-table-column> {{- else }}
@ -117,6 +124,11 @@ export default {
type: "",
deleteVisible: false,
multipleSelection: [],
{{- range .Fields}}
{{- if .DictType }}
{{.DictType}}Options:[],
{{ end -}}
{{end -}}
formData: {
{{range .Fields}}{{.FieldJson}}:null,{{ end }}
}
@ -222,9 +234,14 @@ export default {
this.dialogFormVisible = true;
}
},
created() {
this.getTableData();
}
async created() {
await this.getTableData();
{{- range .Fields -}}
{{- if .DictType -}}
await this.getDict("{{.DictType}}")
{{- end -}}
{{- end -}}
}
};
</script>

12
server/resource/template/te/service.go.tpl

@ -63,7 +63,7 @@ func Get{{.StructName}}(id uint) (err error, {{.Abbreviation}} model.{{.StructNa
}
// @title Get{{.StructName}}InfoList
// @description get {{.StructName}} list by pagination, 分页获取用户列表
// @description get {{.StructName}} list by pagination, 分页获取{{.StructName}}
// @auth (2020/04/05 20:22)
// @param info PageInfo
// @return error
@ -79,23 +79,23 @@ func Get{{.StructName}}InfoList(info request.{{.StructName}}Search) (err error,
{{- if .FieldSearchType}}
{{- if eq .FieldType "string" }}
if info.{{.FieldName}} != "" {
db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+ {{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+ {{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
}
{{- else if eq .FieldType "bool" }}
if info.{{.FieldName}} != nil {
db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
}
{{- else if eq .FieldType "int" }}
if info.{{.FieldName}} != 0 {
db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
}
{{- else if eq .FieldType "float64" }}
if info.{{.FieldName}} != 0 {
db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
}
{{- else if eq .FieldType "time.Time" }}
if !info.{{.FieldName}}.IsZero() {
db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
}
{{- end }}
{{- end }}

Loading…
Cancel
Save