8.0 KiB
8.0 KiB
glass-v2 到 glass 迁移分析
📊 项目对比
glass(主项目)
已完成:
- ✅ 多租户核心架构
- ✅ SuperAdmin 独立后台
- ✅ 权限管理系统(Spatie + Shield)
- ✅ 客户管理(Customer + Prescription)
- ✅ 订单管理(Order + OrderItem)
- ✅ 简化版产品管理(Product)
- ✅ 供应商管理(Supplier)
glass-v2(功能增强版)
额外功能:
- ✅ 产品分类管理(ProductCategory)
- ✅ 品牌管理(Brand)
- ✅ 采购订单管理(PurchaseOrder + PurchaseOrderItem)
- ✅ 库存流水管理(InventoryTransaction)
- ✅ 员工排班管理(StaffSchedule + Calendar Widget)
🎯 需要迁移的功能模块
1. 产品分类管理 🟢
迁移文件:
app/Models/ProductCategory.phpapp/Filament/Resources/ProductCategories/ProductCategoryResource.phpapp/Filament/Resources/ProductCategories/Pages/ManageProductCategories.phpdatabase/migrations/2026_01_31_105640_create_product_categories_table.php
数据库表结构:
product_categories
- id (bigint, PK)
- name (string, 分类名称)
- slug (string, URL友好标识)
- icon (string, Heroicon图标)
- color (string, 颜色)
- description (text, 描述)
- sort_order (integer, 排序)
- is_active (boolean, 是否启用)
- timestamps
特性:
- Heroicon 图标支持(37个预定义图标)
- 自动生成 slug
- 关联产品统计
- 预置5个分类:镜架、镜片、隐形眼镜、护理液、配件
2. 品牌管理 🟢
迁移文件:
app/Models/Brand.phpapp/Filament/Resources/Brands/BrandResource.phpapp/Filament/Resources/Brands/Pages/ManageBrands.phpdatabase/migrations/2026_01_31_105643_create_brands_table.php
数据库表结构:
brands
- id (bigint, PK)
- name (string, 品牌名称)
- slug (string, URL友好标识)
- logo (string, Logo图片)
- description (text, 描述)
- website (string, 官网)
- country (string, 国家/地区)
- sort_order (integer, 排序)
- is_active (boolean, 是否启用)
- timestamps
特性:
- Logo 上传和显示
- 预置7个品牌:雷朋、暴龙、蔡司、依视路、强生、博士伦、海昌
3. 采购订单管理 🟢
迁移文件:
app/Models/PurchaseOrder.phpapp/Models/PurchaseOrderItem.phpapp/Filament/Resources/PurchaseOrders/PurchaseOrderResource.phpapp/Filament/Resources/PurchaseOrders/Pages/ManagePurchaseOrders.phpdatabase/migrations/2026_01_22_063828_create_purchase_orders_table.phpdatabase/migrations/2026_01_22_083144_create_purchase_order_items_table.phpdatabase/migrations/2026_01_28_003412_add_received_quantity_to_purchase_order_items_table.php
数据库表结构:
purchase_orders
- id (bigint, PK)
- supplier_id (bigint, FK)
- staff_id (bigint, FK)
- reference_no (string, 采购单号)
- status (enum: Draft/Ordered/PartialReceived/Received/Cancelled)
- total_amount (decimal, 总金额)
- note (text, 备注)
- timestamps
- soft_deletes
purchase_order_items
- id (bigint, PK)
- purchase_order_id (bigint, FK)
- product_id (bigint, FK)
- quantity (integer, 订购数量)
- received_quantity (integer, 已收货数量, 默认0)
- unit_cost (decimal, 单位成本)
- total_cost (decimal, 总成本)
- note (text, 备注)
- timestamps
特性:
- 状态流转:草稿 → 已下订 → 部分收货 → 已收货
- 自动计算总金额
- 收货时更新库存流水
- 待收货项目查询
4. 库存流水管理 🟢
迁移文件:
app/Models/InventoryTransaction.phpapp/Filament/Resources/InventoryTransactions/InventoryTransactionResource.phpapp/Filament/Resources/InventoryTransactions/Pages/ManageInventoryTransactions.phpdatabase/migrations/2026_01_22_063830_create_inventory_transactions_table.php
数据库表结构:
inventory_transactions
- id (bigint, PK)
- product_id (bigint, FK)
- type (enum: in/out/adjust)
- quantity (integer, 数量)
- reference_type (string, 关联类型)
- reference_id (bigint, 关联ID)
- staff_id (bigint, FK)
- balance_after (integer, 变动后余额)
- timestamps
特性:
- 多态关联(可关联采购订单、销售订单、盘点等)
- 自动记录库存变动
- 支持入库、出库、调整三种类型
5. 员工排班管理 🟢
迁移文件:
app/Models/StaffSchedule.phpapp/Filament/Resources/StaffSchedules/StaffScheduleResource.phpapp/Filament/Resources/StaffSchedules/Pages/ManageStaffSchedules.phpapp/Filament/Resources/StaffSchedules/Widgets/StaffScheduleCalendarWidget.phpdatabase/migrations/2026_01_22_070636_create_staff_schedules_table.phpdatabase/migrations/2026_01_28_002939_add_shift_type_to_staff_schedules_table.phpdatabase/migrations/2026_01_28_004432_make_start_end_datetime_nullable_in_staff_schedules.php
数据库表结构:
staff_schedules
- id (bigint, PK)
- user_id (bigint, FK)
- shift_type (enum: morning/afternoon/full_day/custom)
- schedule_date (date, 排班日期)
- start_time (time, 开始时间)
- end_time (time, 结束时间)
- start_datetime (datetime, 开始日期时间, 可为null)
- end_datetime (datetime, 结束日期时间, 可为null)
- note (text, 备注)
- is_swap (boolean, 是否调班)
- swapped_with_user_id (bigint, FK, 调班对象)
- timestamps
特性:
- 预定义班次:早班(9-16)、下午班(16-21)、全天(9-21)、自定义
- 日历视图 Widget
- 实时在岗查询
- 支持调班功能
🔍 差异分析
glass 项目已有的结构
Product 模型(简化版):
- id
- team_id (FK)
- code (产品编码)
- name (产品名称)
- category (string, 旧字段)
- brand (string, 旧字段)
- type (enum: frame/lens/accessory)
- description (text)
- cost_price (decimal)
- retail_price (decimal)
- stock_quantity (integer)
- status (enum: active/inactive/discontinued)
- timestamps
- soft_deletes
glass-v2 的增强
Product 模型(增强版):
// 新增字段
product_category_id (bigint, FK) // 替代 category
brand_id (bigint, FK) // 替代 brand
需要修改:
- 将
category和brand字段改为外键关联 - 添加 BelongsToTenant trait(确保数据隔离)
⚠️ 迁移注意事项
1. 数据兼容性
问题: glass 项目已有 products 表,字段为 category (string) 和 brand (string)
解决方案:
- 创建迁移添加
product_category_id和brand_id字段 - 保留旧字段用于数据迁移
- 数据迁移后可删除旧字段
2. 多租户隔离
问题: v2 的模型没有使用 BelongsToTenant trait
解决方案:
- 为所有新模型添加
BelongsToTenanttrait - 为所有表添加
team_id字段和索引
3. 外键约束
问题: v2 的外键可能没有级联删除设置
解决方案:
- 供应商删除 → 采购订单设为 null
- 产品删除 → 库存流水设为 null
- 分类/品牌删除 → 产品外键设为 null
4. 中文本地化
状态: glass 项目已有 lang/zh_CN.json
需要添加:
- 采购订单相关术语
- 库存管理相关术语
- 排班管理相关术语
📋 迁移计划
阶段 1: 数据库结构迁移(30分钟)
- 复制所有迁移文件到 glass 项目
- 修改迁移添加
team_id字段 - 运行迁移
阶段 2: 模型迁移(45分钟)
- 复制所有模型文件到 glass 项目
- 为所有模型添加
BelongsToTenanttrait - 更新 Product 模型关联关系
- 测试模型关系
阶段 3: Filament 资源迁移(1小时)
- 复制所有 Resource 文件到 glass 项目
- 添加多租户中间件配置
- 添加权限控制
- 调整导航和分组
阶段 4: 数据本地化和 Seeder(30分钟)
- 创建 ProductCategoryAndBrandSeeder
- 添加中文翻译
- 填充预置数据
阶段 5: 测试和调试(30分钟)
- 测试 CRUD 操作
- 测试权限隔离
- 测试跨租户数据隔离
- 修复发现的问题
总预计时间: 3小时
🚀 开始迁移
准备开始迁移工作!
美羊羊 🐑