2019-01-08 19:01:15 +08:00

189 lines
6.5 KiB
PHP

<?php
namespace App\Domains\Virtual\Commands\Sync;
use Carbon\Carbon;
use App\Models\Card\Card;
use App\Models\Virtual\Order;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Artisan;
use App\Domains\Config\Services\ConfigService;
use App\Domains\Virtual\Services\ProductService;
use App\Domains\Card\Repositories\BlocRepository;
use App\Domains\Card\Repositories\CardRepository;
use App\Domains\Virtual\Repositories\PackageRepository;
use App\Domains\Virtual\Repositories\ProductRepository;
class LogSync extends Command
{
protected $name = 'virtual:sync-log';
protected $description = '同步VD订单数据';
const CURSOR_KEY = 'sync_log_cursor';
protected static $types = [11 => 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('id', '>', $nextId)
->orderBy('id');
$total = $query->count();
$this->line('待同步条数:'.$total);
$page = 1;
while ($total) {
echo 'sync_log_cursor page #: ' . $page . ' nextId #: ' . $nextId . PHP_EOL;
$res = $query->offset(($page - 1) * $this->limit)->limit($this->limit)->get();
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 = [
'sim' => $value['sim'],
'order_id' => $order->id,
'company_id' => $order->company_id,
'package_id' => $order->package_id,
'counts' => 1,
'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();
throw $e;
}
$nextId = $value['id'];
}
DB::commit();
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;
}
}