領収証発行サービス
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

147 lines
4.9KB

  1. <?php
  2. namespace App\Util;
  3. use App\Models\ColumnName;
  4. use App\Models\Contract;
  5. use App\Models\Feature\IModelFeature;
  6. use App\Models\ReceiptIssuingOrder;
  7. use App\Models\SMSProvider;
  8. use App\Models\SMSSendOrder;
  9. use App\Models\User;
  10. use Closure;
  11. use Illuminate\Database\Schema\Blueprint;
  12. use Illuminate\Support\Facades\Schema;
  13. use Illuminate\Support\Str;
  14. class MigrationHelper
  15. {
  16. public static function createTable(string $tableName, Closure $schema)
  17. {
  18. Schema::create($tableName, function (Blueprint $table) use ($tableName, $schema) {
  19. $modelClassName = Str::singular('App\\Models\\' . Str::studly($tableName));
  20. if (class_exists($modelClassName)) {
  21. /**
  22. * @var IModelFeature
  23. */
  24. $model = new $modelClassName();
  25. $table->comment($model->getModelName());
  26. }
  27. $forHistory = false;
  28. if (Str::endsWith($tableName, "histories")) {
  29. $forHistory = true;
  30. }
  31. $helper = new MigrationHelper($table, $forHistory);
  32. $schema($table, $helper);
  33. });
  34. }
  35. public static function alterTable(string $tableName, Closure $schema)
  36. {
  37. Schema::table($tableName, function (Blueprint $table) use ($tableName, $schema) {
  38. $forHistory = false;
  39. if (Str::endsWith($tableName, "histories")) {
  40. $forHistory = true;
  41. }
  42. $helper = new MigrationHelper($table, $forHistory);
  43. $schema($table, $helper);
  44. });
  45. }
  46. private Blueprint $table;
  47. private bool $forHistory = false;
  48. public function __construct(Blueprint $table, bool $forHistory = false)
  49. {
  50. $this->table = $table;
  51. $this->forHistory = $forHistory;
  52. }
  53. public function baseColumn()
  54. {
  55. if ($this->forHistory) {
  56. $this->table->id('history_id')->comment("履歴ID");
  57. $this->table->uuid('id')->comment("ID");
  58. } else {
  59. $this->table->uuid('id')->primary()->comment("ID");
  60. }
  61. $this->table->unsignedBigInteger(ColumnName::CREATED_BY)->nullable()->comment("作成者ID");
  62. $this->table->unsignedBigInteger(ColumnName::UPDATED_BY)->nullable()->comment("更新者ID");
  63. $this->table->timestamp(ColumnName::CREATED_AT)->nullable()->comment("作成日時");
  64. $this->table->timestamp(ColumnName::UPDATED_AT)->nullable()->comment("更新日時");
  65. $this->table->timestamp(ColumnName::DELETED_AT)->nullable()->comment("論理削除日時");
  66. return $this;
  67. }
  68. public function index(int $number, array $columns)
  69. {
  70. $indexName = $this->getIndexName($number);
  71. if ($this->forHistory) {
  72. $this->table->index($columns, $indexName);
  73. } else {
  74. $this->table->index([ColumnName::DELETED_AT, ...$columns], $indexName);
  75. }
  76. return $this;
  77. }
  78. public function dropIndex(int $number)
  79. {
  80. $indexName = $this->getIndexName($number);
  81. $this->table->dropIndex($indexName);
  82. return $this;
  83. }
  84. private function getIndexName(int $number)
  85. {
  86. return sprintf("%s_idx_%02d", $this->table->getTable(), $number);
  87. }
  88. public function contractId(bool $nullable = false)
  89. {
  90. $this->table->uuid(ColumnName::CONTRACT_ID)->comment("契約ID")->nullable($nullable);
  91. $this->table->foreign(ColumnName::CONTRACT_ID)->references(ColumnName::ID)->on(Contract::getTableName());
  92. return $this;
  93. }
  94. public function receiptIssuingOrderId(bool $nullable = false)
  95. {
  96. $this->table->uuid(ColumnName::RECEIPT_ISSUING_ORDER_ID)->comment("領収証発行依頼ID")->nullable($nullable);
  97. $this->table->foreign(ColumnName::RECEIPT_ISSUING_ORDER_ID)->references(ColumnName::ID)->on(ReceiptIssuingOrder::getTableName());
  98. return $this;
  99. }
  100. public function smsSendOrderId(bool $nullable = false)
  101. {
  102. $this->table->uuid(ColumnName::SMS_SEND_ORDER_ID)->comment("SMS送信依頼ID")->nullable($nullable);
  103. $this->table->foreign(ColumnName::SMS_SEND_ORDER_ID)->references(ColumnName::ID)->on(SMSSendOrder::getTableName());
  104. return $this;
  105. }
  106. public function userId(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  107. {
  108. $columnName = $columnName ?? ColumnName::USER_ID;
  109. $comment = $comment ?? "ユーザーID";
  110. $this->table->uuid($columnName)->comment($comment)->nullable($nullable);
  111. $this->table->foreign($columnName)->references(ColumnName::ID)->on(User::getTableName());
  112. return $this;
  113. }
  114. public function smsProviderId(bool $nullable = false)
  115. {
  116. $this->table->uuid(ColumnName::SMS_PROVIDER_ID)->comment("SMSプロバイダーID")->nullable($nullable);
  117. $this->table->foreign(ColumnName::SMS_PROVIDER_ID)->references(ColumnName::ID)->on(SMSProvider::getTableName());
  118. return $this;
  119. }
  120. }