0, 11 => 1, 12 => 2]; protected static $payChannels = [10 => 'wx', 11 => 'alipay', 12 => 'bank']; protected $blocs; protected $packages; protected $product; protected $limit = 1; const FILENAME = 'app/command/sync-card.json'; const INIT_ID = 0; // '2000-01-01 00:00:00' public function handle() { $contents = $this->getFile(); $maxId = $contents['maxId']; $nextId = $contents['maxId']; $this->saveFile(1, $maxId); $query = DB::connection('vd_old')->table('ckb_custom') ->select(['id', 'custom_no', 'imsi', 'carrieroperator', 'iccid', 'card_number', 'card_from', 'iccid', 'company', 'custom_state']) ->where('id', '>', $maxId); $logQuery = DB::connection('vd_old')->table('ckb_custom_handle_log') ->select(['type', 'company', 'pay_type', 'content', 'valid_start_time', 'valid_end_time', 'sale_account', 'order_account', 'create_time']); $total = $query->count(); $this->line('待同步条数:' . $total); if ($total) { $this->blocs = app(BlocRepository::class)->get()->pluck('id', 'shorthand')->toArray(); $this->packages = app(PackageRepository::class)->get()->keyBy('sn'); $this->product = app(ProductRepository::class)->get()->keyBy('sn'); } $page = 1; while ($total) { echo $page . PHP_EOL; $value = $query->offset(($page - 1) * $this->limit)->limit($this->limit)->get()->first(); if (!$value) { break; } $value = (array)$value; $logs = $logQuery->where('custom_no', $value['custom_no'])->get()->collect(); $existedCard = Card::where('sim', $value['card_number'])->first(); dd($logs); $card = $this->transformerCard($value, $logs, $existedCard); $order = $this->transformerOrder($value, $logs, $existedCard); $renewals = $this->transformerRenewals($value, $logs, $existedCard); $renewalPackages = $this->transformerRenewalPackages($value, $logs, $existedCard); $flows = $this->transformerFlows($value, $logs, $existedCard); $nextId = $value['id']; if ($page * $this->limit >= $total) { break; } $page++; } app(CardRepository::class)->forgetCached(); $this->saveFile(0, $nextId); } // 卡数据转换 protected function transformerCard($value, $logs, $existedCard) { // 判断卡类型 $type = ($value['card_number'][3] >= 5) ? 1 : ($existedCard ? 0 : 2); // 激活记录 $activateLog = $logs->where('type', 10)->first(); $activate_at = $activateLog ? date('Y-m-d H:i:s', $activateLog['valid_start_time']) : null; return [ 'sim' => $value['card_number'], 'imsi' => $value['imsi'], 'iccid' => $value['iccid'], 'bloc_id' => $this->blocs[$value['card_from']] ?? 0, 'carrier_operator' => self::$carrierOperators[$value['carrieroperator']], 'activate_at' => $existedCard ? $existedCard->activate_at : $activate_at, 'virtual_activate_at' => $activate_at, 'type' => $type, ]; } // 销售记录 protected function transformerOrder($value, $logs, $existedCard) { if (!$sellLog = $logs->where('type', 13)->first()) { return null; } $package = $this->getPackage($sellLog['content']); $unit_price = floatval($sellLog['sale_account']) * 100; $custom_price = floatval($sellLog['order_account']) * 100; $product = $this->getProduct($package, $sellLog['company'], $unit_price); $order = [ 'type' => 1, 'company_id' => $sellLog['company'], 'package_id' => $package['id'], 'product_id' => $product['id'], 'pay_channel' => self::$payChannels[$sellLog['pay_type']], 'unit_price' => $unit_price, 'counts' => DB::raw('counts + 1'), 'total_price' => DB::raw("total_price + {$unit_price}"), 'custom_price' => DB::raw("custom_price + {$custom_price}"), 'order_at' => date('Y-m-d H:i:s'), 'order_status' => 4, 'transaction_status' => 1, ]; return $order; } // 续费记录 protected function transformerRenewals($value, $logs, $existedCard) { $renewalLogs = $logs->where('type', 11)->get(); if (empty($renewalLog)) { return []; } $array = []; foreach ($renewalLogs as $item) { $package = $this->getPackage($sellLog['content']); $unit_price = floatval($sellLog['sale_account']) * 100; $custom_price = floatval($sellLog['order_account']) * 100; $array[] = [ 'sn' => $sn, 'type' => 1, 'company_id' => $sellLog['company'], 'package_id' => $package['id'], 'product_id' => $product['id'], 'pay_channel' => self::$payChannels[$sellLog['pay_type']], 'unit_price' => $unit_price, 'counts' => DB::raw('counts + 1'), 'total_price' => DB::raw("total_price + {$unit_price}"), 'custom_price' => DB::raw("custom_price + {$custom_price}"), 'order_at' => date('Y-m-d H:i:s'), 'order_status' => 4, 'transaction_status' => 1, ]; } return $array; } // 续费包记录 protected function transformerRenewalPackages($value, $logs, $existedCard) { $renewalLog = $logs->where('type', 14)->get(); if (empty($renewalLog)) { return []; } } // 加油包记录 protected function transformerFlows($value, $logs, $existedCard) { $flowsLog = $logs->where('type', 15)->get(); if (empty($flowsLog)) { return []; } } /** * 获取套餐 * * @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; } /** * 获取文件内容 * * @return void */ protected function getFile() { $file = storage_path(self::FILENAME); if (!file_exists($file)) { $dir = dirname($file); if (null !== $dir && !is_dir($dir)) { mkdir($dir, 0777, true); } $this->saveFile(0, self::INIT_ID); } $contents = file_get_contents($file); return json_decode($contents, 256); } /** * 写入文件 * * @param integer $status 状态 1运行中 0运行结束 * @param integer $maxId 最后查询的ID * @return void */ protected function saveFile(int $status, int $maxId) { $file = storage_path(self::FILENAME); $contents = json_encode([ 'status' => $status, 'maxId' => $maxId, ]); file_put_contents($file, $contents); } }