package service import ( "context" "errors" "github.com/issue9/conv" "math" "time" "youngee_m_api/db" "youngee_m_api/model/http_model" ) var Workspace *workspace type workspace struct{} func (*workspace) GetTakegoodsInfo(ctx context.Context, enterpriseId string, dateRange string) (*http_model.TakegoodsData, error) { takegoodsInfo := http_model.TakegoodsData{} var dates []time.Time switch dateRange { case "7days": dates = getLastNDays(7) case "30days": dates = getLastNDays(30) case "90days": dates = getLastNDays(90) case "monthly": dates = getCurrentMonthDates() default: return nil, errors.New("unsupported date range") } takegoodsInfo = calcTakegoodsInfo(ctx, dates, enterpriseId) if hasNaN(&takegoodsInfo) { return nil, errors.New("calculation resulted in NaN") } return &takegoodsInfo, nil } func hasNaN(data *http_model.TakegoodsData) bool { if math.IsNaN(data.CommissionRate) { return true } return false } 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(ctx context.Context, dates []time.Time, enterpriseId string) http_model.TakegoodsData { var pay, finish, commission, commissionRate float64 var order, person int64 var payList, finishList, commissionList, commissionRateList []float64 var orderList, personList []int64 var count int64 for _, date := range dates { enterprises, _ := db.GetSelectionInfoListOfDay(ctx, enterpriseId, date) if enterprises != nil { var currentPay float64 var currentFinish float64 var currentCommission float64 var currentCommissionRate float64 var currrentseccommissionrate float64 var currentOrder int64 var currentPerson int64 var currentsecperson int64 for _, enterprise := range enterprises { // 带货数据 currentPay += conv.MustFloat64(enterprise.EstimatedCost, 0.0) currentFinish += conv.MustFloat64(enterprise.SettlementAmount, 0.0) currentCommission += conv.MustFloat64(enterprise.EstimatedCost, 0.0) * conv.MustFloat64(enterprise.CommissionRate, 0.0) currentOrder += conv.MustInt64(enterprise.SampleNum, 0) - conv.MustInt64(enterprise.RemainNum, 0) // 出单数量 currentsecperson, _ = db.CountBySelectionId(ctx, enterprise.SelectionID) //当天当前任务达人数 if currentsecperson > 0 { currrentseccommissionrate = conv.MustFloat64(enterprise.SettlementAmount, 0.0) / float64(currentsecperson) //该任务佣金率 } else { currrentseccommissionrate = 0.0 } currentPerson += currentsecperson currentCommissionRate += currrentseccommissionrate count++ } // 带货数据 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) } if count > 0 { commissionRate = commissionRate / float64(count) } else { commissionRate = 0.0 } } res := http_model.TakegoodsData{ 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 }