同步TD流量池编号

This commit is contained in:
邓皓元 2019-02-11 15:37:57 +08:00
parent e8ac469047
commit 68db63e437
6 changed files with 182 additions and 14 deletions

View File

@ -0,0 +1,84 @@
<?php
namespace App\Domains\Real\Commands\Sync;
use Carbon\Carbon;
use App\Models\Real\FlowPool;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Domains\Real\Repositories\CompanyRepository;
use App\Domains\Real\Repositories\PackageRepository;
use App\Domains\Real\Repositories\FlowPoolRepository;
class FlowPoolSync extends Command
{
protected $name = 'real:sync-flow-pool';
protected $description = '同步RD流量池';
protected $carrier_operator = ['lt' => 0, 'yd' => 1, 'dx' => 2, 'qw' => 3];
public function handle()
{
$companies = app(CompanyRepository::class)->withTrashed()->get()->keyBy('sn');
$packages = app(PackageRepository::class)->withTrashed()->get()->keyBy('sn');
$select = ['id', 'shared_type as shared', "level_type as level", 'custom_no', 'cell_number as sn', 'name', 'flows', 'status', 'del', 'create_time'];
$types = DB::connection('real')->table('jxc_flow_cell_type')->select(['id', 'carrieroperator'])->get()->pluck('carrieroperator', 'id')->toArray();
foreach ($types as $key => &$type) {
$type = $this->carrier_operator[$type];
}
$flowPools = DB::connection('real')->table('jxc_flow_cell')->select($select)
->whereIn('custom_no', $companies->keys()->toArray())
->get()->collect()->toArray();
$flowPackages = DB::connection('real')->table('jxc_flow_cell_package')->select([
'flow_cell_id',
DB::raw('group_concat(distinct package_sn) as package_sn'),
])->whereIn('flow_cell_id', array_pluck($flowPools, 'id'))->groupBy('flow_cell_id')->get()->pluck('package_sn', 'flow_cell_id')->toArray();
foreach ($flowPackages as $flowPoolId => $flowPackage) {
$array = explode(',', $flowPackage);
$ids = [];
foreach ($array as $key => $value) {
if (!$packages[$value]) {
continue;
}
$ids[] = $packages[$value]['id'];
}
$flowPackages[$flowPoolId] = $ids;
}
$data = [];
foreach ($flowPools as $value) {
$carrier_operator = $types[$value['typeid']] ?? 3;
$package_ids = $flowPackages[$value['id']] ?? [];
$data[] = [
'id' => $value['id'],
'sn' => $value['sn'],
'company_id' => $companies[$value['custom_no']]['id'],
'name' => $value['name'],
'flows' => $value['flows'] == -255 ? -1 : intval($value['flows']),
'carrier_operator' => $carrier_operator,
'shared' => $value['shared'],
'package_ids' => json_encode($package_ids),
'created_at' => date('Y-m-d H:i:s', $value['create_time']),
'updated_at' => date('Y-m-d H:i:s'),
'deleted_at' => ($value['status'] == 0 || $value['del'] == 1) ? date('Y-m-d H:i:s') : null,
];
}
FlowPool::upsert($data, 'id');
app(FlowPoolRepository::class)->forgetCached();
}
}

View File

@ -27,6 +27,7 @@ class RealServiceProvider extends ServiceProvider
\App\Domains\Real\Commands\Sync\PackageSync::class,
\App\Domains\Real\Commands\Sync\OrderBaseSync::class,
\App\Domains\Real\Commands\Sync\AddedOrderSync::class,
\App\Domains\Real\Commands\Sync\FlowPoolSync::class,
]);
}

View File

@ -0,0 +1,62 @@
<?php
namespace App\Domains\Real\Repositories;
use App\Core\Repository;
use App\Models\Real\FlowPool as Model;
class FlowPoolRepository 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,15 @@
<?php
namespace App\Models\Real;
use App\Core\Model;
use App\Models\Card\Card;
class FlowPool extends Model
{
protected $table = 'real_flow_pools';
protected $casts = [
'package_ids' => 'array',
];
}

View File

@ -25,6 +25,7 @@ class CreateRealAddedOrderCardsTables extends Migration
$table->integer('order_id')->unsigned()->default(0)->comment('订单ID');
$table->integer('company_id')->unsigned()->default(0)->comment('企业ID');
$table->integer('package_id')->unsigned()->default(0)->comment('套餐ID');
$table->integer('unit_price')->unsigned()->default(0)->comment('单价');
$table->integer('counts')->unsigned()->default(0)->comment('数量');
$table->timestamps();

View File

@ -13,16 +13,14 @@ class CreateFlowPoolTables extends Migration
*/
public function up()
{
return;
if (!Schema::hasTable('real_flow_pools')) {
Schema::create('real_flow_pools', function (Blueprint $table) {
$table->increments('id')->comment('自增ID');
$table->string('sn', 32)->default('')->comment('编号');
$table->integer('relation_id')->unsigned()->default(0)->comment('关联ID 1级流量池关联集团 2级流量池关联企业');
$table->integer('company_id')->unsigned()->default(0)->comment('关联企业ID');
$table->string('name', 32)->unsigned()->default(0)->comment('名称');
$table->integer('flows')->default(255)->comment('流量值 -1不限流量 单位MB');
$table->tinyInteger('carrier_operator')->unsigned()->default(255)->comment('运营商(0:联通 1:移动 2:电信)');
$table->tinyInteger('level')->unsigned()->default(0)->comment('等级 0未知 1:一级流量池 2:二级流量池');
$table->tinyInteger('shared')->unsigned()->default(0)->comment('共享类型 0:未知 1同流量横向共享 2基础流量纵向共享 3纵向共享');
$table->text('package_ids')->nullable()->comment('包含套餐');
$table->timestamps();
@ -68,21 +66,28 @@ class CreateFlowPoolTables extends Migration
$table->index('pool_id');
$table->comment('流量池定价');
$table->comment('VD流量池定价');
});
}
if (!Schema::hasTable('virtual_card_flows')) {
Schema::create('virtual_card_flows', function (Blueprint $table) {
$table->increments('id')->comment('订单ID');
if (!Schema::hasTable('virtual_flow_pool_cards')) {
Schema::create('virtual_flow_pool_cards', function (Blueprint $table) {
$table->integer('pool_id')->unsigned()->default(0)->comment('流量池ID');
$table->bigInteger('sim')->unsigned()->default(0)->comment('SIM号');
$table->integer('month')->unsigned()->default(0)->comment('月份');
$table->integer('kilobyte')->unsigned()->default(0)->comment('使用流量 单位KB');
$table->comment('VD卡流量使用情况');
$table->comment('VD流量池卡关联表');
$table->index('month');
$table->index('pool_id');
$table->primary(['pool_id', 'sim']);
});
}
if (!Schema::hasTable('virtual_flow_pool_months')) {
Schema::create('virtual_flow_pool_months', function (Blueprint $table) {
$table->increments('id')->comment('订单ID');
$table->integer('month')->unsigned()->default(0)->comment('月份');
$table->bigInteger('sim')->unsigned()->default(0)->comment('SIM号');
$table->integer('pool_id')->unsigned()->default(0)->comment('流量池ID');
$table->integer('kilobyte')->unsigned()->default(0)->comment('使用流量 单位KB');
$table->comment('VD卡月流量');
$table->partition('month', 'list');
});
@ -96,8 +101,8 @@ class CreateFlowPoolTables extends Migration
*/
public function down()
{
return;
Schema::dropIfExists('virtual_card_flows');
Schema::dropIfExists('virtual_flow_pool_months');
Schema::dropIfExists('virtual_flow_pool_cards');
Schema::dropIfExists('virtual_flow_pool_settings');
Schema::dropIfExists('virtual_flow_pools');
Schema::dropIfExists('real_flow_pools');