|
|
package ProtoController
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
"dsAutoCode/Dao/ProtoDao"
|
|
|
"dsAutoCode/Model"
|
|
|
"dsAutoCode/Service/ProtoService"
|
|
|
"dsAutoCode/Utils/CommonUtil"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
"strings"
|
|
|
"text/template"
|
|
|
)
|
|
|
|
|
|
/**
|
|
|
功能:生成RpcService层的操作代码
|
|
|
作者:黄海
|
|
|
时间:2020-04-23
|
|
|
*/
|
|
|
func CodeRpcService(c *gin.Context) {
|
|
|
var operateType string
|
|
|
var tableName string
|
|
|
type VarStruct struct {
|
|
|
Description string
|
|
|
ParameterName string
|
|
|
HttpType string
|
|
|
IsInt32 bool
|
|
|
Parameters string
|
|
|
}
|
|
|
//接口id
|
|
|
interfaceId := c.Query("interfaceId")
|
|
|
//接口名称
|
|
|
interfaceName := c.Query("interfaceName")
|
|
|
//作者ID
|
|
|
authorId := c.Query("author_id")
|
|
|
//获取作者名称
|
|
|
authorName := ProtoService.GetPersonInfoById(authorId)["person_name"].(string)
|
|
|
|
|
|
//获取接口的描述信息
|
|
|
controllerName := c.Query("controllerName")
|
|
|
_, _, description := ProtoService.GetControllerInfoByName(controllerName)
|
|
|
|
|
|
//操作类型,Page ,Get, Operate
|
|
|
operateType = ProtoService.GetOperateType(interfaceName)
|
|
|
|
|
|
//通过接口ID,获取接口对应的文件名称,截取就是对应表的名称
|
|
|
tableName = ProtoDao.GetTrueTableNameFromInterfaceId(interfaceId)
|
|
|
tableName = strings.Replace(tableName, ".pb.go", "", -1)
|
|
|
|
|
|
//调用模板,
|
|
|
filesName := CommonUtil.GetCurrentPath()+"/Controller/ProtoController/Template/RpcService.template"
|
|
|
t, err := template.ParseFiles(filesName)
|
|
|
if err != nil {
|
|
|
log.Fatalln("parse file err:", err)
|
|
|
return
|
|
|
}
|
|
|
buf := new(bytes.Buffer)
|
|
|
//返回的结构体前缀
|
|
|
var DeleteIdsArg = false
|
|
|
var ModelArg = false
|
|
|
var QueryArg = false
|
|
|
|
|
|
if operateType == "Page" {
|
|
|
QueryArg = true
|
|
|
} else if operateType == "Delete" {
|
|
|
DeleteIdsArg = true
|
|
|
} else {
|
|
|
ModelArg = true
|
|
|
}
|
|
|
|
|
|
var p = map[string]interface{}{
|
|
|
"tableName": tableName,
|
|
|
"currenttime": CommonUtil.GetCurrentTime(),
|
|
|
"description": description,
|
|
|
"authorName": authorName,
|
|
|
"DeleteIdsArg": DeleteIdsArg,
|
|
|
"ModelArg": ModelArg,
|
|
|
"QueryArg": QueryArg,
|
|
|
"interfaceName": interfaceName,
|
|
|
}
|
|
|
if err := t.Execute(buf, p); err != nil {
|
|
|
log.Fatal("There was an error:", err.Error())
|
|
|
}
|
|
|
var content = buf.String()
|
|
|
content = CommonUtil.DeleteBlankString(content)
|
|
|
c.JSON(http.StatusOK, Model.Res{
|
|
|
Data: content,
|
|
|
Msg: tableName,
|
|
|
CodePath: ProtoService.GetGenereateCodePath("RpcService", tableName),
|
|
|
})
|
|
|
}
|