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, ]; /** * Undocumented function * * @param int $page * @param int $limit * @param int $maxId */ public function __construct($page, $limit, $maxId) { $this->page = $page; $this->limit = $limit; $this->maxId = $maxId; } /** * */ public function handle() { $this->packages = app(PackageRepository::class)->get()->keyBy('sn'); $this->products = app(ProductRepository::class)->get()->keyBy('sn'); $query = DB::connection('vd_old')->table('logs')->whereIn('type', array_keys(self::$types)) ->where('id', '>', $this->maxId)->orderBy('id'); $res = $query->forPage($this->page, $this->limit)->get(); if (empty($res)) { Log::notice('LogSyncJob not result!'); return ; } DB::beginTransaction(); foreach ($res as $key => $value) { $value = (array)$value; $package = $this->getPackage($value['content']); $unit_price = intval($value['order_account'] * 100); $custom_price = intval($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', $value['create_time']), 'order_status' => 4, 'transaction_status' => 1, 'created_at' => date('Y-m-d H:i:s', $value['create_time']), ]; try { if ($order = Order::where('sn', $data['sn'])->first()) { $order->counts = $order->counts + 1; $order->total_price = $order->total_price + $unit_price; $order->custom_price = $order->custom_price + $custom_price; $order->save(); } else { $order = Order::create($data); } $relationData = [ 'type' => $type, 'sim' => $value['sim'], 'order_id' => $order->id, 'company_id' => $order->company_id, 'package_id' => $order->package_id, 'counts' => 1, 'service_start_at' => date('Y-m-d H:i:s', intval($value['valid_start_time'])), 'service_end_at' => date('Y-m-d H:i:s', intval($value['valid_end_time'])), 'created_at' => date('Y-m-d H:i:s', $value['create_time']), 'updated_at' => date('Y-m-d H:i:s'), ]; $class = (new self::$orderClasses[$type])->query(); if ($type) { if ($relation = $class->where('sim', $value['sim'])->where('order_id', $order->id)->first()) { $relation->counts = $relation->counts + 1; $relation->save(); } else { $relation = $class->create($relationData); } } else { $relation = $class->upsert($relationData, ['sim', 'order_id', 'deleted_at']); } } catch (\Exception $e) { DB::rollback(); $nextId = $res->first()->id - 1; app(ConfigService::class)->set(LogSync::CURSOR_KEY, $nextId); throw $e; } } DB::commit(); app(OrderRepository::class)->forgetCached(); app(OrderCardRepository::class)->forgetCached(); app(OrderRenewalCardRepository::class)->forgetCached(); app(OrderRenewalPackageCardRepository::class)->forgetCached(); app(OrderFlowPackageCardsRepository::class)->forgetCached(); } /** * 获取套餐 * * @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; } }