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)->withTrashed()->get()->keyBy('sn'); $this->products = app(ProductRepository::class)->withTrashed()->get()->keyBy('sn'); $this->companies = app(CompanyRepository::class)->withTrashed()->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(); $orderArray = []; $relationArray = []; foreach ($res as $key => $value) { $value = (array)$value; $package = $this->getPackage($value['content']); $company = $this->getCompany(CommonService::stringifyCompanyId($value['company'])); $unit_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', $company['id']) . sprintf('%04d', $package['id']) . sprintf('%06d', $unit_price); $counts = isset($orderArray[$sn]) ? $orderArray[$sn]['counts'] + 1 : 1; $total_price = isset($orderArray[$sn]) ? $orderArray[$sn]['total_price'] + $unit_price : $unit_price; $orderArray[$sn] = [ 'sn' => $sn, 'source' => 1, 'type' => $type, 'company_id' => $company['id'], 'package_id' => $package['id'], 'product_id' => $product['id'], 'pay_channel' => $pay_channel, 'unit_price' => $unit_price, 'counts' => $counts, 'total_price' => $total_price, 'custom_price' => $total_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']), ]; $k = $sn . '-' . $value['sim']; $counts = isset($relationArray[$k]) ? $relationArray[$k]['counts'] + 1 : 1; $relationArray[] = [ 'order_sn' => $sn, 'type' => $type, 'sim' => $value['sim'], 'company_id' => $company['id'], 'package_id' => $package['id'], 'counts' => $counts, 'created_at' => date('Y-m-d H:i:s', $value['create_time']), 'updated_at' => date('Y-m-d H:i:s'), ]; } try { $builder = Order::query()->toBase(); $sql = $builder->getGrammar()->compileInsert($builder, $orderArray); $sql .= " on conflict (sn, COALESCE(deleted_at, '1970-01-01 08:00:00'::timestamp)) do update set counts=virtual_orders.counts + excluded.counts, total_price=virtual_orders.unit_price * (virtual_orders.counts + excluded.counts), custom_price=virtual_orders.unit_price * (virtual_orders.counts + excluded.counts)"; $builder->connection->insert($sql, Arr::flatten($orderArray, 1)); $orders = Order::withTrashed()->select(['id', 'sn'])->whereIn('sn', array_pluck($orderArray, 'sn'))->get()->pluck('id', 'sn')->toArray(); foreach ($relationArray as $key => $value) { $relationArray[$key]['order_id'] = $orders[$value['order_sn']]; unset($relationArray[$key]['order_sn']); } $relationArray = array_groupBy($relationArray, 'type'); foreach ($relationArray as $type => $array) { $builder = (new $orderClasses[$type])->query()->toBase(); $sql = $builder->getGrammar()->compileInsert($builder, $array); $sql .= " on conflict (order_id, sim, COALESCE(deleted_at, '1970-01-01 08:00:00'::timestamp)) do update set counts=virtual_order_cards_partition.counts + excluded.counts"; $builder->connection->insert($sql, Arr::flatten($array, 1)); } } catch (\Exception $e) { DB::rollback(); throw $e; } DB::commit(); app(OrderRepository::class)->forgetCached(); app(OrderCardRepository::class)->forgetCached(); app(OrderRenewalCardRepository::class)->forgetCached(); app(OrderRenewalPackageCardRepository::class)->forgetCached(); app(OrderFlowPackageCardsRepository::class)->forgetCached(); app(OrderCardPartitionRepository::class)->forgetCached(); } /** * 获取套餐 * * @param string $sn * @return void */ protected function getCompany($sn) { if (!$company = $this->companies[$sn]) { throw new \Exception('企业不存在'); } return $company; } /** * 获取套餐 * * @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; } }