consume_trend_list.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package accounting
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/common.in/utils"
  6. "gd_management/consts"
  7. "gd_management/errors"
  8. "fmt"
  9. "sort"
  10. "time"
  11. "github.com/astaxie/beego/orm"
  12. "go.uber.org/zap"
  13. )
  14. func ConsumeTrendList(ctx context.Context, req *apis.ConsumeTrendListReq, reply *apis.ConsumeTrendListReply) error {
  15. o := orm.NewOrm()
  16. sql := "select t1.day,SUM(t1.amount) as amount from t_gd_consume as t1 left join t_gd_merchant_data_api t2 on t1.merchant_data_api_id=t2.id left join t_gd_merchants as t3 on t2.merchant_id=t3.id left join t_gd_data_api_query_type as t4 on t4.id=t2.query_type_id left join t_gd_data_api as t5 on t5.id=t4.data_api_id"
  17. where := ""
  18. if req.MerchantId != 0 {
  19. if where == "" {
  20. where = fmt.Sprintf(" where t2.merchant_id=%d", req.MerchantId)
  21. } else {
  22. where = fmt.Sprintf("%s and t2.merchant_id=%d", where, req.MerchantId)
  23. }
  24. }
  25. if req.DataApiId != 0 {
  26. if where == "" {
  27. where = fmt.Sprintf(" where t4.data_api_id=%d", req.DataApiId)
  28. } else {
  29. where = fmt.Sprintf("%s and t4.data_api_id=%d", where, req.DataApiId)
  30. }
  31. }
  32. if req.StartTime != "" {
  33. if len(req.StartTime) > consts.DayLen {
  34. req.StartTime = req.StartTime[0:consts.DayLen]
  35. }
  36. if where == "" {
  37. where = fmt.Sprintf(" where t1.day>='%s'", req.StartTime)
  38. } else {
  39. where = fmt.Sprintf("%s and t1.day>='%s'", where, req.StartTime)
  40. }
  41. }
  42. if req.EndTime != "" {
  43. if len(req.EndTime) > consts.DayLen {
  44. req.EndTime = req.EndTime[0:consts.DayLen]
  45. }
  46. if where == "" {
  47. where = fmt.Sprintf(" where t1.day<='%s'", req.EndTime)
  48. } else {
  49. where = fmt.Sprintf("%s and t1.day<='%s'", where, req.EndTime)
  50. }
  51. }
  52. sql = sql + where + " group by t1.day order by t1.day asc"
  53. _, err := o.Raw(sql).QueryRows(&reply.List)
  54. if err != nil {
  55. l.Error("func",
  56. zap.String("call", "ConsumeList"),
  57. zap.String("args", utils.MarshalJsonString(req)),
  58. zap.String("error", err.Error()))
  59. return errors.DataBaseError
  60. }
  61. endTime, _ := time.ParseInLocation("2006-01-02", req.EndTime, time.Local)
  62. startTime, _ := time.ParseInLocation("2006-01-02", req.StartTime, time.Local)
  63. dateArray := make([]string, 0)
  64. for endTime.After(startTime.AddDate(0, 0, -1)) {
  65. dateArray = append(dateArray, startTime.Format("2006-01-02"))
  66. startTime = startTime.AddDate(0, 0, 1)
  67. }
  68. for _, v := range dateArray {
  69. if !InSlice(reply.List, v) {
  70. reply.List = append(reply.List, apis.ConsumeTrend{
  71. Day: v,
  72. Amount: 0.00,
  73. })
  74. }
  75. }
  76. sort.SliceStable(reply.List, func(i, j int) bool {
  77. return reply.List[i].Day < reply.List[j].Day
  78. })
  79. return nil
  80. }
  81. func InSlice(items []apis.ConsumeTrend, item string) bool {
  82. for _, v := range items {
  83. if v.Day == item {
  84. return true
  85. }
  86. }
  87. return false
  88. }