yuebao_available_interest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\api\controller {
  3. class BaseApiController {
  4. protected int $userId = 0;
  5. protected string $lang = 'zh';
  6. public $request;
  7. public function __construct($userId = 0) { $this->userId = $userId; $this->request = \request(); }
  8. protected function success($message = '', $data = []) { return ['code' => 1, 'data' => $data, 'msg' => $message]; }
  9. protected function fail($message = '') { return ['code' => 0, 'msg' => $message]; }
  10. }
  11. }
  12. namespace app\api\lists {
  13. class BaseApiDataLists {
  14. protected int $userId = 0;
  15. protected string $lang = 'zh';
  16. protected array $params = [];
  17. protected array $searchWhere = [];
  18. public int $limitOffset = 0;
  19. public int $limitLength = 1;
  20. public function __construct($userId = 0, $type = null) { $this->userId = $userId; $this->params = ['type' => $type]; }
  21. public function setParams($params): self { $this->params = $params; return $this; }
  22. }
  23. }
  24. namespace {
  25. function request() { return $GLOBALS['activity_test_request']; }
  26. function config($key, $default = null) { return $default; }
  27. function env($key, $default = '') { return $key === 'app.bot_host' ? 'https://example.test' : $default; }
  28. function ensureUrl($url, $host = '') { return $url; }
  29. function json($value) { return $value; }
  30. class TestRequest {
  31. public array $query = [];
  32. public array $postData = [];
  33. public array $headers = [];
  34. public function param($key, $default = null) { return $this->postData[$key] ?? $this->query[$key] ?? $default; }
  35. public function get($key, $default = null) { return $this->query[$key] ?? $default; }
  36. public function header($key, $default = null) { return $this->headers[strtolower($key)] ?? $default; }
  37. public function post($key, $default = null, $filter = null) { $v = $this->postData[$key] ?? $default; return $filter ? $filter($v) : $v; }
  38. }
  39. $GLOBALS['activity_test_request'] = new TestRequest();
  40. $repo = getenv('ACTIVITY_SITE_REPO') ?: dirname(__DIR__, 3) . '/melbet_sport-api';
  41. $stage = getenv('ACTIVITY_SITE_STAGE') ?: $repo;
  42. $loader = require $repo . '/vendor/autoload.php';
  43. $loader->setPsr4('app\\', [$stage . '/app/', $repo . '/app/']);
  44. $db = new \think\DbManager();
  45. $db->setConfig(['default' => 'sqlite', 'connections' => ['sqlite' => ['type' => 'sqlite', 'database' => ':memory:', 'prefix' => '', 'fields_strict' => false]], 'auto_timestamp' => false]);
  46. \think\Container::getInstance()->instance(\think\DbManager::class, $db);
  47. \think\Container::getInstance()->instance('log', new \Psr\Log\NullLogger());
  48. $tables = [
  49. 'bot_users' => 'id INTEGER PRIMARY KEY,user_id TEXT,member_id TEXT,created_at TEXT,delete_time INTEGER,recharge_channel_group_id INTEGER',
  50. 'bot_user_login' => 'id INTEGER PRIMARY KEY,user_id TEXT,status INTEGER,platform TEXT',
  51. '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',
  52. 'bot_recharge_channel_group' => 'id INTEGER PRIMARY KEY,activity_type TEXT',
  53. '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,game_category TEXT,click_type TEXT DEFAULT "none",link TEXT,link_pc TEXT,link_h5 TEXT,click_position 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',
  54. '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)',
  55. 'bot_wallets' => 'id INTEGER PRIMARY KEY,member_id TEXT,available_balance TEXT,frozen_balance TEXT,yuebao TEXT,yuebao_invest TEXT',
  56. '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',
  57. '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',
  58. ];
  59. foreach ($tables as $table => $columns) { $db->execute('CREATE TABLE ' . $table . '(' . $columns . ')'); }
  60. $assertions = 0;
  61. function check($condition, $message) { global $assertions; ++$assertions; if (!$condition) { throw new \RuntimeException($message); } }
  62. function row($table, $data) { \think\facade\Db::table($table)->insert($data); }
  63. function activity($id, $override = []) {
  64. 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));
  65. }
  66. row('bot_users', ['id' => 1, 'user_id' => '1001', 'member_id' => '2001', 'created_at' => '2026-10-01 12:00:00', 'recharge_channel_group_id' => 1]);
  67. row('bot_recharge_channel_group', ['id' => 1, 'activity_type' => 'recharge,yuebao']);
  68. activity(10, ['sort'=>10, 'type'=>6, 'quota_enabled'=>1, 'reward_quota'=>1, 'params'=>'{"daily_interest_rate":"1.00","number_of_consecutive_days_of_deposit":7}']);
  69. row('bot_wallets', ['id' => 1, 'member_id' => '2001', 'available_balance' => '50.00', 'frozen_balance' => '0.00', 'yuebao' => '100.00', 'yuebao_invest' => '0.00']);
  70. 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]);
  71. $command = new \app\api\command\YuebaoInvest();
  72. $invest = new \ReflectionMethod($command, 'invest');
  73. $invest->invoke($command);
  74. $wallet = \think\facade\Db::table('bot_wallets')->where('id', 1)->find();
  75. check(bccomp($wallet['available_balance'], '151.00', 2) === 0, 'Automatic expiry credits principal and interest available');
  76. check(bccomp($wallet['frozen_balance'], '0.00', 2) === 0, 'Automatic expiry leaves frozen balance unchanged');
  77. check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 10)->value('rewarded_count') === 1, 'Automatic expiry records first distinct winner');
  78. $invest->invoke($command);
  79. check(\think\facade\Db::table('bot_yuebao_log')->count() === 1, 'Repeated expiry does not duplicate settlement');
  80. row('bot_users', ['id' => 2, 'user_id' => '1002', 'member_id' => '2002', 'created_at' => '2026-10-01 12:00:00', 'recharge_channel_group_id' => 1]);
  81. row('bot_wallets', ['id' => 2, 'member_id' => '2002', 'available_balance' => '50.00', 'frozen_balance' => '0.00', 'yuebao' => '100.00', 'yuebao_invest' => '0.00']);
  82. 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]);
  83. $invest->invoke($command);
  84. $wallet = \think\facade\Db::table('bot_wallets')->where('id', 2)->find();
  85. 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');
  86. \think\facade\Db::table('bot_wallets')->where('id', 1)->update(['yuebao' => '100.00']);
  87. 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]);
  88. $invest->invoke($command);
  89. $wallet = \think\facade\Db::table('bot_wallets')->where('id', 1)->find();
  90. check(bccomp($wallet['available_balance'], '252.00', 2) === 0 && bccomp($wallet['frozen_balance'], '0.00', 2) === 0, 'Existing winner continues to receive saved-rate interest at full quota');
  91. 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');
  92. $offset = 12 - (int)gmdate('G');
  93. date_default_timezone_set('Etc/GMT' . ($offset >= 0 ? '-' : '+') . abs($offset));
  94. activity(20, ['quota_enabled' => 1, 'type' => 6, 'reward_quota' => 1, 'params' => '{"daily_interest_rate":"1.00","number_of_consecutive_days_of_deposit":7}']);
  95. \think\facade\Db::table('bot_wallets')->where('id', 1)->update(['yuebao' => '100.00']);
  96. 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]);
  97. request()->postData = ['amount' => '100.00'];
  98. \think\facade\Db::table('bot_wallets')->where('id', 1)->update(['frozen_balance'=>'7.00']);
  99. $yuebao = new \app\api\controller\YuebaoController(1001);
  100. $out = $yuebao->out();
  101. $wallet = \think\facade\Db::table('bot_wallets')->where('id', 1)->find();
  102. check($out['code'] === 1 && bccomp((string)$out['data']['interest'], '1.00', 2) === 0, 'Manual out awards interest and occupies first-user slot');
  103. check(bccomp($wallet['available_balance'], '353.00', 2) === 0 && bccomp($wallet['frozen_balance'], '7.00', 2) === 0, 'Manual out credits principal and interest available and preserves historical frozen funds');
  104. check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', 20)->value('rewarded_count') === 1, 'Manual payout counts one distinct member');
  105. check($yuebao->out()['code'] === 0, 'Repeated manual out cannot settle the same position twice');
  106. \think\facade\Db::table('bot_wallets')->where('id', 2)->update(['yuebao' => '100.00']);
  107. 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]);
  108. check(bccomp((string)(new \app\api\controller\YuebaoController(1002))->out()['data']['interest'], '0', 2) === 0, 'Manual out denies other-member interest at full quota');
  109. $wallet = \think\facade\Db::table('bot_wallets')->where('id', 2)->find();
  110. check(bccomp($wallet['available_balance'], '250.00', 2) === 0 && bccomp($wallet['frozen_balance'], '0.00', 2) === 0, 'Manual quota denial preserves principal');
  111. \think\facade\Db::table('bot_wallets')->where('id', 1)->update(['yuebao' => '100.00']);
  112. 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]);
  113. check(bccomp((string)$yuebao->out()['data']['interest'], '1.00', 2) === 0, 'Legacy unlinked position keeps saved rate without invented activity association');
  114. check(\app\common\model\ActivityRewards::getYuebaoActivity('2001')['id'] === 10, 'Existing winner remains eligible for new positions when quota is full');
  115. row('bot_users', ['id' => 3, 'user_id' => '1003', 'member_id' => '2003', 'created_at' => '2026-10-01 12:00:00', 'recharge_channel_group_id' => 1]);
  116. row('bot_user_login', ['id' => 4, 'user_id' => '2003', 'status' => 1, 'platform' => 'iOS']);
  117. row('bot_wallets', ['id' => 3, 'member_id' => '2003', 'available_balance' => '500.00', 'frozen_balance' => '0.00', 'yuebao' => '0.00', 'yuebao_invest' => '0.00']);
  118. row('bot_balance_logs', ['member_id' => '2003', 'type' => 1, 'change_type' => '极速28投注', 'amount' => '-10000.00', 'created_at' => date('Y-m-d H:i:s')]);
  119. $newMember = new \app\api\controller\YuebaoController(1003);
  120. $beforeItems = \think\facade\Db::table('bot_yuebao_item')->count();
  121. check($newMember->add()['code'] === 0, 'Different member cannot open linked position when all eligible configs have full quotas');
  122. 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');
  123. 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}']);
  124. check($newMember->add()['code'] === 1, 'Qualified new position succeeds with user slots available');
  125. check((int)\think\facade\Db::table('bot_yuebao_item')->order('id', 'desc')->value('activity_id') === 21, 'New position links selected activity for payout');
  126. 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');
  127. activity(22, ['quota_enabled' => 1, 'type' => 6, 'sort' => 9, 'reward_quota' => 3, 'params' => '{"daily_interest_rate":"1.00"}']);
  128. \think\facade\Db::startTrans();
  129. $locked = \app\common\service\ActivityQuotaService::lockActivities([22, 21, 22, null, 0, 999]);
  130. check(array_keys($locked) === [21, 22], 'Quota locks use unique ascending IDs and exclude absent rows');
  131. \think\facade\Db::commit();
  132. foreach ([[4, '3001', [[50,22],[51,21]]], [5, '3002', [[60,21],[61,22]]]] as [$userPk, $userId, $items]) {
  133. row('bot_users', ['id' => $userPk, 'user_id' => $userId, 'member_id' => $userId, 'created_at' => '2026-10-01 12:00:00', 'recharge_channel_group_id' => 1]);
  134. row('bot_wallets', ['id' => $userPk, 'member_id' => $userId, 'available_balance' => '0.00', 'frozen_balance' => '0.00', 'yuebao' => '200.00', 'yuebao_invest' => '0.00']);
  135. foreach ($items as [$itemId, $activityId]) {
  136. 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]);
  137. }
  138. request()->postData = ['amount' => '200.00'];
  139. $multi = (new \app\api\controller\YuebaoController((int)$userId))->out();
  140. 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);
  141. }
  142. foreach ([21, 22] as $id) {
  143. check((int)\think\facade\Db::table('bot_activity_rewards')->where('id', $id)->value('rewarded_count') === 2, 'Each activity tracks two distinct winning members: ' . $id);
  144. }
  145. activity(30, ['quota_enabled' => 1, 'type' => 6, 'reward_quota' => 1]);
  146. row('bot_users', ['id' => 6, 'user_id' => '3003', 'member_id' => '3003', 'created_at' => '2026-10-01 12:00:00', 'recharge_channel_group_id' => 1]);
  147. row('bot_wallets', ['id' => 6, 'member_id' => '3003', 'available_balance' => '0.00', 'frozen_balance' => '0.00', 'yuebao' => '100.00', 'yuebao_invest' => '0.00']);
  148. 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]);
  149. $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");
  150. request()->postData = ['amount' => '100.00'];
  151. $failureController = new \app\api\controller\YuebaoController(3003);
  152. check($failureController->out()['code'] === 0, 'Actual settlement write failure rejects payout');
  153. 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');
  154. $failedWallet = \think\facade\Db::table('bot_wallets')->where('id', 6)->find();
  155. 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');
  156. 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');
  157. $db->execute('DROP TRIGGER fail_yuebao_log');
  158. 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');
  159. row('bot_users', ['id'=>8, 'user_id'=>'4000', 'member_id'=>'4000']);
  160. row('bot_wallets', ['id'=>8, 'member_id'=>'4000', 'available_balance'=>'0.1234567890', 'frozen_balance'=>'9.00', 'yuebao'=>'100', 'yuebao_invest'=>'0']);
  161. row('bot_yuebao_item', ['id'=>80, 'user_id'=>'4000', 'principal'=>'100', 'surplus_principal'=>'100', 'daily_interest_rate'=>'1', 'expire_time'=>time()+3600, 'create_time'=>time()-86400]);
  162. request()->postData = ['amount'=>'50.00'];
  163. $partial = (new \app\api\controller\YuebaoController(4000))->out();
  164. $partialWallet = \think\facade\Db::table('bot_wallets')->where('id',8)->find();
  165. check($partial['code'] === 1 && bccomp($partialWallet['available_balance'],'50.6234567890',10) === 0, 'Partial payout credits principal plus interest without losing wallet precision');
  166. check(bccomp($partialWallet['frozen_balance'],'9',10) === 0 && bccomp($partialWallet['yuebao'],'50',2) === 0, 'Partial payout preserves frozen funds and remaining principal');
  167. $payoutLogs = \think\facade\Db::table('bot_balance_logs')->where('member_id','4000')->order('id')->select()->toArray();
  168. check(count($payoutLogs) === 2 && bccomp($payoutLogs[0]['after_balance'],$payoutLogs[1]['before_balance'],10) === 0, 'Principal and interest ledger balances are continuous');
  169. $logs = \think\facade\Db::table('bot_balance_logs')->where('member_id', '2001')->where('change_type', '余额宝利息')->select()->toArray();
  170. check(count($logs) === 4, 'Exactly one interest credit per successful payout');
  171. foreach ($logs as $log) {
  172. check((int)$log['type'] === 1 && $log['frozen_status'] === null, 'Interest is available and never marked for unfreezing');
  173. check(bccomp(bcsub($log['after_balance'], $log['before_balance'], 10), $log['amount'], 10) === 0, 'Interest ledger balances reconcile');
  174. }
  175. echo 'PASS ' . $assertions . " adapter assertions (actual ThinkORM + isolated in-memory SQLite; no live database)\n";
  176. }