|
1 | 1 | package service |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "gin-vue-admin/global" |
5 | 6 | "gin-vue-admin/model" |
6 | 7 | "gin-vue-admin/model/request" |
7 | 8 | "gin-vue-admin/utils" |
8 | 9 | "io/ioutil" |
9 | 10 | "os" |
| 11 | + "path/filepath" |
10 | 12 | "strings" |
11 | 13 | "text/template" |
12 | 14 | ) |
13 | 15 |
|
14 | 16 | type tplData struct { |
15 | | - template *template.Template |
16 | | - locationPath string |
17 | | - autoCodePath string |
| 17 | + template *template.Template |
| 18 | + locationPath string |
| 19 | + autoCodePath string |
| 20 | + autoMoveFilePath string |
18 | 21 | } |
19 | 22 |
|
20 | 23 | // @title CreateTemp |
@@ -88,23 +91,24 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) { |
88 | 91 | _ = f.Close() |
89 | 92 | } |
90 | 93 |
|
91 | | - defer func() { |
92 | | - // 移除中间文件 |
| 94 | + defer func() { // 移除中间文件 |
93 | 95 | if err := os.RemoveAll(autoPath); err != nil { |
94 | 96 | return |
95 | 97 | } |
96 | 98 | }() |
97 | | - if autoCode.AutoMoveFile { |
98 | | - // 判断是否需要自动转移 |
99 | | - for _, value := range dataList { |
100 | | - // 转移 |
101 | | - err := utils.FileMove(value.locationPath, value.autoCodePath) |
| 99 | + if autoCode.AutoMoveFile { // 判断是否需要自动转移 |
| 100 | + for index, _ := range dataList { |
| 101 | + addAutoMoveFile(&dataList[index]) |
| 102 | + } |
| 103 | + for _, value := range dataList { // 移动文件 |
| 104 | + err := utils.FileMove(value.autoCodePath, value.autoMoveFilePath) |
102 | 105 | if err != nil { |
| 106 | + fmt.Println(err) |
103 | 107 | return err |
104 | 108 | } |
105 | 109 | } |
106 | | - } else { |
107 | | - // 打包 |
| 110 | + return |
| 111 | + } else { // 打包 |
108 | 112 | if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil { |
109 | 113 | return err |
110 | 114 | } |
@@ -144,3 +148,35 @@ func GetColumn(tableName string, dbName string) (err error, Columns []request.Co |
144 | 148 | err = global.GVA_DB.Raw("SELECT COLUMN_NAME column_name,DATA_TYPE data_type,CASE DATA_TYPE WHEN 'longtext' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'varchar' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'double' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'decimal' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'int' THEN c.NUMERIC_PRECISION WHEN 'bigint' THEN c.NUMERIC_PRECISION ELSE '' END AS data_type_long,COLUMN_COMMENT column_comment FROM INFORMATION_SCHEMA.COLUMNS c WHERE table_name = ? AND table_schema = ?", tableName, dbName).Scan(&Columns).Error |
145 | 149 | return err, Columns |
146 | 150 | } |
| 151 | + |
| 152 | +func addAutoMoveFile(data *tplData) { |
| 153 | + if strings.Contains(data.autoCodePath, "server") { |
| 154 | + if strings.Contains(data.autoCodePath, "router") { |
| 155 | + apiList := strings.Split(data.autoCodePath, "/") |
| 156 | + data.autoMoveFilePath = filepath.Join(apiList[len(apiList)-2], apiList[len(apiList)-1]) |
| 157 | + } else if strings.Contains(data.autoCodePath, "api") { |
| 158 | + apiList := strings.Split(data.autoCodePath, "/") |
| 159 | + data.autoMoveFilePath = filepath.Join(apiList[len(apiList)-2], "v1", apiList[len(apiList)-1]) |
| 160 | + } else if strings.Contains(data.autoCodePath, "service") { |
| 161 | + serviceList := strings.Split(data.autoCodePath, "/") |
| 162 | + data.autoMoveFilePath = filepath.Join(serviceList[len(serviceList)-2], serviceList[len(serviceList)-1]) |
| 163 | + } else if strings.Contains(data.autoCodePath, "model") { |
| 164 | + modelList := strings.Split(data.autoCodePath, "/") |
| 165 | + data.autoMoveFilePath = filepath.Join(modelList[len(modelList)-2], modelList[len(modelList)-1]) |
| 166 | + } else if strings.Contains(data.autoCodePath, "request") { |
| 167 | + requestList := strings.Split(data.autoCodePath, "/") |
| 168 | + data.autoMoveFilePath = filepath.Join("model", requestList[len(requestList)-2], requestList[len(requestList)-1]) |
| 169 | + } |
| 170 | + } else if strings.Contains(data.autoCodePath, "web") { |
| 171 | + if strings.Contains(data.autoCodePath, "js") { |
| 172 | + jsList := strings.Split(data.autoCodePath, "/") |
| 173 | + data.autoMoveFilePath = filepath.Join("../", "web", "src", jsList[len(jsList)-2], jsList[len(jsList)-1]) |
| 174 | + } else if strings.Contains(data.autoCodePath, "form") { |
| 175 | + formList := strings.Split(data.autoCodePath, "/") |
| 176 | + data.autoMoveFilePath = filepath.Join("../", "web", "view", formList[len(formList)-3], strings.Split(formList[len(formList)-1], ".")[0]+"From.vue") |
| 177 | + } else if strings.Contains(data.autoCodePath, "form") { |
| 178 | + vueList := strings.Split(data.autoCodePath, "/") |
| 179 | + data.autoMoveFilePath = filepath.Join("../", "web", "view", vueList[len(vueList)-3], vueList[len(vueList)-1]) |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments