272 lines
9.3 KiB
PHP
272 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Real\Commands\Sync;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Models\Real\Card;
|
|
use App\Models\Real\Order;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Domains\Real\Services\CommonService;
|
|
use App\Domains\Real\Repositories\CardRepository;
|
|
use App\Domains\Real\Repositories\OrderRepository;
|
|
use App\Domains\Real\Repositories\CompanyRepository;
|
|
|
|
class OrderBaseSync extends Command
|
|
{
|
|
protected $name = 'real:sync-order';
|
|
|
|
protected $description = '同步RD基础订单数据';
|
|
|
|
protected $companies;
|
|
|
|
protected $packages;
|
|
|
|
protected static $carrierOperators = [1, 0, 2];
|
|
|
|
protected $chunks = 1000;
|
|
|
|
public function handle()
|
|
{
|
|
$this->companies = app(CompanyRepository::class)->get()->pluck('name', 'id')->toArray();
|
|
|
|
$cards = $this->getCards();
|
|
|
|
$this->chunks *
|
|
$bar = $this->output->createProgressBar(count($cards));
|
|
|
|
$card_details = $this->getCardDetails($cards);
|
|
|
|
list($dataOrders, $dataCards, $dataOrderCards) = $this->transforms($cards, $card_details);
|
|
|
|
unset($cards);
|
|
unset($card_details);
|
|
|
|
try {
|
|
$this->line('插入订单数据,条数:'.count($dataOrders));
|
|
foreach (array_chunk($dataOrders, $this->chunks) as $data) {
|
|
echo '.';
|
|
Order::upsert($data, 'id');
|
|
}
|
|
app(OrderRepository::class)->forgetCached();
|
|
unset($dataOrders);
|
|
$this->line('插入订单数据成功');
|
|
|
|
$this->line('插入卡数据,条数:'.count($dataCards));
|
|
foreach (array_chunk($dataCards, $this->chunks) as $data) {
|
|
echo '.';
|
|
Card::upsert($data, 'sim');
|
|
}
|
|
app(CardRepository::class)->forgetCached();
|
|
unset($dataCards);
|
|
$this->line('插入卡数据成功');
|
|
|
|
$this->line('插入订单关联数据,条数:'.count($dataOrderCards));
|
|
foreach (array_chunk($dataOrderCards, $this->chunks) as $data) {
|
|
echo '.';
|
|
DB::table('real_order_base_cards')->upsert($data, 'sim');
|
|
}
|
|
unset($dataOrderCards);
|
|
$this->line('插入订单关联数据成功');
|
|
} catch (\Exception $e) {
|
|
$this->error($e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
// 获取月销售卡数据
|
|
protected function getCards()
|
|
{
|
|
$orders = $this->getOrders();
|
|
$orderRows = $this->getOrderRows($orders);
|
|
$orderItems = $this->getOrderItems($orderRows);
|
|
|
|
$cards = [];
|
|
|
|
foreach ($orderItems as &$item) {
|
|
$item['s_section_number'] = json_decode($item['s_section_number'], true);
|
|
$item['o_number'] = $orderRows[$item['s_number']]['o_number'];
|
|
|
|
foreach ($item['s_section_number'] as $value) {
|
|
$sim = explode('-', $value['section_no'])[0];
|
|
$order = $orders[$item['o_number']];
|
|
$orderRow = $orderRows[$item['s_number']];
|
|
|
|
for ($i=0; $i < $value['counts']; $i++) {
|
|
$cards[] = [
|
|
'sim' => (string)$sim,
|
|
'order_id' => $item['o_number'],
|
|
'company_id' => $order['o_customer_number'],
|
|
'package_id' => $order['o_p_number'],
|
|
'order_at' => $order['o_create_date'],
|
|
'total_price' => intval($order['o_amount'] * 100),
|
|
'unit_price' => intval($order['o_price'] * 100),
|
|
'counts' => $order['o_card_counts'],
|
|
];
|
|
|
|
$sim++;
|
|
}
|
|
}
|
|
}
|
|
|
|
unset($orderRows);
|
|
unset($orderItems);
|
|
|
|
$this->line('排单卡总数: ' . count($cards));
|
|
|
|
$cards = array_sort($cards, function ($item) {
|
|
return $item['order_at'];
|
|
});
|
|
|
|
$cards = array_keyBy($cards, 'sim');
|
|
|
|
$this->line('排重后卡总数: ' . count($cards));
|
|
|
|
if (!count($cards)) {
|
|
throw new \Exception('销售数据为空');
|
|
}
|
|
|
|
return $cards;
|
|
}
|
|
|
|
// 获取卡详细数据
|
|
protected function getCardDetails($cards)
|
|
{
|
|
$this->line('从MongoDB中取卡详细数据');
|
|
$cardChunks = array_chunk($cards, $this->chunks);
|
|
|
|
$card_details = [];
|
|
|
|
foreach ($cardChunks as $cardChunk) {
|
|
echo '.';
|
|
$res = DB::connection('mongo')->table('tblCard')->select(['cNo', 'iccid', 'imsi', 'comId', 'oType'])
|
|
->where('isDel', '<>', 1)
|
|
->whereIn('cNo', array_pluck($cardChunk, 'sim'))->get()->toArray();
|
|
|
|
$card_details = array_merge($card_details, $res);
|
|
}
|
|
|
|
unset($cardChunks);
|
|
|
|
$card_details = array_keyBy($card_details, 'cNo');
|
|
|
|
$this->line('获取成功,卡详情总数:' . count($card_details));
|
|
|
|
return $card_details;
|
|
}
|
|
|
|
// 查询订单
|
|
protected function getOrders()
|
|
{
|
|
$this->line('查询订单记录');
|
|
$datetime = $this->getDateTime();
|
|
|
|
$starttime = $datetime->copy()->startOfMonth()->startOfDay()->format('Y-m-d H:i:s');
|
|
$endtime = $datetime->copy()->endOfMonth()->endOfDay()->format('Y-m-d H:i:s');
|
|
|
|
$company_ids = array_map([CommonService::class, 'stringifyCompanyId'], array_keys($this->companies));
|
|
|
|
$select = ['o_number', 'o_customer_number', 'o_customer_name', 'o_p_number', 'o_p_name', 'o_create_date', 'o_update_time','o_amount','o_price','o_card_counts'];
|
|
|
|
$orders = DB::connection('real')->table('jxc_order')->select($select)->where(function ($query) {
|
|
$query->where('o_status', '已出库待确认')->orWhere('o_status', '已确认');
|
|
})->whereBetween('o_create_date', [$starttime, $endtime])->where('o_card_use', '销售卡')
|
|
->whereNotIn('o_b_number', config('filter.bloc'))->where('o_is_del', 0)
|
|
->whereIn('o_customer_number', $company_ids)
|
|
->orderBy('o_create_date')->get()->keyBy('o_number')->toArray();
|
|
|
|
foreach ($orders as &$item) {
|
|
$item = (array)$item;
|
|
}
|
|
|
|
return $orders;
|
|
}
|
|
|
|
// 查询排单记录
|
|
protected function getOrderRows($orders)
|
|
{
|
|
$this->line('查询排单记录');
|
|
$orderRows = DB::connection('real')->table('jxc_order_single_row')->select('o_number', 's_number', 's_create_time')
|
|
->whereIn('o_number', array_keys($orders))->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;
|
|
}
|
|
|
|
return $orderRows;
|
|
}
|
|
|
|
// 查询排单详情
|
|
protected function getOrderItems($orderRows)
|
|
{
|
|
$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();
|
|
|
|
foreach ($orderItems as &$item) {
|
|
$item = (array)$item;
|
|
}
|
|
|
|
return $orderItems;
|
|
}
|
|
|
|
// 拼装插入数据
|
|
protected function transforms($cards, $card_details)
|
|
{
|
|
$this->line('拼装插入数据');
|
|
|
|
$dataOrders = [];
|
|
$dataCards = [];
|
|
$dataOrderCards = [];
|
|
|
|
foreach ($cards as $key => $value) {
|
|
$card_detail = $card_details[$value['sim']];
|
|
|
|
if (!$card_detail) {
|
|
$this->error('Mongo上未找到卡数据:' . $value['sim']);
|
|
}
|
|
|
|
$value['order_at'] = Carbon::parse($value['order_at'])->format('Y-m-d H:i:s');
|
|
|
|
$dataOrders[$value['order_id']] = [
|
|
'id' => $value['order_id'],
|
|
'type' => 0,
|
|
'company_id' => CommonService::parseCompanyId($value['company_id']),
|
|
'transaction_no' => '',
|
|
'pay_channel' => 0,
|
|
'unit_price' => $value['unit_price'],
|
|
'counts' => $value['counts'],
|
|
'total_price' => $value['total_price'],
|
|
'order_at' => $value['order_at'],
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
];
|
|
|
|
$dataCards[$value['sim']] = [
|
|
'sim' => $value['sim'],
|
|
'imsi' => $card_detail['imsi'] ?? '',
|
|
'iccid' => $card_detail['iccid'],
|
|
'company_id' => CommonService::parseCompanyId($value['company_id']),
|
|
'package_id' => $value['package_id'],
|
|
'bloc_id' => $card_detail['comId'] ?? '',
|
|
'price' => $value['unit_price'],
|
|
'carrier_operator' => self::$carrierOperators[$card_detail['oType']] ?? 255,
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
'updated_at' => date('Y-m-d H:i:s'),
|
|
];
|
|
|
|
$dataOrderCards[$value['sim']] = [
|
|
'sim' => $value['sim'],
|
|
'order_id' => $value['order_id'],
|
|
'counts' => 1,
|
|
'unit_price' => $value['unit_price'],
|
|
];
|
|
}
|
|
|
|
return [array_values($dataOrders), array_values($dataCards), $dataOrderCards];
|
|
}
|
|
}
|