浏览代码

优化细节

Administrator 2 年之前
父节点
当前提交
259021a503
共有 4 个文件被更改,包括 106 次插入50 次删除
  1. 4 1
      main.go
  2. 69 33
      util/MysqlCreateGo.go
  3. 26 10
      util/MysqlCreateVue.go
  4. 7 6
      util/ProjectUrl.go

+ 4 - 1
main.go

@@ -19,9 +19,12 @@ func main() {
 	//util.MysqlToBaseCRUDRouter()
 	//执行生成,vue
 	//util.MysqlToVueApi()
-	//SaveMenu()
 	util.CreateVue()
+	//SaveMenu()
 
+	//for i := 0; i < 101; i++ {
+	//	fmt.Println(fmt.Sprint("INSERT INTO `virtual_mall`.`back_role_menu`( `role_id`, `menu_id`) VALUES ( 1, ", (41 + i), ");"))
+	//}
 }
 
 //

+ 69 - 33
util/MysqlCreateGo.go

@@ -260,40 +260,76 @@ func MysqlToBaseCRUDRouter() {
 				"\n\tdata := share.GetJsonAnyParam(c)" +
 				"\n\t" + table.LittleHump + "Param, _ := data(\"" + table.LittleHump + "\")" +
 				"\n\t" + table.LittleHump + " := domain." + table.GreatHump + "{}" +
-				"\n\terr := mapstructure.Decode(" + table.LittleHump + "Param, &" + table.LittleHump + ")" +
-				"\n\tsave" + table.GreatHump + ", err := manage.Save" + table.GreatHump + "(&" + table.LittleHump + ")" +
-				"\n\tif err != nil {" +
-				"\n\t\tc.JSON(200, CreateResultError(500, \"保存错误\"))" +
-				"\n\t\treturn" +
-				"\n\t}" +
-				"\n\tc.JSON(200, CreateResultData(save" + table.GreatHump + "))" +
-				"\n}" +
-				"\n" +
-				"\nfunc UpdateBase" + table.GreatHump + "(c *gin.Context) {" +
-				"\n\tdata := share.GetJsonAnyParam(c)" +
-				"\n\t" + table.LittleHump + ", _ := data(\"" + table.LittleHump + "\")" +
-				"\n\tid, _ := data(\"id\")" +
-				"\n" +
-				"\n\tset" + table.GreatHump + ", b, err := manage.Set" + table.GreatHump + "(UtilStructToMap(cast.ToStringMap(" + table.LittleHump + ")), cast.ToInt64(id))" +
-				"\n\tif err != nil || !b {" +
-				"\n\t\tc.JSON(200, CreateResultError(500, \"修改错误\"))" +
-				"\n\t\treturn" +
-				"\n\t}" +
-				"\n\tc.JSON(200, CreateResultData(set" + table.GreatHump + "))" +
-				"\n}" +
-				"\n" +
-				"\nfunc DeleteBase" + table.GreatHump + "(c *gin.Context) {" +
-				"\n\tid := c.Query(\"id\")" +
-				"\n\tres := manage.Delete" + table.GreatHump + "(cast.ToInt64(id))" +
-				"\n\tif !res {" +
-				"\n\t\tc.JSON(200, CreateResultError(500, \"删除错误\"))" +
-				"\n\t\treturn" +
-				"\n\t}" +
-				"\n\tc.JSON(200, CreateResult())" +
-				"\n}" +
-				"\n"
-	}
+				"\n\terr := mapstructure.Decode(" + table.LittleHump + "Param, &" + table.LittleHump + ")"
+		//判断是否存在create和update
+		existCreateBy := ""
+		existCreateTime := ""
 
+		existUpdateBy := ""
+		existUpdateTime := ""
+
+		for ci := range table.MColumns {
+			columnName := strings.ToLower(table.MColumns[ci].GreatHump)
+			if columnName == "createby" {
+				if table.MColumns[ci].TypeName == "int64" {
+					existCreateBy += "\n\t" + table.LittleHump + ".CreateBy = GetUserIdByToken(c)"
+				} else {
+					existCreateBy += "\n\t" + table.LittleHump + ".CreateBy = fmt.Sprint(GetUserIdByToken(c))"
+				}
+				continue
+			}
+			if columnName == "createtime" {
+				existCreateTime += "\n\t" + table.LittleHump + ".CreateTime = time.Now()"
+				continue
+			}
+			if columnName == "updateby" {
+				if table.MColumns[ci].TypeName == "int64" {
+					existUpdateBy += "\n\ttoMap[\"create_by\"] = \"GetUserIdByToken(c)\""
+				} else {
+					existUpdateBy += "\n\ttoMap[\"create_by\"] = \"fmt.Sprint(GetUserIdByToken(c))\""
+				}
+				continue
+			}
+			if columnName == "updateby" {
+				existUpdateTime += "\n\ttoMap[\"create_time\"] = \"time.Now()\""
+				continue
+			}
+		}
+		routerBody += existCreateBy + existCreateTime
+		routerBody += "\n\tsave" + table.GreatHump + ", err := manage.Save" + table.GreatHump + "(&" + table.LittleHump + ")" +
+			"\n\tif err != nil {" +
+			"\n\t\tc.JSON(200, CreateResultError(500, \"保存错误\"))" +
+			"\n\t\treturn" +
+			"\n\t}" +
+			"\n\tc.JSON(200, CreateResultData(save" + table.GreatHump + "))" +
+			"\n}" +
+			"\n" +
+			"\nfunc UpdateBase" + table.GreatHump + "(c *gin.Context) {" +
+			"\n\tdata := share.GetJsonAnyParam(c)" +
+			"\n\t" + table.LittleHump + ", _ := data(\"" + table.LittleHump + "\")" +
+			"\n\tid, _ := data(\"id\")" +
+			"\n\ttoMap := UtilStructToMap(cast.ToStringMap(" + table.LittleHump + "))"
+		routerBody += existUpdateBy + existUpdateTime
+		routerBody += "\n\tset" + table.GreatHump + ", b, err := manage.Set" + table.GreatHump + "(toMap, cast.ToInt64(id))"
+		// "\n\tset" + table.GreatHump + ", b, err := manage.Set" + table.GreatHump + "(UtilStructToMap(cast.ToStringMap(" + table.LittleHump + ")), cast.ToInt64(id))" +
+		routerBody += "\n\tif err != nil || !b {" +
+			"\n\t\tc.JSON(200, CreateResultError(500, \"修改错误\"))" +
+			"\n\t\treturn" +
+			"\n\t}" +
+			"\n\tc.JSON(200, CreateResultData(set" + table.GreatHump + "))" +
+			"\n}" +
+			"\n" +
+			"\nfunc DeleteBase" + table.GreatHump + "(c *gin.Context) {" +
+			"\n\tid := c.Query(\"id\")" +
+			"\n\tres := manage.Delete" + table.GreatHump + "(cast.ToInt64(id))" +
+			"\n\tif !res {" +
+			"\n\t\tc.JSON(200, CreateResultError(500, \"删除错误\"))" +
+			"\n\t\treturn" +
+			"\n\t}" +
+			"\n\tc.JSON(200, CreateResult())" +
+			"\n}" +
+			"\n"
+	}
 	//url := "D:\\project\\study\\kkc\\go-xorm-create\\go-xorm-create\\text\\TextFile"
 	WriteFile(GetRouterUrl(), template+getRouterFunc+routerHeader+routerBody)
 }

+ 26 - 10
util/MysqlCreateVue.go

@@ -110,10 +110,18 @@ func MysqlToVueApi() {
 func CreateVue() {
 	//sql表格名
 	for _, gTable := range GTables {
+		//跳过 一些表
+		if gTable.GenTable.Name == "DictData" ||
+			gTable.GenTable.Name == "DictType" ||
+			gTable.GenTable.Name == "GenTableColumn" ||
+			gTable.GenTable.Name == "GenTable" {
+			continue
+		}
 		view := MysqlToVueView(gTable, gTable.GenTableColumn)
-		WriteFile("E:\\project\\kkc\\go-xorm-create\\file\\"+gTable.GenTable.Name+"View.vue", view)
+		WriteFile(ProjectGoCreateUrl+"file\\"+gTable.GenTable.Name+"View.vue", view)
 	}
 }
+
 func MysqlToVueView(table GTable, columns []GenTableColumn) string {
 	vueContent := "<template>" +
 		"\n  <div class=\"back-form-container\">" +
@@ -157,7 +165,7 @@ func MysqlToVueView(table GTable, columns []GenTableColumn) string {
 	//========================================表格部分内容
 	{
 		vueContent += "\n    <div class=\"back-table\">" +
-			"\n      <el-table v-loading=\"listLoading\" :data=\"list\" @selection-change=\"selected\" border>" +
+			"\n      <el-table v-loading=\"listLoading\" :data=\"list\" @selection-change=\"selected\" border :key=\"ListKey\">" +
 			"\n        <el-table-column type=\"selection\" width=\"40\" align=\"center\"/>"
 		//数组排序
 		sort.SliceStable(columns, func(i, j int) bool {
@@ -247,6 +255,7 @@ func MysqlToVueView(table GTable, columns []GenTableColumn) string {
 			"\nlet pageSize = ref(10)" +
 			"\n" +
 			"\nlet list = ref<" + table.GenTable.Name + "[]>([])" +
+			"\nlet ListKey = ref(0)" +
 			"\n//搜索用" +
 			"\nlet search = ref<" + table.GenTable.Name + ">(" + table.GenTable.Name + ".Create())" +
 			"\n//修改,添加用" +
@@ -296,6 +305,7 @@ func MysqlToVueView(table GTable, columns []GenTableColumn) string {
 			"\n    let data = response.data" +
 			"\n    total.value = data.total" +
 			"\n    list.value = response.data.list" +
+			"\n    ListKey.value++" +
 			"\n    listLoading.value = false" +
 			"\n  })" +
 			"\n}" +
@@ -343,7 +353,8 @@ func MysqlToVueView(table GTable, columns []GenTableColumn) string {
 // 文件上传 File
 // 富文本 Editor
 func elTableColumn(name, value, typeName string, dictType string) string {
-	if typeName == "input" || typeName == "textarea" || typeName == "select" || typeName == "radio" || typeName == "checkbox" || typeName == "time" {
+	if typeName == "input" || typeName == "textarea" || typeName == "select" || typeName == "radio" ||
+		typeName == "checkbox" || typeName == "time" || typeName == "text" {
 		if dictType == "" {
 			return "\n        <el-table-column label=\"" + name + "\" align=\"center\" prop=\"" + value + "\"/>"
 		} else {
@@ -357,11 +368,11 @@ func elTableColumn(name, value, typeName string, dictType string) string {
 	if typeName == "image" {
 		return "\n      <el-table-column label=\"" + name + "\" align=\"center\" prop=\"" + value + "\">" +
 			"\n        <template #default=\"scope\">" +
-			"\n          <el-image src=" + value + "></el-image>" +
+			"\n          <el-image  :src=\"scope.row." + value + "\"></el-image>" +
 			"\n        </template>" +
 			"\n      </el-table-column>"
 	}
-	return ""
+	return "\n        <el-table-column label=\"" + name + "\" align=\"center\" prop=\"" + value + "\"/>"
 }
 
 func elDialog(name, value, typeName, dictType string) string {
@@ -379,8 +390,7 @@ func elDialog(name, value, typeName, dictType string) string {
 			"\n          <el-input v-model=\"dialogForm." + value + "\" placeholder=\"请输入" + name + "\" type=\"textarea\"/>" +
 			"\n        </el-form-item>"
 	} else if typeName == "select" {
-		return "" +
-			"\n        <el-form-item label=\"" + name + "\" prop=\"" + value + "\">" +
+		return "\n        <el-form-item label=\"" + name + "\" prop=\"" + value + "\">" +
 			"\n          <el-select v-model=\"dialogForm." + value + "\" placeholder=\"请选择" + name + "\">" +
 			"\n            <el-option" +
 			"\n              v-for=\"item in store.state.dicts.get('" + dictType + "')\"" +
@@ -421,7 +431,7 @@ func elDialog(name, value, typeName, dictType string) string {
 			"\n        </el-form-item>"
 	} else if typeName == "time" {
 		return "\n        <el-form-item label=\"" + name + "\" prop=\"" + value + "\">" +
-			"      <el-date-picker v-model=\"dialogForm." + value + "\" type=\"datetime\" placeholder=\"请选择时间\"\n/>" +
+			"      <el-date-picker v-model=\"dialogForm." + value + "\" type=\"datetime\" placeholder=\"请选择时间\" value-format=\"YYYY-MM-DD hh:mm:ss\"/>" +
 			"\n        </el-form-item>"
 	} else if typeName == "image" {
 		return "\n        <el-form-item label=\"" + name + "\" prop=\"" + value + "\">" +
@@ -434,7 +444,13 @@ func elDialog(name, value, typeName, dictType string) string {
 		return "\n        <el-form-item label=\"" + name + "\" prop=\"" + value + "\">" +
 			"              <EditorComponent v-model:htmlString=\"dialogForm." + value + "\"/>" +
 			"\n        </el-form-item>"
-
+	} else if typeName == "text" {
+		return "\n        <el-form-item label=\"" + name + "\" prop=\"" + value + "\">" +
+			"                <el-text class=\"mx-1\">{{dialogForm." + value + "}}</el-text>" +
+			"\n        </el-form-item>"
 	}
-	return ""
+	//默认的就显示文本
+	return "\n        <el-form-item label=\"" + name + "\" prop=\"" + value + "\">" +
+		"                <el-text class=\"mx-1\">{{dialogForm." + value + "}}</el-text>" +
+		"\n        </el-form-item>"
 }

+ 7 - 6
util/ProjectUrl.go

@@ -1,17 +1,18 @@
 package util
 
-const projectGoUrl = "E:\\project\\kkc\\kkc-go\\"
-const projectVueUrl = "E:\\project\\kkc\\kkc-shop\\"
+const ProjectGoUrl = "D:\\project\\study\\kkc\\kkc-go\\"
+const ProjectVueUrl = "D:\\project\\study\\kkc\\kkc-shop\\"
+const ProjectGoCreateUrl = "D:\\project\\study\\kkc\\go-xorm-create\\go-xorm-create\\"
 
 func GetDomainUrl() string {
-	return projectGoUrl + "data\\domain\\DoMain.go"
+	return ProjectGoUrl + "data\\domain\\DoMain.go"
 }
 func GetDaoUrl() string {
-	return projectGoUrl + "data\\dao\\manage\\"
+	return ProjectGoUrl + "data\\dao\\manage\\"
 }
 func GetRouterUrl() string {
-	return projectGoUrl + "router\\BaseBackRouter.go"
+	return ProjectGoUrl + "router\\BaseBackRouter.go"
 }
 func GetVueApiUrl() string {
-	return projectVueUrl + "src\\api\\back\\BaseBackApi.ts"
+	return ProjectVueUrl + "src\\api\\back\\BaseBackApi.ts"
 }