OrderTestDataSeeder.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Database\Seeders;
  3. use Illuminate\Database\Seeder;
  4. use Illuminate\Support\Facades\DB;
  5. class OrderTestDataSeeder extends Seeder
  6. {
  7. /**
  8. * Run the database seeds.
  9. *
  10. * @return void
  11. */
  12. public function run()
  13. {
  14. // 获取现有的用户、店铺和商品数据
  15. $users = DB::table('member')->where('mark', 1)->pluck('id')->toArray();
  16. $stores = DB::table('stores')->where('mark', 1)->pluck('id')->toArray();
  17. $goods = DB::table('goods')->where('mark', 1)->get()->toArray();
  18. if (empty($users) || empty($stores) || empty($goods)) {
  19. echo "请先创建用户、店铺和商品数据\n";
  20. return;
  21. }
  22. $statuses = [1, 2, 3, 4]; // 1-待付款,2-已付款,3-已发货,4-已完成
  23. $refundStatuses = [0, 1, 2, 3, 4]; // 0-无,1-已退款,2-已审核,3-待审核,4-审核驳回
  24. $afterTypes = [0, 1, 2]; // 0-无,1-售后,2-退款
  25. $provinces = ['广东省', '广西壮族自治区', '北京市', '上海市', '浙江省', '江苏省', '四川省'];
  26. $cities = ['广州市', '南宁市', '朝阳区', '浦东新区', '杭州市', '南京市', '成都市'];
  27. $districts = ['天河区', '青秀区', '三里屯', '陆家嘴', '西湖区', '玄武区', '武侯区'];
  28. $streets = ['中山大道', '民族大道', '建国路', '世纪大道', '延安路', '中山路', '人民南路'];
  29. $deliveryCompanies = [
  30. ['name' => '顺丰速运', 'code' => 'SF'],
  31. ['name' => '圆通速递', 'code' => 'YTO'],
  32. ['name' => '中通快递', 'code' => 'ZTO'],
  33. ['name' => '韵达快递', 'code' => 'YD'],
  34. ['name' => '申通快递', 'code' => 'STO'],
  35. ['name' => '百世快递', 'code' => 'HTKY'],
  36. ['name' => '京东物流', 'code' => 'JD'],
  37. ];
  38. $names = ['张三', '李四', '王五', '赵六', '刘七', '陈八', '杨九', '周十'];
  39. $time = time();
  40. // 生成15个订单
  41. for ($i = 1; $i <= 15; $i++) {
  42. $userId = $users[array_rand($users)];
  43. $storeId = $stores[array_rand($stores)];
  44. $status = $statuses[array_rand($statuses)];
  45. $refundStatus = $i <= 3 ? $refundStatuses[array_rand($refundStatuses)] : 0; // 前3个订单可能有退款
  46. $afterType = $refundStatus > 0 ? $afterTypes[array_rand([1, 2])] : 0;
  47. $orderNo = 'ORD' . date('Ymd') . str_pad($i, 6, '0', STR_PAD_LEFT);
  48. $createTime = $time - (15 - $i) * 86400; // 倒序创建时间
  49. // 随机选择1-3个商品
  50. $orderGoodsCount = rand(1, 3);
  51. $selectedGoods = array_rand(array_flip(array_keys($goods)), min($orderGoodsCount, count($goods)));
  52. if (!is_array($selectedGoods)) {
  53. $selectedGoods = [$selectedGoods];
  54. }
  55. $total = 0;
  56. $orderGoodsData = [];
  57. foreach ($selectedGoods as $goodsIndex) {
  58. $goodsItem = $goods[$goodsIndex];
  59. $quantity = rand(1, 3);
  60. $price = $goodsItem->price;
  61. $subtotal = $price * $quantity;
  62. $total += $subtotal;
  63. $orderGoodsData[] = [
  64. 'goods_name' => $goodsItem->goods_name,
  65. 'category_id' => $goodsItem->category_id,
  66. 'store_id' => $goodsItem->store_id,
  67. 'price' => $price,
  68. 'stock' => $quantity,
  69. 'unit' => $goodsItem->unit ?: '件',
  70. 'weight' => $goodsItem->weight,
  71. 'thumb' => $goodsItem->thumb,
  72. 'sku_type' => $goodsItem->sku_type,
  73. 'sku_id' => 0,
  74. 'create_time' => $createTime,
  75. 'update_time' => $createTime,
  76. 'status' => 1,
  77. 'mark' => 1,
  78. ];
  79. }
  80. $payTotal = $total - rand(0, 50); // 实付可能有优惠
  81. if ($payTotal < 0) $payTotal = $total;
  82. $receiverName = $names[array_rand($names)];
  83. $province = $provinces[array_rand($provinces)];
  84. $city = $cities[array_rand($cities)];
  85. $district = $districts[array_rand($districts)];
  86. $street = $streets[array_rand($streets)];
  87. $receiverArea = $province . ' ' . $city . ' ' . $district;
  88. $receiverAddress = $street . rand(1, 999) . '号' . rand(1, 30) . '栋' . rand(101, 2999) . '室';
  89. $delivery = $deliveryCompanies[array_rand($deliveryCompanies)];
  90. $deliveryNo = $status >= 3 ? $delivery['code'] . rand(1000000000, 9999999999) : '';
  91. $deliveryCompany = $status >= 3 ? $delivery['name'] : '';
  92. $deliveryCode = $status >= 3 ? $delivery['code'] : '';
  93. $transactionId = $status >= 2 ? '4200' . rand(1000000000, 9999999999) . rand(1000000000, 9999999999) : '';
  94. $bonus = $status == 4 ? round($payTotal * 0.05, 2) : 0; // 完成订单才有佣金
  95. $afterRealname = $afterType > 0 ? $receiverName : '';
  96. $afterPhone = $afterType > 0 ? '1' . rand(3, 9) . rand(100000000, 999999999) : '';
  97. $afterRemark = $afterType == 1 ? '商品质量有问题,申请售后' : ($afterType == 2 ? '不想要了,申请退款' : '');
  98. $refundRemark = $refundStatus == 2 ? '已审核通过,退款中' : ($refundStatus == 4 ? '不符合退款条件,驳回' : '');
  99. // 插入订单
  100. $orderId = DB::table('orders')->insertGetId([
  101. 'order_no' => $orderNo,
  102. 'user_id' => $userId,
  103. 'store_id' => $storeId,
  104. 'total' => $total,
  105. 'pay_total' => $payTotal,
  106. 'transaction_id' => $transactionId,
  107. 'receiver_name' => $receiverName,
  108. 'receiver_area' => $receiverArea,
  109. 'receiver_address' => $receiverAddress,
  110. 'delivery_no' => $deliveryNo,
  111. 'delivery_company' => $deliveryCompany,
  112. 'delivery_code' => $deliveryCode,
  113. 'bonus' => $bonus,
  114. 'after_type' => $afterType,
  115. 'after_realname' => $afterRealname,
  116. 'after_phone' => $afterPhone,
  117. 'after_remark' => $afterRemark,
  118. 'refund_remark' => $refundRemark,
  119. 'refund_status' => $refundStatus,
  120. 'create_time' => $createTime,
  121. 'update_time' => $createTime,
  122. 'status' => $status,
  123. 'mark' => 1,
  124. ]);
  125. // 插入订单商品
  126. foreach ($orderGoodsData as &$orderGoodsItem) {
  127. $orderGoodsItem['order_id'] = $orderId;
  128. }
  129. DB::table('orders_goods')->insert($orderGoodsData);
  130. echo "已创建订单 {$i}/15: {$orderNo}\n";
  131. }
  132. echo "订单测试数据创建完成!\n";
  133. }
  134. }