# 实现任务清单 **Change ID**: `add-commission-model-changes` --- ## 阶段 1: 数据库迁移 (1-2 小时) ### Task 1.1: 创建迁移文件 **文件**: `migrations/000010_add_commission_model_changes.up.sql` **实现内容**: - [x] 1.1.1 新增 `tb_commission_withdrawal_request` 表字段 - [x] 1.1.2 新增 `tb_account.is_primary` 字段 - [x] 1.1.3 新增 `tb_commission_record` 表字段(`shop_id`, `balance_after`) - [x] 1.1.4 新增 `tb_commission_withdrawal_setting.daily_withdrawal_limit` 字段 - [x] 1.1.5 新增 `tb_iot_card.shop_id` 字段 - [x] 1.1.6 新增 `tb_device.shop_id` 字段 - [x] 1.1.7 创建 `tb_enterprise_card_authorization` 表 - [x] 1.1.8 创建 `tb_asset_allocation_record` 表 - [x] 1.1.9 创建必要的索引 **验证**: - [x] 迁移脚本语法正确 - [x] 字段类型与需求文档一致 --- ### Task 1.2: 数据迁移 - owner_type 枚举统一 **文件**: `migrations/000010_add_commission_model_changes.up.sql` **实现内容**: - [x] 1.2.1 更新 `tb_iot_card` 表 `owner_type='agent'` 为 `owner_type='shop'` - [x] 1.2.2 更新 `tb_device` 表 `owner_type='agent'` 为 `owner_type='shop'` - [x] 1.2.3 填充 `tb_iot_card.shop_id` 字段(`owner_type='shop'` 时等于 `owner_id`) - [x] 1.2.4 填充 `tb_device.shop_id` 字段(`owner_type='shop'` 时等于 `owner_id`) **验证**: - [x] 数据迁移逻辑正确 - [x] 无数据丢失 --- ### Task 1.3: 创建回滚迁移 **文件**: `migrations/000010_add_commission_model_changes.down.sql` **实现内容**: - [x] 1.3.1 删除新增的表字段 - [x] 1.3.2 删除新增的表 - [x] 1.3.3 恢复 `owner_type` 枚举值(`shop` → `agent`) **验证**: - [x] 回滚脚本可以正确执行 - [x] 回滚后数据库状态正确 --- ## 阶段 2: Model 更新 (1 小时) ### Task 2.1: 更新现有 Model **文件**: - `internal/model/financial.go` - `internal/model/commission.go` - `internal/model/account.go` - `internal/model/iot_card.go` - `internal/model/device.go` **实现内容**: - [x] 2.1.1 `CommissionWithdrawalRequest` 新增字段 - [x] 2.1.2 `Account` 新增 `IsPrimary` 字段 - [x] 2.1.3 `CommissionRecord` 新增 `ShopID`, `BalanceAfter` 字段 - [x] 2.1.4 `CommissionWithdrawalSetting` 新增 `DailyWithdrawalLimit` 字段 - [x] 2.1.5 `IotCard` 新增 `ShopID` 字段 - [x] 2.1.6 `Device` 新增 `ShopID` 字段 **验证**: - [x] 字段标签正确(`gorm`, `json`) - [x] 字段类型与数据库一致 --- ### Task 2.2: 新增 Model **文件**: - `internal/model/enterprise_card_authorization.go` - `internal/model/asset_allocation_record.go` **实现内容**: - [x] 2.2.1 创建 `EnterpriseCardAuthorization` 模型 - [x] 2.2.2 创建 `AssetAllocationRecord` 模型 - [x] 2.2.3 实现 `TableName()` 方法 **验证**: - [x] 模型定义完整 - [x] 遵循项目 Model 规范 --- ### Task 2.3: 更新常量定义 **文件**: `pkg/constants/iot.go` **实现内容**: - [x] 2.3.1 更新 `OwnerType` 常量(移除 `agent`, `user`, `device`,保留 `platform`, `shop`) - [x] 2.3.2 新增 `EnterpriseCardAuthorizationStatus` 常量 - [x] 2.3.3 新增 `AssetAllocationType` 常量 - [x] 2.3.4 新增 `PaymentType` 常量 - [x] 2.3.5 新增 Redis Key 生成函数(如有需要) **验证**: - [x] 常量命名符合规范 - [x] 中文注释完整 --- ## 阶段 3: 代码兼容性修复 (30 分钟) ### Task 3.1: 更新现有代码中的 owner_type 引用 **实现内容**: - [x] 3.1.1 全局搜索 `owner_type.*agent` 引用 - [x] 3.1.2 更新为 `shop` - [x] 3.1.3 验证无遗漏 **验证**: - [x] 编译通过 - [x] 无运行时错误 --- ## 阶段 4: 验证 (30 分钟) ### Task 4.1: 执行迁移 **实现内容**: - [x] 4.1.1 在开发环境执行迁移 - [x] 4.1.2 验证表结构正确 - [x] 4.1.3 验证数据迁移正确 - [x] 4.1.4 验证索引创建正确 **验证**: - [x] `migrate up` 成功 - [x] `migrate down` 可以回滚 --- ### Task 4.2: Model 验证 **实现内容**: - [x] 4.2.1 验证 Model 与数据库表结构一致 - [x] 4.2.2 简单 CRUD 测试 - [x] 4.2.3 验证 GORM 自动迁移无冲突 **验证**: - [x] 所有新字段可正常读写 - [x] 新表 CRUD 正常 --- ## 完成标准 - [x] 所有迁移文件创建完成 - [x] 所有 Model 更新完成 - [x] 常量定义更新完成 - [x] 代码兼容性修复完成 - [x] 迁移执行成功 - [x] 编译通过,无错误