1, 13 => 0, 14 => 2, 15 => 3]; protected static $carrierOperators = [10 => 0, 11 => 1, 12 => 2]; protected static $payChannels = [10 => 'wx', 11 => 'alipay', 12 => 'bank']; protected static $orderClasses = [ \App\Models\Virtual\OrderCard::class, \App\Models\Virtual\OrderRenewalCard::class, \App\Models\Virtual\OrderRenewalPackageCard::class, \App\Models\Virtual\OrderFlowPackageCards::class, ]; protected $limit = 1000; protected $blocs; protected $packages; protected $products; public function handle() { $nextId = $maxId = app(ConfigService::class)->get(self::CURSOR_KEY) ?: 0; $this->blocs = app(BlocRepository::class)->get()->pluck('id', 'shorthand')->toArray(); $this->packages = app(PackageRepository::class)->get()->keyBy('sn'); $this->products = app(ProductRepository::class)->get()->keyBy('sn'); $query = DB::connection('vd_old')->table('logs') ->where('type', '<>', 10) ->where('create_time', '>', $timestamp); $total = $query->count(); $this->line('待同步条数:'.$total); $page = 1; while ($total) { echo $page . PHP_EOL; $res = $query->offset(($page - 1) * $this->limit)->limit($this->limit)->get(); DB::beginTransaction(); foreach ($res as $key => $value) { $package = $this->getPackage($value['content']); $unit_price = floatval($value['sale_account']) * 100; $custom_price = floatval($value['order_account']) * 100; $product = $this->getProduct($package, $value['company'], $unit_price); $type = self::$types[$value['type']]; $pay_channel = self::$payChannels[$value['pay_type']]; // 按规则生成订单编号 (月6+类型1+公司3+套餐4+价格6) $sn = date('Ym', $value['create_time']) . $type . sprintf('%03d', $value['company']) . sprintf('%04d', $package['id']) . sprintf('%06d', $custom_price); $data = [ 'sn' => $sn, 'source' => 1, 'type' => $type, 'company_id' => $value['company'], 'package_id' => $package['id'], 'product_id' => $product['id'], 'pay_channel' => $pay_channel, 'unit_price' => $unit_price, 'counts' => 1, 'total_price' => $unit_price, 'custom_price' => $custom_price, 'order_at' => date('Y-m-d H:i:s', $res['create_time']), 'order_status' => 4, 'transaction_status' => 1, 'created_at' => date('Y-m-d H:i:s', $res['create_time']), ]; $relationData = [ 'sim' => $value['sim'], 'order_id' => $order->id, 'company_id' => $order->company_id, 'package_id' => $order->package_id, 'counts' => 1, 'unit_price' => $order->unit_price ]; try { if ($order = Order::where('sn', $data['sn'])->first()) { $order->count = $order->count + 1; $order->total_price = $order->total_price + $unit_price; $order->custom_price = $order->custom_price + $custom_price; $order->save(); } else { $order = Order::create($data); } $query = (new $orderClasses[$type])->query(); if ($relation = $query->where('sim', $value['sim'])->where('order_id', $order->id)->first()) { $relation->count = $relation->count + 1; $relation->save(); } else { $relation = $query->create($relationData); } } catch (\Exception $e) { DB::rollback(); throw $e; } } DB::commit(); $nextId = $value['id']; app(ConfigService::class)->set(self::CURSOR_KEY, $nextId); if ($page * $this->limit >= $total) { break; } $page++; } } /** * 获取套餐 * * @param string $sn * @return void */ protected function getPackage($sn) { if (!$package = $this->packages[$sn]) { throw new \Exception('套餐不存在'); } return $package; } /** * 获取定价 * * @param string $sn * @return void */ protected function getProduct($package, $companyId, $price) { $sn = strtoupper($package['sn'] . '_' . $companyId . '_' . $price); if (!$product = $this->products[$sn]) { $product = app(ProductService::class)->store([ 'name' => $package['name'] . '' . $price, 'company_id' => $companyId, 'package_id' => $package['id'], 'base_price' => $price, 'renewal_price' => $price, ]); $this->products[$sn] = $product; } return $product; } }