76 lines
2.3 KiB
PHP
76 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\qywx;
|
|
|
|
/** 计算应交给企业微信官方多人路由的成员范围。 */
|
|
class QywxPromotionMemberRange
|
|
{
|
|
/**
|
|
* @param list<array<string,mixed>> $members
|
|
* @return array{userids:list<string>,members:list<array<string,mixed>>,eligible_count:int}
|
|
*/
|
|
public static function evaluate(array $members, string $today, int $now): array
|
|
{
|
|
$userIds = [];
|
|
foreach ($members as &$member) {
|
|
if ((string) ($member['today_date'] ?? '') !== $today) {
|
|
$member['today_date'] = $today;
|
|
$member['today_count'] = 0;
|
|
}
|
|
$member['current_weight'] = 0;
|
|
if (!self::eligible($member, $now)) {
|
|
continue;
|
|
}
|
|
$userId = trim((string) ($member['userid'] ?? ''));
|
|
if ($userId !== '') {
|
|
$userIds[$userId] = true;
|
|
}
|
|
}
|
|
unset($member);
|
|
|
|
return [
|
|
'userids' => array_keys($userIds),
|
|
'members' => array_values($members),
|
|
'eligible_count' => count($userIds),
|
|
];
|
|
}
|
|
|
|
/** @param array<string,mixed> $member */
|
|
public static function eligible(array $member, int $now): bool
|
|
{
|
|
if ((int) ($member['enabled'] ?? 0) !== 1) {
|
|
return false;
|
|
}
|
|
$start = max(0, (int) ($member['active_start'] ?? 0));
|
|
$end = max(0, (int) ($member['active_end'] ?? 0));
|
|
if (($start > 0 && $start > $now) || ($end > 0 && $end < $now)) {
|
|
return false;
|
|
}
|
|
$limit = max(0, (int) ($member['daily_limit'] ?? 0));
|
|
|
|
return $limit === 0 || (int) ($member['today_count'] ?? 0) < $limit;
|
|
}
|
|
|
|
/** @param list<string> $left @param list<string> $right */
|
|
public static function same(array $left, array $right): bool
|
|
{
|
|
$normalise = static function (array $values): array {
|
|
$result = [];
|
|
foreach ($values as $value) {
|
|
$userId = trim((string) $value);
|
|
if ($userId !== '') {
|
|
$result[$userId] = true;
|
|
}
|
|
}
|
|
$result = array_keys($result);
|
|
sort($result, SORT_STRING);
|
|
|
|
return $result;
|
|
};
|
|
|
|
return $normalise($left) === $normalise($right);
|
|
}
|
|
}
|