| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace Database\Seeders;
- use Illuminate\Database\Seeder;
- use Illuminate\Support\Facades\DB;
- class OrderTestDataSeeder extends Seeder
- {
- /**
- * Run the database seeds.
- *
- * @return void
- */
- public function run()
- {
- // 获取现有的用户、店铺和商品数据
- $users = DB::table('member')->where('mark', 1)->pluck('id')->toArray();
- $stores = DB::table('stores')->where('mark', 1)->pluck('id')->toArray();
- $goods = DB::table('goods')->where('mark', 1)->get()->toArray();
- if (empty($users) || empty($stores) || empty($goods)) {
- echo "请先创建用户、店铺和商品数据\n";
- return;
- }
- $statuses = [1, 2, 3, 4]; // 1-待付款,2-已付款,3-已发货,4-已完成
- $refundStatuses = [0, 1, 2, 3, 4]; // 0-无,1-已退款,2-已审核,3-待审核,4-审核驳回
- $afterTypes = [0, 1, 2]; // 0-无,1-售后,2-退款
- $provinces = ['广东省', '广西壮族自治区', '北京市', '上海市', '浙江省', '江苏省', '四川省'];
- $cities = ['广州市', '南宁市', '朝阳区', '浦东新区', '杭州市', '南京市', '成都市'];
- $districts = ['天河区', '青秀区', '三里屯', '陆家嘴', '西湖区', '玄武区', '武侯区'];
- $streets = ['中山大道', '民族大道', '建国路', '世纪大道', '延安路', '中山路', '人民南路'];
- $deliveryCompanies = [
- ['name' => '顺丰速运', 'code' => 'SF'],
- ['name' => '圆通速递', 'code' => 'YTO'],
- ['name' => '中通快递', 'code' => 'ZTO'],
- ['name' => '韵达快递', 'code' => 'YD'],
- ['name' => '申通快递', 'code' => 'STO'],
- ['name' => '百世快递', 'code' => 'HTKY'],
- ['name' => '京东物流', 'code' => 'JD'],
- ];
- $names = ['张三', '李四', '王五', '赵六', '刘七', '陈八', '杨九', '周十'];
- $time = time();
- // 生成15个订单
- for ($i = 1; $i <= 15; $i++) {
- $userId = $users[array_rand($users)];
- $storeId = $stores[array_rand($stores)];
- $status = $statuses[array_rand($statuses)];
- $refundStatus = $i <= 3 ? $refundStatuses[array_rand($refundStatuses)] : 0; // 前3个订单可能有退款
- $afterType = $refundStatus > 0 ? $afterTypes[array_rand([1, 2])] : 0;
- $orderNo = 'ORD' . date('Ymd') . str_pad($i, 6, '0', STR_PAD_LEFT);
- $createTime = $time - (15 - $i) * 86400; // 倒序创建时间
- // 随机选择1-3个商品
- $orderGoodsCount = rand(1, 3);
- $selectedGoods = array_rand(array_flip(array_keys($goods)), min($orderGoodsCount, count($goods)));
- if (!is_array($selectedGoods)) {
- $selectedGoods = [$selectedGoods];
- }
- $total = 0;
- $orderGoodsData = [];
- foreach ($selectedGoods as $goodsIndex) {
- $goodsItem = $goods[$goodsIndex];
- $quantity = rand(1, 3);
- $price = $goodsItem->price;
- $subtotal = $price * $quantity;
- $total += $subtotal;
- $orderGoodsData[] = [
- 'goods_name' => $goodsItem->goods_name,
- 'category_id' => $goodsItem->category_id,
- 'store_id' => $goodsItem->store_id,
- 'price' => $price,
- 'stock' => $quantity,
- 'unit' => $goodsItem->unit ?: '件',
- 'weight' => $goodsItem->weight,
- 'thumb' => $goodsItem->thumb,
- 'sku_type' => $goodsItem->sku_type,
- 'sku_id' => 0,
- 'create_time' => $createTime,
- 'update_time' => $createTime,
- 'status' => 1,
- 'mark' => 1,
- ];
- }
- $payTotal = $total - rand(0, 50); // 实付可能有优惠
- if ($payTotal < 0) $payTotal = $total;
- $receiverName = $names[array_rand($names)];
- $province = $provinces[array_rand($provinces)];
- $city = $cities[array_rand($cities)];
- $district = $districts[array_rand($districts)];
- $street = $streets[array_rand($streets)];
- $receiverArea = $province . ' ' . $city . ' ' . $district;
- $receiverAddress = $street . rand(1, 999) . '号' . rand(1, 30) . '栋' . rand(101, 2999) . '室';
- $delivery = $deliveryCompanies[array_rand($deliveryCompanies)];
- $deliveryNo = $status >= 3 ? $delivery['code'] . rand(1000000000, 9999999999) : '';
- $deliveryCompany = $status >= 3 ? $delivery['name'] : '';
- $deliveryCode = $status >= 3 ? $delivery['code'] : '';
- $transactionId = $status >= 2 ? '4200' . rand(1000000000, 9999999999) . rand(1000000000, 9999999999) : '';
- $bonus = $status == 4 ? round($payTotal * 0.05, 2) : 0; // 完成订单才有佣金
- $afterRealname = $afterType > 0 ? $receiverName : '';
- $afterPhone = $afterType > 0 ? '1' . rand(3, 9) . rand(100000000, 999999999) : '';
- $afterRemark = $afterType == 1 ? '商品质量有问题,申请售后' : ($afterType == 2 ? '不想要了,申请退款' : '');
- $refundRemark = $refundStatus == 2 ? '已审核通过,退款中' : ($refundStatus == 4 ? '不符合退款条件,驳回' : '');
- // 插入订单
- $orderId = DB::table('orders')->insertGetId([
- 'order_no' => $orderNo,
- 'user_id' => $userId,
- 'store_id' => $storeId,
- 'total' => $total,
- 'pay_total' => $payTotal,
- 'transaction_id' => $transactionId,
- 'receiver_name' => $receiverName,
- 'receiver_area' => $receiverArea,
- 'receiver_address' => $receiverAddress,
- 'delivery_no' => $deliveryNo,
- 'delivery_company' => $deliveryCompany,
- 'delivery_code' => $deliveryCode,
- 'bonus' => $bonus,
- 'after_type' => $afterType,
- 'after_realname' => $afterRealname,
- 'after_phone' => $afterPhone,
- 'after_remark' => $afterRemark,
- 'refund_remark' => $refundRemark,
- 'refund_status' => $refundStatus,
- 'create_time' => $createTime,
- 'update_time' => $createTime,
- 'status' => $status,
- 'mark' => 1,
- ]);
- // 插入订单商品
- foreach ($orderGoodsData as &$orderGoodsItem) {
- $orderGoodsItem['order_id'] = $orderId;
- }
- DB::table('orders_goods')->insert($orderGoodsData);
- echo "已创建订单 {$i}/15: {$orderNo}\n";
- }
- echo "订单测试数据创建完成!\n";
- }
- }
|