領収証発行サービス
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

172 lines
5.8KB

  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. $this->table->index([ColumnName::CREATED_AT], sprintf("%s_idx_CREATED_AT", $this->table->getTable()));
  67. $this->table->index([ColumnName::UPDATED_AT], sprintf("%s_idx_UPDATED_AT", $this->table->getTable()));
  68. return $this;
  69. }
  70. public function index(int $number, array $columns)
  71. {
  72. $indexName = $this->getIndexName($number);
  73. if ($this->forHistory) {
  74. $this->table->index($columns, $indexName);
  75. } else {
  76. $this->table->index([...$columns, ColumnName::DELETED_AT], $indexName);
  77. }
  78. return $this;
  79. }
  80. public function unique(int $number, array $columns)
  81. {
  82. $uniqueName = $this->getUniqueName($number);
  83. if ($this->forHistory) {
  84. $this->table->unique($columns, $uniqueName);
  85. } else {
  86. $this->table->unique([...$columns, ColumnName::DELETED_AT,], $uniqueName);
  87. }
  88. return $this;
  89. }
  90. public function dropIndex(int $number)
  91. {
  92. $indexName = $this->getIndexName($number);
  93. $this->table->dropIndex($indexName);
  94. return $this;
  95. }
  96. private function getIndexName(int $number)
  97. {
  98. return sprintf("%s_idx_%02d", $this->table->getTable(), $number);
  99. }
  100. private function getUniqueName(int $number)
  101. {
  102. return sprintf("%s_uq_%02d", $this->table->getTable(), $number);
  103. }
  104. public function contractId(bool $nullable = false)
  105. {
  106. $this->table->uuid(ColumnName::CONTRACT_ID)->comment("契約ID")->nullable($nullable);
  107. // $this->table->foreign(ColumnName::CONTRACT_ID)->references(ColumnName::ID)->on(Contract::getTableName());
  108. return $this;
  109. }
  110. public function receiptIssuingOrderId(bool $nullable = false)
  111. {
  112. $this->table->uuid(ColumnName::RECEIPT_ISSUING_ORDER_ID)->comment("領収証発行依頼ID")->nullable($nullable);
  113. // $this->table->foreign(ColumnName::RECEIPT_ISSUING_ORDER_ID)->references(ColumnName::ID)->on(ReceiptIssuingOrder::getTableName());
  114. return $this;
  115. }
  116. public function smsSendOrderId(bool $nullable = false)
  117. {
  118. $this->table->uuid(ColumnName::SMS_SEND_ORDER_ID)->comment("SMS送信依頼ID")->nullable($nullable);
  119. // $this->table->foreign(ColumnName::SMS_SEND_ORDER_ID)->references(ColumnName::ID)->on(SMSSendOrder::getTableName());
  120. return $this;
  121. }
  122. public function userId(bool $nullable = false, ?string $columnName = null, ?string $comment = null)
  123. {
  124. $columnName = $columnName ?? ColumnName::USER_ID;
  125. $comment = $comment ?? "ユーザーID";
  126. $this->table->uuid($columnName)->comment($comment)->nullable($nullable);
  127. // $this->table->foreign($columnName)->references(ColumnName::ID)->on(User::getTableName());
  128. return $this;
  129. }
  130. public function smsProviderId(bool $nullable = false)
  131. {
  132. $this->table->uuid(ColumnName::SMS_PROVIDER_ID)->comment("SMSプロバイダーID")->nullable($nullable);
  133. // $this->table->foreign(ColumnName::SMS_PROVIDER_ID)->references(ColumnName::ID)->on(SMSProvider::getTableName());
  134. return $this;
  135. }
  136. public function emailId(bool $nullable = false)
  137. {
  138. $this->table->uuid(ColumnName::EMAIL_ID)->comment("EメールID")->nullable($nullable);
  139. return $this;
  140. }
  141. }