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.

96 lines
2.5 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"
)
/**
功能生成Dao层的操作代码
作者:黄海
时间2020-04-23
*/
func CodeDao(c *gin.Context) {
var operateType string
var tableName string
interfaceId := c.Query("interfaceId")
//真实的表名
trueTableName := ProtoService.GetTrueTableNameFromInterfaceId(interfaceId)
//蛇形命名后的表名
tableName = ProtoDao.GetTrueTableNameFromInterfaceId(interfaceId)
tableName = strings.Replace(tableName, ".pb.go", "", -1)
//这个表的主键-->ModelArg:
_, _, pkFiled, _ := ProtoService.GetParameterByInterfaceId(interfaceId)
interfaceName := c.Query("interfaceName")
//获取接口的描述信息
controllerName := c.Query("controllerName")
_, _, description := ProtoService.GetControllerInfoByName(controllerName)
//作者信息
authorId := c.Query("author_id")
authorName := ProtoService.GetPersonInfoById(authorId)["person_name"].(string)
//操作类型
operateType = ProtoService.GetOperateType(interfaceName)
//调用模板,
filesName :=CommonUtil.GetCurrentPath()+ "/Controller/ProtoController/Template/Dao.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 content string
if !DeleteIdsArg{
var p = map[string]interface{}{
"tableName": tableName,
"operateType": operateType,
"CurrentTime": CommonUtil.GetCurrentTime(),
"description": description,
"authorName": authorName,
"modelName": CommonUtil.StrFirstToLower(tableName),
"ModelArg": ModelArg,
"QueryArg": QueryArg,
"trueTableName": trueTableName,
"PK": pkFiled,
"interfaceName": interfaceName,
}
if err := t.Execute(buf, p); err != nil {
log.Fatal("There was an error:", err.Error())
}
content = buf.String()
//删除空格
content = CommonUtil.DeleteBlankString(content)
}else{
content="删除功能请统一使用CommonDao.DeleteIds"
}
//返回结果
c.JSON(http.StatusOK, Model.Res{
Data: content,
Msg: tableName,
CodePath: ProtoService.GetGenereateCodePath("Dao", tableName),
})
}