withdraw.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package income
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "time"
  7. model "go-common/app/admin/main/growup/model/income"
  8. "go-common/app/admin/main/growup/service"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. // UpWithdraw get up_account infos
  13. func (s *Service) UpWithdraw(c context.Context, mids []int64, isDeleted, from, limit int) (data []*model.UpWithdrawRes, total int64, err error) {
  14. query := ""
  15. if len(mids) != 0 {
  16. query += fmt.Sprintf("mid in (%s)", xstr.JoinInts(mids))
  17. }
  18. total, err = s.UpAccountCount(c, query, isDeleted)
  19. if err != nil {
  20. log.Error("s.UpAccountCount error(%v)", err)
  21. return
  22. }
  23. ups, err := s.ListUpAccount(c, query, isDeleted, from, limit)
  24. if err != nil {
  25. log.Error("s.ListUpAccount error(%v)", err)
  26. return
  27. }
  28. if len(mids) == 0 {
  29. for _, up := range ups {
  30. mids = append(mids, up.MID)
  31. }
  32. }
  33. query = ""
  34. if len(mids) != 0 {
  35. query = fmt.Sprintf("mid in (%s)", xstr.JoinInts(mids))
  36. }
  37. upWithdraw, err := s.GetUpWithdraw(c, query)
  38. if err != nil {
  39. log.Error("s.GetUpWithdraw error(%v)", err)
  40. return
  41. }
  42. withdrawMap := make(map[string]int64)
  43. for _, w := range upWithdraw {
  44. key := fmt.Sprintf("%d+%s", w.MID, w.DateVersion)
  45. withdrawMap[key] = w.WithdrawIncome
  46. }
  47. upInfo, err := s.dao.ListUpInfo(c, mids)
  48. if err != nil {
  49. log.Error("s.dao.ListUpInfo error(%v)", err)
  50. return
  51. }
  52. sort.Slice(ups, func(i, j int) bool {
  53. return ups[i].LastWithdrawTime > ups[j].LastWithdrawTime
  54. })
  55. data = make([]*model.UpWithdrawRes, 0)
  56. for _, up := range ups {
  57. key := fmt.Sprintf("%d+%s", up.MID, up.WithdrawDateVersion)
  58. data = append(data, &model.UpWithdrawRes{
  59. MID: up.MID,
  60. Nickname: upInfo[up.MID],
  61. WithdrawIncome: fmt.Sprintf("%0.2f", fromYuanToFen(up.TotalWithdrawIncome)),
  62. UnWithdrawIncome: fmt.Sprintf("%0.2f", fromYuanToFen(up.TotalUnwithdrawIncome)),
  63. LastWithdrawIncome: fmt.Sprintf("%0.2f", fromYuanToFen(withdrawMap[key])),
  64. WithdrawDate: up.LastWithdrawTime.Time().Format(_layout),
  65. MTime: up.MTime,
  66. })
  67. }
  68. return
  69. }
  70. // UpWithdrawExport export up withdraw
  71. func (s *Service) UpWithdrawExport(c context.Context, mids []int64, isDeleted, from, limit int) (res []byte, err error) {
  72. upWithdraw, _, err := s.UpWithdraw(c, mids, isDeleted, from, limit)
  73. if err != nil {
  74. log.Error("s.UpWithdraw error(%v)", err)
  75. return
  76. }
  77. records := formatUpWithdraw(upWithdraw, isDeleted)
  78. res, err = service.FormatCSV(records)
  79. if err != nil {
  80. log.Error("FormatCSV error(%v)")
  81. }
  82. return
  83. }
  84. // GetUpWithdraw get up_withdraw
  85. func (s *Service) GetUpWithdraw(c context.Context, query string) (upW []*model.UpIncomeWithdraw, err error) {
  86. upW = make([]*model.UpIncomeWithdraw, 0)
  87. var id int64
  88. limit := 2000
  89. for {
  90. var w []*model.UpIncomeWithdraw
  91. w, err = s.ListUpWithdraw(c, id, query, limit)
  92. if err != nil {
  93. return
  94. }
  95. upW = append(upW, w...)
  96. if len(w) < limit {
  97. break
  98. }
  99. id = w[len(w)-1].ID
  100. }
  101. return
  102. }
  103. // ListUpWithdraw list up_withdraw
  104. func (s *Service) ListUpWithdraw(c context.Context, id int64, query string, limit int) (upWithdraw []*model.UpIncomeWithdraw, err error) {
  105. if query != "" {
  106. query += " AND"
  107. }
  108. return s.dao.ListUpWithdraw(c, id, query, limit)
  109. }
  110. // UpWithdrawStatis up_withdraw statis
  111. func (s *Service) UpWithdrawStatis(c context.Context, from, to int64, isDeleted int) (data interface{}, err error) {
  112. now := time.Now()
  113. var fromTime, toTime time.Time
  114. if from == 0 || to == 0 {
  115. fromTime = time.Date(now.Year()-1, now.Month(), 1, 0, 0, 0, 0, time.Local)
  116. toTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)
  117. } else {
  118. fromTime = time.Unix(from, 0)
  119. toTime = time.Unix(to, 0)
  120. }
  121. query := fmt.Sprintf("date_version >= '%s' and date_version <= '%s'", fromTime.Format(_layoutMonth), toTime.Format(_layoutMonth))
  122. upWithdraw, err := s.GetUpWithdraw(c, query)
  123. if err != nil {
  124. log.Error("s.GetUpWithdraw error(%v)", err)
  125. return
  126. }
  127. deletedUp, err := s.GetUpAccount(c, "", 1)
  128. if err != nil {
  129. log.Error("s.GetUpAccount error(%v)", err)
  130. return
  131. }
  132. deletedUpMap := make(map[int64]struct{})
  133. for _, up := range deletedUp {
  134. deletedUpMap[up.MID] = struct{}{}
  135. }
  136. data = upWithdrawStatis(upWithdraw, deletedUpMap, isDeleted, fromTime, toTime)
  137. return
  138. }
  139. func upWithdrawStatis(upWithdraw []*model.UpIncomeWithdraw, deletedUp map[int64]struct{}, isDeleted int, startTime, endTime time.Time) interface{} {
  140. dateCount := make(map[string]int)
  141. dateIncome := make(map[string]int64)
  142. for _, up := range upWithdraw {
  143. _, ok := deletedUp[up.MID]
  144. if (ok && isDeleted == 1) || (!ok && isDeleted == 0) {
  145. dateCount[up.DateVersion]++
  146. dateIncome[up.DateVersion] += up.WithdrawIncome
  147. }
  148. }
  149. incomes, counts, xAxis := []string{}, []int{}, []string{}
  150. endTime = endTime.AddDate(0, 0, 1)
  151. for startTime.Before(endTime) {
  152. key := startTime.Format(_layoutMonth)
  153. incomes = append(incomes, fmt.Sprintf("%0.2f", fromYuanToFen(dateIncome[key])))
  154. counts = append(counts, dateCount[key])
  155. xAxis = append(xAxis, key)
  156. startTime = startTime.AddDate(0, 1, 0)
  157. }
  158. return map[string]interface{}{
  159. "incomes": incomes,
  160. "counts": counts,
  161. "xaxis": xAxis,
  162. }
  163. }
  164. // UpWithdrawDetail get up withdraw by mid
  165. func (s *Service) UpWithdrawDetail(c context.Context, mid int64) (upWithdraw []*model.UpIncomeWithdraw, err error) {
  166. query := fmt.Sprintf("mid = %d", mid)
  167. upWithdraw, err = s.GetUpWithdraw(c, query)
  168. if err != nil {
  169. log.Error("s.GetUpWithdraw error(%v)", err)
  170. return
  171. }
  172. if len(upWithdraw) == 0 {
  173. return
  174. }
  175. upInfo, err := s.dao.GetUpInfoNickname(c, []int64{upWithdraw[0].MID})
  176. if err != nil {
  177. log.Error("s.dao.GetUpInfoNickname error(%v)", err)
  178. return
  179. }
  180. for _, up := range upWithdraw {
  181. up.Nickname = upInfo[up.MID]
  182. up.Income = fmt.Sprintf("%.2f", fromYuanToFen(up.WithdrawIncome))
  183. }
  184. return
  185. }
  186. // UpWithdrawDetailExport export up withdraw detail
  187. func (s *Service) UpWithdrawDetailExport(c context.Context, mid int64) (res []byte, err error) {
  188. upWithdraw, err := s.UpWithdrawDetail(c, mid)
  189. if err != nil {
  190. log.Error("s.UpWithdrawDetail error(%v)", err)
  191. return
  192. }
  193. records := formatUpIncomeWithdraw(upWithdraw)
  194. res, err = service.FormatCSV(records)
  195. if err != nil {
  196. log.Error("FormatCSV error(%v)")
  197. }
  198. return
  199. }