csv.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/csv"
  5. "fmt"
  6. "go-common/app/admin/main/laser/model"
  7. "strconv"
  8. "time"
  9. )
  10. var (
  11. csvMetaNodes = []model.CsvMetaNode{
  12. {Index: 0, Name: "日期", DataCode: 25},
  13. {Index: 1, Name: "操作人", DataCode: 26},
  14. {Index: 2, Name: "总操视频量", DataCode: model.TotalVideo},
  15. {Index: 3, Name: "总操作次数", DataCode: model.TotalVideoOper},
  16. {Index: 4, Name: "开放浏视频量", DataCode: model.OpenVideo},
  17. {Index: 5, Name: "开放浏览操作次数", DataCode: model.OpenVideoOper},
  18. {Index: 6, Name: "会员可视频量", DataCode: model.VipAccessVideo},
  19. {Index: 7, Name: "会员可见操作次数", DataCode: model.VipAccessVideoOper},
  20. {Index: 8, Name: "打视频量", DataCode: model.RejectVideo},
  21. {Index: 9, Name: "打回操作次数", DataCode: model.RejectVideoOper},
  22. {Index: 10, Name: "锁视频量", DataCode: model.LockVideo},
  23. {Index: 11, Name: "锁定操作次数", DataCode: model.LockVideoOper},
  24. {Index: 12, Name: "通过视频总时长", DataCode: model.PassVideoTotalDuration},
  25. {Index: 13, Name: "未通过视频总时长", DataCode: model.FailVideoTotalDuration},
  26. {Index: 14, Name: "视频提交到进入待审平均响应时间", DataCode: model.WaitAuditAvgTime},
  27. {Index: 15, Name: "视频提交到进入待审时间", DataCode: model.WaitAuditDuration},
  28. {Index: 16, Name: "视频提交到进入待审次数", DataCode: model.WaitAuditOper},
  29. }
  30. )
  31. // FormatCSV format to csv data
  32. func FormatCSV(records [][]string) (data []byte, err error) {
  33. buf := new(bytes.Buffer)
  34. // add utf bom
  35. if len(records) > 0 {
  36. buf.WriteString("\xEF\xBB\xBF")
  37. }
  38. w := csv.NewWriter(buf)
  39. err = w.WriteAll(records)
  40. if err != nil {
  41. return
  42. }
  43. data = buf.Bytes()
  44. return
  45. }
  46. func formatAuditCargo(wrappers []*model.CargoViewWrapper, lineWidth int) (data [][]string) {
  47. size := len(wrappers)
  48. if size <= 0 {
  49. return
  50. }
  51. data = make([][]string, lineWidth+1)
  52. index := 0
  53. data[index] = []string{"username", "审核时间段", "接收量", "完成量"}
  54. for _, v1 := range wrappers {
  55. for k2, v2 := range v1.Data {
  56. data[index+1] = []string{
  57. v1.Username,
  58. fmt.Sprintf("%s %d:00:00", v1.Date, k2),
  59. strconv.FormatInt(v2.ReceiveValue, 10),
  60. strconv.FormatInt(v2.AuditValue, 10),
  61. }
  62. index = index + 1
  63. }
  64. }
  65. return
  66. }
  67. func formatVideoAuditStat(statViewExts []*model.StatViewExt, lineWidth int) (data [][]string) {
  68. if lineWidth <= 0 {
  69. return
  70. }
  71. data = make([][]string, lineWidth+1)
  72. index := 0
  73. rowHeight := len(csvMetaNodes)
  74. titles := make([]string, rowHeight)
  75. cursorMap := make(map[int]int)
  76. for _, v := range csvMetaNodes {
  77. titles[v.Index] = v.Name
  78. cursorMap[v.DataCode] = v.Index
  79. }
  80. data[index] = titles
  81. for _, v1 := range statViewExts {
  82. date := time.Unix(v1.Date, 0).Format("2006-01-02")
  83. for _, v2 := range v1.Wraps {
  84. name := v2.Uname
  85. tempRows := make([]string, rowHeight)
  86. tempRows = append([]string{date, name}, tempRows[0:]...)
  87. for _, v3 := range v2.Stats {
  88. if cursor, ok := cursorMap[v3.DataCode]; ok {
  89. if v3.DataCode == model.WaitAuditAvgTime || v3.DataCode == model.WaitAuditDuration || v3.DataCode == model.PassVideoTotalDuration || v3.DataCode == model.FailVideoTotalDuration {
  90. tempRows[cursor] = fmt.Sprintf("%d:%d:%d", v3.Value/3600, v3.Value%3600/60, v3.Value%3600%60/1)
  91. } else {
  92. tempRows[cursor] = strconv.FormatInt(v3.Value, 10)
  93. }
  94. }
  95. }
  96. data[index+1] = tempRows
  97. index = index + 1
  98. }
  99. }
  100. return
  101. }