优化代码
This commit is contained in:
+135
-12
@@ -29,6 +29,7 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
||||
|
||||
//加载配置文件
|
||||
btes, err := ioutil.ReadFile(path)
|
||||
Config["name"] = name
|
||||
that.Config = DeepCopyMap(Config).(Map)
|
||||
if err == nil {
|
||||
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 {
|
||||
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")]
|
||||
|
||||
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{
|
||||
"name": info.GetString("name"),
|
||||
"type": info.GetString("type"),
|
||||
"label": info.GetString("label"),
|
||||
//"add": false, "info": false, "edit": false, "list": true,
|
||||
"must": false,
|
||||
//"must": false,
|
||||
}
|
||||
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")
|
||||
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
|
||||
}
|
||||
}
|
||||
coloum["type"] = info.GetString("type")
|
||||
if !coloum.GetBool("notUse") {
|
||||
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")
|
||||
//}
|
||||
|
||||
//暂时不关闭参数,保证表数据完全读取到
|
||||
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
|
||||
|
||||
//生成id,判断数据库是否有改变,以保证数据库和配置文件匹配唯一
|
||||
id := Md5(ObjToStr(idSlice))
|
||||
|
||||
if id == that.Config.GetString("id") {
|
||||
@@ -172,6 +220,65 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
||||
}
|
||||
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"))
|
||||
that.Config["id"] = id
|
||||
//init文件初始化
|
||||
@@ -194,9 +301,13 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
||||
fmt.Println("有新的代码生成,请重新运行")
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
func (that *MakeCode) Info(table string) string {
|
||||
reStr := ""
|
||||
for _, v := range that.TableColumns[table] {
|
||||
if v == nil {
|
||||
continue
|
||||
}
|
||||
if v.Get("info") == nil || v.GetBool("info") {
|
||||
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 {
|
||||
data := Map{}
|
||||
for _, v := range that.TableColumns[table] {
|
||||
//不可使用,未在前端展示,但在内存中保持有
|
||||
if v.GetBool("notUse") {
|
||||
continue
|
||||
}
|
||||
if v.Get("add") == nil || v.GetBool("add") {
|
||||
reqValue := req.FormValue(v.GetString("name"))
|
||||
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 {
|
||||
data := Map{}
|
||||
for _, v := range that.TableColumns[table] {
|
||||
//不可使用,未在前端展示,但在内存中保持有
|
||||
if v.GetBool("notUse") {
|
||||
continue
|
||||
}
|
||||
if v.Get("edit") == nil || v.GetBool("edit") {
|
||||
reqValue := req.FormValue(v.GetString("name"))
|
||||
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) {
|
||||
reStr := ""
|
||||
for _, v := range that.TableColumns[table] {
|
||||
//不可使用,未在前端展示,但在内存中保持有
|
||||
if v.GetBool("notUse") {
|
||||
continue
|
||||
}
|
||||
if v.Get("list") == nil || v.GetBool("list") {
|
||||
reStr += v.GetString("name") + ","
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user