forked from golang/hotime
优化代码
This commit is contained in:
parent
642bf97272
commit
1077f09755
@ -23,3 +23,58 @@ var Config = Map{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
var ColumnDataType = map[string]string{
|
||||||
|
//sqlite专有类型
|
||||||
|
"real": "number",
|
||||||
|
//mysql数据类型宽泛类型
|
||||||
|
"int": "number",
|
||||||
|
"float": "number",
|
||||||
|
"double": "number",
|
||||||
|
"decimal": "number",
|
||||||
|
"char": "text",
|
||||||
|
"text": "text",
|
||||||
|
"blob": "text",
|
||||||
|
"date": "time",
|
||||||
|
"time": "time",
|
||||||
|
"year": "time",
|
||||||
|
"geometry": "gis", //不建议使用gis类型,建议使用其他代替
|
||||||
|
}
|
||||||
|
|
||||||
|
type ColumnShow struct {
|
||||||
|
Name string
|
||||||
|
List bool
|
||||||
|
Edit bool
|
||||||
|
Info bool
|
||||||
|
Must bool
|
||||||
|
Type string //空字符串表示
|
||||||
|
Strict bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var ColumnNameType = []ColumnShow{
|
||||||
|
//通用
|
||||||
|
{"idcard", false, true, true, false, "", false},
|
||||||
|
{"id", true, false, true, false, "", true},
|
||||||
|
//"sn"{true,true,true,""},
|
||||||
|
{"statu", true, true, true, false, "select", false},
|
||||||
|
{"state", false, true, true, false, "select", false},
|
||||||
|
{"delete", false, false, false, false, "", false},
|
||||||
|
{"index", false, false, false, false, "", false},
|
||||||
|
{"password", false, true, false, false, "password", false},
|
||||||
|
{"info", false, true, true, false, "", false},
|
||||||
|
{"version", false, false, false, false, "", false},
|
||||||
|
{"seq", false, true, true, false, "", false},
|
||||||
|
{"sort", false, true, true, false, "", false},
|
||||||
|
{"note", false, true, true, false, "", false},
|
||||||
|
{"description", false, true, true, false, "", false},
|
||||||
|
{"address", false, true, true, false, "", false},
|
||||||
|
{"full_name", false, true, true, false, "", false},
|
||||||
|
{"create_time", false, false, true, false, "", false},
|
||||||
|
{"modify_time", false, false, true, false, "", false},
|
||||||
|
{"image", false, true, true, false, "image", false},
|
||||||
|
{"img", false, true, true, false, "image", false},
|
||||||
|
{"file", false, true, true, false, "file", false},
|
||||||
|
{"age", false, true, true, false, "", false},
|
||||||
|
{"email", false, true, true, false, "", false},
|
||||||
|
{"sex", true, true, true, false, "select", false},
|
||||||
|
{"level", false, false, true, false, "", false},
|
||||||
|
}
|
||||||
|
149
code/makecode.go
149
code/makecode.go
@ -29,6 +29,7 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
|||||||
|
|
||||||
//加载配置文件
|
//加载配置文件
|
||||||
btes, err := ioutil.ReadFile(path)
|
btes, err := ioutil.ReadFile(path)
|
||||||
|
Config["name"] = name
|
||||||
that.Config = DeepCopyMap(Config).(Map)
|
that.Config = DeepCopyMap(Config).(Map)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
cmap := Map{}
|
cmap := Map{}
|
||||||
@ -95,13 +96,14 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if !(that.TableConfig.GetMap(v.GetString("name")).GetString("label") != "备注" &&
|
|
||||||
v.GetString("label") == "备注") {
|
|
||||||
that.TableConfig.GetMap(v.GetString("name"))["label"] = v.GetString("label")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
//else {
|
||||||
|
// if !(that.TableConfig.GetMap(v.GetString("name")).GetString("label") != "备注" &&
|
||||||
|
// v.GetString("label") == "备注") {
|
||||||
|
// that.TableConfig.GetMap(v.GetString("name"))["label"] = v.GetString("label")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//}
|
||||||
//初始化
|
//初始化
|
||||||
if that.TableColumns[v.GetString("name")] == nil {
|
if that.TableColumns[v.GetString("name")] == nil {
|
||||||
that.TableColumns[v.GetString("name")] = make(map[string]Map)
|
that.TableColumns[v.GetString("name")] = make(map[string]Map)
|
||||||
@ -122,22 +124,67 @@ 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 {
|
||||||
|
if strings.Contains(info.GetString("type"), k) {
|
||||||
|
info["type"] = v
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
coloum = Map{
|
coloum = Map{
|
||||||
"name": info.GetString("name"),
|
"name": info.GetString("name"),
|
||||||
"type": info.GetString("type"),
|
"type": info.GetString("type"),
|
||||||
"label": info.GetString("label"),
|
"label": info.GetString("label"),
|
||||||
//"add": false, "info": false, "edit": false, "list": true,
|
//"add": false, "info": false, "edit": false, "list": true,
|
||||||
"must": false,
|
//"must": false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, v := range ColumnNameType {
|
||||||
|
if (v.Strict && coloum.GetString("name") == v.Name) || strings.Contains(coloum.GetString("name"), v.Name) {
|
||||||
|
//全部都不需要则不加入
|
||||||
|
if v.Edit == false && v.List == false && v.Info == false {
|
||||||
|
coloum["notUse"] = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if v.Info == false {
|
||||||
|
coloum["info"] = v.Info
|
||||||
|
}
|
||||||
|
if v.Edit == false {
|
||||||
|
coloum["edit"] = v.Edit
|
||||||
|
coloum["add"] = v.Edit
|
||||||
|
}
|
||||||
|
if v.List == false {
|
||||||
|
coloum["list"] = v.List
|
||||||
|
}
|
||||||
|
if v.Must == true {
|
||||||
|
coloum["must"] = v.Must
|
||||||
|
}
|
||||||
|
if v.Type != "" {
|
||||||
|
coloum["type"] = v.Type
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
} else {
|
|
||||||
|
|
||||||
if !(coloum.GetString("label") != "备注" && info.GetString("label") == "备注") {
|
|
||||||
coloum["label"] = info.GetString("label")
|
|
||||||
}
|
|
||||||
coloum["type"] = info.GetString("type")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
//else {
|
||||||
|
//
|
||||||
|
// //if !(coloum.GetString("label") != "备注" && info.GetString("label") == "备注") {
|
||||||
|
// // coloum["label"] = info.GetString("label")
|
||||||
|
// //}
|
||||||
|
// //coloum["type"] = info.GetString("type")
|
||||||
|
//}
|
||||||
|
|
||||||
|
//暂时不关闭参数,保证表数据完全读取到
|
||||||
that.TableColumns[v.GetString("name")][info.GetString("name")] = coloum
|
that.TableColumns[v.GetString("name")][info.GetString("name")] = coloum
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -163,6 +210,7 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
|||||||
|
|
||||||
that.Config["tables"] = that.TableConfig
|
that.Config["tables"] = that.TableConfig
|
||||||
|
|
||||||
|
//生成id,判断数据库是否有改变,以保证数据库和配置文件匹配唯一
|
||||||
id := Md5(ObjToStr(idSlice))
|
id := Md5(ObjToStr(idSlice))
|
||||||
|
|
||||||
if id == that.Config.GetString("id") {
|
if id == that.Config.GetString("id") {
|
||||||
@ -172,6 +220,65 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
//数据生成完后开始关联id
|
||||||
|
for fk, fv := range that.TableColumns {
|
||||||
|
for k, v := range fv {
|
||||||
|
if len(k) <= 3 || strings.LastIndex(k, "_id") != len(k)-3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
//普通表匹配
|
||||||
|
oldTableName := k[:len(k)-3]
|
||||||
|
|
||||||
|
//上级ID匹配
|
||||||
|
if oldTableName == "parent" {
|
||||||
|
oldTableName = fk
|
||||||
|
}
|
||||||
|
//字段有动词前缀,自动进行解析
|
||||||
|
prefixColumn := strings.Index(oldTableName, "_")
|
||||||
|
if prefixColumn > -1 && that.TableConfig[oldTableName[prefixColumn+1:]] != nil {
|
||||||
|
oldTableName = oldTableName[prefixColumn+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
//普通方式查询不到,则转换为大型项目模块划分,暂时只支持一级模块划分,比如表sys_user 字段org_id,查询不到sys_org表则查询org表,
|
||||||
|
//都查询不到则找不到,
|
||||||
|
prefix := strings.Index(fk, "_")
|
||||||
|
tableName := oldTableName
|
||||||
|
if prefix > 0 {
|
||||||
|
//表模块前缀
|
||||||
|
tableName = fk[:prefix+1] + oldTableName
|
||||||
|
if that.TableConfig[tableName] == nil {
|
||||||
|
tableName = oldTableName
|
||||||
|
}
|
||||||
|
//表前缀+去除字段前缀
|
||||||
|
prefixColumn := strings.Index(oldTableName, "_")
|
||||||
|
if prefixColumn > -1 {
|
||||||
|
tableName = fk[:prefix+1] + oldTableName[prefixColumn+1:]
|
||||||
|
if that.TableConfig[tableName] == nil {
|
||||||
|
tableName = oldTableName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if that.TableConfig[tableName] != nil {
|
||||||
|
v["link"] = tableName
|
||||||
|
//一般查询name字段,如果没有name字段则默认第二个地段
|
||||||
|
if that.TableColumns[tableName]["name"] != nil {
|
||||||
|
v["value"] = tableName + "." + "name"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(that.TableConfig.GetMap(tableName).GetSlice("columns")) > 2 {
|
||||||
|
v["value"] = tableName + "." + that.TableConfig.GetMap(tableName).GetSlice("columns").GetMap(1).GetString("name")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(id, "---", that.Config.GetString("id"))
|
fmt.Println(id, "---", that.Config.GetString("id"))
|
||||||
that.Config["id"] = id
|
that.Config["id"] = id
|
||||||
//init文件初始化
|
//init文件初始化
|
||||||
@ -194,9 +301,13 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
|||||||
fmt.Println("有新的代码生成,请重新运行")
|
fmt.Println("有新的代码生成,请重新运行")
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (that *MakeCode) Info(table string) string {
|
func (that *MakeCode) Info(table string) string {
|
||||||
reStr := ""
|
reStr := ""
|
||||||
for _, v := range that.TableColumns[table] {
|
for _, v := range that.TableColumns[table] {
|
||||||
|
if v == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if v.Get("info") == nil || v.GetBool("info") {
|
if v.Get("info") == nil || v.GetBool("info") {
|
||||||
reStr += v.GetString("name") + ","
|
reStr += v.GetString("name") + ","
|
||||||
}
|
}
|
||||||
@ -209,6 +320,10 @@ func (that *MakeCode) Info(table string) string {
|
|||||||
func (that *MakeCode) Add(table string, req *http.Request) Map {
|
func (that *MakeCode) Add(table string, req *http.Request) Map {
|
||||||
data := Map{}
|
data := Map{}
|
||||||
for _, v := range that.TableColumns[table] {
|
for _, v := range that.TableColumns[table] {
|
||||||
|
//不可使用,未在前端展示,但在内存中保持有
|
||||||
|
if v.GetBool("notUse") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if v.Get("add") == nil || v.GetBool("add") {
|
if v.Get("add") == nil || v.GetBool("add") {
|
||||||
reqValue := req.FormValue(v.GetString("name"))
|
reqValue := req.FormValue(v.GetString("name"))
|
||||||
if reqValue == "" {
|
if reqValue == "" {
|
||||||
@ -226,6 +341,10 @@ func (that *MakeCode) Add(table string, req *http.Request) Map {
|
|||||||
func (that *MakeCode) Edit(table string, req *http.Request) Map {
|
func (that *MakeCode) Edit(table string, req *http.Request) Map {
|
||||||
data := Map{}
|
data := Map{}
|
||||||
for _, v := range that.TableColumns[table] {
|
for _, v := range that.TableColumns[table] {
|
||||||
|
//不可使用,未在前端展示,但在内存中保持有
|
||||||
|
if v.GetBool("notUse") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if v.Get("edit") == nil || v.GetBool("edit") {
|
if v.Get("edit") == nil || v.GetBool("edit") {
|
||||||
reqValue := req.FormValue(v.GetString("name"))
|
reqValue := req.FormValue(v.GetString("name"))
|
||||||
if reqValue == "" {
|
if reqValue == "" {
|
||||||
@ -245,6 +364,10 @@ func (that *MakeCode) Edit(table string, req *http.Request) Map {
|
|||||||
func (that *MakeCode) Search(table string, req *http.Request) (string, Map) {
|
func (that *MakeCode) Search(table string, req *http.Request) (string, Map) {
|
||||||
reStr := ""
|
reStr := ""
|
||||||
for _, v := range that.TableColumns[table] {
|
for _, v := range that.TableColumns[table] {
|
||||||
|
//不可使用,未在前端展示,但在内存中保持有
|
||||||
|
if v.GetBool("notUse") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if v.Get("list") == nil || v.GetBool("list") {
|
if v.Get("list") == nil || v.GetBool("list") {
|
||||||
reStr += v.GetString("name") + ","
|
reStr += v.GetString("name") + ","
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,23 @@ import (
|
|||||||
var {{table}}Ctr = Ctr{
|
var {{table}}Ctr = Ctr{
|
||||||
"info": func(that *Context) {
|
"info": func(that *Context) {
|
||||||
re := that.Db.Get(that.RouterString[1], that.MakeCode.Info(that.RouterString[1]), Map{"id": that.RouterString[2]})
|
re := that.Db.Get(that.RouterString[1], that.MakeCode.Info(that.RouterString[1]), Map{"id": that.RouterString[2]})
|
||||||
|
|
||||||
|
if re == nil {
|
||||||
|
that.Display(4, "找不到对应信息")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range re {
|
||||||
|
|
||||||
|
column:=that.MakeCode.TableColumns[that.RouterString[1]][k]
|
||||||
|
if column==nil{
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (column["list"]==nil||column.GetBool("list"))&&column.GetString("link")!=""{
|
||||||
|
re[column.GetString("link")] = that.Db.Get(column.GetString("link"), column.GetString("value"), Map{"id": v})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
that.Display(0, re)
|
that.Display(0, re)
|
||||||
},
|
},
|
||||||
"add": func(that *Context) {
|
"add": func(that *Context) {
|
||||||
@ -70,7 +87,20 @@ var {{table}}Ctr = Ctr{
|
|||||||
columnStr, where := that.MakeCode.Search(that.RouterString[1], that.Req)
|
columnStr, where := that.MakeCode.Search(that.RouterString[1], that.Req)
|
||||||
reData := that.Db.Page(ObjToInt(that.Req.FormValue("page")), ObjToInt(that.Req.FormValue("pageRow"))).
|
reData := that.Db.Page(ObjToInt(that.Req.FormValue("page")), ObjToInt(that.Req.FormValue("pageRow"))).
|
||||||
Select(that.RouterString[1], columnStr, where)
|
Select(that.RouterString[1], columnStr, where)
|
||||||
|
for _, v := range reData {
|
||||||
|
for k, _ := range v {
|
||||||
|
column:=that.MakeCode.TableColumns[that.RouterString[1]][k]
|
||||||
|
if column==nil{
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (column["list"]==nil||column.GetBool("list"))&&column.GetString("link")!=""{
|
||||||
|
v[column.GetString("link")] = that.Db.Get(column.GetString("link"), column.GetString("value"), Map{"id": v.GetCeilInt(k)})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
that.Display(0, reData)
|
that.Display(0, reData)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user