同步基础订单

This commit is contained in:
邓皓元 2018-11-09 16:42:03 +08:00
parent a656a0a50c
commit e02e0e1017
13 changed files with 635 additions and 15 deletions

View File

@ -0,0 +1,63 @@
<?php
namespace App\Domains;
use App\Core\Repository;
use App\Models\Card as Model;
class CardRepository extends Repository
{
/**
* 是否关闭缓存
*
* @var boolean
*/
protected $cacheSkip = false;
/**
* 是否开启数据转化
*
* @var bool
*/
protected $needTransform = false;
/**
* @var array
*/
protected $fieldSearchable = [
'id' => '=',
'created_at' => 'like',
];
public function model()
{
return Model::class;
}
/**
* 数据格式化
*
* @param mixed $result
*
* @return mixed
*/
public function transform($model)
{
return $model->toArray();
}
/**
* 查询条件
*
* @return void
*/
public function withConditions(array $conditions = [])
{
if (isset($conditions['id'])) {
$conditions['id'] = array_wrap($conditions['id']);
$this->model = $this->model->whereIn('id', $conditions['id']);
}
return $this;
}
}

View File

@ -5,6 +5,7 @@ namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Real\Bloc;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Repositories\BlocRepository;
class BlocSync extends Command
{
@ -30,5 +31,7 @@ class BlocSync extends Command
}
Bloc::replace($data);
app(BlocRepository::class)->forgetCached();
}
}

View File

@ -5,6 +5,7 @@ namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Real\Company;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Repositories\CompanyRepository;
class CompanySync extends Command
{
@ -30,5 +31,7 @@ class CompanySync extends Command
}
Company::replace($data);
app(CompanyRepository::class)->forgetCached();
}
}

View File

@ -3,14 +3,263 @@
namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Card;
use App\Models\Real\Order;
use App\Domains\CardRepository;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Repositories\OrderRepository;
use App\Domains\Real\Repositories\CompanyRepository;
use App\Domains\Real\Repositories\PackageRepository;
class OrderBaseSync extends Command
{
protected $signature = 'sync:order-base';
protected $name = 'sync:order-base';
protected $description = '同步RD基础订单数据';
protected $companies;
protected $packages;
protected static $carrierOperators = [1, 0, 2];
protected $chunks = 5000;
public function handle()
{
$this->line('开始同步基础订单数据');
$this->companies = app(CompanyRepository::class)->get()->pluck('name', 'id')->toArray();
$this->packages = app(PackageRepository::class)->get()->pluck('name', 'id')->toArray();
$cards = $this->getCards();
$card_details = $this->getCardDetails($cards);
list($dataOrders, $dataCards, $dataOrderCards) = $this->transforms($cards, $card_details);
unset($cards);
unset($card_details);
$this->line('插入订单数据,条数:'.count($dataOrders));
foreach (array_chunk($dataOrders, $this->chunks) as $data) {
echo '.';
Order::replace($data);
}
app(OrderRepository::class)->forgetCached();
unset($dataOrders);
$this->line('插入订单数据成功');
$this->line('插入卡数据,条数:'.count($dataCards));
foreach (array_chunk($dataCards, $this->chunks) as $data) {
echo '.';
Card::replace($data);
}
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_card_relations')->replace($data);
}
unset($dataOrderCards);
$this->line('插入关联数据成功');
$this->line('同步成功');
}
// 获取月销售卡数据
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' => $order['o_amount'] * 100,
'unit_price' => $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', 'bNo', 'sPCode', 'iccid', 'imsi', 'soDate', 'comId', 'oType', 'jBatchNo'])
->whereIn('cNo', array_pluck($cardChunk, 'sim'))->get()->toArray();
$card_details = array_merge($card_details, $res);
}
unset($cardChunks);
if (count($cards) !== count($card_details)) {
throw new \Exception('卡数据中心数据与销售数据不对应');
}
$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');
$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', array_keys($this->companies))
->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) {
throw new \Exception('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' => $value['company_id'],
'package_id' => $value['package_id'],
'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' => $value['iccid'],
'real_company_id' => $value['company_id'],
'real_package_id' => $value['package_id'],
'real_bloc_id' => $card_detail['comId'],
'real_price' => $value['unit_price'],
'carrier_operator' => $carrierOperators[$card_detail['oType']],
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
$dataOrderCards[] = [
'sim' => $value['sim'],
'orders_id' => $value['order_id'],
];
}
return [array_values($dataOrders), array_values($dataCards), $dataOrderCards];
}
}

View File

@ -6,7 +6,7 @@ use Carbon\Carbon;
class OrderRenewalSync extends Command
{
protected $signature = 'sync:order-renewal';
protected $name = 'sync:order-renewal';
protected $description = '同步RD续费订单数据';

View File

@ -3,9 +3,10 @@
namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Real\Company;
use App\Models\Real\Package;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Repositories\CompanyRepository;
use App\Domains\Real\Repositories\PackageRepository;
class PackageSync extends Command
{
@ -15,16 +16,18 @@ class PackageSync extends Command
protected $companies;
public function handle()
{
$datetime = $this->getDateTime();
$this->companies = Company::all()->pluck('id')->toArray();
$this->companies = app(CompanyRepository::class)->get()->pluck('id')->toArray();
$basePackages = $this->getBasePackages();
$renewalPackages = $this->getRenewalPackages();
foreach ($renewalPackages as &$item) {
$item['carrier_operator'] = $basePackages[$item['parent_id']]['carrier_operator'];
$item['flows'] = $basePackages[$item['parent_id']]['flows'];
$item['voices'] = $basePackages[$item['parent_id']]['voices'];
$item['messages'] = $basePackages[$item['parent_id']]['messages'];
@ -40,6 +43,8 @@ class PackageSync extends Command
$packages = array_merge($basePackages, $renewalPackages, $flowPackages, $optionalPackages, $additionalPackages);
Package::replace($packages);
app(PackageRepository::class)->forgetCached();
}
// 基础包
@ -186,6 +191,19 @@ class PackageSync extends Command
foreach ($packages as &$package) {
$package = (array)$package;
$package['parent_id'] = $package['parent_id'] ?? '';
$package['carrier_operator'] = $package['carrier_operator'] ?? 255;
$package['cost_price'] = $package['cost_price'] ?? 0;
$package['guide_price'] = $package['guide_price'] ?? 0;
$package['flows'] = $package['flows'] ?? 0;
$package['voices'] = $package['voices'] ?? 0;
$package['messages'] = $package['messages'] ?? 0;
$package['has_message_switch'] = $package['has_message_switch'] ?? 0;
$package['has_lbs'] = $package['has_lbs'] ?? 0;
$package['reset_cycle'] = $package['reset_cycle'] ?? 0;
$package['service_cycle'] = $package['service_cycle'] ?? 0;
$package['updated_at'] = date('Y-m-d H:i:s');
}

View File

@ -27,7 +27,7 @@ class CreateRealTables extends Migration
Schema::create('real_blocs', function (Blueprint $table) {
$table->string('id', 20)->comment('集团编号');
$table->string('name', 32)->comment('集团名称');
$table->tinyInteger('carrier_operator')->comment('运营商(0:联通 1:移动 2:电信)');
$table->tinyInteger('carrier_operator')->unsigned()->default(255)->comment('运营商(0:联通 1:移动 2:电信)');
$table->timestamps();
$table->primary('id');
@ -41,15 +41,15 @@ class CreateRealTables extends Migration
$table->string('id', 20)->comment('套餐编号');
$table->string('parent_id', 20)->comment('父级编号');
$table->string('name', 32)->comment('套餐名称');
$table->tinyInteger('type')->unsigned()->default(0)->comment('套餐类型0:基础套餐 1续费包 2:加油包 3:可选包 4:附加包)');
$table->tinyInteger('carrier_operator')->comment('运营商(0:联通 1:移动 2:电信)');
$table->tinyInteger('type')->unsigned()->default(255)->comment('套餐类型0:基础套餐 1续费包 2:加油包 3:可选包 4:附加包)');
$table->tinyInteger('carrier_operator')->unsigned()->default(255)->comment('运营商(0:联通 1:移动 2:电信)');
$table->integer('cost_price')->unsigned()->default(0)->comment('成本价格');
$table->integer('guide_price')->unsigned()->default(0)->comment('指导价格');
$table->integer('flows')->unsigned()->default(0)->comment('套餐流量(M)');
$table->integer('voices')->unsigned()->default(0)->comment('套餐语音(分钟)');
$table->integer('messages')->unsigned()->default(0)->comment('套餐短信(条)');
$table->tinyInteger('has_message_switch')->unsigned()->default(0)->comment('短信开关0:无 1');
$table->tinyInteger('has_lbs')->unsigned()->default(0)->comment('lbs位置服务0:无 1');
$table->tinyInteger('has_message_switch')->unsigned()->default(255)->comment('短信开关0:无 1');
$table->tinyInteger('has_lbs')->unsigned()->default(255)->comment('lbs位置服务0:无 1');
$table->tinyInteger('reset_cycle')->unsigned()->default(0)->comment('流量周期(月)');
$table->tinyInteger('service_cycle')->unsigned()->default(0)->comment('套餐周期(月)');
$table->timestamps();
@ -74,21 +74,23 @@ class CreateRealTables extends Migration
Schema::create('cards', function (Blueprint $table) {
$table->string('sim', 20)->comment('sim号');
$table->string('imei', 20)->comment('imei号');
$table->string('imsi', 20)->comment('imsi号');
$table->string('iccid', 20)->comment('iccid号');
$table->string('real_company_id', 20)->comment('RD企业编号');
$table->string('real_package_id', 20)->comment('RD套餐编号');
$table->string('real_bloc_id', 20)->comment('RD来源集团编号');
$table->integer('real_price')->unsigned()->default(0)->comment('RD售价');
$table->tinyInteger('carrier_operator')->comment('运营商(0:联通 1:移动 2:电信)');
$table->tinyInteger('carrier_operator')->unsigned()->default(255)->comment('运营商(0:联通 1:移动 2:电信)');
$table->string('virtual_company_id', 20)->comment('VD企业编号');
$table->string('virtual_package_id', 20)->comment('VD套餐编号');
$table->integer('virtual_price')->unsigned()->default(0)->comment('VD售价');
$table->timestamp('activate_at')->nullable()->comment('激活时间');
$table->timestamp('service_start_at')->nullable()->comment('服务开始时间');
$table->timestamp('service_end_at')->nullable()->comment('服务结束时间');
$table->timestamps();
$table->primary('sim');
$table->index('imei');
$table->index('imsi');
$table->index('iccid');
$table->index('real_company_id');
$table->index('real_package_id');
@ -97,23 +99,24 @@ class CreateRealTables extends Migration
$table->index('virtual_company_id');
$table->index('virtual_package_id');
$table->index('activate_at');
$table->index('service_start_at');
$table->index('service_end_at');
});
db_alter('cards', '卡基础信息表');
Schema::create('real_orders', function (Blueprint $table) {
$table->string('id', 20)->comment('订单编号');
$table->tinyInteger('type')->unsigned()->default(0)->comment('套餐类型0:基础套餐 1续费包 2:加油包 3:可选包 4:附加包)');
$table->tinyInteger('type')->unsigned()->default(255)->comment('套餐类型0:基础套餐 1续费包 2:加油包 3:可选包 4:附加包)');
$table->string('company_id', 20)->comment('RD企业编号');
$table->string('package_id', 20)->comment('RD套餐编号');
$table->integer('unit_price')->unsigned()->default(0)->comment('订单单价');
$table->integer('counts')->unsigned()->default(0)->comment('出卡数量');
$table->integer('total_price')->unsigned()->default(0)->comment('订单总价');
$table->timestamp('order_at')->nullable()->comment('订单时间');
$table->timestamp('service_start_at')->nullable()->comment('服务开始时间');
$table->timestamp('service_end_at')->nullable()->comment('服务结束时间');
$table->timestamps();
$table->primary('id');
$table->index('type');
$table->index('company_id');
$table->index('package_id');

View File

@ -0,0 +1,62 @@
<?php
namespace App\Domains\Real\Repositories;
use App\Core\Repository;
use App\Models\Real\Bloc as Model;
class BlocRepository extends Repository
{
/**
* 是否关闭缓存
*
* @var boolean
*/
protected $cacheSkip = false;
/**
* 是否开启数据转化
*
* @var bool
*/
protected $needTransform = false;
/**
* @var array
*/
protected $fieldSearchable = [
'id' => '=',
'created_at' => 'like',
];
public function model() {
return Model::class;
}
/**
* 数据格式化
*
* @param mixed $result
*
* @return mixed
*/
public function transform($model)
{
return $model->toArray();
}
/**
* 查询条件
*
* @return void
*/
public function withConditions(array $conditions = [])
{
if (isset($conditions['id'])) {
$conditions['id'] = array_wrap($conditions['id']);
$this->model = $this->model->whereIn('id', $conditions['id']);
}
return $this;
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace App\Domains\Real\Repositories;
use App\Core\Repository;
use App\Models\Real\Company as Model;
class CompanyRepository extends Repository
{
/**
* 是否关闭缓存
*
* @var boolean
*/
protected $cacheSkip = false;
/**
* 是否开启数据转化
*
* @var bool
*/
protected $needTransform = false;
/**
* @var array
*/
protected $fieldSearchable = [
'id' => '=',
'created_at' => 'like',
];
public function model() {
return Model::class;
}
/**
* 数据格式化
*
* @param mixed $result
*
* @return mixed
*/
public function transform($model)
{
return $model->toArray();
}
/**
* 查询条件
*
* @return void
*/
public function withConditions(array $conditions = [])
{
if (isset($conditions['id'])) {
$conditions['id'] = array_wrap($conditions['id']);
$this->model = $this->model->whereIn('id', $conditions['id']);
}
return $this;
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace App\Domains\Real\Repositories;
use App\Core\Repository;
use App\Models\Real\Order as Model;
class OrderRepository extends Repository
{
/**
* 是否关闭缓存
*
* @var boolean
*/
protected $cacheSkip = false;
/**
* 是否开启数据转化
*
* @var bool
*/
protected $needTransform = false;
/**
* @var array
*/
protected $fieldSearchable = [
'id' => '=',
'created_at' => 'like',
];
public function model() {
return Model::class;
}
/**
* 数据格式化
*
* @param mixed $result
*
* @return mixed
*/
public function transform($model)
{
return $model->toArray();
}
/**
* 查询条件
*
* @return void
*/
public function withConditions(array $conditions = [])
{
if (isset($conditions['id'])) {
$conditions['id'] = array_wrap($conditions['id']);
$this->model = $this->model->whereIn('id', $conditions['id']);
}
return $this;
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace App\Domains\Real\Repositories;
use App\Core\Repository;
use App\Models\Real\Package as Model;
class PackageRepository extends Repository
{
/**
* 是否关闭缓存
*
* @var boolean
*/
protected $cacheSkip = false;
/**
* 是否开启数据转化
*
* @var bool
*/
protected $needTransform = false;
/**
* @var array
*/
protected $fieldSearchable = [
'id' => '=',
'created_at' => 'like',
];
public function model() {
return Model::class;
}
/**
* 数据格式化
*
* @param mixed $result
*
* @return mixed
*/
public function transform($model)
{
return $model->toArray();
}
/**
* 查询条件
*
* @return void
*/
public function withConditions(array $conditions = [])
{
if (isset($conditions['id'])) {
$conditions['id'] = array_wrap($conditions['id']);
$this->model = $this->model->whereIn('id', $conditions['id']);
}
return $this;
}
}

View File

@ -72,6 +72,7 @@ class AppServiceProvider extends ServiceProvider
{
$this->app->configure('icon');
$this->app->configure('regex');
$this->app->configure('filter');
}
/**

32
config/filter.php Normal file
View File

@ -0,0 +1,32 @@
<?php
return [
// RD同步基础套餐时过滤的卡源
'bloc' => [
'FXFT_szjcdsj', // 福州电信-自管理门户
'FXFT_LT-101728118', // 【联通】黑龙江联通
'FXFT_13103495', // 龙岩移动
'FXFT_1381409', // 莆田移动
'FXFT_LT-HLJ', // 【联通】黑龙江测试集团
'FXFT_lt_jasper_gdfs', // lt_jasper_gdfs
'FXFT_yika', // yika云创集团
'FXFT_yd_test', // yd_test云创测试
'FXFT_LT-muke', // LT-muke
'FXFT_zylh_report', // 浙江移动(刘晓松)
'FXFT_ylreport', // 上海移动(刘晓松)
'FXFT_xiaomi', // 小米
'FXFT_lt_yika', // lt_yika云创卡源
'FXFT_lt_88', // lt_88云创卡源
'FXFT_yd_shanghaika', // yd_shanghaika
'FXFT_yd_weimi', // Weimi
'FXFT_12345', // 枢建通信
'FXFT_0595-1', // 【移动】泉州公田软件有限公司(泉州)
'FXFT_0755-1', // 【移动】深圳市博睿大地科技有限公司(深圳南山)
'FXFT_023-2', // 【移动】深圳市迈明瑞科技文化有限公(重庆南岸)
'FXFT_0595-2', // 【移动】深圳万里通网络信息技术有限公司1泉州
'FXFT_shangtong', // 尚通科技发展有限公司
'FXFT_SH-DX', // 上海电信(第三方)
'FXFT_weimi-LT', // weimi-联通
'FXFT_1343142', // yd_qzlc
],
];