seed_mini_commissions_pagination_demo.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. require dirname(__DIR__) . '/vendor/autoload.php';
  4. \Dotenv\Dotenv::createUnsafeMutable(dirname(__DIR__))->load();
  5. \support\App::loadAllConfig(['route']);
  6. date_default_timezone_set('Asia/Shanghai');
  7. use app\miniapi\auth\TestPhoneIdentityProvider;
  8. use plugin\saiadmin\app\model\water\WaterAgent;
  9. use plugin\saiadmin\app\model\water\WaterLedger;
  10. use plugin\saiadmin\app\model\water\WaterModel;
  11. if (!TestPhoneIdentityProvider::enabled()) {
  12. throw new RuntimeException('Requires local test environment');
  13. }
  14. $result = WaterModel::transaction(function (): array {
  15. $agent = WaterAgent::query()->where('id', 7)->lockForUpdate()->first();
  16. if (!$agent || $agent->name !== '测试商贸') {
  17. throw new RuntimeException('Expected named test agent 7');
  18. }
  19. $base = 'TEST-MINI-COMMISSIONS-7-20261007';
  20. $header = WaterLedger::query()->where('enterprise_id', 49)->where('kind', 'demo_seed')->where('reference', $base)->first();
  21. if (!$header) {
  22. throw new RuntimeException('Initialize the original commission demo first');
  23. }
  24. $summary = json_decode($header->note, true, 512, JSON_THROW_ON_ERROR);
  25. $created = 0;
  26. for ($index = 1; $index <= 20; $index++) {
  27. $id = 100 + $index;
  28. $key = ['enterprise_id' => 49, 'kind' => 'demo_seed', 'reference' => $base . '-ITEM-' . $id];
  29. if (WaterLedger::query()->where($key)->exists()) {
  30. continue;
  31. }
  32. $status = $index % 5 === 0 ? 'adjustment' : ($index % 2 === 0 ? 'settled' : 'pending');
  33. $amount = $status === 'adjustment' ? -(500 + $index * 25) : 2400 + $index * 160;
  34. $item = [
  35. 'id' => $id,
  36. 'customer_name' => '【测试】分页企业' . str_pad((string) $index, 2, '0', STR_PAD_LEFT),
  37. 'title' => $status === 'adjustment' ? '售后退款调整' : '企业套餐佣金',
  38. 'status' => $status,
  39. 'status_text' => match ($status) {
  40. 'pending' => '待结算(观察期)', 'settled' => '已结算', default => '部分退款冲减'
  41. },
  42. 'amount_cents' => $amount,
  43. 'created_at' => date('Y-m-d', strtotime('-' . ($index % 7) . ' days')),
  44. ];
  45. WaterLedger::query()->create($key + [
  46. 'sku_id' => 5, 'quantity' => 0, 'created_by' => 1,
  47. 'note' => json_encode($item, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
  48. 'created_at' => date('Y-m-d H:i:s'),
  49. ]);
  50. $summary['total_cents'] += $amount;
  51. if ($status === 'pending') {
  52. $summary['pending_cents'] += $amount;
  53. } else {
  54. $summary['withdrawable_cents'] += $amount;
  55. }
  56. $created++;
  57. }
  58. if ($created > 0) {
  59. $header->note = json_encode($summary, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
  60. $header->save();
  61. }
  62. return ['created' => $created, 'total_records' => WaterLedger::query()->where('enterprise_id', 49)->where('kind', 'demo_seed')->where('reference', 'like', $base . '-ITEM-%')->count()];
  63. });
  64. echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL;