同步优化

This commit is contained in:
邓皓元 2019-08-08 18:14:47 +08:00
parent fd2bb26303
commit a7773e5c07
2 changed files with 64 additions and 40 deletions

View File

@ -6,6 +6,7 @@ use App\Models\Real\Order;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use App\Domains\Real\Services\CommonService; use App\Domains\Real\Services\CommonService;
use Symfony\Component\Console\Input\InputOption;
use App\Domains\Real\Repositories\OrderRepository; use App\Domains\Real\Repositories\OrderRepository;
use App\Domains\Real\Repositories\CompanyRepository; use App\Domains\Real\Repositories\CompanyRepository;
use App\Domains\Real\Repositories\PackageRepository; use App\Domains\Real\Repositories\PackageRepository;
@ -99,29 +100,31 @@ class AddedOrderSync extends Command
$starttime = $this->datetime->copy()->startOfMonth()->startOfDay(); $starttime = $this->datetime->copy()->startOfMonth()->startOfDay();
$endtime = $this->datetime->copy()->endOfMonth()->endOfDay(); $endtime = $this->datetime->copy()->endOfMonth()->endOfDay();
$orders = DB::table($virtualTable)->selectRaw('sim, order_id, unit_price, counts') if ($this->option('with-vd')) {
->where('created_at', '>=', $starttime->format('Y-m-d H:i:s')) $orders = DB::table($virtualTable)->selectRaw('sim, order_id, unit_price, counts')
->where('created_at', '<=', $endtime->format('Y-m-d H:i:s')) ->where('created_at', '>=', $starttime->format('Y-m-d H:i:s'))
->whereIn('sim', array_pluck($data, 'sim')) ->where('created_at', '<=', $endtime->format('Y-m-d H:i:s'))
->whereNull('deleted_at') ->whereIn('sim', array_pluck($data, 'sim'))
->orderBy('sim') ->whereNull('deleted_at')
->orderBy('unit_price') ->orderBy('sim')
->get() ->orderBy('unit_price')
->collect() ->get()
->groupBy('sim')->toArray(); ->collect()
->groupBy('sim')->toArray();
foreach ($data as &$value) { foreach ($data as &$value) {
$i = array_count_values($simArray)[$value['sim']] ?? 0; $i = array_count_values($simArray)[$value['sim']] ?? 0;
array_push($simArray, $value['sim']); array_push($simArray, $value['sim']);
$valueOrders = $orders[$value['sim']] ?? []; $virtualOrders = $orders[$value['sim']] ?? [];
$value['virtual_order_id'] = 0; $value['virtual_order_id'] = 0;
foreach ($valueOrders as $key => $order) { foreach ($virtualOrders as $key => $order) {
if ($order['counts'] === $value['counts'] && $order['unit_price'] === $value['unit_price']) { if ($order['counts'] === $value['counts'] && $order['unit_price'] === $value['unit_price']) {
$value['virtual_order_id'] = $valueOrders[0]['order_id'] ?? 0; $value['virtual_order_id'] = $virtualOrders[0]['order_id'] ?? 0;
unset($valueOrders[$key]); unset($virtualOrders[$key]);
$orders[$value['sim']] = array_values($valueOrders); $orders[$value['sim']] = array_values($virtualOrders);
}
} }
} }
} }
@ -267,4 +270,11 @@ class AddedOrderSync extends Command
return $orderArray; return $orderArray;
} }
protected function getOptions()
{
return [
['--with-vd', null, InputOption::VALUE_NONE, '同步VD订单编号'],
];
}
} }

View File

@ -5,6 +5,7 @@ namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon; use Carbon\Carbon;
use App\Models\Real\Order; use App\Models\Real\Order;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Input\InputOption;
use App\Domains\Real\Repositories\OrderRepository; use App\Domains\Real\Repositories\OrderRepository;
use App\Domains\Real\Repositories\CompanyRepository; use App\Domains\Real\Repositories\CompanyRepository;
use App\Domains\Real\Repositories\PackageRepository; use App\Domains\Real\Repositories\PackageRepository;
@ -39,7 +40,7 @@ class OrderBaseSync extends Command
DB::transaction(function () use ($orders, $cards) { DB::transaction(function () use ($orders, $cards) {
$datetime = $this->getDateTime(); $datetime = $this->getDateTime();
$this->line('插入订单数据,条数:'.count($orders)); $this->line('插入订单数据,条数:' . count($orders));
foreach (array_chunk($orders, $this->chunks) as $data) { foreach (array_chunk($orders, $this->chunks) as $data) {
$this->getOutput()->write('.'); $this->getOutput()->write('.');
Order::upsert($data, 'id'); Order::upsert($data, 'id');
@ -47,21 +48,26 @@ class OrderBaseSync extends Command
app(OrderRepository::class)->forgetCached(); app(OrderRepository::class)->forgetCached();
$this->line('插入订单数据成功'); $this->line('插入订单数据成功');
$this->line('插入订单关联数据,条数:'.count($cards)); $this->line('插入订单关联数据,条数:' . count($cards));
foreach (array_chunk($cards, $this->chunks) as $data) { foreach (array_chunk($cards, $this->chunks) as $data) {
$this->getOutput()->write('.'); $this->getOutput()->write('.');
$orders = DB::table('virtual_order_cards')->select(['sim', 'order_id'])
->whereIn('sim', array_pluck($data, 'sim'))
->whereNull('refunded_at')
->whereNull('deleted_at')
->get()->pluck('order_id', 'sim');
foreach ($data as &$value) { if ($this->option('with-vd')) {
$value['virtual_order_id'] = $orders[$value['sim']] ?? 0; $orders = DB::table('virtual_order_cards')->select(['sim', 'order_id'])
->whereIn('sim', array_pluck($data, 'sim'))
->whereNull('refunded_at')
->whereNull('deleted_at')
->get()->pluck('order_id', 'sim');
foreach ($data as &$value) {
$value['virtual_order_id'] = $orders[$value['sim']] ?? 0;
}
$only = ['company_id', 'package_id', 'counts', 'unit_price', 'virtual_order_id'];
} else {
$only = ['company_id', 'package_id', 'counts', 'unit_price'];
} }
$only = ['company_id', 'package_id', 'counts', 'unit_price', 'virtual_order_id'];
DB::table('real_order_cards')->upsert($data, ['sim', 'order_id'], $only); DB::table('real_order_cards')->upsert($data, ['sim', 'order_id'], $only);
} }
@ -113,7 +119,7 @@ class OrderBaseSync extends Command
$array = []; $array = [];
foreach ($orders as $item) { foreach ($orders as $item) {
$item = (array)$item; $item = (array) $item;
$array[] = [ $array[] = [
'id' => $item['o_id'], 'id' => $item['o_id'],
@ -190,7 +196,7 @@ class OrderBaseSync extends Command
$refunded_at = isset($refunds[$sim]) ? (strtotime($refunds[$sim]) >= strtotime($order['created_at']) ? $refunds[$sim] : null) : null; $refunded_at = isset($refunds[$sim]) ? (strtotime($refunds[$sim]) >= strtotime($order['created_at']) ? $refunds[$sim] : null) : null;
for ($i=0; $i < $value['counts']; $i++) { for ($i = 0; $i < $value['counts']; $i++) {
$cards[] = [ $cards[] = [
'type' => 0, 'type' => 0,
'sim' => intval($sim), 'sim' => intval($sim),
@ -231,12 +237,12 @@ class OrderBaseSync extends Command
{ {
$this->line('查询排单记录'); $this->line('查询排单记录');
$orderRows = DB::connection('real')->table('jxc_order_single_row')->select('o_number', 's_number', 's_create_time') $orderRows = DB::connection('real')->table('jxc_order_single_row')->select('o_number', 's_number', 's_create_time')
->whereIn('o_number', array_pluck($orders, 'sn'))->where(function ($query) { ->whereIn('o_number', array_pluck($orders, 'sn'))->where(function ($query) {
$query->where('s_status', 4)->where('s_card_counts', '>', 0); $query->where('s_status', 4)->where('s_card_counts', '>', 0);
})->get()->keyBy('s_number')->toArray(); })->get()->keyBy('s_number')->toArray();
foreach ($orderRows as &$item) { foreach ($orderRows as &$item) {
$item = (array)$item; $item = (array) $item;
} }
return $orderRows; return $orderRows;
@ -248,13 +254,21 @@ class OrderBaseSync extends Command
$this->line('查询排单详情'); $this->line('查询排单详情');
$orderItems = DB::connection('real')->table('jxc_order_single_row_item') $orderItems = DB::connection('real')->table('jxc_order_single_row_item')
->select('i_id', 's_number', 's_section_number') ->select('i_id', 's_number', 's_section_number')
->whereIn('s_number', array_keys($orderRows))->get()->toArray(); ->whereIn('s_number', array_keys($orderRows))->get()->toArray();
foreach ($orderItems as &$item) { foreach ($orderItems as &$item) {
$item = (array)$item; $item = (array) $item;
} }
return $orderItems; return $orderItems;
} }
protected function getOptions()
{
return [
['--with-vd', null, InputOption::VALUE_NONE, '同步VD订单编号'],
];
}
} }