20250724_create_message_tables.php 10 KB

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