stat.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/laser/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. "net/url"
  11. )
  12. const (
  13. _UrlUnames = "/x/admin/manager/users/unames"
  14. _UrlUids = "/x/admin/manager/users/uids"
  15. _queryArchiveStatSQL = " SELECT stat_date, business, stat_type, typeid, uid, stat_value FROM archive_stat WHERE stat_date = '%s' AND business = %d %s "
  16. _queryArchiveAuditCargoSQL = " SELECT uid, stat_date, receive_value, audit_value FROM archive_audit_cargo_hour %s "
  17. _queryArchiveStatStreamSQL = " SELECT stat_time, business, stat_type, typeid, uid, stat_value FROM archive_stat_stream WHERE stat_time = '%s' AND business = %d %s "
  18. )
  19. // StatArchiveStat is stat archive data.
  20. func (d *Dao) StatArchiveStat(c context.Context, business int, typeIDS []int64, uids []int64, statTypes []int64, statDate time.Time) (statNodes []*model.StatNode, err error) {
  21. var queryStmt string
  22. if len(statTypes) != 0 {
  23. queryStmt = queryStmt + fmt.Sprintf(" And stat_type in ( %s ) ", xstr.JoinInts(statTypes))
  24. }
  25. if len(typeIDS) != 0 {
  26. queryStmt = queryStmt + fmt.Sprintf(" AND typeid IN ( %s ) ", xstr.JoinInts(typeIDS))
  27. }
  28. if len(uids) != 0 {
  29. queryStmt = queryStmt + fmt.Sprintf(" AND uid IN ( %s ) ", xstr.JoinInts(uids))
  30. }
  31. rows, err := d.laserDB.Query(c, fmt.Sprintf(_queryArchiveStatSQL, statDate.Format("2006-01-02"), business, queryStmt))
  32. if err != nil {
  33. log.Error("d.laserDB.Query() error(%v)", err)
  34. return
  35. }
  36. defer rows.Close()
  37. for rows.Next() {
  38. item := &model.StatNode{}
  39. if err = rows.Scan(&item.StatDate, &item.Business, &item.StatType, &item.TypeID, &item.UID, &item.StatValue); err != nil {
  40. log.Error("rows.Scan() error(%v)", err)
  41. return
  42. }
  43. statNodes = append(statNodes, item)
  44. }
  45. return
  46. }
  47. // QueryArchiveCargo is query archive audit and receive value.
  48. func (d *Dao) QueryArchiveCargo(c context.Context, statTime time.Time, uids []int64) (items []*model.CargoDetail, err error) {
  49. whereStmt := fmt.Sprintf(" WHERE stat_date = '%s' ", statTime.Format("2006-01-02 15:04:05"))
  50. if len(uids) != 0 {
  51. uidStr := xstr.JoinInts(uids)
  52. whereStmt = whereStmt + fmt.Sprintf(" AND uid in ( %s ) ", uidStr)
  53. }
  54. rows, err := d.laserDB.Query(c, fmt.Sprintf(_queryArchiveAuditCargoSQL, whereStmt))
  55. if err != nil {
  56. log.Error("d.laserDB.Query() error(%v)", err)
  57. return
  58. }
  59. defer rows.Close()
  60. for rows.Next() {
  61. item := &model.CargoDetail{}
  62. if err = rows.Scan(&item.UID, &item.StatDate, &item.ReceiveValue, &item.AuditValue); err != nil {
  63. log.Error("rows.Scan() error(%v)", err)
  64. return
  65. }
  66. items = append(items, item)
  67. }
  68. return
  69. }
  70. //GetUIDByNames is query uids by uname array separated by comma.
  71. func (d *Dao) GetUIDByNames(c context.Context, unames string) (res map[string]int64, err error) {
  72. var param = url.Values{}
  73. param.Set("unames", unames)
  74. var httpRes struct {
  75. Code int `json:"code"`
  76. Data map[string]int64 `json:"data"`
  77. Message string `json:"message"`
  78. }
  79. err = d.HTTPClient.Get(c, d.c.Host.Manager+_UrlUids, "", param, &httpRes)
  80. if err != nil {
  81. log.Error("d.client.Get(%s) error(%v)", d.c.Host.Manager+_UrlUids+"?"+param.Encode(), err)
  82. return
  83. }
  84. if httpRes.Code != ecode.OK.Code() {
  85. log.Error("url(%s) error(%v), code(%d), message(%s)", d.c.Host.Manager+_UrlUids+"?"+param.Encode(), err, httpRes.Code, httpRes.Message)
  86. }
  87. res = httpRes.Data
  88. return
  89. }
  90. //GetUNamesByUids is query usernames by uids.
  91. func (d *Dao) GetUNamesByUids(c context.Context, uids []int64) (res map[int64]string, err error) {
  92. var param = url.Values{}
  93. var uidStr = xstr.JoinInts(uids)
  94. param.Set("uids", uidStr)
  95. var httpRes struct {
  96. Code int `json:"code"`
  97. Data map[int64]string `json:"data"`
  98. Message string `json:"message"`
  99. }
  100. err = d.HTTPClient.Get(c, d.c.Host.Manager+_UrlUnames, "", param, &httpRes)
  101. if err != nil {
  102. log.Error("d.client.Get(%s) error(%v)", d.c.Host.Manager+_UrlUnames+"?"+param.Encode(), err)
  103. return
  104. }
  105. if httpRes.Code != 0 {
  106. log.Error("url(%s) error(%v), code(%d), message(%s)", d.c.Host.Manager+_UrlUnames+"?"+param.Encode(), err, httpRes.Code, httpRes.Message)
  107. }
  108. res = httpRes.Data
  109. return
  110. }
  111. // StatArchiveStatStream is stat archive data.
  112. func (d *Dao) StatArchiveStatStream(c context.Context, business int, typeIDS []int64, uids []int64, statTypes []int64, statDate time.Time) (statNodes []*model.StatNode, err error) {
  113. var queryStmt string
  114. if len(statTypes) != 0 {
  115. queryStmt = queryStmt + fmt.Sprintf(" And stat_type in ( %s ) ", xstr.JoinInts(statTypes))
  116. }
  117. if len(typeIDS) != 0 {
  118. queryStmt = queryStmt + fmt.Sprintf(" AND typeid IN ( %s ) ", xstr.JoinInts(typeIDS))
  119. }
  120. if len(uids) != 0 {
  121. queryStmt = queryStmt + fmt.Sprintf(" AND uid IN ( %s ) ", xstr.JoinInts(uids))
  122. }
  123. rows, err := d.laserDB.Query(c, fmt.Sprintf(_queryArchiveStatStreamSQL, statDate.Format("2006-01-02"), business, queryStmt))
  124. if err != nil {
  125. log.Error("d.laserDB.Query() error(%v)", err)
  126. return
  127. }
  128. defer rows.Close()
  129. for rows.Next() {
  130. node := &model.StatNode{}
  131. if err = rows.Scan(&node.StatDate, &node.Business, &node.StatType, &node.TypeID, &node.UID, &node.StatValue); err != nil {
  132. log.Error("rows.Scan() error(%v)", err)
  133. return
  134. }
  135. statNodes = append(statNodes, node)
  136. }
  137. return
  138. }