package service import ( "context" "encoding/csv" "fmt" "io" "os" "sort" "strconv" "strings" "time" "go-common/app/job/main/growup/model" "go-common/library/log" ) const ( _topTen = "%d年%d月%d日UP主,稿件收入top 10 统计" ) func (s *Service) execSendTopTen(c context.Context, date time.Time) (body string, err error) { var ups []*model.MIDInfo ups, err = s.getTopTenUps(c, date) if err != nil { log.Error("s.execSendTopTen s.getTopTenUps error(%v)", err) return } avs, err := s.getTopTenAVs(c, date) if err != nil { log.Error("s.execSendTopTen s.getTopTenAVs error(%v)", err) return } data := [][]string{ {fmt.Sprintf(_topTen, date.Year(), date.Month(), date.Day())}, {"日期", "名次", "UID", "昵称", "新增收入(元)", "累计收入(元)"}, } for i, up := range ups { a := []string{ fmt.Sprintf("%d-%d-%d", date.Year(), date.Month(), date.Day()), strconv.Itoa(i + 1), strconv.FormatInt(up.MID, 10), up.NickName, strconv.FormatFloat(float64(up.Income)/100, 'f', 2, 32), strconv.FormatFloat(float64(up.TotalIncome)/100, 'f', 2, 32), } data = append(data, a) } t := []string{"日期", "名次", "avid", "UID", "昵称", "新增收入(元)", "累计收入(元)"} data = append(data, t) for i, av := range avs { a := []string{ fmt.Sprintf("%d-%d-%d", date.Year(), date.Month(), date.Day()), strconv.Itoa(i + 1), strconv.FormatInt(av.AVID, 10), strconv.FormatInt(av.MID, 10), av.NickName, strconv.FormatFloat(float64(av.Income)/100, 'f', 2, 32), strconv.FormatFloat(float64(av.TotalIncome)/100, 'f', 2, 32), } data = append(data, a) } f, err := os.Create("top10.csv") if err != nil { log.Error("s.execSendTopTen create top10.csv error(%v)", err) return } defer f.Close() w := csv.NewWriter(f) w.WriteAll(data) w.Flush() rfile, err := os.Open("top10.csv") if err != nil { log.Error("s.execSendTopTen open top10.csv error(%v)", err) return } defer rfile.Close() r := csv.NewReader(rfile) for { var strs []string strs, err = r.Read() if err == io.EOF { break } body += fmt.Sprintf("