Files
school2/database/migrations/2026_08_09_000001_create_activity_table.php
2026-08-09 11:23:54 +08:00

119 lines
3.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// 首页活动专区表(结构同轮播图表,多一个描述字段)
if (! Schema::hasTable('sys_activity')) {
Schema::create('sys_activity', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 100)->comment('活动标题');
$table->integer('image_id')->comment('活动图片信息');
$table->string('link', 500)->nullable()->default('')->comment('跳转链接');
$table->unsignedTinyInteger('link_type')->default(0)->comment('跳转类型:0=链接,1=分类');
$table->integer('category_id')->nullable()->default(0)->comment('分类IDlink_type=1 时生效)');
$table->text('description')->nullable()->comment('活动描述');
$table->unsignedTinyInteger('status')->default(0)->comment('状态:0=启用,1=禁用');
$table->integer('sort')->default(0)->comment('排序(数字越大越靠前)');
$table->timestamps();
$table->comment('首页活动专区管理表');
});
}
$this->insertMenuRules();
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('sys_activity');
$this->deleteMenuRules();
}
/**
* 向权限菜单表插入「活动专区」菜单(幂等,已存在则跳过)
*/
protected function insertMenuRules(): void
{
if (! Schema::hasTable('sys_rule')) {
return;
}
$exists = DB::table('sys_rule')->where('key', 'system.activity')->exists();
if ($exists) {
return;
}
$date = date('Y-m-d H:i:s');
$menuId = DB::table('sys_rule')->insertGetId([
'parent_id' => 0,
'type' => 'route',
'key' => 'system.activity',
'name' => '活动专区管理',
'path' => '/system/activity',
'icon' => '',
'order' => 0,
'local' => 'menu.system.activity',
'status' => 1,
'hidden' => 1,
'link' => 0,
'created_at' => $date,
'updated_at' => $date,
]);
$children = [
['name' => '查询活动列表', 'key' => 'system.activity.query'],
['name' => '新增活动', 'key' => 'system.activity.create'],
['name' => '编辑活动', 'key' => 'system.activity.update'],
['name' => '删除活动', 'key' => 'system.activity.delete'],
];
foreach ($children as $i => $child) {
DB::table('sys_rule')->insert([
'parent_id' => $menuId,
'type' => 'rule',
'key' => $child['key'],
'name' => $child['name'],
'path' => '',
'icon' => '',
'order' => $i,
'local' => '',
'status' => 1,
'hidden' => 1,
'link' => 0,
'created_at' => $date,
'updated_at' => $date,
]);
}
}
/**
* 删除「活动专区」菜单及其子规则
*/
protected function deleteMenuRules(): void
{
if (! Schema::hasTable('sys_rule')) {
return;
}
$menuId = DB::table('sys_rule')->where('key', 'system.activity')->value('id');
if ($menuId) {
DB::table('sys_rule')->where('key', 'system.activity')->delete();
DB::table('sys_rule')->where('parent_id', $menuId)->delete();
}
}
};