|
@@ -0,0 +1,275 @@
|
|
|
|
|
+package util
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "bufio"
|
|
|
|
|
+ "fmt"
|
|
|
|
|
+ "os"
|
|
|
|
|
+ "strings"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+// const base_url = "E:\\project\\kkc\\kkc-go\\"
|
|
|
|
|
+const base_url = "D:\\project\\study\\kkc\\kkc-go\\"
|
|
|
|
|
+
|
|
|
|
|
+// 修改mysql数据库对应的go类型
|
|
|
|
|
+func formatSqlType(str string) string {
|
|
|
|
|
+ str = strings.ToLower(str)
|
|
|
|
|
+ switch str {
|
|
|
|
|
+ case "varchar":
|
|
|
|
|
+ return "string"
|
|
|
|
|
+ case "int":
|
|
|
|
|
+ return "int64"
|
|
|
|
|
+ case "decimal":
|
|
|
|
|
+ return "float64"
|
|
|
|
|
+ case "datetime":
|
|
|
|
|
+ return "time.Time"
|
|
|
|
|
+ case "longblob":
|
|
|
|
|
+ return "string"
|
|
|
|
|
+ default:
|
|
|
|
|
+ panic("未定义的类型:" + str)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+//----------------------------------------------------------
|
|
|
|
|
+
|
|
|
|
|
+// MysqlToStruct 根据mysql 生成对应的struct及String函数
|
|
|
|
|
+func MysqlToStruct() {
|
|
|
|
|
+ var structString = "package domain\n\nimport (\n \"fmt\"\n \"time\"\n)\n\n"
|
|
|
|
|
+ for _, mtable := range MTables {
|
|
|
|
|
+ structString += fmt.Sprint("//", mtable.AaBbName, " ", mtable.Comment, "\n",
|
|
|
|
|
+ "type ", mtable.AaBbName, " struct {\n")
|
|
|
|
|
+
|
|
|
|
|
+ cols := mtable.MColumns
|
|
|
|
|
+ var toString = ""
|
|
|
|
|
+ for i := range cols {
|
|
|
|
|
+ if cols[i].aaBbName == "id" && cols[i].IsKey {
|
|
|
|
|
+ structString += fmt.Sprint(" ", cols[i].AaBbName, " int64 `json:\"", cols[i].aaBbName, "\"`",
|
|
|
|
|
+ "// ", cols[i].Length, " ", cols[i].Length2, " 注释:", cols[i].Comment, "\n")
|
|
|
|
|
+ toString += fmt.Sprint(cols[i].AaBbName, ":\", receiver.", cols[i].AaBbName, ", \",")
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ structString += fmt.Sprint(" ", cols[i].AaBbName, " ", cols[i].TypeName, " `xorm:\"", cols[i].Name, "\" json:\"", cols[i].aaBbName, "\"`",
|
|
|
|
|
+ "// ", cols[i].Length, " ", cols[i].Length2, " 注释:", cols[i].Comment, "\n")
|
|
|
|
|
+ toString += fmt.Sprint(cols[i].AaBbName, ":\", receiver.", cols[i].AaBbName, ", \",")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ structString += "}\n\n"
|
|
|
|
|
+ structString += fmt.Sprint("func (receiver ", mtable.AaBbName, ") String() string {\n")
|
|
|
|
|
+ structString += fmt.Sprint(" return fmt.Sprint(\"", mtable.AaBbName, "{", toString[0:len(toString)-1], "}\")\n}\n\n")
|
|
|
|
|
+ fmt.Println(structString)
|
|
|
|
|
+ }
|
|
|
|
|
+ fileUrl := base_url + "\\data\\domain\\DoMain.go"
|
|
|
|
|
+ //清空文件
|
|
|
|
|
+ os.Truncate(fileUrl, 0)
|
|
|
|
|
+ file, _ := os.OpenFile(fileUrl, os.O_CREATE|os.O_RDWR, 0666)
|
|
|
|
|
+ defer file.Close()
|
|
|
|
|
+
|
|
|
|
|
+ writer := bufio.NewWriter(file)
|
|
|
|
|
+ //writer.WriteString("package domain\n\nimport (\n \"fmt\"\n \"time\"\n)\n\n")
|
|
|
|
|
+ writer.WriteString(structString)
|
|
|
|
|
+ writer.Flush()
|
|
|
|
|
+
|
|
|
|
|
+ //file.Write([]byte("package domain\n\nimport \"time\"\n"))
|
|
|
|
|
+ //file.Write([]byte(structString))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// MysqlToDao 生成简单的CRUD dao层
|
|
|
|
|
+func MysqlToDao() {
|
|
|
|
|
+ var baseUrl = base_url + "data\\dao\\manage\\"
|
|
|
|
|
+ tables := MTables
|
|
|
|
|
+ for _, table := range tables {
|
|
|
|
|
+ var dao = ""
|
|
|
|
|
+ fileName := table.AaBbName + "Dao.go"
|
|
|
|
|
+ dao += "package manage\n\nimport (\n\t\"demo/configs\"\n\t\"demo/data/domain\"\n\t\"demo/data/domain/vo\"\n)\n"
|
|
|
|
|
+ //get
|
|
|
|
|
+ if table.Name == "role_authority" {
|
|
|
|
|
+ fmt.Println(table.existId, "====================>")
|
|
|
|
|
+ }
|
|
|
|
|
+ var idColumn MColumn
|
|
|
|
|
+ for _, column := range table.MColumns {
|
|
|
|
|
+ if column.IsKey {
|
|
|
|
|
+ idColumn = column
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ //没有id的不适用
|
|
|
|
|
+ if table.existId {
|
|
|
|
|
+ dao += "func Get" + table.AaBbName + "ById(id int64) (domain." + table.AaBbName + ", error) {" +
|
|
|
|
|
+ "\n\tvar " + table.aaBbName + " domain." + table.AaBbName + "" +
|
|
|
|
|
+ "\n\t_, err := configs.Engine.Where(\"id = ?\", id).Get(&" + table.aaBbName + ")" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\treturn " + table.aaBbName + ", err" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\treturn " + table.aaBbName + ", nil" +
|
|
|
|
|
+ "\n}\n\n"
|
|
|
|
|
+ }
|
|
|
|
|
+ dao += "func Get" + table.AaBbName + "ListInId(ids []int64) ([]domain." + table.AaBbName + ", error) {" +
|
|
|
|
|
+ "\n\trows, err := configs.Engine.In(\"id\", ids).Rows(&domain." + table.AaBbName + "{})" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\treturn make([]domain." + table.AaBbName + ", 0), err" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\tdefer rows.Close()" +
|
|
|
|
|
+ "\n\tvar list []domain." + table.AaBbName + "" +
|
|
|
|
|
+ "\n\tfor rows.Next() {" +
|
|
|
|
|
+ "\n\t\tvar u domain." + table.AaBbName + "" +
|
|
|
|
|
+ "\n\t\tlist = append(list, u)" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\treturn list, nil" +
|
|
|
|
|
+ "\n}\n\n"
|
|
|
|
|
+
|
|
|
|
|
+ //list
|
|
|
|
|
+ dao += "func Get" + table.AaBbName + "List(" + table.aaBbName + " domain." + table.AaBbName + ", pageNum, pageSize int) (vo.BaseListVo, error) {" +
|
|
|
|
|
+ "\n\tvar arr []domain." + table.AaBbName +
|
|
|
|
|
+ "\n\tCount, err := configs.Engine.Limit(pageSize, (pageNum-1)*pageSize).Desc(\"id\").FindAndCount(&arr, &" + table.aaBbName + ")" +
|
|
|
|
|
+ "\n\tvar vo vo.BaseListVo" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\treturn vo, err" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\treturn vo, err" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\tvo.PageNum = pageNum" +
|
|
|
|
|
+ "\n\tvo.PageSize = pageSize" +
|
|
|
|
|
+ "\n\tvo.List = make([]any, len(arr))" +
|
|
|
|
|
+ "\n\tfor i, v := range arr {" +
|
|
|
|
|
+ "\n\t\tvo.List[i] = v" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\tvo.Total = int(Count)" +
|
|
|
|
|
+ "\n\treturn vo, nil" +
|
|
|
|
|
+ "\n}\n\n"
|
|
|
|
|
+
|
|
|
|
|
+ dao += "func Save" + table.AaBbName + "(" + table.aaBbName + " *domain." + table.AaBbName + ") (domain." + table.AaBbName + ", error) {" +
|
|
|
|
|
+ "\n\t_, err := configs.Engine.Insert(&" + table.aaBbName + ")" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\treturn *" + table.aaBbName + ", err" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\treturn *" + table.aaBbName + ", nil" +
|
|
|
|
|
+ "\n}\n\n"
|
|
|
|
|
+ dao += "func Set" + table.AaBbName + "(" + table.aaBbName + "Map map[string]interface{}, id int64) (domain." + table.AaBbName + ", bool, error) {" +
|
|
|
|
|
+ "\n\tvar " + table.aaBbName + " domain." + table.AaBbName + "" +
|
|
|
|
|
+ "\n\t_, err := configs.Engine.Table(\"" + table.Name + "\").Where(\"" + idColumn.Name + " = ?\", id).Update(" + table.aaBbName + "Map)" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\treturn " + table.aaBbName + ", false, err" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\treturn " + table.aaBbName + ", true, nil" +
|
|
|
|
|
+ "\n}\n\n"
|
|
|
|
|
+ if table.existId {
|
|
|
|
|
+ dao += "func Delete" + table.AaBbName + "(id int64) bool {" +
|
|
|
|
|
+ "\n\tvar " + table.aaBbName + " domain." + table.AaBbName +
|
|
|
|
|
+ "\n\ti, err := configs.Engine.Where(\"id = ?\", id).Delete(&" + table.aaBbName + ")" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\treturn false" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\treturn i > 0" +
|
|
|
|
|
+ "\n}"
|
|
|
|
|
+ }
|
|
|
|
|
+ //创建文件
|
|
|
|
|
+ url := baseUrl + fileName
|
|
|
|
|
+ WriteFile(url, dao)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func MysqlToBaseCRUDRouter() {
|
|
|
|
|
+ tables := MTables
|
|
|
|
|
+ var template = "package router" +
|
|
|
|
|
+ "\n" +
|
|
|
|
|
+ "\nimport (" +
|
|
|
|
|
+ "\n\t\"demo/data/dao/manage\"" +
|
|
|
|
|
+ "\n\t\"demo/data/domain\"" +
|
|
|
|
|
+ "\n\t\"demo/share\"" +
|
|
|
|
|
+ "\n\t\"encoding/json\"" +
|
|
|
|
|
+ "\n\t\"github.com/gin-gonic/gin\"" +
|
|
|
|
|
+ "\n\t\"github.com/spf13/cast\"" +
|
|
|
|
|
+ "\n)" +
|
|
|
|
|
+ "\n"
|
|
|
|
|
+ var routerHeader = ""
|
|
|
|
|
+ var routerBody = ""
|
|
|
|
|
+ var getRouterFunc = ""
|
|
|
|
|
+ for i := range tables {
|
|
|
|
|
+ table := tables[i]
|
|
|
|
|
+ getRouterFunc += "//Base" + table.AaBbName + "Router(group)\n"
|
|
|
|
|
+ routerHeader +=
|
|
|
|
|
+ "\nfunc Base" + table.AaBbName + "Router(group *gin.RouterGroup) {" +
|
|
|
|
|
+ "\n\tBase" + table.AaBbName + "Group := group.Group(\"/back/base\")" +
|
|
|
|
|
+ "\n\tPushRouter(Base" + table.AaBbName + "Group, \"GET\", \"/" + table.aaBbName + "\", GetBase" + table.AaBbName + "ById)" +
|
|
|
|
|
+ "\n\tPushRouter(Base" + table.AaBbName + "Group, \"POST\", \"/" + table.aaBbName + "/list\", GetBase" + table.AaBbName + "List)" +
|
|
|
|
|
+ "\n\tPushRouter(Base" + table.AaBbName + "Group, \"POST\", \"/" + table.aaBbName + "/in\", GetBase" + table.AaBbName + "ListInId)" +
|
|
|
|
|
+ "\n\tPushRouter(Base" + table.AaBbName + "Group, \"POST\", \"/" + table.aaBbName + "\", SaveBase" + table.AaBbName + ")" +
|
|
|
|
|
+ "\n\tPushRouter(Base" + table.AaBbName + "Group, \"PUT\", \"/" + table.aaBbName + "\", UpdateBase" + table.AaBbName + ")" +
|
|
|
|
|
+ "\n\tPushRouter(Base" + table.AaBbName + "Group, \"DELETE\", \"/" + table.aaBbName + "\", DeleteBase" + table.AaBbName + ")" +
|
|
|
|
|
+ "\n}\n"
|
|
|
|
|
+ routerBody +=
|
|
|
|
|
+ "\nfunc GetBase" + table.AaBbName + "ById(c *gin.Context) {" +
|
|
|
|
|
+ "\n\tid := c.Param(\"id\")" +
|
|
|
|
|
+ "\n\t" + table.aaBbName + ", err := manage.Get" + table.AaBbName + "ById(cast.ToInt64(id))" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\tc.JSON(200, CreateResultError(500, \"用户查询不存在\"))" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\tc.JSON(200, CreateResultData(" + table.aaBbName + "))" +
|
|
|
|
|
+ "\n}" +
|
|
|
|
|
+ "\n" +
|
|
|
|
|
+ "\nfunc GetBase" + table.AaBbName + "List(c *gin.Context) {" +
|
|
|
|
|
+ "\n\tdata := share.GetJsonAnyParam(c)" +
|
|
|
|
|
+ "\n\tparam, _ := data(\"param\")" +
|
|
|
|
|
+ "\n\tpageNum, _ := data(\"pageNum\")" +
|
|
|
|
|
+ "\n\tpageSize, _ := data(\"pageSize\")" +
|
|
|
|
|
+ "\n\t" + table.aaBbName + " := domain." + table.AaBbName + "{}" +
|
|
|
|
|
+ "\n\terr := json.Unmarshal([]byte(cast.ToString(param)), &" + table.aaBbName + ")" +
|
|
|
|
|
+ "\n\tlist, err := manage.Get" + table.AaBbName + "List(" + table.aaBbName + ", cast.ToInt(pageNum), cast.ToInt(pageSize))" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\tc.JSON(200, CreateResultError(500, \"查询错误\"))" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\tc.JSON(200, CreateResultData(list))" +
|
|
|
|
|
+ "\n}" +
|
|
|
|
|
+ "\n" +
|
|
|
|
|
+ "\nfunc GetBase" + table.AaBbName + "ListInId(c *gin.Context) {" +
|
|
|
|
|
+ "\n\tdata := share.GetJsonAnyParam(c)" +
|
|
|
|
|
+ "\n\tidsString, _ := data(\"ids\")" +
|
|
|
|
|
+ "\n\tvar ids []int64" +
|
|
|
|
|
+ "\n\terr := json.Unmarshal([]byte(cast.ToString(idsString)), &ids)" +
|
|
|
|
|
+ "\n\tlist, err := manage.Get" + table.AaBbName + "ListInId(ids)" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\tc.JSON(200, CreateResultError(500, \"查询错误\"))" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\tc.JSON(200, CreateResultData(list))" +
|
|
|
|
|
+ "\n}" +
|
|
|
|
|
+ "\n" +
|
|
|
|
|
+ "\nfunc SaveBase" + table.AaBbName + "(c *gin.Context) {" +
|
|
|
|
|
+ "\n\tdata := share.GetJsonAnyParam(c)" +
|
|
|
|
|
+ "\n\t" + table.aaBbName + "Param, _ := data(\"" + table.aaBbName + "\")" +
|
|
|
|
|
+ "\n\t" + table.aaBbName + " := domain." + table.AaBbName + "{}" +
|
|
|
|
|
+ "\n\terr := json.Unmarshal([]byte(cast.ToString(" + table.aaBbName + "Param)), &" + table.aaBbName + ")" +
|
|
|
|
|
+ "\n\tsave" + table.AaBbName + ", err := manage.Save" + table.AaBbName + "(&" + table.aaBbName + ")" +
|
|
|
|
|
+ "\n\tif err != nil {" +
|
|
|
|
|
+ "\n\t\tc.JSON(200, CreateResultError(500, \"保存错误\"))" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\tc.JSON(200, CreateResultData(save" + table.AaBbName + "))" +
|
|
|
|
|
+ "\n}" +
|
|
|
|
|
+ "\n" +
|
|
|
|
|
+ "\nfunc UpdateBase" + table.AaBbName + "(c *gin.Context) {" +
|
|
|
|
|
+ "\n\tdata := share.GetJsonAnyParam(c)" +
|
|
|
|
|
+ "\n\t" + table.aaBbName + ", _ := data(\"" + table.aaBbName + "\")" +
|
|
|
|
|
+ "\n\tid, _ := data(\"id\")" +
|
|
|
|
|
+ "\n" +
|
|
|
|
|
+ "\n\tset" + table.AaBbName + ", b, err := manage.Set" + table.AaBbName + "(cast.ToStringMap(" + table.aaBbName + "), cast.ToInt64(id))" +
|
|
|
|
|
+ "\n\tif err != nil || !b {" +
|
|
|
|
|
+ "\n\t\tc.JSON(200, CreateResultError(500, \"修改错误\"))" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\tc.JSON(200, CreateResultData(set" + table.AaBbName + "))" +
|
|
|
|
|
+ "\n}" +
|
|
|
|
|
+ "\n" +
|
|
|
|
|
+ "\nfunc DeleteBase" + table.AaBbName + "(c *gin.Context) {" +
|
|
|
|
|
+ "\n\tid := c.Param(\"id\")" +
|
|
|
|
|
+ "\n\tres := manage.Delete" + table.AaBbName + "(cast.ToInt64(id))" +
|
|
|
|
|
+ "\n\tif !res {" +
|
|
|
|
|
+ "\n\t\tc.JSON(200, CreateResultError(500, \"删除错误\"))" +
|
|
|
|
|
+ "\n\t}" +
|
|
|
|
|
+ "\n\tc.JSON(200, CreateResult())" +
|
|
|
|
|
+ "\n}" +
|
|
|
|
|
+ "\n"
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //url := "D:\\project\\study\\kkc\\go-xorm-create\\go-xorm-create\\text\\TextFile"
|
|
|
|
|
+ url := "D:\\project\\study\\kkc\\kkc-go\\router\\BasaeBackRouter.go"
|
|
|
|
|
+ WriteFile(url, template+getRouterFunc+routerHeader+routerBody)
|
|
|
|
|
+}
|