You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.9 KiB

package main
import (
"bufio"
"dsSso/Utils/CommonUtil"
"dsSso/Utils/ConfigUtil"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"
)
func main() {
// go get github.com/go-xorm/cmd/xorm
//执行一次
var para = ConfigUtil.MysqlUser + ":" + ConfigUtil.MysqlPwd + "@(" + ConfigUtil.MysqlIp + ":" + ConfigUtil.MysqlPort + ")/" + ConfigUtil.MysqlDataBase + "?charset=utf8"
CommonUtil.Exec("xorm", "reverse", "mysql", para, "Templates")
//替换生成
path := "./models"
fs, _ := ioutil.ReadDir(path)
for _, file := range fs {
if !file.IsDir() {
var writeString = ""
//按行读取
f, _ := os.Open(path + "/" + file.Name())
defer f.Close()
buff := bufio.NewReader(f)
//是不是发现了时间类型
var foundTime = false
for {
line, err := buff.ReadString('\n')
if err != nil || io.EOF == err {
break
}
//修复int和in32的兼容问题
line = strings.Replace(line, " int ", " int32 ", -1)
//如果包含LastUpdatedTime
if strings.Index(line, "LastUpdatedTime") >= 0 {
continue
}
//如果包含id_int
if strings.Index(line, "IdInt") >= 0 {
continue
}
//去掉
if strings.Index(line, "time.Time") >= 0 {
foundTime = true
}
writeString += line
}
//保存文件
if !foundTime {
replaceContent := `import (
"time"
)`
writeString = strings.Replace(writeString, replaceContent, "", -1)
}
var d1 = []byte(writeString)
modelFile := path + "/" + file.Name()
ioutil.WriteFile(modelFile, d1, 0666) //写入文件(字节数组)
//格式化文件
cmd := exec.Command("gofmt", "-l", "-w", modelFile)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
err := cmd.Run()
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(CommonUtil.GetCurrentTime(), "\t", "成功完成"+file.Name())
}
}
fmt.Println("恭喜,全部实体表成功完成!")
}