123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package service
- import (
- "time"
- "youngee_b_api/app/dao"
- "youngee_b_api/app/vo"
- )
- type EnterpriseService struct{}
- func (e EnterpriseService) GetEnterpriseTakegoodsInfo(enterpriseId string, dateRange string) vo.ReWorkspaceTakegoods {
- var result vo.ReWorkspaceTakegoods
- //result.Pay = 1234.5
- //result.PayList = []float64{4, 6, 9}
- // 处理时间范围
- switch dateRange {
- case "7days":
- dates := getLastNDays(7)
- result = calcTakegoodsInfo(dates, enterpriseId)
- break
- case "30days":
- dates := getLastNDays(30)
- result = calcTakegoodsInfo(dates, enterpriseId)
- break
- case "90days":
- dates := getLastNDays(90)
- result = calcTakegoodsInfo(dates, enterpriseId)
- break
- case "monthly":
- dates := getCurrentMonthDates()
- result = calcTakegoodsInfo(dates, enterpriseId)
- break
- }
- return result
- }
- func getLastNDays(n int) []time.Time {
- var dates []time.Time
- today := time.Now()
- for i := 0; i < n; i++ {
- date := today.AddDate(0, 0, -i)
- dates = append(dates, date)
- }
- return dates
- }
- func getCurrentMonthDates() []time.Time {
- var dates []time.Time
- today := time.Now()
- year, month, _ := today.Date()
- location := today.Location()
- firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, location)
- nextMonth := firstOfMonth.AddDate(0, 1, 0)
- for current := firstOfMonth; current.Before(nextMonth); current = current.AddDate(0, 0, 1) {
- dates = append(dates, current)
- }
- return dates
- }
- func calcTakegoodsInfo(dates []time.Time, enterpriseId string) vo.ReWorkspaceTakegoods {
- var pay, finish, commission, commissionRate float64
- var order, person int64
- var payList, finishList, commissionList, commissionRateList []float64
- var orderList, personList []int64
- for _, date := range dates {
- enterprises, _ := (&dao.SelectionInfoDAO{}).GetSelectionInfoListOfDay(enterpriseId, date)
- if enterprises != nil {
- var currentPay float64
- var currentFinish float64
- var currentCommission float64
- var currentCommissionRate float64
- var currentOrder int64
- var currentPerson int64
- for _, enterprise := range enterprises {
- // 带货数据
- currentPay += enterprise.EstimatedCost
- currentFinish += enterprise.SettlementAmount
- currentCommission += enterprise.EstimatedCost * enterprise.CommissionRate
- currentOrder += enterprise.SampleNum - enterprise.RemainNum
- // 出单数量
- currentPerson, _ = (&dao.SelectionTaskInfoDao{}).CountBySelectionId(enterprise.SelectionID)
- currentCommissionRate = enterprise.SettlementAmount / float64(currentPerson)
- }
- // 带货数据
- pay += currentPay
- payList = append(payList, currentPay)
- finish += currentFinish
- finishList = append(finishList, currentFinish)
- commission += currentCommission
- commissionList = append(commissionList, currentCommission)
- order += currentOrder
- orderList = append(orderList, currentOrder)
- // 出单数量
- person += currentPerson
- personList = append(personList, person)
- commissionRate += currentCommissionRate
- commissionRateList = append(commissionRateList, currentCommissionRate)
- }
- }
- res := vo.ReWorkspaceTakegoods{
- Pay: pay,
- PayList: payList,
- Finish: finish,
- FinishList: finishList,
- Commission: commission,
- CommissionList: commissionList,
- Order: order,
- OrderList: orderList,
- Person: person,
- PersonList: personList,
- CommissionRate: commissionRate,
- CommissionRateList: commissionRateList,
- }
- return res
- }
|