format.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/csv"
  5. "fmt"
  6. "os"
  7. "strconv"
  8. "go-common/app/job/main/growup/model"
  9. )
  10. // WriteCSV write data to csv
  11. func WriteCSV(records [][]string, filename string) (err error) {
  12. f, err := os.Create(filename)
  13. if err != nil {
  14. return
  15. }
  16. defer f.Close()
  17. w := csv.NewWriter(f)
  18. w.WriteAll(records)
  19. w.Flush()
  20. return
  21. }
  22. // FormatCSV format to csv data
  23. func FormatCSV(records [][]string) (data []byte, err error) {
  24. buf := new(bytes.Buffer)
  25. // add utf bom
  26. buf.WriteString("\xEF\xBB\xBF")
  27. w := csv.NewWriter(buf)
  28. err = w.WriteAll(records)
  29. if err != nil {
  30. return
  31. }
  32. data = buf.Bytes()
  33. return
  34. }
  35. func formatAvIncome(list []*model.AvIncome) (data [][]string) {
  36. if len(list) <= 0 {
  37. return
  38. }
  39. data = make([][]string, len(list)+1)
  40. data[0] = []string{"稿件id", "UP主UID", "稿件月收入", "累计收入", "分区id", "最后收入时间"}
  41. for i := 0; i < len(list); i++ {
  42. l := list[i]
  43. data[i+1] = []string{
  44. strconv.FormatInt(l.AvID, 10),
  45. strconv.FormatInt(l.MID, 10),
  46. fmt.Sprintf("%.2f", float64(l.Income)/float64(100)),
  47. fmt.Sprintf("%.2f", float64(l.TotalIncome)/float64(100)),
  48. strconv.FormatInt(l.TagID, 10),
  49. l.Date.Time().Format(_layout),
  50. }
  51. }
  52. return
  53. }
  54. func formatUpAccount(list []*model.UpAccount, month int) (data [][]string) {
  55. if len(list) <= 0 {
  56. return
  57. }
  58. data = make([][]string, len(list)+1)
  59. data[0] = []string{"UP主UID", "昵称", fmt.Sprintf("%d月有收入稿件数", month), fmt.Sprintf("%d月收入", month), "累计收入", "待结算收入", fmt.Sprintf("%d月收入-待结算", month)}
  60. for i := 0; i < len(list); i++ {
  61. l := list[i]
  62. data[i+1] = []string{
  63. strconv.FormatInt(l.MID, 10),
  64. l.Nickname,
  65. strconv.FormatInt(l.AvCount, 10),
  66. fmt.Sprintf("%.2f", float64(l.MonthIncome)/float64(100)),
  67. fmt.Sprintf("%.2f", float64(l.TotalIncome)/float64(100)),
  68. fmt.Sprintf("%.2f", float64(l.TotalUnwithdrawIncome)/float64(100)),
  69. fmt.Sprintf("%.2f", float64(l.MonthIncome-l.TotalUnwithdrawIncome)/float64(100)),
  70. }
  71. }
  72. return
  73. }