package customer import ( "fmt" "math/rand" "server/common" "server/config" "server/db" "server/modules/customer/bdb" "server/modules/customer/values" "server/util" "time" "github.com/liangdas/mqant/log" ) func StartTimer() { count := config.GetConfig().Customer.MaxOrderCount if count > 0 { MaxOrderCount = count } CustomerOrderAssignTimer() } const ( // MaxOrderCount = 10 // 暂定客诉最大同时分配支付订单数 ) var ( MaxOrderCount int64 = 10 ) func CustomerOrderAssignTimer() { time.AfterFunc(2*time.Second, func() { defer func() { CustomerOrderAssignTimer() }() users := []*values.User{} bdb.BackDB.QueryAll(fmt.Sprintf("role = %d and online = 1", values.UserRole2), "", &values.User{}, &users) if len(users) == 0 { return } orderCount := map[int]int64{} uids := []int{} for _, v := range users { list := []common.CustomerOrder{} count, _ := db.Mysql().QueryList(0, 100, fmt.Sprintf(`customer_uid = %d and status = %d and order_id <> ""`, v.ID, common.CustomerOrderCreate), "", &common.CustomerOrder{}, &list) orderCount[int(v.ID)] = count uids = append(uids, int(v.ID)) } rechargeList := []common.CustomerOrder{} db.Mysql().QueryList(0, 100, fmt.Sprintf(`status = %d and order_id <> ""`, common.CustomerOrderCreate), "", &common.CustomerOrder{}, &rechargeList) normalList := []common.CustomerOrder{} db.Mysql().QueryList(0, 100, fmt.Sprintf(`status = %d`, common.CustomerOrderCreate), "", &common.CustomerOrder{}, &normalList) if len(rechargeList) == 0 && len(normalList) == 0 { return } begin := time.Now() // log.Debug("start CustomerOrderAssignTimer......") count := 0 now := time.Now().Unix() // 先分配充值订单 for _, v := range rechargeList { if util.SliceContain(uids, v.CustomerUid) { continue } indexs := rand.Perm(len(users)) var u *values.User for _, l := range indexs { diff := MaxOrderCount - orderCount[int(users[l].ID)] if diff <= 0 { continue } u = users[l] break } if u == nil { break } orderCount[int(u.ID)]++ db.Mysql().Update(&common.CustomerOrder{ID: v.ID}, map[string]interface{}{"customer_uid": u.ID, "assign_time": now}) count++ } for _, v := range normalList { if util.SliceContain(uids, v.CustomerUid) { continue } indexs := rand.Perm(len(users)) var u *values.User for _, l := range indexs { diff := MaxOrderCount - orderCount[int(users[l].ID)] if diff <= 0 { continue } u = users[l] break } if u == nil { break } orderCount[int(u.ID)]++ db.Mysql().Update(&common.CustomerOrder{ID: v.ID}, map[string]interface{}{"customer_uid": u.ID, "assign_time": now}) count++ } if count > 0 { log.Debug("finish CustomerOrderAssignTimer since:%v,rechargeList:%d,normalList:%d,count:%d", time.Since(begin), len(rechargeList), len(normalList), count) } }) }