生成代码,逐步进行优化
This commit is contained in:
parent
6aa2b74840
commit
79453d4d2a
@ -6,9 +6,7 @@ import (
|
||||
. "./common"
|
||||
. "./db"
|
||||
. "./log"
|
||||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
@ -226,24 +224,25 @@ func (that *Application) SetConfig(configPath ...string) {
|
||||
|
||||
//文件如果损坏则不写入配置防止配置文件数据丢失
|
||||
if that.Error.GetError() == nil {
|
||||
var configByte bytes.Buffer
|
||||
//var configByte bytes.Buffer
|
||||
|
||||
err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t")
|
||||
//判断配置文件是否序列有变化,有则修改配置,无则不变
|
||||
//fmt.Println(len(btes))
|
||||
if len(btes) != 0 && configByte.String() == string(btes) {
|
||||
configStr := that.Config.ToJsonString()
|
||||
if len(btes) != 0 && configStr == string(btes) {
|
||||
return
|
||||
}
|
||||
//写入配置说明
|
||||
var configNoteByte bytes.Buffer
|
||||
_ = json.Indent(&configNoteByte, []byte(ConfigNote.ToJsonString()), "", "\t")
|
||||
//var configNoteByte bytes.Buffer
|
||||
configNoteStr := ConfigNote.ToJsonString()
|
||||
//_ = json.Indent(&configNoteByte, []byte(ConfigNote.ToJsonString()), "", "\t")
|
||||
|
||||
_ = os.MkdirAll(filepath.Dir(that.configPath), os.ModeDir)
|
||||
err = ioutil.WriteFile(that.configPath, configByte.Bytes(), os.ModeAppend)
|
||||
err = ioutil.WriteFile(that.configPath, []byte(configStr), os.ModePerm)
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
}
|
||||
_ = ioutil.WriteFile(filepath.Dir(that.configPath)+"/configNote.json", configNoteByte.Bytes(), os.ModeAppend)
|
||||
_ = ioutil.WriteFile(filepath.Dir(that.configPath)+"/configNote.json", []byte(configNoteStr), os.ModePerm)
|
||||
|
||||
}
|
||||
|
||||
@ -451,7 +450,7 @@ func Init(config string) Application {
|
||||
appIns.SetCache()
|
||||
appIns.MakeCode = &code.MakeCode{}
|
||||
codeConfig := appIns.Config.GetMap("codeConfig")
|
||||
if codeConfig != nil {
|
||||
if codeConfig != nil && appIns.Config.GetInt("mode") == 2 {
|
||||
for k, _ := range codeConfig {
|
||||
appIns.MakeCode.Db2JSON(k, codeConfig.GetString(k), appIns.Db)
|
||||
}
|
||||
@ -476,6 +475,7 @@ func SetDB(appIns *Application) {
|
||||
func SetMysqlDB(appIns *Application, config Map) {
|
||||
|
||||
appIns.Db.Type = "mysql"
|
||||
appIns.Db.DBName = config.GetString("name")
|
||||
appIns.Db.Prefix = config.GetString("prefix")
|
||||
appIns.SetConnectDB(func(err ...*Error) (master, slave *sql.DB) {
|
||||
//master数据库配置
|
||||
|
@ -3,8 +3,6 @@ package code
|
||||
import (
|
||||
. "../common"
|
||||
"../db"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
@ -22,6 +20,9 @@ type MakeCode struct {
|
||||
}
|
||||
|
||||
func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
||||
isMake := false
|
||||
idSlice := Slice{}
|
||||
|
||||
if that.TableColumns == nil {
|
||||
that.TableColumns = make(map[string]map[string]Map)
|
||||
}
|
||||
@ -60,13 +61,22 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
||||
|
||||
myInit := strings.Replace(InitTpt, "{{name}}", name, -1)
|
||||
ctrList := ""
|
||||
if db.Type == "sqlite" {
|
||||
tables := db.Select("sqlite_sequence", "name")
|
||||
|
||||
for _, v := range tables {
|
||||
nowTables := make([]Map, 0)
|
||||
if db.Type == "mysql" {
|
||||
nowTables = db.Select("INFORMATION_SCHEMA.TABLES", "TABLE_NAME as name,TABLE_COMMENT as label", Map{"TABLE_SCHEMA": db.DBName})
|
||||
}
|
||||
if db.Type == "sqlite" {
|
||||
nowTables = db.Select("sqlite_sequence", "name")
|
||||
}
|
||||
//idSlice=append(idSlice,nowTables)
|
||||
for _, v := range nowTables {
|
||||
if that.TableConfig.GetMap(v.GetString("name")) == nil {
|
||||
if v.GetString("label") == "" {
|
||||
v["label"] = "备注"
|
||||
}
|
||||
that.TableConfig[v.GetString("name")] = Map{
|
||||
"label": "备注",
|
||||
"label": v.GetString("label"),
|
||||
"table": v.GetString("name"),
|
||||
"auth": []string{"add", "delete", "edit", "info"},
|
||||
"columns": []Map{},
|
||||
@ -83,24 +93,45 @@ 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")
|
||||
}
|
||||
|
||||
}
|
||||
//初始化
|
||||
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") + "]);")
|
||||
tableInfo := make([]Map, 0)
|
||||
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")}})
|
||||
}
|
||||
if db.Type == "sqlite" {
|
||||
tableInfo = db.Query("pragma table_info([" + v.GetString("name") + "]);")
|
||||
}
|
||||
|
||||
idSlice = append(idSlice, tableInfo)
|
||||
for _, info := range tableInfo {
|
||||
if info.GetString("label") == "" {
|
||||
info["label"] = "备注"
|
||||
}
|
||||
coloums := that.TableColumns[v.GetString("name")][info.GetString("name")]
|
||||
|
||||
if coloums == nil {
|
||||
coloums = Map{
|
||||
"name": info.GetString("name"),
|
||||
"type": info.GetString("type"),
|
||||
"label": "备注",
|
||||
"label": info.GetString("label"),
|
||||
//"add": false, "info": false, "edit": false, "list": true,
|
||||
"must": false,
|
||||
}
|
||||
} else {
|
||||
//coloums["name"]=info.GetString("name")
|
||||
|
||||
if !(coloums.GetString("label") != "备注" && info.GetString("label") == "备注") {
|
||||
coloums["label"] = info.GetString("label")
|
||||
}
|
||||
coloums["type"] = info.GetString("type")
|
||||
}
|
||||
|
||||
@ -110,18 +141,20 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
||||
|
||||
//创建模块文件
|
||||
//判断文件是否存在
|
||||
_, err := os.OpenFile(name+"/"+v.GetString("name"), os.O_RDONLY, os.ModePerm)
|
||||
//_, err := os.OpenFile(name+"/"+v.GetString("name"), os.O_RDONLY, os.ModePerm)
|
||||
_, err := os.Stat(name + "/" + v.GetString("name") + ".go")
|
||||
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)
|
||||
err = ioutil.WriteFile(name+"/"+v.GetString("name")+".go", []byte(myCtr), os.ModePerm)
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
}
|
||||
isMake = true
|
||||
}
|
||||
|
||||
ctrList = ctrList + `"` + v.GetString("name") + `":` + v.GetString("name") + `Ctr,`
|
||||
ctrList = ctrList + `"` + v.GetString("name") + `":` + v.GetString("name") + "Ctr,\r\n "
|
||||
|
||||
}
|
||||
|
||||
@ -137,26 +170,33 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
|
||||
}
|
||||
that.Config["tables"] = newTables
|
||||
|
||||
}
|
||||
id := Md5(ObjToStr(that.Config["tables"]))
|
||||
//}
|
||||
//fmt.Println(ObjToStr(idSlice))
|
||||
id := Md5(ObjToStr(idSlice))
|
||||
|
||||
if id == that.Config.GetString("id") {
|
||||
if isMake { //有生成包文件
|
||||
fmt.Println("有新的业务代码生成,请重新运行")
|
||||
os.Exit(-1)
|
||||
}
|
||||
return
|
||||
}
|
||||
fmt.Println(id, "---", that.Config.GetString("id"))
|
||||
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)
|
||||
err = ioutil.WriteFile(name+"/init.go", []byte(myInit), os.ModePerm)
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
}
|
||||
//写入配置文件
|
||||
var configByte bytes.Buffer
|
||||
//var configByte bytes.Buffer
|
||||
|
||||
err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t")
|
||||
//err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t")
|
||||
_ = os.MkdirAll(filepath.Dir(path), os.ModeDir)
|
||||
err = ioutil.WriteFile(path, configByte.Bytes(), os.ModeAppend)
|
||||
err = ioutil.WriteFile(path, []byte(that.Config.ToJsonString()), os.ModePerm)
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ import (
|
||||
// return res
|
||||
//}
|
||||
|
||||
//字符串首字符大写
|
||||
// StrFirstToUpper 字符串首字符大写
|
||||
func StrFirstToUpper(str string) string {
|
||||
if len(str) == 0 {
|
||||
return str
|
||||
@ -36,7 +36,7 @@ func StrFirstToUpper(str string) string {
|
||||
return strings.ToUpper(first) + other
|
||||
}
|
||||
|
||||
//字符串截取
|
||||
// Substr 字符串截取
|
||||
func Substr(str string, start int, length int) string {
|
||||
rs := []rune(str)
|
||||
rl := len(rs)
|
||||
@ -67,7 +67,7 @@ func Substr(str string, start int, length int) string {
|
||||
return string(rs[start:end])
|
||||
}
|
||||
|
||||
//获取最后出现字符串的下标
|
||||
// IndexLastStr 获取最后出现字符串的下标
|
||||
//return 找不到返回 -1
|
||||
func IndexLastStr(str, sep string) int {
|
||||
sepSlice := []rune(sep)
|
||||
@ -98,7 +98,7 @@ func IndexLastStr(str, sep string) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
//md5
|
||||
// Md5 md5
|
||||
func Md5(req string) string {
|
||||
md5Ctx := md5.New()
|
||||
md5Ctx.Write([]byte(req))
|
||||
|
@ -1,6 +1,7 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
@ -258,8 +259,10 @@ func ObjToStr(obj interface{}) string {
|
||||
str = strconv.FormatFloat(obj.(float64), 'f', -1, 64)
|
||||
default:
|
||||
strbte, err := json.Marshal(obj)
|
||||
var configByte bytes.Buffer
|
||||
err = json.Indent(&configByte, strbte, "", "\t")
|
||||
if err == nil {
|
||||
str = string(strbte)
|
||||
str = configByte.String()
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import (
|
||||
type HoTimeDB struct {
|
||||
*sql.DB
|
||||
ContextBase
|
||||
DBName string
|
||||
*cache.HoTimeCache
|
||||
Type string
|
||||
Prefix string
|
||||
|
@ -1,13 +0,0 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
. "../../../hotime"
|
||||
)
|
||||
|
||||
var ID = "bc662f6bf19a1f3f269045167a2f273d"
|
||||
|
||||
// Project 管理端项目
|
||||
var Project = Proj{
|
||||
//"user": UserCtr,
|
||||
"user": userCtr,
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
. "../../../hotime"
|
||||
. "../../../hotime/common"
|
||||
)
|
||||
|
||||
var userCtr = Ctr{
|
||||
"info": func(that *Context) {
|
||||
re := that.Db.Get(that.RouterString[1], that.MakeCode.Info(that.RouterString[1]), Map{"id": that.RouterString[2]})
|
||||
that.Display(0, re)
|
||||
},
|
||||
"add": func(that *Context) {
|
||||
inData := that.MakeCode.Add(that.RouterString[1], that.Req)
|
||||
if inData == nil {
|
||||
that.Display(3, "请求参数不足")
|
||||
return
|
||||
}
|
||||
re := that.Db.Insert(that.RouterString[1], inData)
|
||||
|
||||
if re == 0 {
|
||||
that.Display(4, "无法插入对应数据")
|
||||
return
|
||||
}
|
||||
|
||||
that.Display(0, re)
|
||||
},
|
||||
"update": func(that *Context) {
|
||||
inData := that.MakeCode.Edit(that.RouterString[1], that.Req)
|
||||
if inData == nil {
|
||||
that.Display(3, "没有找到要更新的数据")
|
||||
return
|
||||
}
|
||||
re := that.Db.Update(that.RouterString[1], inData, Map{"id": that.RouterString[2]})
|
||||
|
||||
if re == 0 {
|
||||
that.Display(4, "更新数据失败")
|
||||
return
|
||||
}
|
||||
|
||||
that.Display(0, re)
|
||||
},
|
||||
"remove": func(that *Context) {
|
||||
re := that.Db.Delete(that.RouterString[1], Map{"id": that.RouterString[2]})
|
||||
|
||||
if re == 0 {
|
||||
that.Display(4, "删除数据失败")
|
||||
return
|
||||
}
|
||||
that.Display(0, re)
|
||||
},
|
||||
"search": func(that *Context) {
|
||||
|
||||
columnStr, where := that.MakeCode.Search(that.RouterString[1], that.Req)
|
||||
reData := that.Db.Page(ObjToInt(that.Req.FormValue("page")), ObjToInt(that.Req.FormValue("pageRow"))).
|
||||
Select(that.RouterString[1], columnStr, where)
|
||||
|
||||
that.Display(0, reData)
|
||||
},
|
||||
}
|
@ -1,107 +0,0 @@
|
||||
{
|
||||
"id": "bc662f6bf19a1f3f269045167a2f273d",
|
||||
"label": "HoTime管理平台",
|
||||
"menu": [
|
||||
{
|
||||
"icon": "el-icon-s-home",
|
||||
"label": "平台首页",
|
||||
"name": "HelloWorld"
|
||||
},
|
||||
{
|
||||
"icon": "el-icon-suitcase",
|
||||
"label": "测试表格",
|
||||
"table": "table"
|
||||
},
|
||||
{
|
||||
"icon": "el-icon-setting",
|
||||
"label": "系统管理",
|
||||
"menu": [
|
||||
{
|
||||
"label": "用户管理",
|
||||
"table": "user"
|
||||
},
|
||||
{
|
||||
"label": "组织管理",
|
||||
"table": "organization"
|
||||
},
|
||||
{
|
||||
"label": "角色管理",
|
||||
"table": "role"
|
||||
},
|
||||
{
|
||||
"default": "edit",
|
||||
"label": "系统设置",
|
||||
"table": "system"
|
||||
}
|
||||
],
|
||||
"name": "setting"
|
||||
}
|
||||
],
|
||||
"name": "HoTimeDashBoard",
|
||||
"tables": [
|
||||
{
|
||||
"auth": [
|
||||
"add",
|
||||
"delete",
|
||||
"edit",
|
||||
"info"
|
||||
],
|
||||
"columns": [
|
||||
{
|
||||
"label": "备注",
|
||||
"must": false,
|
||||
"name": "id",
|
||||
"type": "integer"
|
||||
},
|
||||
{
|
||||
"label": "备注",
|
||||
"must": false,
|
||||
"name": "name",
|
||||
"type": "text"
|
||||
},
|
||||
{
|
||||
"label": "备注",
|
||||
"must": false,
|
||||
"name": "age",
|
||||
"type": "int"
|
||||
}
|
||||
],
|
||||
"label": "备注",
|
||||
"search": [
|
||||
{
|
||||
"label": "请输入关键词",
|
||||
"name": "keyword",
|
||||
"type": "text",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"label": "时间段",
|
||||
"name": "date",
|
||||
"type": "date",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"label": "状态",
|
||||
"name": "state",
|
||||
"option": [
|
||||
{
|
||||
"name": "正常",
|
||||
"value": 0
|
||||
},
|
||||
{
|
||||
"name": "异常",
|
||||
"value": 1
|
||||
},
|
||||
{
|
||||
"name": "全部",
|
||||
"value": null
|
||||
}
|
||||
],
|
||||
"type": "select",
|
||||
"value": null
|
||||
}
|
||||
],
|
||||
"table": "user"
|
||||
}
|
||||
]
|
||||
}
|
@ -10,8 +10,13 @@
|
||||
"admin": "config/app.json"
|
||||
},
|
||||
"db": {
|
||||
"sqlite": {
|
||||
"path": "config/data.db"
|
||||
"mysql": {
|
||||
"host": "cloud.hoteas.com",
|
||||
"name": "ljf",
|
||||
"password": "root",
|
||||
"port": "3306",
|
||||
"prefix": "",
|
||||
"user": "root"
|
||||
}
|
||||
},
|
||||
"defFile": [
|
||||
|
@ -2,7 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"../../hotime"
|
||||
"./admin"
|
||||
//"./admin"
|
||||
"errors"
|
||||
"fmt"
|
||||
"golang.org/x/net/websocket"
|
||||
@ -58,7 +58,7 @@ func main() {
|
||||
//fmt.Println(common.ObjToStr(makeCode.Db2JSON("admin","test",appIns.Db)))
|
||||
|
||||
appIns.Run(hotime.Router{
|
||||
"admin": admin.Project,
|
||||
//"admin": admin.Project,
|
||||
"app": hotime.Proj{
|
||||
"index": hotime.Ctr{
|
||||
"test": func(this *hotime.Context) {
|
||||
|
Loading…
Reference in New Issue
Block a user