team.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package daytask
  2. import (
  3. "app/commons/model/entity"
  4. "app/commons/services"
  5. "github.com/gin-gonic/gin"
  6. "time"
  7. )
  8. // TeamInfo 团队信息
  9. func (s *Server) TeamInfo(c *gin.Context) {
  10. ctx := s.FromContext(c)
  11. db := s.DB()
  12. userId := ctx.UserId()
  13. user := &entity.DtUser{}
  14. if err := db.Where("id = ?", userId).First(user).Error; err != nil {
  15. ctx.Fail("user_not_found")
  16. return
  17. }
  18. // 今日新增
  19. today := time.Now().Format("2006-01-02")
  20. var todayCount int64
  21. db.Model(&entity.DtUser{}).
  22. Where("parent_id = ? AND DATE(FROM_UNIXTIME(created_at)) = ?", userId, today).
  23. Count(&todayCount)
  24. // 本月新增
  25. month := time.Now().Format("2006-01")
  26. var monthCount int64
  27. db.Model(&entity.DtUser{}).
  28. Where("parent_id = ? AND DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y-%m') = ?", userId, month).
  29. Count(&monthCount)
  30. // 今日佣金
  31. var todayCommission float64
  32. db.Model(&entity.DtBalanceLog{}).
  33. Where("user_id = ? AND type = ? AND DATE(FROM_UNIXTIME(created_at)) = ?", userId, entity.BalanceLogTypeCommission, today).
  34. Select("COALESCE(SUM(amount), 0)").
  35. Scan(&todayCommission)
  36. ctx.OK(gin.H{
  37. "directInviteCount": user.DirectInviteCount,
  38. "teamCount": user.TeamCount,
  39. "todayNewCount": todayCount,
  40. "monthNewCount": monthCount,
  41. "todayCommission": todayCommission,
  42. "totalInviteIncome": user.TotalInviteIncome,
  43. "inviteCode": user.InviteCode,
  44. })
  45. }
  46. // TeamMembers 团队成员
  47. func (s *Server) TeamMembers(c *gin.Context) {
  48. ctx := s.FromContext(c)
  49. db := s.DB()
  50. userId := ctx.UserId()
  51. paging := &services.Pagination{
  52. Current: ctx.QueryInt64("current", 1),
  53. Size: ctx.QueryInt64("size", 20),
  54. }
  55. query := db.Model(&entity.DtUser{}).Where("parent_id = ?", userId)
  56. query.Count(&paging.Total)
  57. paging.Computer()
  58. type MemberInfo struct {
  59. Id int64 `json:"id"`
  60. Uid string `json:"uid"`
  61. Nickname string `json:"nickname"`
  62. Avatar string `json:"avatar"`
  63. Phone string `json:"phone"`
  64. CreatedAt int64 `json:"createdAt"`
  65. }
  66. members := make([]*MemberInfo, 0)
  67. query.Select("id, uid, nickname, avatar, CONCAT(LEFT(phone, 3), '****', RIGHT(phone, 4)) as phone, created_at").
  68. Order("created_at DESC").
  69. Offset(int(paging.Start)).
  70. Limit(int(paging.Size)).
  71. Scan(&members)
  72. ctx.OK(gin.H{
  73. "list": members,
  74. "paging": paging,
  75. })
  76. }
  77. // TeamIncome 团队收益
  78. func (s *Server) TeamIncome(c *gin.Context) {
  79. ctx := s.FromContext(c)
  80. db := s.DB()
  81. userId := ctx.UserId()
  82. // 今日返佣
  83. today := time.Now().Format("2006-01-02")
  84. var todayIncome float64
  85. db.Model(&entity.DtBalanceLog{}).
  86. Where("user_id = ? AND type = ? AND DATE(FROM_UNIXTIME(created_at)) = ?", userId, entity.BalanceLogTypeCommission, today).
  87. Select("COALESCE(SUM(amount), 0)").
  88. Scan(&todayIncome)
  89. // 本月返佣
  90. month := time.Now().Format("2006-01")
  91. var monthIncome float64
  92. db.Model(&entity.DtBalanceLog{}).
  93. Where("user_id = ? AND type = ? AND DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y-%m') = ?", userId, entity.BalanceLogTypeCommission, month).
  94. Select("COALESCE(SUM(amount), 0)").
  95. Scan(&monthIncome)
  96. // 累计返佣
  97. var totalIncome float64
  98. db.Model(&entity.DtBalanceLog{}).
  99. Where("user_id = ? AND type = ?", userId, entity.BalanceLogTypeCommission).
  100. Select("COALESCE(SUM(amount), 0)").
  101. Scan(&totalIncome)
  102. // 返佣记录
  103. paging := &services.Pagination{
  104. Current: ctx.QueryInt64("current", 1),
  105. Size: ctx.QueryInt64("size", 20),
  106. }
  107. query := db.Model(&entity.DtBalanceLog{}).
  108. Where("user_id = ? AND type = ?", userId, entity.BalanceLogTypeCommission)
  109. query.Count(&paging.Total)
  110. paging.Computer()
  111. logs := make([]*entity.DtBalanceLog, 0)
  112. query.Order("created_at DESC").
  113. Offset(int(paging.Start)).
  114. Limit(int(paging.Size)).
  115. Find(&logs)
  116. ctx.OK(gin.H{
  117. "todayIncome": todayIncome,
  118. "monthIncome": monthIncome,
  119. "totalIncome": totalIncome,
  120. "list": logs,
  121. "paging": paging,
  122. })
  123. }