diff --git a/code.go b/code.go index 517acf0..48b6f8e 100644 --- a/code.go +++ b/code.go @@ -2,6 +2,8 @@ package hotime import ( . "code.hoteas.com/golang/hotime/common" + "fmt" + "github.com/360EntSecGroup-Skylar/excelize" "io" "io/ioutil" "os" @@ -261,6 +263,12 @@ var TptProject = Proj{ } + if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"download"`) && + strings.Contains(v1.GetString("auth"), `"download"`) { + auth = append(auth, "download") + + } + if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"add"`) && strings.Contains(v1.GetString("auth"), `"add"`) { auth = append(auth, "add") @@ -315,7 +323,11 @@ var TptProject = Proj{ auth = append(auth, "show") } + if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"download"`) && + strings.Contains(v1.GetString("auth"), `"download"`) { + auth = append(auth, "download") + } if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"add"`) && strings.Contains(v2.GetString("auth"), `"add"`) { auth = append(auth, "add") @@ -516,7 +528,11 @@ var TptProject = Proj{ auth = append(auth, "show") } + if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"download"`) && + strings.Contains(v1.GetString("auth"), `"download"`) { + auth = append(auth, "download") + } if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"add"`) && strings.Contains(v1.GetString("auth"), `"add"`) { auth = append(auth, "add") @@ -571,7 +587,11 @@ var TptProject = Proj{ auth = append(auth, "show") } + if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"download"`) && + strings.Contains(v1.GetString("auth"), `"download"`) { + auth = append(auth, "download") + } if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"add"`) && strings.Contains(v2.GetString("auth"), `"add"`) { auth = append(auth, "add") @@ -657,7 +677,6 @@ var TptProject = Proj{ } that.Display(0, "删除成功") }, - "search": func(that *Context) { hotimeName := that.RouterString[0] fileConfig := that.MakeCodeRouter[hotimeName].FileConfig @@ -669,6 +688,8 @@ var TptProject = Proj{ page := ObjToInt(that.Req.FormValue("page")) pageSize := ObjToInt(that.Req.FormValue("pageSize")) + download := ObjToInt(that.Req.FormValue("download")) + if page < 1 { page = 1 } @@ -677,23 +698,39 @@ var TptProject = Proj{ pageSize = 20 } - count := that.Db.Count(tableName, leftJoin, where) - reData := that.Db.Page(page, pageSize). - PageSelect(tableName, leftJoin, columnStr, where) + var count int + var reData []Map + //是否下载 + if download == 0 { + count = that.Db.Count(tableName, leftJoin, where) + reData = that.Db.Page(page, pageSize). + PageSelect(tableName, leftJoin, columnStr, where) + } + if download == 1 { + reData = that.Db.Select(tableName, leftJoin, columnStr, where) + } for _, v := range reData { v.RangeSort(func(k string, v1 interface{}) (isEnd bool) { - //如果有table字段则代为link if v["table"] != nil && v["table_id"] != nil { id := v.GetCeilInt64("table_id") tableName := that.MakeCodeRouter[hotimeName].TableConfig.GetMap(v.GetString("table")).GetString("label") v["table_table_name"] = strings.Replace(tableName, "管理", "", -1) - parentC := that.Db.Get(v.GetString("table"), "name", Map{"id": id}) + + sname := "name" + if that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("table")][sname] == nil { + sname = "title" + if that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("table")][sname] == nil { + sname = "id" + } + } + + parentC := that.Db.Get(v.GetString("table"), sname, Map{"id": id}) v["table_table_id_name"] = "" if parentC != nil { - v["table_table_id_name"] = parentC.GetString("name") + v["table_table_id_name"] = parentC.GetString(sname) } } @@ -717,6 +754,93 @@ var TptProject = Proj{ }) } + if download == 1 { + + tableNameLabel := that.MakeCodeRouter[hotimeName].TableConfig.GetMap(tableName).GetString("label") + f := excelize.NewFile() + // 创建一个工作表 + f.NewSheet(tableNameLabel) + + f.DeleteSheet("Sheet1") + columns := that.MakeCodeRouter[hotimeName].TableConfig.GetMap(tableName).GetSlice("columns") + //单列 + n := 0 + for k, _ := range columns { + v := columns.GetMap(k) + if v["list"] != nil && v.GetBool("list") == false { + continue + } + n++ + //第一个要加状态行 + //单行 + for k1, v1 := range reData { + if k1 == 0 { + f.SetCellValue(tableNameLabel, convertToTitle(n)+"1", v.GetString("label")) + } + + if v.GetString("link") != "" { + f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), v1.GetString(v.GetString("link")+"_"+v.GetString("name")+"_"+v.GetString("value"))) + continue + } + + if v.GetString("name") == "table" { + f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), that.MakeCodeRouter[hotimeName].TableConfig.GetMap(v1.GetString("table")).GetString("label")) + continue + } + + if v.GetString("name") == "table_id" { + f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), v1.GetString("table_"+v.GetString("name")+"_name")) + continue + } + + //select类型 + options := v.GetSlice("options") + if len(options) != 0 { + isEnd := false + for ok, _ := range options { + ov := options.GetMap(ok) + if ov.GetString("value") == v1.GetString(v.GetString("name")) { + f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), ov.GetString("name")) + isEnd = true + break + } + + } + if isEnd { + continue + } + + } + + f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), v1.GetString(v.GetString("name"))) + } + } + filePath := that.Config.GetString("filePath") + if filePath == "" { + filePath = "/file/temp/" + } + + //path := time.Now().Format(filePath) + e := os.MkdirAll(that.Config.GetString("tpt")+filePath, os.ModeDir) + if e != nil { + that.Display(3, e) + return + } + filePath = filePath + tableName + time.Now().Format("2006-01-02-15-04-05") + ".xlsx" + + // 根据指定路径保存文件 + if err := f.SaveAs(that.Config.GetString("tpt") + filePath); err != nil { + fmt.Println(err) + that.Display(4, "输出异常") + return + } + f.Save() + //that.Resp.Header().Set("Location", filePath) + //that.Resp.WriteHeader(307) //关键在这里! + that.Display(0, filePath) + return + } + that.Display(0, Map{"count": count, "data": reData}) }, }, @@ -1030,3 +1154,20 @@ var TptProject = Proj{ }, }, } + +func convertToTitle(columnNumber int) string { + var res []byte + for columnNumber > 0 { + a := columnNumber % 26 + if a == 0 { + a = 26 + } + res = append(res, 'A'+byte(a-1)) + columnNumber = (columnNumber - a) / 26 + } + // 上面输出的res是反着的,前后交换 + for i, n := 0, len(res); i < n/2; i++ { + res[i], res[n-1-i] = res[n-1-i], res[i] + } + return string(res) +} diff --git a/code/makecode.go b/code/makecode.go index 13d3656..1609e30 100644 --- a/code/makecode.go +++ b/code/makecode.go @@ -176,7 +176,7 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { that.TableConfig[v.GetString("name")] = Map{ "label": v.GetString("label"), "table": v.GetString("name"), - "auth": []string{"show", "add", "delete", "edit", "info"}, + "auth": []string{"show", "add", "delete", "edit", "info", "download"}, "columns": []Map{}, "search": []Map{ diff --git a/db/hotimedb.go b/db/hotimedb.go index 6001945..4ebbb33 100644 --- a/db/hotimedb.go +++ b/db/hotimedb.go @@ -6,6 +6,7 @@ import ( "database/sql" "encoding/json" "errors" + "fmt" _ "github.com/go-sql-driver/mysql" _ "github.com/mattn/go-sqlite3" "github.com/sirupsen/logrus" @@ -618,8 +619,8 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) { if v != nil && reflect.ValueOf(v).Type().String() == "common.Slice" && len(v.(Slice)) == 0 { continue } - - if v != nil && reflect.ValueOf(v).Type().String() == "[]interface {}" && len(v.([]interface{})) == 0 { + fmt.Println(reflect.ValueOf(v).Type().String()) + if v != nil && strings.Contains(reflect.ValueOf(v).Type().String(), "[]") && len(ObjToSlice(v)) == 0 { continue } diff --git a/example/app/article.go b/example/app/article.go index 0d29e40..5731087 100644 --- a/example/app/article.go +++ b/example/app/article.go @@ -58,7 +58,7 @@ var ArticleCtr = Ctr{ sort := that.Req.FormValue("sort") - where := Map{"article.push_time[<=]": time.Now().Format("2006-01-02 15:04"), "article.state": 0} + where := Map{"article.push_time[<]": time.Now().Format("2006-01-02 15:04"), "article.state": 0} if sn != "" { ctg := that.Db.Get("ctg", "id", Map{"sn": sn}) if ctg != nil { @@ -66,10 +66,20 @@ var ArticleCtr = Ctr{ } } + startTime := that.Req.FormValue("start_time") //ctgsn + finishTime := that.Req.FormValue("finish_time") //ctgsn + if lunbo != 0 { where["article.lunbo"] = lunbo } + if len(startTime) > 5 { + where["article.push_time[>=]"] = startTime + } + if len(finishTime) > 5 { + where["article.push_time[<=]"] = finishTime + } + if keywords != "" { where["OR"] = Map{"article.title[~]": keywords, "article.description[~]": keywords, "article.author[~]": keywords, "article.sn[~]": keywords, "article.origin[~]": keywords, "article.url[~]": keywords} } diff --git a/example/app/mail.go b/example/app/mail.go index 91d1d10..a7b9502 100644 --- a/example/app/mail.go +++ b/example/app/mail.go @@ -70,7 +70,7 @@ var MailCtr = Ctr{ //sort:=that.Req.FormValue("sort") - where := Map{"state": 0} + where := Map{"state": 0, "show": 1} //if keywords!=""{ // where["OR"]=Map{"title[~]":keywords,"description[~]":keywords,"author[~]":keywords,"sn[~]":keywords,"origin[~]":keywords,"url[~]":keywords} diff --git a/example/main.go b/example/main.go index 7f4f6b1..1d05c33 100644 --- a/example/main.go +++ b/example/main.go @@ -8,6 +8,10 @@ import ( func main() { appIns := Init("config/config.json") + appIns.SetConnectListener(func(that *Context) (isFinished bool) { + + return isFinished + }) appIns.Run(Router{"app": app.AppProj}) }