export.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package http
  2. import (
  3. "bytes"
  4. "encoding/csv"
  5. "strconv"
  6. "go-common/app/admin/main/creative/model/whitelist"
  7. "go-common/library/log"
  8. )
  9. // FormatCSV format csv data.
  10. func FormatCSV(records [][]string) (res []byte) {
  11. buf := new(bytes.Buffer)
  12. w := csv.NewWriter(buf)
  13. for _, record := range records {
  14. if err := w.Write(record); err != nil {
  15. log.Error("error writing record to csv:", err)
  16. return
  17. }
  18. }
  19. w.Flush()
  20. res = buf.Bytes()
  21. return
  22. }
  23. func formatWhilteList(wl []*whitelist.Whitelist) (data [][]string, err error) {
  24. if len(wl) < 0 {
  25. return
  26. }
  27. data = append(data, []string{"MID", "昵称", "AdminMID", "备注", "粉丝数", "等级", "创建时间"})
  28. for _, v := range wl {
  29. var fields []string
  30. fields = append(fields, strconv.FormatInt(int64(v.MID), 10))
  31. fields = append(fields, v.Name)
  32. fields = append(fields, strconv.FormatInt(int64(v.AdminMID), 10))
  33. fields = append(fields, v.Comment)
  34. fields = append(fields, strconv.FormatInt(int64(v.Fans), 10))
  35. fields = append(fields, strconv.FormatInt(int64(v.CurrentLevel), 10))
  36. fields = append(fields, v.Ctime)
  37. data = append(data, fields)
  38. }
  39. return
  40. }