last_self_order_time ?? null); $userId = is_array($agent) ? ($agent['user_id'] ?? 0) : ($agent->user_id ?? 0); $at = $atTime ?: time(); $start = $this->windowStart($at); if ($last) { return intval($last) >= $start; } if (!$userId) { return false; } return $this->hasQualifiedOrder($userId, $start, $at); } public function hasQualifiedOrder($userId, $start, $end) { $order = OrderModel::where('user_id', $userId) ->where('type', '<>', 'score') ->where('pay_fee', '>', 0) ->paid() ->where('paid_time', '>=', $start) ->where('paid_time', '<=', $end) ->find(); return (bool) $order; } public function activeUserIds($start, $end, $extraWhere = []) { $createBaseQuery = function () use ($extraWhere) { $q = \think\Db::name('shopro_commission_agent') ->where('status', 'in', ['normal', 'freeze']) ->where('level', '>=', 1); foreach ($extraWhere as $field => $value) { $q->where($field, $value); } return $q; }; // 1. 优先通过 last_self_order_time 索引快速命中 $hitUserIds = $createBaseQuery()->where(function ($q) use ($start, $end) { $q->where('last_self_order_time', '>=', $start) ->where('last_self_order_time', '<=', $end); })->column('user_id'); // 2. 容错:查找 last_self_order_time 为空或为 0 的代理商 $nullUserIds = $createBaseQuery()->where(function ($q) { $q->whereNull('last_self_order_time')->whereOr('last_self_order_time', 0); })->column('user_id'); if (!empty($nullUserIds)) { $orderHits = \think\Db::name('shopro_order') ->whereIn('user_id', $nullUserIds) ->where('type', '<>', 'score') ->where('pay_fee', '>', 0) ->whereIn('status', ['paid', 'completed']) ->where('paid_time', '>=', $start) ->where('paid_time', '<=', $end) ->group('user_id') ->column('max(paid_time)', 'user_id'); foreach ($orderHits as $uId => $lastTime) { \think\Db::name('shopro_commission_agent')->where('user_id', $uId)->update(['last_self_order_time' => $lastTime]); $hitUserIds[] = intval($uId); } } return array_values(array_unique(array_map('intval', $hitUserIds))); } }