240 lines
6.3 KiB
Go
240 lines
6.3 KiB
Go
package code
|
|
|
|
import (
|
|
. "../common"
|
|
"../db"
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type MakeCode struct {
|
|
TableConfig Map
|
|
TableColumns map[string]map[string]Map
|
|
Config Map
|
|
Error
|
|
}
|
|
|
|
func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
|
if that.TableColumns == nil {
|
|
that.TableColumns = make(map[string]map[string]Map)
|
|
}
|
|
|
|
//加载配置文件
|
|
btes, err := ioutil.ReadFile(path)
|
|
that.Config = DeepCopyMap(Config).(Map)
|
|
if err == nil {
|
|
cmap := Map{}
|
|
//文件是否损坏
|
|
cmap.JsonToMap(string(btes), &that.Error)
|
|
for k, v := range cmap {
|
|
that.Config[k] = v //程序配置
|
|
Config[k] = v //系统配置
|
|
}
|
|
} else {
|
|
that.Error.SetError(errors.New("配置文件不存在,或者配置出错,使用缺省默认配置"))
|
|
}
|
|
tables := that.Config.GetSlice("tables")
|
|
that.TableConfig = Map{}
|
|
for k, _ := range tables {
|
|
tableName := tables.GetMap(k).GetString("table")
|
|
that.TableConfig[tableName] = tables.GetMap(k)
|
|
columns := tables.GetMap(k).GetSlice("columns")
|
|
//初始化
|
|
if that.TableColumns[tableName] == nil {
|
|
that.TableColumns[tableName] = map[string]Map{}
|
|
}
|
|
//注入源数据
|
|
for k, _ := range columns {
|
|
columnsName := columns.GetMap(k).GetString("name")
|
|
that.TableColumns[tableName][columnsName] = columns.GetMap(k)
|
|
}
|
|
|
|
}
|
|
|
|
myInit := strings.Replace(InitTpt, "{{name}}", name, -1)
|
|
ctrList := ""
|
|
if db.Type == "sqlite" {
|
|
tables := db.Select("sqlite_sequence", "name")
|
|
|
|
for _, v := range tables {
|
|
if that.TableConfig.GetMap(v.GetString("name")) == nil {
|
|
that.TableConfig[v.GetString("name")] = Map{
|
|
"label": "备注",
|
|
"table": v.GetString("name"),
|
|
"auth": []string{"add", "delete", "edit", "info"},
|
|
"columns": []Map{},
|
|
"search": []Map{
|
|
//{"type": "tree", "name": "oid", "label": "组织", "table": "organization", "showName": "label", "children": "children"},
|
|
{"type": "text", "name": "keyword", "label": "请输入关键词", "value": nil},
|
|
{"type": "date", "name": "date", "label": "时间段", "value": nil},
|
|
{"type": "select", "name": "state", "label": "状态", "value": nil,
|
|
"option": []Map{
|
|
{"name": "正常", "value": 0},
|
|
{"name": "异常", "value": 1},
|
|
{"name": "全部", "value": nil},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
//初始化
|
|
if that.TableColumns[v.GetString("name")] == nil {
|
|
that.TableColumns[v.GetString("name")] = make(map[string]Map)
|
|
}
|
|
tableInfo := db.Query("pragma table_info([" + v.GetString("name") + "]);")
|
|
for _, info := range tableInfo {
|
|
coloums := that.TableColumns[v.GetString("name")][info.GetString("name")]
|
|
if coloums == nil {
|
|
coloums = Map{
|
|
"name": info.GetString("name"),
|
|
"type": info.GetString("type"),
|
|
"label": "备注",
|
|
//"add": false, "info": false, "edit": false, "list": true,
|
|
"must": false,
|
|
}
|
|
} else {
|
|
//coloums["name"]=info.GetString("name")
|
|
coloums["type"] = info.GetString("type")
|
|
}
|
|
|
|
that.TableColumns[v.GetString("name")][info.GetString("name")] = coloums
|
|
|
|
}
|
|
|
|
//创建模块文件
|
|
//判断文件是否存在
|
|
_, err := os.OpenFile(name+"/"+v.GetString("name"), os.O_RDONLY, os.ModePerm)
|
|
if err != nil { //文件不存在,则根据模板创建
|
|
myCtr := strings.Replace(CtrTpt, "{{name}}", name, -1)
|
|
myCtr = strings.Replace(myCtr, "{{table}}", v.GetString("name"), -1)
|
|
_ = os.MkdirAll(name, os.ModeDir)
|
|
err = ioutil.WriteFile(name+"/"+v.GetString("name")+".go", []byte(myCtr), os.ModeAppend)
|
|
if err != nil {
|
|
that.Error.SetError(err)
|
|
}
|
|
}
|
|
|
|
ctrList = ctrList + `"` + v.GetString("name") + `":` + v.GetString("name") + `Ctr,`
|
|
|
|
}
|
|
|
|
newTables := []Map{}
|
|
for k, _ := range that.TableConfig {
|
|
newTable := that.TableConfig.GetMap(k)
|
|
newTables = append(newTables, newTable)
|
|
columns := []Map{}
|
|
for _, v := range that.TableColumns[newTable.GetString("table")] {
|
|
columns = append(columns, v)
|
|
}
|
|
newTable["columns"] = columns
|
|
}
|
|
that.Config["tables"] = newTables
|
|
|
|
}
|
|
id := Md5(ObjToStr(that.Config["tables"]))
|
|
if id == that.Config.GetString("id") {
|
|
return
|
|
}
|
|
that.Config["id"] = id
|
|
//init文件初始化
|
|
myInit = strings.Replace(myInit, "{{id}}", id, -1)
|
|
myInit = strings.Replace(myInit, "{{tablesCtr}}", ctrList, -1)
|
|
_ = os.MkdirAll(name, os.ModeDir)
|
|
err = ioutil.WriteFile(name+"/init.go", []byte(myInit), os.ModeAppend)
|
|
if err != nil {
|
|
that.Error.SetError(err)
|
|
}
|
|
//写入配置文件
|
|
var configByte bytes.Buffer
|
|
|
|
err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t")
|
|
_ = os.MkdirAll(filepath.Dir(path), os.ModeDir)
|
|
err = ioutil.WriteFile(path, configByte.Bytes(), os.ModeAppend)
|
|
if err != nil {
|
|
that.Error.SetError(err)
|
|
}
|
|
fmt.Println("有新的代码生成,请重新运行")
|
|
os.Exit(-1)
|
|
}
|
|
func (that *MakeCode) Info(table string) string {
|
|
reStr := ""
|
|
for _, v := range that.TableColumns[table] {
|
|
if v.Get("info") == nil || v.GetBool("info") {
|
|
reStr += v.GetString("name") + ","
|
|
}
|
|
}
|
|
if len(reStr) != 0 {
|
|
reStr = reStr[:len(reStr)-1]
|
|
}
|
|
return reStr
|
|
}
|
|
func (that *MakeCode) Add(table string, req *http.Request) Map {
|
|
data := Map{}
|
|
for _, v := range that.TableColumns[table] {
|
|
if v.Get("add") == nil || v.GetBool("add") {
|
|
reqValue := req.FormValue(v.GetString("name"))
|
|
if reqValue == "" {
|
|
return nil
|
|
}
|
|
data[v.GetString("name")] = reqValue
|
|
}
|
|
}
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
|
|
return data
|
|
}
|
|
func (that *MakeCode) Edit(table string, req *http.Request) Map {
|
|
data := Map{}
|
|
for _, v := range that.TableColumns[table] {
|
|
if v.Get("edit") == nil || v.GetBool("edit") {
|
|
reqValue := req.FormValue(v.GetString("name"))
|
|
if reqValue == "" {
|
|
continue
|
|
}
|
|
data[v.GetString("name")] = reqValue
|
|
}
|
|
}
|
|
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
func (that *MakeCode) Search(table string, req *http.Request) (string, Map) {
|
|
reStr := ""
|
|
for _, v := range that.TableColumns[table] {
|
|
if v.Get("list") == nil || v.GetBool("list") {
|
|
reStr += v.GetString("name") + ","
|
|
}
|
|
}
|
|
if len(reStr) != 0 {
|
|
reStr = reStr[:len(reStr)-1]
|
|
}
|
|
|
|
data := Map{}
|
|
search := that.TableConfig.GetSlice("search")
|
|
for k, _ := range search {
|
|
|
|
reqValue := req.FormValue(search.GetMap(k).GetString("name"))
|
|
if reqValue == "" {
|
|
continue
|
|
}
|
|
|
|
data[search.GetMap(k).GetString("name")] = reqValue
|
|
|
|
}
|
|
|
|
return reStr, data
|
|
}
|