enterprise_service.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package service
  2. import (
  3. "time"
  4. "youngee_b_api/app/dao"
  5. "youngee_b_api/app/vo"
  6. )
  7. type EnterpriseService struct{}
  8. func (e EnterpriseService) GetEnterpriseTakegoodsInfo(enterpriseId string, dateRange string) vo.ReWorkspaceTakegoods {
  9. var result vo.ReWorkspaceTakegoods
  10. //result.Pay = 1234.5
  11. //result.PayList = []float64{4, 6, 9}
  12. // 处理时间范围
  13. switch dateRange {
  14. case "7days":
  15. dates := getLastNDays(7)
  16. result = calcTakegoodsInfo(dates, enterpriseId)
  17. break
  18. case "30days":
  19. dates := getLastNDays(30)
  20. result = calcTakegoodsInfo(dates, enterpriseId)
  21. break
  22. case "90days":
  23. dates := getLastNDays(90)
  24. result = calcTakegoodsInfo(dates, enterpriseId)
  25. break
  26. case "monthly":
  27. dates := getCurrentMonthDates()
  28. result = calcTakegoodsInfo(dates, enterpriseId)
  29. break
  30. }
  31. return result
  32. }
  33. func getLastNDays(n int) []time.Time {
  34. var dates []time.Time
  35. today := time.Now()
  36. for i := 0; i < n; i++ {
  37. date := today.AddDate(0, 0, -i)
  38. dates = append(dates, date)
  39. }
  40. return dates
  41. }
  42. func getCurrentMonthDates() []time.Time {
  43. var dates []time.Time
  44. today := time.Now()
  45. year, month, _ := today.Date()
  46. location := today.Location()
  47. firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, location)
  48. nextMonth := firstOfMonth.AddDate(0, 1, 0)
  49. for current := firstOfMonth; current.Before(nextMonth); current = current.AddDate(0, 0, 1) {
  50. dates = append(dates, current)
  51. }
  52. return dates
  53. }
  54. func calcTakegoodsInfo(dates []time.Time, enterpriseId string) vo.ReWorkspaceTakegoods {
  55. var pay, finish, commission, commissionRate float64
  56. var order, person int64
  57. var payList, finishList, commissionList, commissionRateList []float64
  58. var orderList, personList []int64
  59. for _, date := range dates {
  60. enterprises, _ := (&dao.SelectionInfoDAO{}).GetSelectionInfoListOfDay(enterpriseId, date)
  61. if enterprises != nil {
  62. var currentPay float64
  63. var currentFinish float64
  64. var currentCommission float64
  65. var currentCommissionRate float64
  66. var currentOrder int64
  67. var currentPerson int64
  68. for _, enterprise := range enterprises {
  69. // 带货数据
  70. currentPay += enterprise.EstimatedCost
  71. currentFinish += enterprise.SettlementAmount
  72. currentCommission += enterprise.EstimatedCost * enterprise.CommissionRate
  73. currentOrder += enterprise.SampleNum - enterprise.RemainNum
  74. // 出单数量
  75. currentPerson, _ = (&dao.SelectionTaskInfoDao{}).CountBySelectionId(enterprise.SelectionID)
  76. currentCommissionRate = enterprise.SettlementAmount / float64(currentPerson)
  77. }
  78. // 带货数据
  79. pay += currentPay
  80. payList = append(payList, currentPay)
  81. finish += currentFinish
  82. finishList = append(finishList, currentFinish)
  83. commission += currentCommission
  84. commissionList = append(commissionList, currentCommission)
  85. order += currentOrder
  86. orderList = append(orderList, currentOrder)
  87. // 出单数量
  88. person += currentPerson
  89. personList = append(personList, person)
  90. commissionRate += currentCommissionRate
  91. commissionRateList = append(commissionRateList, currentCommissionRate)
  92. }
  93. }
  94. res := vo.ReWorkspaceTakegoods{
  95. Pay: pay,
  96. PayList: payList,
  97. Finish: finish,
  98. FinishList: finishList,
  99. Commission: commission,
  100. CommissionList: commissionList,
  101. Order: order,
  102. OrderList: orderList,
  103. Person: person,
  104. PersonList: personList,
  105. CommissionRate: commissionRate,
  106. CommissionRateList: commissionRateList,
  107. }
  108. return res
  109. }