20250724080000_create_message_tables.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. declare(strict_types=1);
  3. class CreateMessageTables extends \Phinx\Migration\AbstractMigration
  4. {
  5. /**
  6. * 执行迁移
  7. */
  8. public function change(): void
  9. {
  10. // 创建系统公告表
  11. $this->table('message_announcements', [
  12. 'engine' => 'InnoDB',
  13. 'comment' => '系统公告表',
  14. 'charset' => 'utf8mb4',
  15. 'collation' => 'utf8mb4_general_ci',
  16. ])
  17. ->addColumn('title', 'string', ['limit' => 255, 'null' => false, 'comment' => '公告标题'])
  18. ->addColumn('content', 'text', ['null' => false, 'comment' => '公告内容'])
  19. ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '状态:0-禁用,1-启用'])
  20. ->addColumn('type', 'integer', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '类型:1-普通公告,2-重要公告,3-紧急公告'])
  21. ->addColumn('start_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '开始时间'])
  22. ->addColumn('end_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '结束时间'])
  23. ->addColumn('create_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '创建时间'])
  24. ->addColumn('update_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '更新时间'])
  25. ->addColumn('delete_time', 'integer', ['null' => true, 'comment' => '删除时间'])
  26. ->create();
  27. // 创建用户通知表
  28. $this->table('message_notifications', [
  29. 'engine' => 'InnoDB',
  30. 'comment' => '用户通知表',
  31. 'charset' => 'utf8mb4',
  32. 'collation' => 'utf8mb4_general_ci',
  33. ])
  34. ->addColumn('user_id', 'integer', ['limit' => 11, 'default' => 0, 'null' => false, 'comment' => '用户ID,0表示全部用户'])
  35. ->addColumn('title', 'string', ['limit' => 255, 'null' => false, 'comment' => '通知标题'])
  36. ->addColumn('content', 'text', ['null' => false, 'comment' => '通知内容'])
  37. ->addColumn('type', 'integer', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '类型:1-系统通知,2-活动通知,3-订单通知,4-物流通知,5-其他'])
  38. ->addColumn('is_read', 'integer', ['limit' => 1, 'default' => 0, 'null' => false, 'comment' => '是否已读:0-未读,1-已读'])
  39. ->addColumn('read_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '阅读时间'])
  40. ->addColumn('create_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '创建时间'])
  41. ->addColumn('update_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '更新时间'])
  42. ->addColumn('delete_time', 'integer', ['null' => true, 'comment' => '删除时间'])
  43. ->addIndex(['user_id'], ['name' => 'idx_user_id'])
  44. ->create();
  45. // 创建私信表
  46. $this->table('message_privates', [
  47. 'engine' => 'InnoDB',
  48. 'comment' => '私信表',
  49. 'charset' => 'utf8mb4',
  50. 'collation' => 'utf8mb4_general_ci',
  51. ])
  52. ->addColumn('from_user_id', 'integer', ['limit' => 11, 'null' => false, 'comment' => '发送者用户ID,0表示系统'])
  53. ->addColumn('to_user_id', 'integer', ['limit' => 11, 'null' => false, 'comment' => '接收者用户ID'])
  54. ->addColumn('content', 'text', ['null' => false, 'comment' => '私信内容'])
  55. ->addColumn('is_read', 'integer', ['limit' => 1, 'default' => 0, 'null' => false, 'comment' => '是否已读:0-未读,1-已读'])
  56. ->addColumn('read_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '阅读时间'])
  57. ->addColumn('create_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '创建时间'])
  58. ->addColumn('update_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '更新时间'])
  59. ->addColumn('delete_time', 'integer', ['null' => true, 'comment' => '删除时间'])
  60. ->addIndex(['from_user_id'], ['name' => 'idx_from_user_id'])
  61. ->addIndex(['to_user_id'], ['name' => 'idx_to_user_id'])
  62. ->create();
  63. // 创建消息设置表
  64. $this->table('message_settings', [
  65. 'engine' => 'InnoDB',
  66. 'comment' => '消息设置表',
  67. 'charset' => 'utf8mb4',
  68. 'collation' => 'utf8mb4_general_ci',
  69. ])
  70. ->addColumn('user_id', 'integer', ['limit' => 11, 'null' => false, 'comment' => '用户ID,0表示系统默认设置'])
  71. ->addColumn('system_notify', 'integer', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '系统通知:0-关闭,1-开启'])
  72. ->addColumn('activity_notify', 'integer', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '活动通知:0-关闭,1-开启'])
  73. ->addColumn('order_notify', 'integer', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '订单通知:0-关闭,1-开启'])
  74. ->addColumn('logistics_notify', 'integer', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '物流通知:0-关闭,1-开启'])
  75. ->addColumn('private_message', 'integer', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '私信:0-关闭,1-开启'])
  76. ->addColumn('create_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '创建时间'])
  77. ->addColumn('update_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '更新时间'])
  78. ->addIndex(['user_id'], ['name' => 'idx_user_id', 'unique' => true])
  79. ->create();
  80. // 创建消息模板表
  81. $this->table('message_templates', [
  82. 'engine' => 'InnoDB',
  83. 'comment' => '消息模板表',
  84. 'charset' => 'utf8mb4',
  85. 'collation' => 'utf8mb4_general_ci',
  86. ])
  87. ->addColumn('code', 'string', ['limit' => 50, 'null' => false, 'comment' => '模板代码'])
  88. ->addColumn('name', 'string', ['limit' => 100, 'null' => false, 'comment' => '模板名称'])
  89. ->addColumn('title', 'string', ['limit' => 255, 'null' => false, 'comment' => '标题模板'])
  90. ->addColumn('content', 'text', ['null' => false, 'comment' => '内容模板'])
  91. ->addColumn('type', 'integer', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '类型:1-系统通知,2-活动通知,3-订单通知,4-物流通知,5-私信,6-其他'])
  92. ->addColumn('status', 'integer', ['limit' => 1, 'default' => 1, 'null' => false, 'comment' => '状态:0-禁用,1-启用'])
  93. ->addColumn('create_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '创建时间'])
  94. ->addColumn('update_time', 'integer', ['default' => 0, 'null' => false, 'comment' => '更新时间'])
  95. ->addIndex(['code'], ['name' => 'idx_code', 'unique' => true])
  96. ->create();
  97. if (!$this->isMigratingUp()) {
  98. return;
  99. }
  100. // 插入默认消息模板数据
  101. $currentTime = time();
  102. $this->table('message_templates')
  103. ->insert([
  104. [
  105. 'code' => 'system_welcome',
  106. 'name' => '欢迎注册',
  107. 'title' => '欢迎加入{site_name}',
  108. 'content' => '亲爱的{user_name},欢迎您加入{site_name},祝您购物愉快!',
  109. 'type' => 1,
  110. 'status' => 1,
  111. 'create_time' => $currentTime,
  112. 'update_time' => $currentTime
  113. ],
  114. [
  115. 'code' => 'order_created',
  116. 'name' => '订单创建',
  117. 'title' => '您的订单已创建',
  118. 'content' => '亲爱的{user_name},您的订单{order_no}已创建成功,请及时支付。',
  119. 'type' => 3,
  120. 'status' => 1,
  121. 'create_time' => $currentTime,
  122. 'update_time' => $currentTime
  123. ],
  124. [
  125. 'code' => 'order_paid',
  126. 'name' => '订单支付',
  127. 'title' => '您的订单已支付',
  128. 'content' => '亲爱的{user_name},您的订单{order_no}已支付成功,我们将尽快为您发货。',
  129. 'type' => 3,
  130. 'status' => 1,
  131. 'create_time' => $currentTime,
  132. 'update_time' => $currentTime
  133. ],
  134. [
  135. 'code' => 'order_shipped',
  136. 'name' => '订单发货',
  137. 'title' => '您的订单已发货',
  138. 'content' => '亲爱的{user_name},您的订单{order_no}已发货,物流单号:{tracking_no}。',
  139. 'type' => 4,
  140. 'status' => 1,
  141. 'create_time' => $currentTime,
  142. 'update_time' => $currentTime
  143. ],
  144. [
  145. 'code' => 'order_completed',
  146. 'name' => '订单完成',
  147. 'title' => '您的订单已完成',
  148. 'content' => '亲爱的{user_name},您的订单{order_no}已完成,感谢您的购买!',
  149. 'type' => 3,
  150. 'status' => 1,
  151. 'create_time' => $currentTime,
  152. 'update_time' => $currentTime
  153. ]
  154. ])
  155. ->save();
  156. // 插入默认系统设置
  157. $this->table('message_settings')
  158. ->insert([
  159. [
  160. 'user_id' => 0,
  161. 'system_notify' => 1,
  162. 'activity_notify' => 1,
  163. 'order_notify' => 1,
  164. 'logistics_notify' => 1,
  165. 'private_message' => 1,
  166. 'create_time' => $currentTime,
  167. 'update_time' => $currentTime
  168. ]
  169. ])
  170. ->save();
  171. }
  172. }