extra_func.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/csv"
  6. "io"
  7. "strconv"
  8. "time"
  9. coinclient "go-common/app/service/main/coin/api"
  10. "go-common/library/log"
  11. )
  12. // FormatCSV format csv data.
  13. func (s *Service) FormatCSV(records [][]string) (res []byte) {
  14. buf := new(bytes.Buffer)
  15. w := csv.NewWriter(buf)
  16. for _, record := range records {
  17. if err := w.Write(record); err != nil {
  18. log.Error("error(%+v) writing record to csv:", err)
  19. return
  20. }
  21. }
  22. w.Flush()
  23. res = buf.Bytes()
  24. return
  25. }
  26. // Upload http upload file.
  27. func (s *Service) Upload(c context.Context, fileName, fileType string, expire int64, body io.Reader) (location string, err error) {
  28. if location, err = s.uploadDao.Upload(c, fileName, fileType, expire, body); err != nil {
  29. log.Error("s.upload.Upload() error(%v)", err)
  30. }
  31. return
  32. }
  33. // AnnualCoins .
  34. func (s *Service) AnnualCoins(c context.Context, reader *csv.Reader) (fmid []int64) {
  35. for {
  36. record, err := reader.Read()
  37. if err == io.EOF {
  38. log.Warn("AnnualCoins is over!")
  39. err = nil
  40. break
  41. }
  42. if err != nil {
  43. log.Error("AnnualCoins(%+v) Error(%+v)", record, err)
  44. err = nil
  45. continue
  46. }
  47. log.Info("AnnualCoins record(%+v)", record)
  48. if len(record) < 2 {
  49. log.Error("AnnualCoins record(%+v) len<2", record)
  50. continue
  51. }
  52. mid, err := strconv.ParseInt(record[0], 10, 64)
  53. if err != nil {
  54. log.Error("AnnualCoins strconv.ParseInt mid(%+v) err(%+v)", record, err)
  55. err = nil
  56. continue
  57. }
  58. coins, err := strconv.ParseInt(record[1], 10, 64)
  59. if err != nil {
  60. log.Error("AnnualCoins strconv.ParseInt coins(%+v) err(%+v)", record, err)
  61. err = nil
  62. continue
  63. }
  64. arg := &coinclient.ModifyCoinsReq{
  65. Mid: mid,
  66. Count: float64(coins),
  67. Reason: "风纪委奖励",
  68. IP: "",
  69. Operator: "credit",
  70. CheckZero: 1,
  71. Ts: time.Now().Unix(),
  72. }
  73. _, err = s.coinClient.ModifyCoins(context.Background(), arg)
  74. if err != nil {
  75. fmid = append(fmid, mid)
  76. log.Error("ModifyCoins arg(%+v), err(%+v)", arg, err)
  77. continue
  78. }
  79. if err = s.msgDao.SendSysMsg(context.Background(), mid, "您的风纪委硬币礼包已到账", "Hi 风纪委员你好,你的2018年风纪委硬币礼包已到账!快进入你的硬币账户看看吧!"); err != nil {
  80. log.Error("SendSysMsg mid(%d), err(%+v)", mid, err)
  81. }
  82. time.Sleep(time.Duration(50) * time.Millisecond)
  83. }
  84. return
  85. }