|
|
package ProtoController
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
"dsAutoCode/Dao/CommonDao"
|
|
|
"dsAutoCode/Dao/ProtoDao"
|
|
|
"dsAutoCode/Model"
|
|
|
"dsAutoCode/Utils/CommonUtil"
|
|
|
"dsAutoCode/Utils/ConfigUtil"
|
|
|
"dsAutoCode/Utils/DbUtil"
|
|
|
"dsAutoCode/Utils/FileUtil"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"html/template"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
/**
|
|
|
功能:为rpc层创建目录及文件
|
|
|
作者:黄海
|
|
|
时间:2020-04-23
|
|
|
*/
|
|
|
func CreateRpcFiles(c *gin.Context) {
|
|
|
var tableName string
|
|
|
interfaceId := c.Query("interfaceId")
|
|
|
type Result struct {
|
|
|
Num int
|
|
|
Item string
|
|
|
Success bool
|
|
|
Message string
|
|
|
}
|
|
|
var ListResult []Result
|
|
|
//操作类型
|
|
|
tableName = ProtoDao.GetTrueTableNameFromInterfaceId(interfaceId)
|
|
|
tableName = strings.Replace(tableName, ".pb.go", "", -1)
|
|
|
//还原真实映射表名
|
|
|
realTableName := CommonUtil.ConvertTrueTableName(tableName)
|
|
|
//主键名称
|
|
|
pk, dataType := CommonDao.GetTablePk(realTableName)
|
|
|
pkView := CommonUtil.GetSnakeCaseStr(pk)
|
|
|
//实体赋值
|
|
|
var modelFill = GetAllFieldsByTableName(realTableName, []string{"last_updated_time"})
|
|
|
|
|
|
//1、Service目录是否存在?
|
|
|
workingPath := CommonUtil.GetCurrentParentPath() + "\\dsBaseRpc\\RpcService\\#tableName#\\#tableName#Service"
|
|
|
workingPath = strings.Replace(workingPath, "#tableName#", tableName, -1)
|
|
|
exists, _ := CommonUtil.PathExists(workingPath)
|
|
|
if !exists {
|
|
|
os.MkdirAll(workingPath, os.ModePerm)
|
|
|
//创建Service文件
|
|
|
fi := workingPath + "\\" + tableName + "Service.go"
|
|
|
|
|
|
//调用模板,
|
|
|
filesName := CommonUtil.GetCurrentDirectory()+"/Controller/ProtoController/Template/RpcServiceFramework.template"
|
|
|
t, err := template.ParseFiles(filesName)
|
|
|
if err != nil {
|
|
|
log.Fatalln("parse file err:", err)
|
|
|
return
|
|
|
}
|
|
|
buf := new(bytes.Buffer)
|
|
|
var s1 string
|
|
|
//是否需要进行整数转换
|
|
|
if strings.Index(dataType, "int") >= 0 {
|
|
|
s1 = "{CommonUtil.ConvertInt32ToString(in." + pkView + ")}"
|
|
|
} else {
|
|
|
s1 = "{in."+pkView+"}"
|
|
|
}
|
|
|
var p = map[string]interface{}{
|
|
|
"tableName": tableName,
|
|
|
"realTableName": realTableName,
|
|
|
"pkView": pkView,
|
|
|
"modelFill": modelFill,
|
|
|
"s1": s1,
|
|
|
}
|
|
|
if err := t.Execute(buf, p); err != nil {
|
|
|
log.Fatal("There was an error:", err.Error())
|
|
|
}
|
|
|
var content = buf.String()
|
|
|
content = CommonUtil.DeleteBlankString(content)
|
|
|
//写入文件
|
|
|
FileUtil.WriteFileContent(fi, content)
|
|
|
//格式化文件
|
|
|
CommonUtil.Exec("go", "fmt", fi)
|
|
|
|
|
|
var r Result
|
|
|
r.Item = "创建RpcService目录"
|
|
|
r.Message = "创建成功!"
|
|
|
r.Num = 1
|
|
|
r.Success = true
|
|
|
ListResult = append(ListResult, r)
|
|
|
} else {
|
|
|
var r Result
|
|
|
r.Item = "创建RpcService目录"
|
|
|
r.Message = "已存在!"
|
|
|
r.Num = 1
|
|
|
r.Success = false
|
|
|
ListResult = append(ListResult, r)
|
|
|
}
|
|
|
//2、Dao目录是不是存在
|
|
|
workingPath = CommonUtil.GetParentPath(CommonUtil.GetCurrentDirectory()) + "\\dsBaseRpc\\RpcService\\#tableName#\\#tableName#Dao"
|
|
|
workingPath = strings.Replace(workingPath, "#tableName#", tableName, -1)
|
|
|
exists, _ = CommonUtil.PathExists(workingPath)
|
|
|
if !exists {
|
|
|
os.MkdirAll(workingPath, os.ModePerm)
|
|
|
//创建空的Dao文件,注意包名称
|
|
|
fi := workingPath + "\\" + tableName + "Dao.go"
|
|
|
//调用模板,
|
|
|
filesName := CommonUtil.GetCurrentPath()+"/Controller/ProtoController/Template/RpcDaoFramework.template"
|
|
|
t, err := template.ParseFiles(filesName)
|
|
|
if err != nil {
|
|
|
log.Fatalln("parse file err:", err)
|
|
|
return
|
|
|
}
|
|
|
buf := new(bytes.Buffer)
|
|
|
var p = map[string]interface{}{
|
|
|
"tableName": tableName,
|
|
|
"realTableName": realTableName,
|
|
|
"pk": pk,
|
|
|
"pkView": pkView,
|
|
|
"modelFill": modelFill,
|
|
|
}
|
|
|
if err := t.Execute(buf, p); err != nil {
|
|
|
log.Fatal("There was an error:", err.Error())
|
|
|
}
|
|
|
var content = buf.String()
|
|
|
content = CommonUtil.DeleteBlankString(content)
|
|
|
//写入文件
|
|
|
FileUtil.WriteFileContent(fi, content)
|
|
|
//格式化文件
|
|
|
CommonUtil.Exec("go", "fmt", fi)
|
|
|
|
|
|
var r Result
|
|
|
r.Item = "创建Dao目录"
|
|
|
r.Message = "创建成功!"
|
|
|
r.Num = 2
|
|
|
r.Success = true
|
|
|
ListResult = append(ListResult, r)
|
|
|
} else {
|
|
|
var r Result
|
|
|
r.Item = "创建Dao目录"
|
|
|
r.Message = "已存在!"
|
|
|
r.Num = 2
|
|
|
r.Success = false
|
|
|
ListResult = append(ListResult, r)
|
|
|
}
|
|
|
|
|
|
c.JSON(http.StatusOK, Model.Res{
|
|
|
Data: ListResult,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:获取指定表的所有字段
|
|
|
作者:黄海
|
|
|
时间:2020-05-30
|
|
|
*/
|
|
|
func GetAllFieldsByTableName(tableName string, exclude []string) string {
|
|
|
sql := "select column_name,column_comment,data_type,column_key from information_schema.columns where table_name = '" + tableName + "' and table_schema = '" + ConfigUtil.WorkingDataBase + "'"
|
|
|
var db = DbUtil.Engine
|
|
|
list, _ := db.SQL(sql).Query().List()
|
|
|
var str string
|
|
|
for i := range list {
|
|
|
columnKey := list[i]["column_key"].(string)
|
|
|
columnName := list[i]["column_name"].(string)
|
|
|
columnComment := list[i]["column_comment"].(string)
|
|
|
data_type := list[i]["data_type"].(string)
|
|
|
//手工排除一些字段
|
|
|
exclude = append(exclude, "IdInt")
|
|
|
exclude = append(exclude, "LastUpdatedTime")
|
|
|
//排除一些字段
|
|
|
if CommonUtil.IsContainString(exclude, CommonUtil.GetSnakeCaseStr(columnName)) {
|
|
|
continue
|
|
|
}
|
|
|
str += "//" + columnComment + "\n"
|
|
|
//主键
|
|
|
if columnKey == "PRI" {
|
|
|
snakeColumnName := CommonUtil.GetSnakeCaseStr(columnName)
|
|
|
str += "model." + snakeColumnName + "=" + "CommonUtil.GetUUID()\n"
|
|
|
} else {
|
|
|
snakeColumnName := CommonUtil.GetSnakeCaseStr(columnName)
|
|
|
if data_type == "timestamp" || data_type == "date" {
|
|
|
str += "model." + snakeColumnName + "=DateUtil.ConvertDateTime(" + "in." + snakeColumnName + ")\n"
|
|
|
} else {
|
|
|
str += "model." + snakeColumnName + "=" + "in." + snakeColumnName + "\n"
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return str
|
|
|
}
|