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.

145 lines
4.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package ProtoController
import (
"dsAutoCode/Model"
"dsAutoCode/Service/ProtoService"
"dsAutoCode/Utils/CommonUtil"
"dsAutoCode/Utils/FileUtil"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"os"
"strings"
)
/**
功能为web层创建目录及文件
作者:黄海
时间2020-04-23
*/
func CreateWebFiles(c *gin.Context) {
var tableName string
var operateType string
interfaceName := c.Query("interfaceName")
type Result struct {
Num int
Item string
Success bool
Message string
}
var ListResult []Result
//操作类型
operateType = ProtoService.GetOperateType(interfaceName)
tableName = strings.Replace(interfaceName, operateType, "", -1)
//1、Controller目录是否存在
workingPath := CommonUtil.GetParentPath(CommonUtil.GetCurrentDirectory()) + "\\dsBaseWeb\\Business\\#tableName#\\#tableName#Controller"
workingPath = strings.Replace(workingPath, "#tableName#", tableName, -1)
exists, _ := CommonUtil.PathExists(workingPath)
if !exists {
os.MkdirAll(workingPath, os.ModePerm)
//创建Controller文件
head:=
`package #tableName#Controller
import (
"dsBaseWeb/Business/#tableName#/#tableName#Service"
"dsBaseWeb/Model"
"dsBaseWeb/Utils/CommonUtil"
"github.com/gin-gonic/gin"
"net/http"
)
`
tableName, content := getFrameworkContent(interfaceName)
fi:=workingPath+"\\"+tableName+"Controller.go"
head = strings.Replace(head, "#tableName#", tableName, -1)
//写入文件
FileUtil.WriteFileContent(fi,head+content)
var r Result
r.Item = "创建Controller目录"
r.Message = "创建成功!"
r.Num = 1
r.Success = true
ListResult = append(ListResult, r)
} else {
var r Result
r.Item = "创建Controller目录"
r.Message = "已存在!"
r.Num = 1
r.Success = false
ListResult = append(ListResult, r)
}
//2、Service目录是不是存在
workingPath = CommonUtil.GetParentPath(CommonUtil.GetCurrentDirectory()) + "\\dsBaseWeb\\Business\\#tableName#\\#tableName#Service"
workingPath = strings.Replace(workingPath, "#tableName#", tableName, -1)
exists, _ = CommonUtil.PathExists(workingPath)
if !exists {
os.MkdirAll(workingPath, os.ModePerm)
//创建空的Service文件,注意包名称
//GovAreaService.go
fi:=workingPath+"\\"+tableName+"ProtoService.go"
content:=
`package #tableName#Service
import (
"context"
"dsBaseWeb/Business/#tableName#/#tableName#Proto"
"dsBaseWeb/Utils/GRpcUtil"
"dsBaseWeb/Utils/LogUtil"
"time"
)
`
content = strings.Replace(content, "#tableName#", tableName, -1)
//写入文件
FileUtil.WriteFileContent(fi,content)
var r Result
r.Item = "创建Service目录"
r.Message = "创建成功!"
r.Num = 2
r.Success = true
ListResult = append(ListResult, r)
} else {
var r Result
r.Item = "创建Service目录"
r.Message = "已存在!"
r.Num = 2
r.Success = false
ListResult = append(ListResult, r)
}
//3、Proto目录是不是存在
sourcePath := CommonUtil.GetParentPath(CommonUtil.GetCurrentDirectory()) + "\\dsBaseRpc\\RpcService\\#tableName#\\#tableName#Proto"
sourcePath = strings.Replace(sourcePath, "#tableName#", tableName, -1)
workingPath = CommonUtil.GetParentPath(CommonUtil.GetCurrentDirectory()) + "\\dsBaseWeb\\Business\\#tableName#\\#tableName#Proto"
workingPath = strings.Replace(workingPath, "#tableName#", tableName, -1)
exists, _ = CommonUtil.PathExists(workingPath)
if !exists {
os.MkdirAll(workingPath, os.ModePerm)
var r Result
r.Item = "创建Proto目录及拷贝pb.go文件"
r.Message = "创建成功!"
r.Num = 2
r.Success = true
ListResult = append(ListResult, r)
//如果不存在则拷贝rpc过来
fileInfoList, _ := ioutil.ReadDir(sourcePath)
for i := range fileInfoList {
s := sourcePath + "\\" + fileInfoList[i].Name()
t := workingPath + "\\" + fileInfoList[i].Name()
CommonUtil.CopyFile(s, t)
}
} else {
var r Result
r.Item = "创建Proto目录及拷贝pb.go文件"
r.Message = "已存在,请手工替换!"
r.Num = 3
r.Success = false
ListResult = append(ListResult, r)
}
c.JSON(http.StatusOK, Model.Res{
Data: ListResult,
})
}