csv.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package http
  2. import (
  3. "bytes"
  4. "encoding/csv"
  5. "fmt"
  6. "net/http"
  7. "go-common/app/admin/main/aegis/model"
  8. "go-common/library/log"
  9. "github.com/pkg/errors"
  10. )
  11. // CSVContentType
  12. var (
  13. CSVContentType = []string{"application/csv"}
  14. _ Render = CSV{}
  15. )
  16. // Render http reponse render.
  17. type Render interface {
  18. Render(http.ResponseWriter) error
  19. WriteContentType(w http.ResponseWriter)
  20. }
  21. // CSV str.
  22. type CSV struct {
  23. Content []byte
  24. Title string
  25. }
  26. // WriteContentType fn
  27. func (j CSV) WriteContentType(w http.ResponseWriter) {
  28. writeContentType(w, CSVContentType, j.Title, "csv")
  29. }
  30. func writeContentType(w http.ResponseWriter, value []string, title, ext string) {
  31. header := w.Header()
  32. if val := header["Content-Type"]; len(val) == 0 {
  33. header["Content-Type"] = value
  34. }
  35. header["Content-Disposition"] = append(header["Content-Disposition"], fmt.Sprintf("attachment; filename=%s.%s", title, ext))
  36. }
  37. // Render (JSON) writes data with json ContentType.
  38. func (j CSV) Render(w http.ResponseWriter) (err error) {
  39. if _, err = w.Write(j.Content); err != nil {
  40. err = errors.WithStack(err)
  41. }
  42. return
  43. }
  44. // FormatCSV format csv data.
  45. func FormatCSV(records [][]string) (res []byte) {
  46. buf := new(bytes.Buffer)
  47. w := csv.NewWriter(buf)
  48. for _, record := range records {
  49. if err := w.Write(record); err != nil {
  50. log.Error("error writing record to csv:", err)
  51. return
  52. }
  53. }
  54. w.Flush()
  55. res = buf.Bytes()
  56. return
  57. }
  58. func formatReport(wl map[string][]*model.ReportFlowItem) (data [][]string, err error) {
  59. if len(wl) < 0 {
  60. return
  61. }
  62. tab := []string{"审核员"}
  63. for k, v := range wl {
  64. var fields []string
  65. fields = append(fields, k)
  66. for _, item := range v {
  67. fields = append(fields, fmt.Sprintf("%d/%d", item.Out, item.In))
  68. if len(tab) != len(v)+1 {
  69. tab = append(tab, item.Hour)
  70. }
  71. }
  72. data = append(data, fields)
  73. }
  74. data = append([][]string{tab}, data...)
  75. return
  76. }
  77. func formatReportTaskSubmit(res *model.ReportSubmitRes) (data [][]string, err error) {
  78. if res == nil || len(res.Order) <= 0 || len(res.Header) <= 0 || len(res.Rows) <= 0 {
  79. return
  80. }
  81. llen := len(res.Order)
  82. header := make([]string, llen)
  83. for i, k := range res.Order {
  84. header[i] = res.Header[k]
  85. }
  86. data = [][]string{header}
  87. for _, list := range res.Rows {
  88. for _, one := range list {
  89. fields := make([]string, llen)
  90. for i, k := range res.Order {
  91. fields[i] = one[k]
  92. }
  93. data = append(data, fields)
  94. }
  95. }
  96. return
  97. }