261 lines
9.2 KiB
PHP
261 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Real\Commands\Sync;
|
|
|
|
use App\Models\Real\Order;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Domains\Card\Jobs\MongoCardJob;
|
|
use App\Domains\Real\Services\CommonService;
|
|
use App\Domains\Real\Repositories\OrderRepository;
|
|
use App\Domains\Real\Repositories\CompanyRepository;
|
|
use App\Domains\Real\Repositories\PackageRepository;
|
|
use App\Domains\Real\Repositories\OrderCardPartitionRepository;
|
|
|
|
class AddedOrderSync extends Command
|
|
{
|
|
protected $name = 'real:sync-added-order';
|
|
|
|
protected $description = '同步RD企业订单数据';
|
|
|
|
protected $chunks = 1000;
|
|
|
|
protected $business_type = ['3875569' => 0, '9632627' => 1];
|
|
|
|
protected $types;
|
|
|
|
protected $tables = [
|
|
'',
|
|
'real_order_renewal_cards',
|
|
'real_order_renewal_package_cards',
|
|
'real_order_flows_package_cards',
|
|
'real_order_optional_package_cards',
|
|
'real_order_additional_package_cards',
|
|
'real_order_group_package_cards',
|
|
];
|
|
|
|
public function handle()
|
|
{
|
|
$this->datetime = $this->getDateTime();
|
|
$this->companies = app(CompanyRepository::class)->withTrashed()->get()->keyBy('sn');
|
|
$this->packages = app(PackageRepository::class)->withTrashed()->get()->keyBy('sn');
|
|
|
|
$orders = $this->getOrders();
|
|
$cards = $this->getOrderItems($orders);
|
|
$orders = $this->transformer($orders, $cards);
|
|
|
|
$this->line('插入订单数据,条数:'.count($orders));
|
|
foreach (array_chunk($orders, $this->chunks) as $data) {
|
|
$this->getOutput()->write('.');
|
|
Order::upsert($orders, ['sn', 'deleted_at']);
|
|
}
|
|
|
|
$orders = Order::whereIn('sn', array_pluck($orders, 'sn'))->get()->keyBy('sn');
|
|
|
|
app(OrderRepository::class)->forgetCached();
|
|
|
|
$dataOrderCards = [];
|
|
|
|
foreach ($cards as $value) {
|
|
$sn = $value['sn'] . $value['package_id'];
|
|
|
|
if (!$order = $orders[$sn]) {
|
|
Log::notice("卡 {$value['sim']} 订单未找到");
|
|
}
|
|
|
|
$dataOrderCards[$value['type']][] = [
|
|
'type' => $value['type'],
|
|
'sim' => $value['sim'],
|
|
'company_id' => $value['company_id'],
|
|
'order_id' => $order['id'],
|
|
'package_id' => $value['package_id'],
|
|
'counts' => $value['counts'],
|
|
'unit_price' => $value['unit_price'],
|
|
'created_at' => $value['created_at'],
|
|
'updated_at' => $value['updated_at'],
|
|
'deleted_at' => $value['deleted_at'],
|
|
];
|
|
}
|
|
|
|
$this->line('插入订单关联数据,条数:'.count(array_collapse($dataOrderCards)));
|
|
|
|
$simArray = [];
|
|
|
|
foreach ($dataOrderCards as $type => $orderCards) {
|
|
foreach (array_chunk($orderCards, $this->chunks) as $data) {
|
|
$this->getOutput()->write('.');
|
|
|
|
$table = $this->tables[$type];
|
|
|
|
$virtualTable = str_replace('real_', 'virtual_', $table);
|
|
|
|
if (in_array($virtualTable, ['virtual_order_optional_package_cards', 'virtual_order_additional_package_cards'])) {
|
|
$virtualTable = 'virtual_order_flows_package_cards';
|
|
}
|
|
|
|
if (in_array($virtualTable, ['virtual_order_group_package_cards'])) {
|
|
$virtualTable = 'virtual_order_renewal_cards';
|
|
}
|
|
|
|
$starttime = $this->datetime->copy()->startOfMonth()->startOfDay();
|
|
$endtime = $this->datetime->copy()->endOfMonth()->endOfDay();
|
|
|
|
$orders = DB::table($virtualTable)->selectRaw('sim, order_id')
|
|
->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('created_at')
|
|
->get()
|
|
->collect()
|
|
->groupBy('sim')->toArray();
|
|
|
|
foreach ($data as &$value) {
|
|
$i = array_count_values($simArray)[$value['sim']] ?? 0;
|
|
array_push($simArray, $value['sim']);
|
|
$value['virtual_order_id'] = $orders[$value['sim']][$i]['order_id'] ?? 0;
|
|
}
|
|
|
|
MongoCardJob::dispatch(array_pluck($data, 'sim'));
|
|
|
|
$only = ['company_id', 'package_id', 'counts', 'unit_price'];
|
|
|
|
DB::table($table)->upsert($data, ['sim', 'order_id', 'deleted_at'], $only);
|
|
}
|
|
}
|
|
|
|
app(OrderCardPartitionRepository::class)->forgetCached();
|
|
|
|
$this->line('插入订单关联数据成功');
|
|
}
|
|
|
|
// 查询订单
|
|
protected function getOrders()
|
|
{
|
|
$this->line('查询订单记录');
|
|
|
|
$starttime = $this->datetime->copy()->startOfMonth()->startOfDay();
|
|
$endtime = $this->datetime->copy()->endOfMonth()->endOfDay();
|
|
|
|
$select = [
|
|
'sn',
|
|
'account_no',
|
|
'status',
|
|
'custom_no as company_id',
|
|
'transaction_no',
|
|
'paytype as pay_channel',
|
|
'create_time as order_at',
|
|
'update_time as updated_at',
|
|
];
|
|
|
|
$orders = DB::connection('real')->table('jxc_custom_order')->select($select)->whereIn('status', [3, 7, 8])
|
|
->whereIn('custom_no', $this->companies->keys())
|
|
->whereIn('account_no', array_keys($this->business_type))
|
|
->where('create_time', '>=', $starttime->timestamp)
|
|
->where('create_time', '<=', $endtime->timestamp)
|
|
->orderBy('create_time')->get()->collect()->toArray();
|
|
|
|
$array = [];
|
|
|
|
foreach ($orders as $item) {
|
|
$item['business_type'] = $this->business_type[$item['account_no']];
|
|
$item['company_id'] = $this->companies[$item['company_id']]['id'] ?? 0;
|
|
$item['pay_channel'] = CommonService::transformerPayChannel($item['pay_channel']);
|
|
$item['order_at'] = date('Y-m-d H:i:s', $item['order_at']);
|
|
$item['created_at'] = $item['order_at'];
|
|
$item['updated_at'] = ($item['updated_at'] == '0000-00-00 00:00:00') ? $item['order_at'] : $item['updated_at'];
|
|
$item['deleted_at'] = $item['status'] === 3 ? null : $item['updated_at'];
|
|
unset($item['account_no']);
|
|
unset($item['status']);
|
|
$array[] = $item;
|
|
}
|
|
|
|
return $array;
|
|
}
|
|
|
|
// 订单详情
|
|
protected function getOrderItems($orders)
|
|
{
|
|
$orders = array_keyBy($orders, 'sn');
|
|
|
|
$this->line('查询订单详情');
|
|
|
|
$select = [
|
|
'sn',
|
|
'sim',
|
|
'goods_type as type',
|
|
'goods_no as package_sn',
|
|
'unit_price as unit_price',
|
|
'quantity as counts',
|
|
];
|
|
|
|
$cards = DB::connection('real')->table('jxc_custom_order_item')->select($select)
|
|
->whereIn('sn', array_keys($orders))->where('goods_type', '<=', 6)->get()->collect()->toArray();
|
|
|
|
$array = [];
|
|
|
|
foreach ($cards as $item) {
|
|
$order = $orders[$item['sn']];
|
|
$item['company_id'] = $order['company_id'] ?? 0;
|
|
$item['package_id'] = $this->packages[$item['package_sn']]['id'] ?? 0;
|
|
$item['unit_price'] = intval($item['unit_price'] * 100) ;
|
|
$item['created_at'] = $order['created_at'];
|
|
$item['updated_at'] = $order['updated_at'];
|
|
$item['deleted_at'] = $order['deleted_at'];
|
|
$array[] = $item;
|
|
}
|
|
|
|
$this->types = array_pluck($array, 'type', 'sn');
|
|
|
|
return $array;
|
|
}
|
|
|
|
// 整理数据
|
|
protected function transformer($orders, $cards)
|
|
{
|
|
$orderArray = [];
|
|
$orderPackages = [];
|
|
|
|
$tmpCards = $cards;
|
|
$tmpCards = array_groupBy($tmpCards, 'sn');
|
|
|
|
foreach ($tmpCards as $sn => $item) {
|
|
$array = array_groupBy($item, 'package_id');
|
|
foreach ($array as $package_id => $values) {
|
|
if (empty($values)) {
|
|
Log::notice("订单 {$sn} 数据为空");
|
|
continue;
|
|
}
|
|
|
|
$orderPackages[$sn][$package_id] = [
|
|
'unit_price' => $values[0]['unit_price'],
|
|
'counts' => array_sum(array_pluck($values, 'counts')),
|
|
'total_price' => $values[0]['unit_price'] * array_sum(array_pluck($values, 'counts')),
|
|
];
|
|
}
|
|
}
|
|
|
|
foreach ($orders as $item) {
|
|
$item['type'] = $this->types[$item['sn']] ?? 255;
|
|
$packages = $orderPackages[$item['sn']];
|
|
|
|
if (!$packages) {
|
|
Log::notice("订单 {$sn} 关联套餐不正确");
|
|
continue;
|
|
}
|
|
|
|
foreach ($packages as $package_id => $array) {
|
|
$order = $item;
|
|
$order['package_id'] = $package_id;
|
|
$order['sn'] = $item['sn'] . $package_id;
|
|
$order['counts'] = $array['counts'];
|
|
$order['unit_price'] = $array['unit_price'];
|
|
$order['total_price'] = $array['total_price'];
|
|
$orderArray[] = $order;
|
|
}
|
|
}
|
|
|
|
return $orderArray;
|
|
}
|
|
}
|