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.

147 lines
5.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 (
"bytes"
"dsAutoCode/Dao/ProtoDao"
"dsAutoCode/Model"
"dsAutoCode/Service/ProtoService"
"dsAutoCode/Utils/CommonUtil"
"github.com/gin-gonic/gin"
"log"
"net/http"
"strings"
"text/template"
)
/**
功能生成Service层的操作代码
作者:黄海
时间2020-04-23
*/
func CodeService(c *gin.Context) {
var operateType string
var tableName string
type VarStruct struct {
Description string
ParameterName string
HttpType string
IsInt32 bool
Parameters string
}
var Parameters string
var RpcServiceParameters string
interfaceId := c.Query("interfaceId")
interfaceName := c.Query("interfaceName")
controllerName := c.Query("controllerName")
authorId := c.Query("author_id")
authorName := ProtoService.GetPersonInfoById(authorId)["person_name"].(string)
var description string
//返回的结构体前缀
var argPrefix string
//对应的结构体
structId,_:=ProtoDao.GetStructIdByInterfaceId(interfaceId)
//argPrefix 替换为结构体名称
_,_,structBean:=ProtoDao.GetStructInfoById(structId)
argPrefix=structBean["struct_name"].(string)
//如果存在swagger的接口配置一般是指常规的接口,读取swagger参数配置
if controllerName != "" {
var controllerId string
_, controllerId, _ = ProtoDao.GetControllerInfoByName(controllerName)
//swagger接口对应的参数
list := ProtoDao.GetControllerInfoById(controllerId)
for i := 0; i < len(list); i++ {
controllerParameterName := list[i]["controller_parameter_name"].(string)
controllerParameterType := list[i]["controller_parameter_type"].(string)
if controllerParameterType == "integer" {
controllerParameterType = "int32"
}
Parameters += controllerParameterName + " " + controllerParameterType + ","
RpcServiceParameters += CommonUtil.StrFirstToUpper(controllerParameterName) + " : " + CommonUtil.StrFirstToLower(controllerParameterName) + " ,"
}
_, _, description = ProtoDao.GetControllerInfoByName(controllerName)
operateType = ProtoService.GetOperateType(interfaceName)
//如果是Delete操作那么需要更改RpcServiceParameters,Parameters
//真实表名:
if operateType == "Delete" {
success, message, pkField, pkValue := ProtoService.GetParameterByInterfaceId(interfaceId)
if !success {
c.JSON(http.StatusOK, Model.Res{
Code: 500,
Msg: message,
})
return
}
v := CommonUtil.StrFirstToLower(CommonUtil.GetSnakeCaseStr(pkField))
Parameters = v + " " + pkValue+" "
//RpcServiceParameters = "{" + CommonUtil.GetSnakeCaseStr(pkField) + " : " + v + "} "
RpcServiceParameters = CommonUtil.GetSnakeCaseStr(pkField) + " : " + v
}
} else {
//swagger接口不存在需要读取proto参数配置
//proto的参数
list:=ProtoDao.GetParameterStructInfo(structId)
for i := 0; i < len(list); i++ {
structParameterName := list[i]["struct_parameter_name"].(string)
structParameterType := list[i]["struct_parameter_type"].(string)
//判断是不是数组类型
if list[i]["is_repeate"].(int64)==1{
Parameters += CommonUtil.StrFirstToLower(structParameterName) + " []" + structParameterType + ","
}else{
//普通参数
Parameters += CommonUtil.StrFirstToLower(structParameterName) + " " + structParameterType + ","
}
RpcServiceParameters += CommonUtil.StrFirstToUpper(structParameterName) + " : " + CommonUtil.StrFirstToLower(structParameterName) + " ,"
}
}
tableName = ProtoDao.GetTrueTableNameFromInterfaceId(interfaceId)
tableName = strings.Replace(tableName, ".pb.go", "", -1)
//此接口在proto文件中对应的入参结构体
list := ProtoDao.GetStructParameterInfoById(structId)
var _map = make(map[string]string)
for i := 0; i < len(list); i++ {
structParaName := CommonUtil.StrFirstToLower(list[i]["struct_parameter_name"].(string))
structParaType := list[i]["struct_parameter_type"].(string)
_map[structParaName] = structParaType
}
//调用模板
filesName := CommonUtil.GetCurrentPath()+"/Controller/ProtoController/Template/Service.template"
t, err := template.ParseFiles(filesName)
if err != nil {
log.Fatalln("parse file err:", err)
return
}
buf := new(bytes.Buffer)
if len(Parameters) > 0 {
Parameters = Parameters[0 : len(Parameters)-1]
}
if len(RpcServiceParameters) > 0 {
RpcServiceParameters = "{ " + RpcServiceParameters[:len(RpcServiceParameters)-1] + "}"
}
var p = map[string]interface{}{
"tableName": tableName,
"Parameters": Parameters,
"RpcServiceParameters": RpcServiceParameters,
"argPrefix": argPrefix,
"currenttime": CommonUtil.GetCurrentTime(),
"description": description,
"authorName": authorName,
"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("Service", tableName),
})
}