同步优化

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\Log;
use App\Domains\Real\Services\CommonService;
use Symfony\Component\Console\Input\InputOption;
use App\Domains\Real\Repositories\OrderRepository;
use App\Domains\Real\Repositories\CompanyRepository;
use App\Domains\Real\Repositories\PackageRepository;
@ -99,29 +100,31 @@ class AddedOrderSync extends Command
$starttime = $this->datetime->copy()->startOfMonth()->startOfDay();
$endtime = $this->datetime->copy()->endOfMonth()->endOfDay();
$orders = DB::table($virtualTable)->selectRaw('sim, order_id, unit_price, counts')
->where('created_at', '>=', $starttime->format('Y-m-d H:i:s'))
->where('created_at', '<=', $endtime->format('Y-m-d H:i:s'))
->whereIn('sim', array_pluck($data, 'sim'))
->whereNull('deleted_at')
->orderBy('sim')
->orderBy('unit_price')
->get()
->collect()
->groupBy('sim')->toArray();
if ($this->option('with-vd')) {
$orders = DB::table($virtualTable)->selectRaw('sim, order_id, unit_price, counts')
->where('created_at', '>=', $starttime->format('Y-m-d H:i:s'))
->where('created_at', '<=', $endtime->format('Y-m-d H:i:s'))
->whereIn('sim', array_pluck($data, 'sim'))
->whereNull('deleted_at')
->orderBy('sim')
->orderBy('unit_price')
->get()
->collect()
->groupBy('sim')->toArray();
foreach ($data as &$value) {
$i = array_count_values($simArray)[$value['sim']] ?? 0;
array_push($simArray, $value['sim']);
$valueOrders = $orders[$value['sim']] ?? [];
foreach ($data as &$value) {
$i = array_count_values($simArray)[$value['sim']] ?? 0;
array_push($simArray, $value['sim']);
$virtualOrders = $orders[$value['sim']] ?? [];
$value['virtual_order_id'] = 0;
$value['virtual_order_id'] = 0;
foreach ($valueOrders as $key => $order) {
if ($order['counts'] === $value['counts'] && $order['unit_price'] === $value['unit_price']) {
$value['virtual_order_id'] = $valueOrders[0]['order_id'] ?? 0;
unset($valueOrders[$key]);
$orders[$value['sim']] = array_values($valueOrders);
foreach ($virtualOrders as $key => $order) {
if ($order['counts'] === $value['counts'] && $order['unit_price'] === $value['unit_price']) {
$value['virtual_order_id'] = $virtualOrders[0]['order_id'] ?? 0;
unset($virtualOrders[$key]);
$orders[$value['sim']] = array_values($virtualOrders);
}
}
}
}
@ -267,4 +270,11 @@ class AddedOrderSync extends Command
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 App\Models\Real\Order;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Input\InputOption;
use App\Domains\Real\Repositories\OrderRepository;
use App\Domains\Real\Repositories\CompanyRepository;
use App\Domains\Real\Repositories\PackageRepository;
@ -39,7 +40,7 @@ class OrderBaseSync extends Command
DB::transaction(function () use ($orders, $cards) {
$datetime = $this->getDateTime();
$this->line('插入订单数据,条数:'.count($orders));
$this->line('插入订单数据,条数:' . count($orders));
foreach (array_chunk($orders, $this->chunks) as $data) {
$this->getOutput()->write('.');
Order::upsert($data, 'id');
@ -47,21 +48,26 @@ class OrderBaseSync extends Command
app(OrderRepository::class)->forgetCached();
$this->line('插入订单数据成功');
$this->line('插入订单关联数据,条数:'.count($cards));
$this->line('插入订单关联数据,条数:' . count($cards));
foreach (array_chunk($cards, $this->chunks) as $data) {
$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) {
$value['virtual_order_id'] = $orders[$value['sim']] ?? 0;
if ($this->option('with-vd')) {
$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);
}
@ -113,7 +119,7 @@ class OrderBaseSync extends Command
$array = [];
foreach ($orders as $item) {
$item = (array)$item;
$item = (array) $item;
$array[] = [
'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;
for ($i=0; $i < $value['counts']; $i++) {
for ($i = 0; $i < $value['counts']; $i++) {
$cards[] = [
'type' => 0,
'sim' => intval($sim),
@ -231,12 +237,12 @@ class OrderBaseSync extends Command
{
$this->line('查询排单记录');
$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) {
$query->where('s_status', 4)->where('s_card_counts', '>', 0);
})->get()->keyBy('s_number')->toArray();
->whereIn('o_number', array_pluck($orders, 'sn'))->where(function ($query) {
$query->where('s_status', 4)->where('s_card_counts', '>', 0);
})->get()->keyBy('s_number')->toArray();
foreach ($orderRows as &$item) {
$item = (array)$item;
$item = (array) $item;
}
return $orderRows;
@ -248,13 +254,21 @@ class OrderBaseSync extends Command
$this->line('查询排单详情');
$orderItems = DB::connection('real')->table('jxc_order_single_row_item')
->select('i_id', 's_number', 's_section_number')
->whereIn('s_number', array_keys($orderRows))->get()->toArray();
->select('i_id', 's_number', 's_section_number')
->whereIn('s_number', array_keys($orderRows))->get()->toArray();
foreach ($orderItems as &$item) {
$item = (array)$item;
$item = (array) $item;
}
return $orderItems;
}
protected function getOptions()
{
return [
['--with-vd', null, InputOption::VALUE_NONE, '同步VD订单编号'],
];
}
}