| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
- namespace app\api\controller {
- class BaseApiController {
- protected int $userId = 0;
- protected string $lang = 'zh';
- public $request;
- public function __construct($userId = 0) { $this->userId = $userId; $this->request = \request(); }
- protected function success($message = '', $data = []) { return ['code' => 1, 'data' => $data, 'msg' => $message]; }
- protected function fail($message = '') { return ['code' => 0, 'msg' => $message]; }
- }
- }
- namespace app\api\lists {
- class BaseApiDataLists {
- protected int $userId = 0;
- protected string $lang = 'zh';
- protected array $params = [];
- protected array $searchWhere = [];
- public int $limitOffset = 0;
- public int $limitLength = 1;
- public function __construct($userId = 0, $type = null) { $this->userId = $userId; $this->params = ['type' => $type]; }
- public function setParams($params): self { $this->params = $params; return $this; }
- }
- }
- namespace {
- function request() { return $GLOBALS['activity_test_request']; }
- function env($key, $default = '') { return $default; }
- function ensureUrl($url, $host = '') { return $url; }
- function json($value) { return $value; }
- class TestRequest {
- public array $query = [];
- public array $postData = [];
- public array $headers = [];
- public function get($key, $default = null) { return $this->query[$key] ?? $default; }
- public function header($key, $default = null) { return $this->headers[strtolower($key)] ?? $default; }
- public function post($key, $default = null, $filter = null) { $v = $this->postData[$key] ?? $default; return $filter ? $filter($v) : $v; }
- }
- $GLOBALS['activity_test_request'] = new TestRequest();
- $repo = getenv('ACTIVITY_SITE_REPO') ?: dirname(__DIR__, 3) . '/melbet_sport-api';
- $stage = getenv('ACTIVITY_SITE_STAGE') ?: $repo;
- $loader = require $repo . '/vendor/autoload.php';
- $loader->setPsr4('app\\', [$stage . '/app/', $repo . '/app/']);
- $db = new \think\DbManager();
- $db->setConfig(['default' => 'sqlite', 'connections' => ['sqlite' => ['type' => 'sqlite', 'database' => ':memory:', 'prefix' => '', 'fields_strict' => false]], 'auto_timestamp' => false]);
- \think\Container::getInstance()->instance(\think\DbManager::class, $db);
- $tables = [
- 'bot_users' => 'id INTEGER PRIMARY KEY,user_id TEXT,member_id TEXT,created_at TEXT,delete_time INTEGER,recharge_channel_group_id INTEGER',
- 'bot_user_login' => 'id INTEGER PRIMARY KEY,user_id TEXT,status INTEGER,platform TEXT',
- 'bot_balance_logs' => 'id INTEGER PRIMARY KEY,member_id TEXT,type INTEGER,change_type TEXT,amount TEXT,created_at TEXT,updated_at TEXT,remark TEXT,before_balance TEXT,after_balance TEXT,related_id INTEGER,frozen_status INTEGER',
- 'bot_recharge_channel_group' => 'id INTEGER PRIMARY KEY,activity_type TEXT',
- 'bot_activity_rewards' => 'id INTEGER PRIMARY KEY,title TEXT,sub_title TEXT,start_time INTEGER,end_time INTEGER,detail_image TEXT,type INTEGER,lang TEXT,pc_image TEXT,detail_banner TEXT,pc_banner TEXT,publish_platforms TEXT,budget TEXT,budget_used TEXT DEFAULT "0",quota_enabled INTEGER DEFAULT 0,reward_quota INTEGER,rewarded_count INTEGER DEFAULT 0,sort INTEGER DEFAULT 0,register_time_limited INTEGER,register_start_time TEXT,register_end_time TEXT,register_platforms TEXT,deposit_limited INTEGER,deposit_min TEXT,deposit_max TEXT,show_ineligible INTEGER DEFAULT 1,status INTEGER,params TEXT,created_at TEXT,updated_at TEXT',
- 'bot_activity_reward_recipients' => 'id INTEGER PRIMARY KEY,activity_id INTEGER NOT NULL,member_id TEXT NOT NULL,created_at TEXT,UNIQUE(activity_id,member_id)',
- 'bot_wallets' => 'id INTEGER PRIMARY KEY,member_id TEXT,available_balance TEXT,frozen_balance TEXT,yuebao TEXT,yuebao_invest TEXT',
- 'bot_yuebao_item' => 'id INTEGER PRIMARY KEY,user_id TEXT,activity_id INTEGER,principal TEXT,surplus_principal TEXT,status INTEGER DEFAULT 1,daily_interest_rate TEXT,expire_time INTEGER,create_time INTEGER DEFAULT 0,update_time INTEGER',
- 'bot_yuebao_log' => 'id INTEGER PRIMARY KEY,user_id TEXT,item_id INTEGER,amount TEXT,type INTEGER,interest TEXT,hold_hours TEXT,create_time INTEGER,update_time INTEGER',
- ];
- foreach ($tables as $table => $columns) { $db->execute('CREATE TABLE ' . $table . '(' . $columns . ')'); }
- $assertions = 0;
- function check($condition, $message) { global $assertions; ++$assertions; if (!$condition) { throw new \RuntimeException($message); } }
- function row($table, $data) { \think\facade\Db::table($table)->insert($data); }
- function activity($id, $override = []) {
- row('bot_activity_rewards', array_merge(['id' => $id, 'title' => 'Activity ' . $id, 'status' => 1, 'type' => 5, 'lang' => 'zh', 'sort' => 0, 'start_time' => time() - 100, 'end_time' => time() + 10000, 'show_ineligible' => 1, 'budget_used' => '0.00', 'detail_image' => '', 'pc_image' => '', 'detail_banner' => '', 'pc_banner' => ''], $override));
- }
- row('bot_users', ['id' => 1, 'user_id' => '1001', 'member_id' => '2001', 'created_at' => '2026-10-01 12:00:00', 'recharge_channel_group_id' => 1]);
- row('bot_user_login', ['id' => 1, 'user_id' => '2001', 'status' => 0, 'platform' => 'PC']);
- row('bot_user_login', ['id' => 2, 'user_id' => '2001', 'status' => 1, 'platform' => 'iOS']);
- row('bot_user_login', ['id' => 3, 'user_id' => '2001', 'status' => 1, 'platform' => 'PC']);
- row('bot_recharge_channel_group', ['id' => 1, 'activity_type' => 'recharge,yuebao']);
- row('bot_balance_logs', ['member_id' => '2001', 'type' => 1, 'change_type' => '充值', 'amount' => '99.99']);
- row('bot_balance_logs', ['member_id' => '2001', 'type' => 1, 'change_type' => '三方充值', 'amount' => '100.00']);
- row('bot_balance_logs', ['member_id' => '2001', 'type' => 2, 'change_type' => '充值', 'amount' => '999.00']);
- row('bot_balance_logs', ['member_id' => '2001', 'type' => 1, 'change_type' => '即充即送', 'amount' => '999.00']);
- $service = new \app\common\service\MemberActivityService(1001, 'PC');
- check($service->memberId() === '2001', 'User ID must map to member ID');
- $context = $service->context();
- check($context['registered_at'] === '2026-10-01 12:00:00', 'Raw member registration time');
- check($context['register_platform'] === 'APP', 'First successful login must determine registration platform');
- check(bccomp($context['deposit_total'], '199.99', 2) === 0, 'Lifetime deposit excludes bonuses and frozen balance');
- $queryCount = $db->getQueryTimes();
- $service->context();
- check($db->getQueryTimes() === $queryCount, 'Member context is cached');
- activity(1, ['sort' => 4, 'deposit_limited' => 1, 'deposit_min' => '199.99', 'deposit_max' => '199.99', 'register_platforms' => '["APP"]']);
- activity(2, ['sort' => 9, 'publish_platforms' => '["H5"]']);
- activity(3, ['sort' => 8, 'show_ineligible' => 0, 'deposit_limited' => 1, 'deposit_min' => '300', 'deposit_max' => '400']);
- activity(4, ['sort' => 7, 'show_ineligible' => 1, 'register_platforms' => '["PC"]']);
- activity(5, ['sort' => 10, 'status' => 0]);
- activity(6, ['type' => 2, 'sort' => 100, 'publish_platforms' => '["H5"]', 'show_ineligible' => 0, 'register_platforms' => '["PC"]']);
- request()->query = ['platform' => 'PC'];
- $lists = new \app\api\lists\ActivityRewardsLists(1001);
- check($lists->count() === 3, 'Visible count is before pagination');
- $queryCount = $db->getQueryTimes();
- $page = $lists->lists();
- check(count($page) === 1 && $page[0]['id'] === 4 && $page[0]['is_eligible'] === false, 'Visible ineligible activity sorted first');
- check($db->getQueryTimes() === $queryCount, 'List and count share same visible rows');
- $lists->limitOffset = 1;
- check($lists->lists()[0]['id'] === 1, 'Filtered-out rows do not create pagination gaps');
- $lists->limitOffset = 2;
- check($lists->lists()[0]['id'] === 6, 'Football ignores new fields and has effective sort zero');
- $controller = new \app\api\controller\IndexController(1001);
- foreach ([2, 3, 5] as $id) { request()->query['id'] = $id; check($controller->activityInfo()['code'] === 0, 'Hidden ID detail must be denied: ' . $id); }
- request()->query['id'] = 4;
- check($controller->activityInfo()['data']['is_eligible'] === false, 'Visible ineligible detail exposes eligibility flag');
- activity(7, ['type' => 4]);
- request()->query['id'] = 7;
- check($controller->activityInfo()['code'] === 0, 'Detail enforces recharge group without type query');
- check((new \app\api\lists\ActivityRewardsLists(1001))->count() === 3, 'Omitted type list applies same disabled old-user group as detail');
- check((new \app\api\lists\ActivityRewardsLists(1001, 4))->count() === 0, 'Explicit old-user type cannot bypass disabled group');
- request()->query = [];
- check((new \app\common\service\MemberActivityService(1001))->visibleActivity(['type' => 5, 'publish_platforms' => '["PC"]']) === null, 'Limited publication fails closed without explicit platform');
- activity(8, ['type' => 6]);
- \think\facade\Db::table('bot_recharge_channel_group')->where('id', 1)->update(['activity_type' => 'recharge']);
- check((new \app\api\lists\ActivityRewardsLists(1001, 6))->count() === 0, 'Explicit Yuebao type cannot bypass disabled group');
- request()->query = ['id' => 8];
- check($controller->activityInfo()['code'] === 0, 'Yuebao detail matches group-disabled list');
- \think\facade\Db::table('bot_recharge_channel_group')->where('id', 1)->update(['activity_type' => 'recharge,yuebao']);
- \think\facade\Db::table('bot_activity_rewards')->where('id', 8)->delete();
- $guest = new \app\common\service\MemberActivityService(0, 'PC');
- check($guest->visibleActivity(['type' => 5, 'show_ineligible' => 0]) === null, 'Guests cannot see hidden ineligible activities');
- check($guest->visibleActivity(['type' => 2, 'show_ineligible' => 0])['is_eligible'], 'Football ignores guest eligibility');
- activity(10, ['quota_enabled' => 1, 'type' => 6, 'sort' => 3, 'reward_quota' => 1, 'params' => '{"daily_interest_rate":"1.00","number_of_consecutive_days_of_deposit":7}']);
- activity(11, ['quota_enabled' => 1, 'type' => 6, 'sort' => 4, 'register_platforms' => '["PC"]']);
- check(\app\common\model\ActivityRewards::getYuebaoActivity('2001')['id'] === 10, 'Yuebao skips higher-sorted ineligible activity');
- check(\think\facade\Db::table('bot_activity_reward_recipients')->count() === 0, 'Eligibility and config reads do not occupy a winner slot');
- \think\facade\Db::startTrans();
- check(\app\common\service\ActivityQuotaService::consume(10, '2001', '100.00'), 'First positive reward occupies one distinct-user slot');
- $recipientCurrentReads = 0;
- $db->event('before_find', function ($query) use (&$recipientCurrentReads) {
- if ($query->getTable() === 'bot_activity_reward_recipients' && $query->getOptions('lock')) {
- ++$recipientCurrentReads;
- }
- });
- $fullQuota = \think\facade\Db::table('bot_activity_rewards')->where('id', 10)->lock(true)->find();
- check(\app\common\service\ActivityQuotaService::canAward($fullQuota, '2001', true), 'Current-read availability recognizes recorded winner at full quota');
- check($recipientCurrentReads === 1, 'Locked availability reads recipient with row-lock query');
- $beforeRepeatReads = $recipientCurrentReads;
- check(\app\common\service\ActivityQuotaService::consume(10, '2001', '999.00'), 'Same winner can receive another reward when the quota is full');
- check($recipientCurrentReads === $beforeRepeatReads + 1, 'Repeated consume uses exactly one current recipient read after activity lock');
- check(!\app\common\service\ActivityQuotaService::consume(10, '2002', '0.01'), 'A different member cannot win after the user quota is full');
- check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 10)->value('rewarded_count') === 1, 'Repeated same-member rewards count once independent of monetary amount');
- \think\facade\Db::rollback();
- check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 10)->value('rewarded_count') === 0 && \think\facade\Db::table('bot_activity_reward_recipients')->count() === 0, 'Recipient and awarded-user counter roll back together');
- activity(12, ['quota_enabled' => 1, 'reward_quota' => 0]);
- \think\facade\Db::startTrans();
- check(\app\common\service\ActivityQuotaService::consume(12, '2001', '0.00'), 'Zero reward does not need or occupy a winner slot');
- check(!\app\common\service\ActivityQuotaService::consume(12, '2001', '1.00'), 'Zero quota blocks first positive reward');
- check(!\app\common\service\ActivityQuotaService::consume(12, '2001', '-1.00'), 'Negative reward does not occupy a winner slot');
- \think\facade\Db::commit();
- check(\think\facade\Db::table('bot_activity_reward_recipients')->where('activity_id', 12)->count() === 0, 'No recipient records for zero or denied rewards');
- activity(13, ['quota_enabled' => 1, 'reward_quota' => 10]);
- \think\facade\Db::startTrans();
- for ($i = 1; $i <= 10; ++$i) {
- check(\app\common\service\ActivityQuotaService::consume(13, 'winner-' . $i, (string)$i . '.00'), 'Distinct winner slot ' . $i . ' is allowed');
- }
- check(!\app\common\service\ActivityQuotaService::consume(13, 'winner-11', '1.00'), 'The eleventh distinct member cannot receive a quota-ten reward');
- check(\app\common\service\ActivityQuotaService::consume(13, 'winner-1', '100000.00'), 'Existing winner remains allowed at ten distinct recipients');
- \think\facade\Db::commit();
- check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 13)->value('rewarded_count') === 10 && \think\facade\Db::table('bot_activity_reward_recipients')->where('activity_id', 13)->count() === 10, 'Quota ten counts exactly ten unique winners');
- activity(14, ['quota_enabled' => 1, 'reward_quota' => null, 'budget' => '0.00', 'budget_used' => '999999.00']);
- \think\facade\Db::startTrans();
- check(\app\common\service\ActivityQuotaService::consume(14, '2001', '99999.00'), 'Unlimited user quota ignores historical monetary budget');
- \think\facade\Db::commit();
- $public = $service->visibleActivity(\think\facade\Db::table('bot_activity_rewards')->where('id', 14)->find());
- check(array_key_exists('reward_quota', $public) && array_key_exists('rewarded_count', $public) && (int)$public['quota_enabled'] === 1, 'New activity response exposes quota mode and distinct-winner metadata');
- row('bot_wallets', ['id' => 1, 'member_id' => '2001', 'available_balance' => '50.00', 'frozen_balance' => '0.00', 'yuebao' => '100.00', 'yuebao_invest' => '0.00']);
- row('bot_yuebao_item', ['id' => 1, 'user_id' => '1001', 'activity_id' => 10, 'principal' => '100.00', 'surplus_principal' => '100.00', 'daily_interest_rate' => '1.00', 'expire_time' => time() - 1, 'create_time' => time() - 86400]);
- $command = new \app\api\command\YuebaoInvest();
- $invest = new \ReflectionMethod($command, 'invest');
- $invest->invoke($command);
- $wallet = \think\facade\Db::table('bot_wallets')->where('id', 1)->find();
- check(bccomp($wallet['available_balance'], '150.00', 2) === 0, 'Automatic expiry returns principal');
- check(bccomp($wallet['frozen_balance'], '1.00', 2) === 0, 'Automatic expiry awards interest once');
- check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 10)->value('rewarded_count') === 1, 'Automatic expiry records first distinct winner');
- $invest->invoke($command);
- check(\think\facade\Db::table('bot_yuebao_log')->count() === 1, 'Repeated expiry does not duplicate settlement');
- row('bot_users', ['id' => 2, 'user_id' => '1002', 'member_id' => '2002', 'created_at' => '2026-10-01 12:00:00', 'recharge_channel_group_id' => 1]);
- row('bot_wallets', ['id' => 2, 'member_id' => '2002', 'available_balance' => '50.00', 'frozen_balance' => '0.00', 'yuebao' => '100.00', 'yuebao_invest' => '0.00']);
- row('bot_yuebao_item', ['id' => 2, 'user_id' => '1002', 'activity_id' => 10, 'principal' => '100.00', 'surplus_principal' => '100.00', 'daily_interest_rate' => '1.00', 'expire_time' => time() - 1, 'create_time' => time() - 86400]);
- $invest->invoke($command);
- $wallet = \think\facade\Db::table('bot_wallets')->where('id', 2)->find();
- check(bccomp($wallet['available_balance'], '150.00', 2) === 0 && bccomp($wallet['frozen_balance'], '0.00', 2) === 0, 'Full winner quota returns other member principal without interest');
- \think\facade\Db::table('bot_wallets')->where('id', 1)->update(['yuebao' => '100.00']);
- row('bot_yuebao_item', ['id' => 3, 'user_id' => '1001', 'activity_id' => 10, 'principal' => '100.00', 'surplus_principal' => '100.00', 'daily_interest_rate' => '1.00', 'expire_time' => time() - 1, 'create_time' => time() - 86400]);
- $invest->invoke($command);
- $wallet = \think\facade\Db::table('bot_wallets')->where('id', 1)->find();
- check(bccomp($wallet['available_balance'], '250.00', 2) === 0 && bccomp($wallet['frozen_balance'], '2.00', 2) === 0, 'Existing winner continues to receive saved-rate interest at full quota');
- check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 10)->value('rewarded_count') === 1, 'Second position of same member does not add another winner');
- $offset = 12 - (int)gmdate('G');
- date_default_timezone_set('Etc/GMT' . ($offset >= 0 ? '-' : '+') . abs($offset));
- activity(20, ['quota_enabled' => 1, 'type' => 6, 'reward_quota' => 1, 'params' => '{"daily_interest_rate":"1.00","number_of_consecutive_days_of_deposit":7}']);
- \think\facade\Db::table('bot_wallets')->where('id', 1)->update(['yuebao' => '100.00']);
- row('bot_yuebao_item', ['id' => 4, 'user_id' => '1001', 'activity_id' => 20, 'principal' => '100.00', 'surplus_principal' => '100.00', 'daily_interest_rate' => '1.00', 'expire_time' => time() + 100, 'create_time' => time() - 86400]);
- request()->postData = ['amount' => '100.00'];
- $yuebao = new \app\api\controller\YuebaoController(1001);
- $out = $yuebao->out();
- $wallet = \think\facade\Db::table('bot_wallets')->where('id', 1)->find();
- check($out['code'] === 1 && bccomp((string)$out['data']['interest'], '1.00', 2) === 0, 'Manual out awards interest and occupies first-user slot');
- check(bccomp($wallet['available_balance'], '350.00', 2) === 0 && bccomp($wallet['frozen_balance'], '3.00', 2) === 0, 'Manual out credits principal available and interest frozen once');
- check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 20)->value('rewarded_count') === 1, 'Manual payout counts one distinct member');
- check($yuebao->out()['code'] === 0, 'Repeated manual out cannot settle the same position twice');
- \think\facade\Db::table('bot_wallets')->where('id', 2)->update(['yuebao' => '100.00']);
- row('bot_yuebao_item', ['id' => 5, 'user_id' => '1002', 'activity_id' => 20, 'principal' => '100.00', 'surplus_principal' => '100.00', 'daily_interest_rate' => '1.00', 'expire_time' => time() + 100, 'create_time' => time() - 86400]);
- check(bccomp((string)(new \app\api\controller\YuebaoController(1002))->out()['data']['interest'], '0', 2) === 0, 'Manual out denies other-member interest at full quota');
- $wallet = \think\facade\Db::table('bot_wallets')->where('id', 2)->find();
- check(bccomp($wallet['available_balance'], '250.00', 2) === 0 && bccomp($wallet['frozen_balance'], '0.00', 2) === 0, 'Manual quota denial preserves principal');
- \think\facade\Db::table('bot_wallets')->where('id', 1)->update(['yuebao' => '100.00']);
- row('bot_yuebao_item', ['id' => 6, 'user_id' => '1001', 'activity_id' => null, 'principal' => '100.00', 'surplus_principal' => '100.00', 'daily_interest_rate' => '1.00', 'expire_time' => time() + 100, 'create_time' => time() - 86400]);
- check(bccomp((string)$yuebao->out()['data']['interest'], '1.00', 2) === 0, 'Legacy unlinked position keeps saved rate without invented activity association');
- check(\app\common\model\ActivityRewards::getYuebaoActivity('2001')['id'] === 10, 'Existing winner remains eligible for new positions when quota is full');
- row('bot_users', ['id' => 3, 'user_id' => '1003', 'member_id' => '2003', 'created_at' => '2026-10-01 12:00:00', 'recharge_channel_group_id' => 1]);
- row('bot_user_login', ['id' => 4, 'user_id' => '2003', 'status' => 1, 'platform' => 'iOS']);
- row('bot_wallets', ['id' => 3, 'member_id' => '2003', 'available_balance' => '500.00', 'frozen_balance' => '0.00', 'yuebao' => '0.00', 'yuebao_invest' => '0.00']);
- row('bot_balance_logs', ['member_id' => '2003', 'type' => 1, 'change_type' => '极速28投注', 'amount' => '-10000.00', 'created_at' => date('Y-m-d H:i:s')]);
- $newMember = new \app\api\controller\YuebaoController(1003);
- $beforeItems = \think\facade\Db::table('bot_yuebao_item')->count();
- check($newMember->add()['code'] === 0, 'Different member cannot open linked position when all eligible configs have full quotas');
- check(bccomp((string)\think\facade\Db::table('bot_wallets')->where('id', 3)->value('available_balance'), '500.00', 2) === 0 && \think\facade\Db::table('bot_yuebao_item')->count() === $beforeItems, 'Denied new position leaves wallet and positions unchanged');
- activity(21, ['quota_enabled' => 1, 'type' => 6, 'sort' => 8, 'reward_quota' => 3, 'params' => '{"daily_interest_rate":"1.00","number_of_consecutive_days_of_deposit":7}']);
- check($newMember->add()['code'] === 1, 'Qualified new position succeeds with user slots available');
- check((int)\think\facade\Db::table('bot_yuebao_item')->order('id', 'desc')->value('activity_id') === 21, 'New position links selected activity for payout');
- check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 21)->value('rewarded_count') === 0 && \think\facade\Db::table('bot_activity_reward_recipients')->where('activity_id', 21)->count() === 0, 'Principal transfer and position creation do not consume a winner slot');
- activity(22, ['quota_enabled' => 1, 'type' => 6, 'sort' => 9, 'reward_quota' => 3, 'params' => '{"daily_interest_rate":"1.00"}']);
- \think\facade\Db::startTrans();
- $locked = \app\common\service\ActivityQuotaService::lockActivities([22, 21, 22, null, 0, 999]);
- check(array_keys($locked) === [21, 22], 'Quota locks use unique ascending IDs and exclude absent rows');
- \think\facade\Db::commit();
- foreach ([[4, '3001', [[50,22],[51,21]]], [5, '3002', [[60,21],[61,22]]]] as [$userPk, $userId, $items]) {
- row('bot_users', ['id' => $userPk, 'user_id' => $userId, 'member_id' => $userId, 'created_at' => '2026-10-01 12:00:00', 'recharge_channel_group_id' => 1]);
- row('bot_wallets', ['id' => $userPk, 'member_id' => $userId, 'available_balance' => '0.00', 'frozen_balance' => '0.00', 'yuebao' => '200.00', 'yuebao_invest' => '0.00']);
- foreach ($items as [$itemId, $activityId]) {
- row('bot_yuebao_item', ['id' => $itemId, 'user_id' => $userId, 'activity_id' => $activityId, 'principal' => '100.00', 'surplus_principal' => '100.00', 'daily_interest_rate' => '1.00', 'expire_time' => time() + 100, 'create_time' => time() - 86400]);
- }
- request()->postData = ['amount' => '200.00'];
- $multi = (new \app\api\controller\YuebaoController((int)$userId))->out();
- check($multi['code'] === 1 && bccomp((string)$multi['data']['interest'], '2.00', 2) === 0, 'Two linked quota payouts succeed independently of FIFO activity order for ' . $userId);
- }
- foreach ([21, 22] as $id) {
- check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', $id)->value('rewarded_count') === 2, 'Each activity tracks two distinct winning members: ' . $id);
- }
- activity(23, ['quota_enabled' => 1, 'type' => 6, 'sort' => 100, 'reward_quota' => 1, 'params' => '{"daily_interest_rate":"999.00"}']);
- $invalidateCandidates = true;
- $db->event('before_find', function ($query) use (&$invalidateCandidates) {
- if ($invalidateCandidates && $query->getTable() === 'bot_activity_rewards' && $query->getOptions('lock')) {
- $invalidateCandidates = false;
- \think\facade\Db::table('bot_activity_rewards')->where('id', 23)->delete();
- \think\facade\Db::table('bot_activity_rewards')->where('id', 22)->update(['status' => 0]);
- }
- });
- \think\facade\Db::startTrans();
- $fresh = \app\common\model\ActivityRewards::getYuebaoActivity('2001');
- \think\facade\Db::commit();
- check($fresh['id'] === 21 && (string)$fresh['daily_interest_rate'] === '1.00', 'New position config excludes deleted or disabled snapshots after quota locks');
- activity(30, ['quota_enabled' => 1, 'type' => 6, 'reward_quota' => 1]);
- row('bot_users', ['id' => 6, 'user_id' => '3003', 'member_id' => '3003', 'created_at' => '2026-10-01 12:00:00', 'recharge_channel_group_id' => 1]);
- row('bot_wallets', ['id' => 6, 'member_id' => '3003', 'available_balance' => '0.00', 'frozen_balance' => '0.00', 'yuebao' => '100.00', 'yuebao_invest' => '0.00']);
- row('bot_yuebao_item', ['id' => 70, 'user_id' => '3003', 'activity_id' => 30, 'principal' => '100.00', 'surplus_principal' => '100.00', 'daily_interest_rate' => '1.00', 'expire_time' => time() + 100, 'create_time' => time() - 86400]);
- $db->execute("CREATE TRIGGER fail_yuebao_log BEFORE INSERT ON bot_yuebao_log WHEN NEW.item_id = 70 BEGIN SELECT RAISE(ABORT, 'simulated payout write failure'); END");
- request()->postData = ['amount' => '100.00'];
- $failureController = new \app\api\controller\YuebaoController(3003);
- check($failureController->out()['code'] === 0, 'Actual settlement write failure rejects payout');
- check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 30)->value('rewarded_count') === 0 && \think\facade\Db::table('bot_activity_reward_recipients')->where('activity_id', 30)->count() === 0, 'Failed payout rolls back recipient and distinct-winner slot');
- $failedWallet = \think\facade\Db::table('bot_wallets')->where('id', 6)->find();
- check(bccomp($failedWallet['yuebao'], '100.00', 2) === 0 && bccomp($failedWallet['available_balance'], '0.00', 2) === 0 && bccomp($failedWallet['frozen_balance'], '0.00', 2) === 0, 'Failed payout leaves original principal and balances intact');
- check((int)\think\facade\Db::table('bot_yuebao_item')->where('id', 70)->value('status') === 1 && bccomp((string)\think\facade\Db::table('bot_yuebao_item')->where('id', 70)->value('surplus_principal'), '100.00', 2) === 0, 'Failed payout keeps the position unsettled');
- $db->execute('DROP TRIGGER fail_yuebao_log');
- check($failureController->out()['code'] === 1 && (int)\think\facade\Db::table('bot_activity_rewards')->where('id', 30)->value('rewarded_count') === 1, 'Retried successful payout occupies exactly one winner slot');
- activity(31, ['quota_enabled' => 0, 'budget' => '10.00', 'budget_used' => '0.00', 'reward_quota' => 0]);
- \think\facade\Db::startTrans();
- check(\app\common\service\ActivityQuotaService::consume(31, '2001', '6.00'), 'Legacy activity still accepts six under monetary budget ten');
- check(!\app\common\service\ActivityQuotaService::consume(31, '2002', '5.00'), 'Legacy activity denies five when only four money units remain');
- check(!\app\common\service\ActivityQuotaService::consume(31, '2001', '5.00'), 'Legacy monetary cap applies to repeat member exactly as before');
- check(bccomp((string)\think\facade\Db::table('bot_activity_rewards')->where('id', 31)->value('budget_used'), '6.00', 2) === 0, 'Denied legacy payout preserves consumed monetary budget');
- check(\app\common\service\ActivityQuotaService::consume(31, '2001', '4.00'), 'Legacy activity accepts exact remaining monetary budget');
- \think\facade\Db::commit();
- $legacyData = \think\facade\Db::table('bot_activity_rewards')->where('id', 31)->find();
- check(!\app\common\service\ActivityQuotaService::canAward($legacyData, '2001'), 'Legacy exhausted monetary budget remains exhausted');
- check((int)$legacyData['rewarded_count'] === 0 && \think\facade\Db::table('bot_activity_reward_recipients')->where('activity_id', 31)->count() === 0, 'Legacy payouts never write distinct-user recipients or new count');
- $legacyPublic = $service->visibleActivity($legacyData);
- check(array_key_exists('budget', $legacyPublic) && array_key_exists('budget_used', $legacyPublic) && bccomp((string)$legacyPublic['budget'], '10.00', 2) === 0 && bccomp((string)$legacyPublic['budget_used'], '10.00', 2) === 0 && (int)$legacyPublic['quota_enabled'] === 0, 'Legacy activity response preserves original monetary fields and marks legacy mode');
- activity(32, ['quota_enabled' => 0, 'budget' => null, 'reward_quota' => 0]);
- \think\facade\Db::startTrans();
- check(\app\common\service\ActivityQuotaService::consume(32, '2002', '1000.00'), 'Legacy unlimited monetary budget ignores new zero-user quota');
- \think\facade\Db::commit();
- check(bccomp((string)\think\facade\Db::table('bot_activity_rewards')->where('id', 32)->value('budget_used'), '1000.00', 2) === 0 && \think\facade\Db::table('bot_activity_reward_recipients')->where('activity_id', 32)->count() === 0, 'Legacy unlimited payouts retain money usage audit and do not count quota winners');
- activity(33, ['type' => 2, 'quota_enabled' => 1, 'reward_quota' => 0, 'budget' => '0.00']);
- $footballQuota = \think\facade\Db::table('bot_activity_rewards')->where('id', 33)->find();
- check(\app\common\service\ActivityQuotaService::canAward($footballQuota, null), 'Football bypasses both modes even with new immutable marker enabled');
- \think\facade\Db::startTrans();
- check(\app\common\service\ActivityQuotaService::consume(33, '2001', '1000.00'), 'Football bypasses zero quota and zero monetary budget');
- \think\facade\Db::commit();
- check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 33)->value('rewarded_count') === 0 && \think\facade\Db::table('bot_activity_reward_recipients')->where('activity_id', 33)->count() === 0, 'Football never registers or consumes quota winners');
- echo 'PASS ' . $assertions . " adapter assertions (actual ThinkORM + isolated in-memory SQLite; no live database)\n";
- }
|