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.

159 lines
4.2 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 (
"bytes"
"dsAutoCode/Dao/ProtoDao"
"dsAutoCode/Model"
"dsAutoCode/Service/ProtoService"
"dsAutoCode/Utils/CommonUtil"
"dsAutoCode/Utils/FileUtil"
"github.com/gin-gonic/gin"
"log"
"net/http"
"strings"
"text/template"
)
//模块的路由配置
func Routers(r *gin.RouterGroup) {
rr := r.Group("/proto")
//注册get方法
rr.GET("/ListProto", ListProto)
rr.GET("/ListPerson", ListPerson)
rr.GET("/GetParameterStructInfo", GetParameterStructInfo)
rr.GET("/CodeController", CodeController)
rr.GET("/CodeControllerFramework", CodeControllerFramework)
rr.GET("/CodeService", CodeService)
rr.GET("/CodeRpcService", CodeRpcService)
rr.GET("/CodeDao", CodeDao)
rr.GET("/BindInterfaceList", BindInterfaceList)
rr.GET("/CreateWebFiles", CreateWebFiles)
rr.GET("/CreateRpcFiles", CreateRpcFiles)
rr.GET("/CreateTestUnit", CreateTestUnit)
return
}
/**
功能:获取开发者名单
作者:黄海
时间2020-04-23
*/
func ListPerson(c *gin.Context) {
list := ProtoService.ListPerson()
//3、返回回执信息
c.JSON(http.StatusOK, Model.Res{
Code: 0,
Count: len(list),
Data: list,
Msg: "获取数据成功!",
})
}
/**
功用:绑定接口下拉框
作者:黄海
时间2020-04-23
*/
func BindInterfaceList(c *gin.Context) {
list := ProtoService.BindInterfaceList()
c.JSON(http.StatusOK, Model.Res{
Code: 0,
Count: len(list),
Data: list,
Msg: "获取数据成功!",
})
}
/**
功能列举出proto的文件有哪些
作者:黄海
时间2020-04-22
*/
func ListProto(c *gin.Context) {
var interfaceId = c.Query("interfaceId")
var page = CommonUtil.ConvertStringToInt(c.Query("page"))
var limit = CommonUtil.ConvertStringToInt(c.Query("limit"))
list, count := ProtoService.ListProto(interfaceId, page, limit)
//3、返回回执信息
c.JSON(http.StatusOK, Model.Res{
Code: 0,
Count: CommonUtil.ConvertInt64ToInt(count),
Data: list,
Msg: "获取数据成功!",
})
}
/**
功能:获取指定参数结构体下的参数属性
作者:黄海
时间2020-04-22
*/
func GetParameterStructInfo(c *gin.Context) {
list := ProtoService.GetParameterStructInfo(c.Query("struct_id"))
//3、返回回执信息
c.JSON(http.StatusOK, Model.Res{
Code: 0,
Count: len(list),
Data: list,
Msg: "获取数据成功!",
})
}
/**
功能:创建单元测试文件
作者:黄海
时间:
*/
func CreateTestUnit(c *gin.Context) {
var interfaceId = c.Query("interfaceId")
var interfaceName = c.Query("interfaceName")
var authorId = c.Query("author_id")
var controllerName = c.Query("ControllerName")
_, _, description := ProtoDao.GetControllerInfoByName(controllerName)
authorName := ProtoService.GetPersonInfoById(authorId)["person_name"].(string)
tableName := ProtoDao.GetTrueTableNameFromInterfaceId(interfaceId)
tableName = strings.Replace(tableName, ".pb.go", "", -1)
//获取结构体名称
var structId, _ = ProtoDao.GetStructIdByInterfaceId(interfaceId)
_, _, m := ProtoDao.GetStructInfoById(structId)
var structName = m["struct_name"]
list:=ProtoDao.GetParameterStructInfo(structId)
var RpcServiceParameters string
for i := 0; i < len(list); i++ {
structParameterName := list[i]["struct_parameter_name"].(string)
RpcServiceParameters += CommonUtil.StrFirstToUpper(structParameterName) + " : " + CommonUtil.StrFirstToLower(structParameterName) + " ,"
}
//调用模板
filesName :=CommonUtil.GetCurrentDirectory()+ "/Controller/ProtoController/Template/TestUnit.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{}{
"currenttime": CommonUtil.GetCurrentTime(),
"description": description,
"interfaceName": interfaceName,
"tableName": tableName,
"authorName": authorName,
"struct_name": structName,
"RpcServiceParameters":RpcServiceParameters,
}
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(CommonUtil.GetParentPath(CommonUtil.GetCurrentDirectory())+"/dsBaseRpc/Test/"+interfaceName+"_test.go", content)
c.JSON(http.StatusOK, Model.Res{
Code: "0",
Msg: "生成成功",
})
}