优化表单生成规则

This commit is contained in:
hoteas 2021-06-12 13:33:35 +08:00
parent cc4d91cdbb
commit 5c8df885a2
2 changed files with 77 additions and 22 deletions

View File

@ -62,6 +62,11 @@ var ColumnNameType = []ColumnShow{
{"sex", true, true, true, false, "select", false}, {"sex", true, true, true, false, "select", false},
{"delete", false, false, false, false, "", false}, {"delete", false, false, false, false, "", false},
{"lat", false, true, true, false, "", false},
{"lng", false, true, true, false, "", false},
{"latitude", false, true, true, false, "", false},
{"longitude", false, true, true, false, "", false},
{"index", false, false, false, false, "", false}, {"index", false, false, false, false, "", false},
{"password", false, true, false, false, "password", false}, {"password", false, true, false, false, "password", false},
{"info", false, true, true, false, "", false}, {"info", false, true, true, false, "", false},

View File

@ -16,6 +16,7 @@ type MakeCode struct {
IndexMenus Map IndexMenus Map
TableConfig Map TableConfig Map
TableColumns map[string]map[string]Map TableColumns map[string]map[string]Map
SearchColumns map[string]map[string]Map
Config Map Config Map
Error Error
} }
@ -27,6 +28,9 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
if that.TableColumns == nil { if that.TableColumns == nil {
that.TableColumns = make(map[string]map[string]Map) that.TableColumns = make(map[string]map[string]Map)
} }
if that.SearchColumns == nil {
that.SearchColumns = make(map[string]map[string]Map)
}
//加载配置文件 //加载配置文件
btes, err := ioutil.ReadFile(path) btes, err := ioutil.ReadFile(path)
@ -84,16 +88,28 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
for tableName, _ := range that.TableConfig { for tableName, _ := range that.TableConfig {
columns := that.TableConfig.GetMap(tableName).GetSlice("columns") columns := that.TableConfig.GetMap(tableName).GetSlice("columns")
search := that.TableConfig.GetMap(tableName).GetSlice("search")
//初始化 //初始化
if that.TableColumns[tableName] == nil { if that.TableColumns[tableName] == nil {
that.TableColumns[tableName] = map[string]Map{} that.TableColumns[tableName] = map[string]Map{}
} }
//初始化搜索项
if that.SearchColumns[tableName] == nil {
that.SearchColumns[tableName] = map[string]Map{}
}
//注入源数据 //注入源数据
for index, _ := range columns { for index, _ := range columns {
columnsName := columns.GetMap(index).GetString("name") columnsName := columns.GetMap(index).GetString("name")
that.TableColumns[tableName][columnsName] = columns.GetMap(index) that.TableColumns[tableName][columnsName] = columns.GetMap(index)
} }
//注入search源数据
for index, _ := range search {
searchName := search.GetMap(index).GetString("name")
that.SearchColumns[tableName][searchName] = search.GetMap(index)
}
} }
myInit := strings.Replace(InitTpt, "{{name}}", name, -1) myInit := strings.Replace(InitTpt, "{{name}}", name, -1)
@ -121,13 +137,13 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
//{"type": "tree", "name": "oid", "label": "组织", "table": "organization", "showName": "label", "children": "children"}, //{"type": "tree", "name": "oid", "label": "组织", "table": "organization", "showName": "label", "children": "children"},
{"type": "text", "name": "keyword", "label": "请输入关键词", "value": nil}, {"type": "text", "name": "keyword", "label": "请输入关键词", "value": nil},
{"type": "date", "name": "date", "label": "时间段", "value": nil}, {"type": "date", "name": "date", "label": "时间段", "value": nil},
{"type": "select", "name": "state", "label": "状态", "value": nil, //{"type": "select", "name": "state", "label": "状态", "value": nil,
"option": []Map{ // "options": []Map{
{"name": "正常", "value": 0}, // {"name": "正常", "value": 0},
{"name": "异常", "value": 1}, // {"name": "异常", "value": 1},
{"name": "全部", "value": nil}, // {"name": "全部", "value": nil},
}, // },
}, //},
}, },
} }
} }
@ -143,6 +159,11 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
that.TableColumns[v.GetString("name")] = make(map[string]Map) that.TableColumns[v.GetString("name")] = make(map[string]Map)
} }
//初始化
if that.SearchColumns[v.GetString("name")] == nil {
that.SearchColumns[v.GetString("name")] = make(map[string]Map)
}
tableInfo := make([]Map, 0) tableInfo := make([]Map, 0)
if db.Type == "mysql" { if db.Type == "mysql" {
tableInfo = db.Select("INFORMATION_SCHEMA.COLUMNS", "COLUMN_NAME AS name,COLUMN_TYPE AS type,COLUMN_COMMENT AS label", Map{"AND": Map{"TABLE_SCHEMA": db.DBName, "TABLE_NAME": v.GetString("name")}}) tableInfo = db.Select("INFORMATION_SCHEMA.COLUMNS", "COLUMN_NAME AS name,COLUMN_TYPE AS type,COLUMN_COMMENT AS label", Map{"AND": Map{"TABLE_SCHEMA": db.DBName, "TABLE_NAME": v.GetString("name")}})
@ -159,11 +180,6 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
coloum := that.TableColumns[v.GetString("name")][info.GetString("name")] coloum := that.TableColumns[v.GetString("name")][info.GetString("name")]
if coloum == nil { if coloum == nil {
//备注以空格隔开,空格后的是其他备注
indexNum := strings.Index(info.GetString("label"), " ")
if indexNum >= 0 {
info["label"] = info.GetString("label")[:]
}
//根据类型判断真实类型 //根据类型判断真实类型
for k, v := range ColumnDataType { for k, v := range ColumnDataType {
@ -179,6 +195,11 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
//"add": false, "info": false, "edit": false, "list": true, //"add": false, "info": false, "edit": false, "list": true,
//"must": false, //"must": false,
} }
//备注以空格隔开,空格后的是其他备注
indexNum := strings.Index(info.GetString("label"), " ")
if indexNum > 0 {
coloum["label"] = info.GetString("label")[:indexNum]
}
for _, v := range ColumnNameType { for _, v := range ColumnNameType {
if (v.Strict && coloum.GetString("name") == v.Name) || strings.Contains(coloum.GetString("name"), v.Name) { if (v.Strict && coloum.GetString("name") == v.Name) || strings.Contains(coloum.GetString("name"), v.Name) {
@ -206,6 +227,24 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
break break
} }
} }
//如果是select类型需要设置options
if coloum.GetString("type") == "select" {
options := Slice{}
comments := strings.Split(info.GetString("label"), " ")
if len(comments) >= 2 {
optionComment := strings.Split(comments[1], ",")
for _, v := range optionComment {
optionSlice := strings.Split(v, "-")
if len(optionSlice) >= 2 {
options = append(options, Map{"name": optionSlice[1], "value": optionSlice[0]})
}
}
}
coloum["options"] = options
}
if !coloum.GetBool("notUse") { if !coloum.GetBool("notUse") {
that.TableConfig.GetMap(v.GetString("name"))["columns"] = append(that.TableConfig.GetMap(v.GetString("name")).GetSlice("columns"), coloum) that.TableConfig.GetMap(v.GetString("name"))["columns"] = append(that.TableConfig.GetMap(v.GetString("name")).GetSlice("columns"), coloum)
} }
@ -311,6 +350,17 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
} }
for k, v := range fv { for k, v := range fv {
//搜索服务
if that.SearchColumns[fk][v.GetString("name")] == nil &&
v.GetString("type") == "select" {
search := that.TableConfig.GetMap(fk).GetSlice("search")
sv := DeepCopyMap(v).(Map)
sv["value"] = nil
sv["options"] = append(sv.GetSlice("options"), Map{"name": "全部", "value": nil})
that.TableConfig.GetMap(fk)["search"] = append(search, sv)
}
//虚招后缀是_id结尾的表字段 假设org_id //虚招后缀是_id结尾的表字段 假设org_id
if len(k) <= 3 || strings.LastIndex(k, "_id") != len(k)-3 { if len(k) <= 3 || strings.LastIndex(k, "_id") != len(k)-3 {
continue continue
@ -366,17 +416,17 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
v["link"] = tableName v["link"] = tableName
//一般查询name字段或者label字段如果没有name字段则默认第二个地段 //一般查询name字段或者label字段如果没有name字段则默认第二个地段
if that.TableColumns[tableName]["name"] != nil { if that.TableColumns[tableName]["name"] != nil {
v["value"] = tableName + "." + "name" v["value"] = "name"
break continue
} }
if that.TableColumns[tableName]["label"] != nil { if that.TableColumns[tableName]["label"] != nil {
v["value"] = tableName + "." + "label" v["value"] = "label"
break continue
} }
if len(that.TableConfig.GetMap(tableName).GetSlice("columns")) > 2 { if len(that.TableConfig.GetMap(tableName).GetSlice("columns")) > 2 {
v["value"] = tableName + "." + that.TableConfig.GetMap(tableName).GetSlice("columns").GetMap(1).GetString("name") v["value"] = that.TableConfig.GetMap(tableName).GetSlice("columns").GetMap(1).GetString("name")
break continue
} }
} }