diff --git a/QMPlusServer/model/autoCodeModel/autoCode.go b/QMPlusServer/model/autoCodeModel/autoCode.go index 7d4f0ff566476fd46d363dce8fab0370718eb24e..dd2cf8d773b319c7c58def23a2c8f4c6aa7ae03e 100644 --- a/QMPlusServer/model/autoCodeModel/autoCode.go +++ b/QMPlusServer/model/autoCodeModel/autoCode.go @@ -1,14 +1,24 @@ package autoCodeModel +import ( + "fmt" + log "gin-vue-admin/init/initlog" + "html/template" + "os" +) + //开发中功能,若您发现这块代码可以研究,可以无视 type AutoCodeStruct struct { - StructName string `json:"structName"` - Components []Component `json:"components"` + StructName string `json:"structName"` + PackageName string `json:"packageName"` + Abbreviation string `json:"abbreviation"` + Components []Component `json:"components"` } type Component struct { ComponentName string `json:"componentName"` ComponentType string `json:"componentType"` + ComponentJson string `json:"componentJson"` Ismultiple bool `json:"isMultiple"` ComponentShowType string `json:"componentShowType"` NideDictionary bool `json:"nideDictionary"` @@ -20,3 +30,66 @@ type Dictionary struct { Label string `json:"label"` Value string `json:"value"` } + +func Temp() { + tmpl, err := template.ParseFiles("../../tpl/te/struct.go.tpl") + fmt.Println(tmpl, err) + a1 := Component{ + ComponentName: "TestComponent", + ComponentType: "string", + ComponentJson: "testComponent", + Ismultiple: false, + ComponentShowType: "", + NideDictionary: false, + DictionaryName: "", + ComponentDictionary: nil, + } + a2 := Component{ + ComponentName: "TestBigComponent", + ComponentType: "int", + ComponentJson: "testBigComponent", + Ismultiple: false, + ComponentShowType: "", + NideDictionary: false, + DictionaryName: "", + ComponentDictionary: nil, + } + a := AutoCodeStruct{ + StructName: "Test", + PackageName: "autocode", + Components: []Component{a1, a2}, + } + + _dir := "../" + a.PackageName + exist, err := pathExists(_dir) + if err != nil { + log.L.Info("get dir error![%v]\n", err) + return + } + if exist { + log.L.Info("has dir![%v]\n", _dir) + } else { + log.L.Info("no dir![%v]\n", _dir) + // 创建文件夹 + err := os.Mkdir(_dir, os.ModePerm) + if err != nil { + log.L.Error("mkdir failed![%v]\n", err) + } else { + log.L.Info("mkdir success!\n") + } + } + file, err := os.OpenFile("../"+a.PackageName+"/struct.go", os.O_CREATE|os.O_WRONLY, 0755) + err = tmpl.Execute(file, a) +} + +// 判断文件夹是否存在 +func pathExists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} diff --git a/QMPlusServer/model/autoCodeModel/autoCode_test.go b/QMPlusServer/model/autoCodeModel/autoCode_test.go new file mode 100644 index 0000000000000000000000000000000000000000..8423e68f6914b8634bdc01b30986344d2d999b8e --- /dev/null +++ b/QMPlusServer/model/autoCodeModel/autoCode_test.go @@ -0,0 +1,7 @@ +package autoCodeModel + +import "testing" + +func TestTemp(t *testing.T) { + Temp() +} diff --git a/QMPlusServer/tpl/te/struct.go.tpl b/QMPlusServer/tpl/te/struct.go.tpl new file mode 100644 index 0000000000000000000000000000000000000000..290141009d05ab7745eab60c25b15dfa57b8be96 --- /dev/null +++ b/QMPlusServer/tpl/te/struct.go.tpl @@ -0,0 +1,36 @@ +// 自动生成模板{{.StructName}} +package {{.PackageName}} + +import ( + "gin-vue-admin/controller/servers" + "gin-vue-admin/init/qmsql" + "gin-vue-admin/model/modelInterface" + "github.com/jinzhu/gorm" + "github.com/pkg/errors" +) + +type {{.StructName}} struct { + gorm.Model {{range .Components}} + {{.ComponentName}} {{.ComponentType}} `json:"{{.ComponentJson}}"` {{ end }} +} + +// 创建{{.StructName}} +func ({{.Abbreviation}} *{{StructName}})Create{{.StructName}}()(err error){ + err = qmsql.DEFAULTDB.Create({{.Abbreviation}}).Error + return err +} + +// 删除{{.StructName}} +func ({{.Abbreviation}} *{{StructName}})Delete{{.StructName}}()(err error){ + err = qmsql.DEFAULTDB.Delete({{.Abbreviation}}).Error + return err +} + +// 更新{{.StructName}} +func ({{.Abbreviation}} *{{StructName}})Update{{.StructName}}()(err error, re{{.Abbreviation}} {{.StructName}}){ + +} + +func ({{.Abbreviation}} *{{StructName}})Create(){ + +} \ No newline at end of file diff --git a/QMPlusVuePage/src/view/superAdmin/autoCode/component/componentDialog.vue b/QMPlusVuePage/src/view/superAdmin/autoCode/component/componentDialog.vue index be83695ca00f37075e8009c267d3c56045e9260a..efd4502ff7990ed1c2c5b164ef6ff0aeee589fa5 100644 --- a/QMPlusVuePage/src/view/superAdmin/autoCode/component/componentDialog.vue +++ b/QMPlusVuePage/src/view/superAdmin/autoCode/component/componentDialog.vue @@ -4,6 +4,9 @@ + + + + + +