package main import ( "dsBaseWeb/Utils/CommonUtil" "dsBaseWeb/Utils/ConfigUtil" "dsBaseWeb/Utils/DbUtil" "fmt" "github.com/lunny/log" "io/ioutil" "os" "os/exec" "strconv" "strings" ) func main() { //1、遍历数据库所有的表 sql := "select table_name from information_schema.tables where table_schema=?" list, _ := DbUtil.Engine.SQL(sql, ConfigUtil.MysqlDbName).QueryString() for i := 0; i < len(list); i++ { tableName := list[i]["table_name"] //查询所有字段 sql2 := "select column_name,data_type,column_comment,column_key from information_schema.columns where table_schema=? and table_name=?" listField, _ := DbUtil.Engine.SQL(sql2, ConfigUtil.MysqlDbName, tableName).QueryString() tableName = CommonUtil.GetSnakeCaseStr(strings.Replace(tableName, "t_", "", -1)) str := `syntax = "proto3";` + "\n\n" str += `//在web层重新生成pb.go的方法 `+ "\n" str +=`/*`+"\n" str += `protoc -I Business/` + tableName + "/" + tableName + "Proto" + `/ Business/` + tableName + `/` + tableName + "Proto/" + tableName + `.proto --go_out=plugins=grpc:Business/` + tableName + "/" + tableName + "Proto" + "\n" str +=`*/`+"\n" str += `package ` + tableName + `Proto;` + "\n\n" //golang中的包 str += `option go_package = ".;` + tableName + `Proto";` + "\n\n" //用于生成java的代码配置 str += `option java_multiple_files = true;` + "\n" str += `option java_package = "com.dsideal.dsmin.base";` + "\n" str += `option java_outer_classname = "` + tableName + `";` + "\n" str += `option objc_class_prefix = "";` + "\n\n" //暴露的接口 str += `service ` + tableName + `Manage {` + "\n" str += " //获取单条\n" str += ` rpc Get` + tableName + ` (ModelArg) returns (Reply) {}` + "\n" str += " //增加\n" str += ` rpc Add` + tableName + ` (ModelArg) returns (Reply) {}` + "\n" str += " //批量删除\n" str += ` rpc Delete` + tableName + ` (DeleteIdsArg) returns (Reply) {}` + "\n" str += " //修改\n" str += ` rpc Update` + tableName + ` (ModelArg) returns (Reply) {}` + "\n" str += " //查询(分页)\n" str += ` rpc Page` + tableName + ` (QueryArg) returns (Reply) {}` + "\n" str += `}` + "\n\n" str += "message ModelArg {\n" for j := 0; j < len(listField); j++ { columnName := listField[j]["column_name"] //这个属性不用生成了 黄海添加于2020-04-26 if columnName == "LastUpdatedTime " { continue } dataType := listField[j]["data_type"] columnComment := listField[j]["column_comment"] str += " //" + columnComment + "\n" if dataType == "int" || dataType == "tinyint" { str += " int32 " + CommonUtil.GetSnakeCaseStr(columnName) + " = " + strconv.Itoa(j+1) + ";\n" }else if dataType == "float" || dataType == "double" { str += " float " + CommonUtil.GetSnakeCaseStr(columnName) + " = " + strconv.Itoa(j+1) + ";\n" }else { str += " string " + CommonUtil.GetSnakeCaseStr(columnName) + " = " + strconv.Itoa(j+1) + ";\n" } } //添加ForceUpdateFields 黄海添加于2020-05-20 str+=" //需要强制更新的列"+"\n" str+=" repeated string ForceUpdateFields = "+ strconv.Itoa(len(listField)+1)+";"+"\n" str += "}\n\n" //查询参数 str += "// 查询参数\n" str += "message QueryArg{\n" str += " int32 Page = 1;\n" str += " int32 Limit = 2;\n" str += "}\n\n" //查询请求响应内容 str += "// 查询请求响应\n" str += "message Reply{\n" str += " bool Success = 1;\n" str += " string Message = 2;\n" str += " int32 Count = 3;\n" str += " string List = 4;\n" str += "}\n" //删除参数 str += "//删除参数\n" str += "message DeleteIdsArg {\n" str += " //一个或多个ID\n" str += " repeated string Ids = 1;\n" str += "}\n" //按文件分别保存 workingPath := "./Tools/proto/" + tableName + "/" + tableName + "Proto/" if !CommonUtil.Exists(workingPath) { err := os.MkdirAll(workingPath, os.ModePerm) if err != nil { log.Fatal(err) } } path := workingPath + tableName + ".proto" ioutil.WriteFile(path, []byte(str), 0666) //生成一次pb.go cmd := "protoc -I Tools/proto/" + tableName + "/" + tableName + "Proto/ Tools/proto/" + tableName + "/" + tableName + "Proto/" + tableName + ".proto --go_out=plugins=grpc:Tools/proto/" + tableName + "/" + tableName + "Proto" runInWindows(cmd) } fmt.Println("恭喜,所有表对应的proto文件内容生成成功!") } /** 功能:在windows下执行一次dos命令 作者:黄海 时间:2020-05-06 */ func runInWindows(cmd string) string { fmt.Println("Running Win cmd:", cmd) result, err := exec.Command("cmd", "/c", cmd).Output() if err != nil { fmt.Println(err.Error()) } return strings.TrimSpace(string(result)) }