|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "goAdmin/connections/mysql" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// 表单列 |
| 10 | +type FormStruct struct { |
| 11 | + Field string |
| 12 | + TypeName string |
| 13 | + Head string |
| 14 | + Default string |
| 15 | + Editable bool |
| 16 | + FormType string |
| 17 | + Value string |
| 18 | +} |
| 19 | + |
| 20 | +// 数据过滤函数 |
| 21 | +type FieldValueFun func(value string) string |
| 22 | + |
| 23 | +// 展示列 |
| 24 | +type FieldStruct struct { |
| 25 | + ExcuFun FieldValueFun |
| 26 | + Field string |
| 27 | + TypeName string |
| 28 | + Head string |
| 29 | +} |
| 30 | + |
| 31 | +func (field *FieldStruct) SetHead(head string) *FieldStruct { |
| 32 | + (*field).Head = head |
| 33 | + return field |
| 34 | +} |
| 35 | + |
| 36 | +func (field *FieldStruct) SetTypeName(typeName string) *FieldStruct { |
| 37 | + (*field).TypeName = typeName |
| 38 | + return field |
| 39 | +} |
| 40 | + |
| 41 | +func (field *FieldStruct) SetField(fieldName string) *FieldStruct { |
| 42 | + (*field).Field = fieldName |
| 43 | + return field |
| 44 | +} |
| 45 | + |
| 46 | +// 展示面板 |
| 47 | +type InfoPanel struct { |
| 48 | + FieldList []FieldStruct |
| 49 | + Table string |
| 50 | + Title string |
| 51 | + Description string |
| 52 | +} |
| 53 | + |
| 54 | +// 表单面板 |
| 55 | +type FormPanel struct { |
| 56 | + FormList []FormStruct |
| 57 | + Table string |
| 58 | + Title string |
| 59 | + Description string |
| 60 | +} |
| 61 | + |
| 62 | +// 一个管理数据模块的抽象表示 |
| 63 | +type GlobalTable struct { |
| 64 | + Info InfoPanel |
| 65 | + Form FormPanel |
| 66 | +} |
| 67 | + |
| 68 | +// 查数据 |
| 69 | +func (tableModel GlobalTable) GetDataFromDatabase(queryParam map[string]string) ([]string, []map[string]string, map[string]interface{}, string, string) { |
| 70 | + |
| 71 | + pageInt, _ := strconv.Atoi(queryParam["page"]) |
| 72 | + |
| 73 | + title := tableModel.Info.Title |
| 74 | + description := tableModel.Info.Description |
| 75 | + |
| 76 | + thead := make([]string, 0) |
| 77 | + fields := "" |
| 78 | + |
| 79 | + for i := 0; i < len(tableModel.Info.FieldList); i++ { |
| 80 | + if tableModel.Info.FieldList[i].Field != "id" { |
| 81 | + fields += tableModel.Info.FieldList[i].Field + "," |
| 82 | + } |
| 83 | + thead = append(thead, tableModel.Info.FieldList[i].Head) |
| 84 | + } |
| 85 | + |
| 86 | + fields += "id" |
| 87 | + |
| 88 | + res, _ := mysql.Query("select "+fields+" from "+tableModel.Info.Table+" where id > 0 order by created_at desc LIMIT ? OFFSET ?", |
| 89 | + queryParam["pageSize"], (pageInt-1)*10) |
| 90 | + |
| 91 | + infoList := make([]map[string]string, 0) |
| 92 | + |
| 93 | + for i := 0; i < len(res); i++ { |
| 94 | + |
| 95 | + // TODO: 加入对象池 |
| 96 | + tempModelData := make(map[string]string, 0) |
| 97 | + |
| 98 | + for j := 0; j < len(tableModel.Info.FieldList); j++ { |
| 99 | + tempModelData[tableModel.Info.FieldList[j].Head] = tableModel.Info.FieldList[j].ExcuFun(GetStringFromType(tableModel.Info.FieldList[j].TypeName, res[i][tableModel.Info.FieldList[j].Field])) |
| 100 | + } |
| 101 | + |
| 102 | + tempModelData["id"] = GetStringFromType("int", res[i]["id"]) |
| 103 | + |
| 104 | + infoList = append(infoList, tempModelData) |
| 105 | + } |
| 106 | + |
| 107 | + total, _ := mysql.Query("select count(*) from "+tableModel.Info.Table+" where id > ?", 0) |
| 108 | + size := int(total[0]["count(*)"].(int64)) |
| 109 | + |
| 110 | + paginator := GetPaginator(queryParam["path"], pageInt, queryParam["page"], queryParam["pageSize"], size, queryParam["prefix"]) |
| 111 | + |
| 112 | + return thead, infoList, paginator, title, description |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +// 查单个数据 |
| 117 | +func (tableModel GlobalTable) GetDataFromDatabaseWithId(prefix string, id string) ([]FormStruct, string, string) { |
| 118 | + |
| 119 | + fields := "" |
| 120 | + |
| 121 | + for i := 0; i < len(tableModel.Form.FormList); i++ { |
| 122 | + fields += tableModel.Form.FormList[i].Field + "," |
| 123 | + } |
| 124 | + |
| 125 | + fields = fields[0 : len(fields)-1] |
| 126 | + |
| 127 | + res, _ := mysql.Query("select "+fields+" from "+tableModel.Form.Table+" where id = ?", id) |
| 128 | + |
| 129 | + for i := 0; i < len(tableModel.Form.FormList); i++ { |
| 130 | + tableModel.Form.FormList[i].Value = GetStringFromType(tableModel.Form.FormList[i].TypeName, res[0][tableModel.Form.FormList[i].Field]) |
| 131 | + } |
| 132 | + |
| 133 | + return tableModel.Form.FormList, tableModel.Form.Title, tableModel.Form.Description |
| 134 | +} |
| 135 | + |
| 136 | +// 改数据 |
| 137 | +func (tableModel GlobalTable) UpdateDataFromDatabase(prefix string, dataList map[string][]string) { |
| 138 | + |
| 139 | + fields := "" |
| 140 | + valueList := make([]interface{}, 0) |
| 141 | + for k, v := range dataList { |
| 142 | + if k != "id" && k != "_previous_" && k != "_method" && k != "_token" { |
| 143 | + fields += k + " = ?," |
| 144 | + valueList = append(valueList, v[0]) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + fields = fields[0 : len(fields)-1] |
| 149 | + valueList = append(valueList, dataList["id"][0]) |
| 150 | + |
| 151 | + mysql.Exec("update "+tableModel.Form.Table+" set "+fields+" where id = ?", valueList...) |
| 152 | +} |
| 153 | + |
| 154 | +// 增数据 |
| 155 | +func (tableModel GlobalTable) InsertDataFromDatabase(prefix string, dataList map[string][]string) { |
| 156 | + |
| 157 | + fields := "" |
| 158 | + queStr := "" |
| 159 | + valueList := make([]interface{}, 0) |
| 160 | + for k, v := range dataList { |
| 161 | + if k != "id" && k != "_previous_" && k != "_method" && k != "_token" { |
| 162 | + fields += k + " = ?," |
| 163 | + queStr += "?," |
| 164 | + valueList = append(valueList, v[0]) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + fields = fields[0 : len(fields)-1] |
| 169 | + queStr = queStr[0 : len(queStr)-1] |
| 170 | + valueList = append(valueList, dataList["id"][0]) |
| 171 | + |
| 172 | + mysql.Exec("insert into "+tableModel.Form.Table+"("+fields+") values ("+queStr+")", valueList...) |
| 173 | +} |
| 174 | + |
| 175 | +// 删数据 |
| 176 | +func (tableModel GlobalTable) DeleteDataFromDatabase(prefix string, id string) { |
| 177 | + mysql.Exec("delete from "+tableModel.Form.Table+" where id = ?", id) |
| 178 | +} |
| 179 | + |
| 180 | +func GetStringFromType(typeName string, value interface{}) string { |
| 181 | + typeName = strings.ToUpper(typeName) |
| 182 | + if value == nil { |
| 183 | + return "" |
| 184 | + } |
| 185 | + switch typeName { |
| 186 | + case "INT": |
| 187 | + return strconv.FormatInt(value.(int64), 10) |
| 188 | + case "TINYINT": |
| 189 | + return strconv.FormatInt(value.(int64), 10) |
| 190 | + case "MEDIUMINT": |
| 191 | + return strconv.FormatInt(value.(int64), 10) |
| 192 | + case "SMALLINT": |
| 193 | + return strconv.FormatInt(value.(int64), 10) |
| 194 | + case "BIGINT": |
| 195 | + return strconv.FormatInt(value.(int64), 10) |
| 196 | + case "FLOAT": |
| 197 | + return strconv.FormatFloat(value.(float64), 'g', 5, 32) |
| 198 | + case "DOUBLE": |
| 199 | + return strconv.FormatFloat(value.(float64), 'g', 5, 32) |
| 200 | + case "DECIMAL": |
| 201 | + return string(value.(uint8)) |
| 202 | + case "DATE": |
| 203 | + return value.(string) |
| 204 | + case "TIME": |
| 205 | + return value.(string) |
| 206 | + case "YEAR": |
| 207 | + return value.(string) |
| 208 | + case "DATETIME": |
| 209 | + return value.(string) |
| 210 | + case "TIMESTAMP": |
| 211 | + return value.(string) |
| 212 | + case "VARCHAR": |
| 213 | + return value.(string) |
| 214 | + case "MEDIUMTEXT": |
| 215 | + return value.(string) |
| 216 | + case "LONGTEXT": |
| 217 | + return value.(string) |
| 218 | + case "TINYTEXT": |
| 219 | + return value.(string) |
| 220 | + case "TEXT": |
| 221 | + return value.(string) |
| 222 | + default: |
| 223 | + return "" |
| 224 | + } |
| 225 | +} |
0 commit comments