179 lines
6.0 KiB
PHP
179 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Virtual\Commands\Sync;
|
|
|
|
use App\Dicts;
|
|
use Carbon\Carbon;
|
|
use App\Models\Real\Order;
|
|
use MongoDB\BSON\UTCDateTime;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Domains\Real\Repositories\OrderRepository;
|
|
|
|
class OrderCustomSync extends Command
|
|
{
|
|
protected $name = 'virtual:sync-custom-order';
|
|
|
|
protected $description = '同步VD企业订单数据';
|
|
|
|
protected $chunks = 5000;
|
|
|
|
public function handle()
|
|
{
|
|
$this->datetime = $this->getDateTime();
|
|
|
|
$orders = $this->getOrders();
|
|
$orders = array_keyBy($orders, 'order_id');
|
|
$orderItems = $this->getOrderItems(array_keys($orders));
|
|
|
|
$dataOrders = [];
|
|
$dataOrderCards = [];
|
|
|
|
foreach ($orderItems as $key => $value) {
|
|
$order = $orders[$value['order_id']];
|
|
|
|
if (!$order) {
|
|
throw new \Exception('未找到订单数据:' . $value['order_id']);
|
|
}
|
|
|
|
$dataOrders[$value['order_id']] = [
|
|
'id' => $value['order_id'],
|
|
'type' => $value['type'],
|
|
'company_id' => $order['company_id'],
|
|
'transaction_no' => $order['transaction_no'],
|
|
'pay_channel' => $order['pay_channel'],
|
|
'unit_price' => $order['unit_price'],
|
|
'counts' => $order['counts'],
|
|
'total_price' => $order['total_price'],
|
|
'order_at' => $order['order_at'],
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
];
|
|
|
|
$dataOrderCards[$value['type']][] = [
|
|
'sim' => $value['sim'],
|
|
'order_id' => $value['order_id'],
|
|
'counts' => $value['item_counts'],
|
|
'unit_price' => $value['item_unit_price'],
|
|
];
|
|
|
|
$dataPackageCards[$value['type']][] = [
|
|
'package_id' => $value['package_id'],
|
|
'sim' => $value['sim'],
|
|
];
|
|
}
|
|
|
|
$this->line('插入订单数据,条数:'.count($dataOrders));
|
|
foreach (array_chunk($dataOrders, $this->chunks) as $data) {
|
|
echo '.';
|
|
Order::replace($data);
|
|
}
|
|
app(OrderRepository::class)->forgetCached();
|
|
|
|
unset($dataOrders);
|
|
|
|
$this->line('插入订单关联数据,条数:'.count(array_collapse($dataOrderCards)));
|
|
$tables = [
|
|
'real_order_base_cards',
|
|
'real_order_renewal_cards',
|
|
'real_order_renewal_package_cards',
|
|
'real_order_flows_package_cards',
|
|
'real_order_optional_package_cards',
|
|
'real_order_additional_package_cards',
|
|
];
|
|
foreach ($dataOrderCards as $type => $orderCards) {
|
|
foreach (array_chunk($orderCards, $this->chunks) as $data) {
|
|
echo '.';
|
|
DB::table($tables[$type])->replace($data);
|
|
}
|
|
}
|
|
unset($dataOrderCards);
|
|
$this->line('插入订单关联数据成功');
|
|
|
|
$this->line('插入套餐关联数据,条数:'.count(array_collapse($dataPackageCards)));
|
|
$tables = [
|
|
'',
|
|
'real_package_renewal_cards',
|
|
'real_package_renewal_package_cards',
|
|
'real_package_flows_package_cards',
|
|
'real_package_optional_package_cards',
|
|
'real_package_additional_package_cards',
|
|
];
|
|
foreach ($dataPackageCards as $type => $packageCards) {
|
|
foreach (array_chunk($packageCards, $this->chunks) as $data) {
|
|
echo '.';
|
|
DB::table($tables[$type])->replace($data);
|
|
}
|
|
}
|
|
unset($dataPackageCards);
|
|
$this->line('插入套餐关联数据成功');
|
|
}
|
|
|
|
// 查询订单
|
|
protected function getOrders()
|
|
{
|
|
$this->line('查询订单记录');
|
|
|
|
$starttime = $this->datetime->copy()->startOfMonth()->startOfDay();
|
|
$endtime = $this->datetime->copy()->endOfMonth()->endOfDay();
|
|
|
|
$select = [
|
|
'sn as order_id',
|
|
'custom_no as company_id',
|
|
'transaction_no',
|
|
'sim_count as counts',
|
|
'amount_money as total_price',
|
|
'paytype as pay_channel',
|
|
'create_time as order_at',
|
|
];
|
|
|
|
$orders = DB::connection('real')->table('jxc_custom_order')->select($select)->where('status', 3)
|
|
->where('create_time', '>=', $starttime->timestamp)
|
|
->where('create_time', '<=', $endtime->timestamp)
|
|
->orderBy('create_time')->get()->toArray();
|
|
|
|
$pay_channel = app(Dicts::class)->get('pay_channel');
|
|
$pay_channel = array_values($pay_channel);
|
|
|
|
foreach ($orders as &$item) {
|
|
$item = (array)$item;
|
|
$item['total_price'] = intval($item['total_price'] * 100);
|
|
$item['counts'] = !empty($item['counts']) ? $item['counts'] : 1;
|
|
$item['pay_channel'] = 0;
|
|
foreach ($pay_channel as $key => $value) {
|
|
if (in_array($item['pay_channel'], $value)) {
|
|
$item['pay_channel'] = $key;
|
|
}
|
|
}
|
|
|
|
$item['unit_price'] = floor($total_price/$item['counts']);
|
|
$item['order_at'] = date('Y-m-d H:i:s', $item['order_at']);
|
|
}
|
|
|
|
return $orders;
|
|
}
|
|
|
|
// 订单详情
|
|
protected function getOrderItems($order_ids)
|
|
{
|
|
$this->line('查询订单详情');
|
|
$select = [
|
|
'sn as order_id',
|
|
'sim',
|
|
'goods_no as package_id',
|
|
'goods_type as type',
|
|
'unit_price as item_unit_price',
|
|
'quantity as item_counts',
|
|
];
|
|
|
|
$orderItems = DB::connection('real')->table('jxc_custom_order_item')->select($select)
|
|
->whereIn('sn', $order_ids)->where('goods_type', '<', 6)->get()->toArray();
|
|
|
|
foreach ($orderItems as &$item) {
|
|
$item = (array)$item;
|
|
$item['item_unit_price'] = intval($item['item_unit_price'] * 100);
|
|
}
|
|
|
|
return $orderItems;
|
|
}
|
|
}
|