workspace.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/issue9/conv"
  6. "math"
  7. "time"
  8. "youngee_m_api/db"
  9. "youngee_m_api/model/http_model"
  10. )
  11. var Workspace *workspace
  12. type workspace struct{}
  13. func (*workspace) GetTakegoodsInfo(ctx context.Context, enterpriseId string, dateRange string) (*http_model.TakegoodsData, error) {
  14. takegoodsInfo := http_model.TakegoodsData{}
  15. var dates []time.Time
  16. switch dateRange {
  17. case "7days":
  18. dates = getLastNDays(7)
  19. case "30days":
  20. dates = getLastNDays(30)
  21. case "90days":
  22. dates = getLastNDays(90)
  23. case "monthly":
  24. dates = getCurrentMonthDates()
  25. default:
  26. return nil, errors.New("unsupported date range")
  27. }
  28. takegoodsInfo = calcTakegoodsInfo(ctx, dates, enterpriseId)
  29. if hasNaN(&takegoodsInfo) {
  30. return nil, errors.New("calculation resulted in NaN")
  31. }
  32. return &takegoodsInfo, nil
  33. }
  34. func hasNaN(data *http_model.TakegoodsData) bool {
  35. if math.IsNaN(data.CommissionRate) {
  36. return true
  37. }
  38. return false
  39. }
  40. func getLastNDays(n int) []time.Time {
  41. var dates []time.Time
  42. today := time.Now()
  43. for i := 0; i < n; i++ {
  44. date := today.AddDate(0, 0, -i)
  45. dates = append(dates, date)
  46. }
  47. return dates
  48. }
  49. func getCurrentMonthDates() []time.Time {
  50. var dates []time.Time
  51. today := time.Now()
  52. year, month, _ := today.Date()
  53. location := today.Location()
  54. firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, location)
  55. nextMonth := firstOfMonth.AddDate(0, 1, 0)
  56. for current := firstOfMonth; current.Before(nextMonth); current = current.AddDate(0, 0, 1) {
  57. dates = append(dates, current)
  58. }
  59. return dates
  60. }
  61. func calcTakegoodsInfo(ctx context.Context, dates []time.Time, enterpriseId string) http_model.TakegoodsData {
  62. var pay, finish, commission, commissionRate float64
  63. var order, person int64
  64. var payList, finishList, commissionList, commissionRateList []float64
  65. var orderList, personList []int64
  66. var count int64
  67. for _, date := range dates {
  68. enterprises, _ := db.GetSelectionInfoListOfDay(ctx, enterpriseId, date)
  69. if enterprises != nil {
  70. var currentPay float64
  71. var currentFinish float64
  72. var currentCommission float64
  73. var currentCommissionRate float64
  74. var currrentseccommissionrate float64
  75. var currentOrder int64
  76. var currentPerson int64
  77. var currentsecperson int64
  78. for _, enterprise := range enterprises {
  79. // 带货数据
  80. currentPay += conv.MustFloat64(enterprise.EstimatedCost, 0.0)
  81. currentFinish += conv.MustFloat64(enterprise.SettlementAmount, 0.0)
  82. currentCommission += conv.MustFloat64(enterprise.EstimatedCost, 0.0) * conv.MustFloat64(enterprise.CommissionRate, 0.0)
  83. currentOrder += conv.MustInt64(enterprise.SampleNum, 0) - conv.MustInt64(enterprise.RemainNum, 0)
  84. // 出单数量
  85. currentsecperson, _ = db.CountBySelectionId(ctx, enterprise.SelectionID) //当天当前任务达人数
  86. if currentsecperson > 0 {
  87. currrentseccommissionrate = conv.MustFloat64(enterprise.SettlementAmount, 0.0) / float64(currentsecperson) //该任务佣金率
  88. } else {
  89. currrentseccommissionrate = 0.0
  90. }
  91. currentPerson += currentsecperson
  92. currentCommissionRate += currrentseccommissionrate
  93. count++
  94. }
  95. // 带货数据
  96. pay += currentPay
  97. payList = append(payList, currentPay)
  98. finish += currentFinish
  99. finishList = append(finishList, currentFinish)
  100. commission += currentCommission
  101. commissionList = append(commissionList, currentCommission)
  102. order += currentOrder
  103. orderList = append(orderList, currentOrder)
  104. // 出单数量
  105. person += currentPerson
  106. personList = append(personList, person)
  107. commissionRate += currentCommissionRate
  108. commissionRateList = append(commissionRateList, currentCommissionRate)
  109. }
  110. if count > 0 {
  111. commissionRate = commissionRate / float64(count)
  112. } else {
  113. commissionRate = 0.0
  114. }
  115. }
  116. res := http_model.TakegoodsData{
  117. Pay: pay,
  118. PayList: payList,
  119. Finish: finish,
  120. FinishList: finishList,
  121. Commission: commission,
  122. CommissionList: commissionList,
  123. Order: order,
  124. OrderList: orderList,
  125. Person: person,
  126. PersonList: personList,
  127. CommissionRate: commissionRate,
  128. CommissionRateList: commissionRateList,
  129. }
  130. return res
  131. }