領収証発行サービス
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

163 行
5.4KB

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