From b8d3f1bbcb2eeade73228f495d3205eaaa95e97c Mon Sep 17 00:00:00 2001 From: denghy Date: Tue, 29 Jan 2019 17:22:52 +0800 Subject: [PATCH] vd --- app/helpers.php | 34 ++++-- ...4_164722_create_virtual_packages_table.php | 2 +- ...2_29_112239_create_stats_exports_table.php | 4 + ...9_01_24_175246_create_flow_pool_tables.php | 112 ++++++++++++++++++ tests/ExampleTest.php | 14 +++ vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + .../src/Database/DatabaseServiceProvider.php | 55 +++++++-- .../foundation/src/Database/MySqlGrammar.php | 2 +- .../src/Database/PostgresSchemaGrammar.php | 54 ++++++++- 10 files changed, 254 insertions(+), 25 deletions(-) create mode 100644 database/migrations/2019_01_24_175246_create_flow_pool_tables.php diff --git a/app/helpers.php b/app/helpers.php index 4626e06e..6dbdbfad 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -139,7 +139,7 @@ if (! function_exists('array_merge_sum')) { $array[$key] = isset($array[$key]) ? $array[$key] + $value : $value; } } - + return $array; } } @@ -148,14 +148,34 @@ if (! function_exists('human_filesize')) { /** * 文件大小可读性 * - * @param array $arr1 - * @param array $arr2 + * @param int $int + * @param int $decimals + * @param array $options * @return void */ - function human_filesize($bytes, $decimals = 2) + function human_filesize($int, $decimals = 2, array $options = []) { - $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB'); - $factor = floor((strlen($bytes) - 1) / 3); - return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor]; + $size = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + $factor = floor((strlen($int) - 1) / 3); + + $options = array_map('strtoupper', $options); + + if (isset($options['unit']) && array_search($options['unit'], $size) !== false) { + for ($i=0; $i < array_search($options['unit'], $size); $i++) { + unset($size[$i]); + } + + $size = array_values($size); + } + + if (isset($options['min']) && array_search($options['min'], $size) !== false) { + $factor = $factor < array_search($options['min'], $size) ? array_search($options['min'], $size) : $factor; + } + + if (isset($options['max']) && array_search($options['max'], $size) !== false) { + $factor = $factor > array_search($options['max'], $size) ? array_search($options['max'], $size) : $factor; + } + + return sprintf("%.{$decimals}f", $int / pow(1024, $factor)) . @$size[$factor]; } } diff --git a/database/migrations/2018_12_24_164722_create_virtual_packages_table.php b/database/migrations/2018_12_24_164722_create_virtual_packages_table.php index ed214c2a..ab085ba2 100644 --- a/database/migrations/2018_12_24_164722_create_virtual_packages_table.php +++ b/database/migrations/2018_12_24_164722_create_virtual_packages_table.php @@ -28,7 +28,7 @@ class CreateVirtualPackagesTable extends Migration $table->integer('guide_price')->unsigned()->default(0)->comment('指导价格'); $table->integer('renewal_cost_price')->unsigned()->default(0)->comment('续费成本价格'); $table->integer('renewal_guide_price')->unsigned()->default(0)->comment('续费指导价格'); - $table->integer('flows')->unsigned()->default(0)->comment('套餐流量(M)'); + $table->integer('flows')->unsigned()->default(0)->comment('套餐流量 单位MB'); $table->integer('voices')->unsigned()->default(0)->comment('套餐语音(分钟)'); $table->integer('messages')->unsigned()->default(0)->comment('套餐短信(条)'); $table->tinyInteger('has_messages')->unsigned()->default(255)->comment('是否开通短信服务(0:无 1:有)'); diff --git a/database/migrations/2018_12_29_112239_create_stats_exports_table.php b/database/migrations/2018_12_29_112239_create_stats_exports_table.php index 20441d52..91990047 100644 --- a/database/migrations/2018_12_29_112239_create_stats_exports_table.php +++ b/database/migrations/2018_12_29_112239_create_stats_exports_table.php @@ -13,6 +13,10 @@ class CreateStatsExportsTable extends Migration */ public function up() { + if (Schema::hasTable('exports')) { + return; + } + Schema::create('exports', function (Blueprint $table) { $table->increments('id')->comment('自增ID'); $table->string('sn', 32)->default('')->comment('命令编号'); diff --git a/database/migrations/2019_01_24_175246_create_flow_pool_tables.php b/database/migrations/2019_01_24_175246_create_flow_pool_tables.php new file mode 100644 index 00000000..0d910526 --- /dev/null +++ b/database/migrations/2019_01_24_175246_create_flow_pool_tables.php @@ -0,0 +1,112 @@ +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->primary('month'); + + $table->partition('month', 'list'); + $table->addPartition('virtual_card_flows_201801', 'list', [201801]); + }); + } + + 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->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(); + $table->softDeletes(); + + $table->unique(['sn', 'deleted_at']); + + $table->comment('RD流量池'); + }); + } + + if (!Schema::hasTable('virtual_flow_pools')) { + Schema::create('virtual_flow_pools', function (Blueprint $table) { + $table->increments('id')->comment('自增ID'); + $table->string('sn', 32)->default('')->comment('编号'); + $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('shared')->unsigned()->default(0)->comment('共享类型 0:未知 1纵向共享 2横向共享'); + $table->tinyInteger('status')->unsigned()->default(0)->comment('状态 0:正常 1:禁用'); + $table->text('package_ids')->nullable()->comment('包含套餐'); + $table->text('real_pool_ids')->nullable()->comment('RD流量池ID'); + $table->timestamps(); + $table->softDeletes(); + + $table->unique(['sn', 'deleted_at']); + + $table->comment('VD流量池'); + }); + } + + if (!Schema::hasTable('virtual_flow_pool_settings')) { + Schema::create('virtual_flow_pools', function (Blueprint $table) { + $table->increments('id')->comment('自增ID'); + $table->integer('pool_id')->unsigned()->default(0)->comment('流量池ID'); + $table->text('minimum_settings')->nullable()->comment('套餐保底配置'); + $table->integer('first_month_price')->unsigned()->default(0)->comment('首月单价'); + $table->integer('other_month_price')->unsigned()->default(0)->comment('次月单价'); + $table->timestamp('start_at'); + $table->timestamp('end_at'); + $table->softDeletes(); + + $table->index('pool_id'); + + $table->comment('流量池定价'); + }); + } + + if (!Schema::hasTable('virtual_card_flows')) { + Schema::create('virtual_card_flows', 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->primary('sim'); + }); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('virtual_card_flows'); + } +} diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index af1222e2..ef25fb3e 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -1,3 +1,17 @@ 'KB', 'min' => 'MB', 'max' => 'GB']) . PHP_EOL; +echo human_filesize(20*pow(1024, 1), 2, ['unit' => 'KB', 'min' => 'MB', 'max' => 'GB']) . PHP_EOL; +echo human_filesize(20*pow(1024, 2), 2, ['unit' => 'KB', 'min' => 'MB', 'max' => 'GB']) . PHP_EOL; +echo human_filesize(20*pow(1024, 3), 2, ['unit' => 'KB', 'min' => 'MB', 'max' => 'GB']) . PHP_EOL; +echo human_filesize(20*pow(1024, 4), 2, ['unit' => 'KB', 'min' => 'MB', 'max' => 'GB']) . PHP_EOL; diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 5c8c1c81..2c436ca6 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -14,6 +14,7 @@ return array( 'CreateBlocsTable' => $baseDir . '/database/migrations/2018_12_24_164210_create_blocs_table.php', 'CreateCardsTable' => $baseDir . '/database/migrations/2018_12_24_164218_create_cards_table.php', 'CreateFailedJobsTable' => $baseDir . '/database/migrations/0000_00_00_000000_create_failed_jobs_table.php', + 'CreateFlowPoolTables' => $baseDir . '/database/migrations/2019_01_24_175246_create_flow_pool_tables.php', 'CreateJobsTable' => $baseDir . '/database/migrations/0000_00_00_000000_create_jobs_table.php', 'CreateRealAddedOrderCardsTables' => $baseDir . '/database/migrations/2018_12_24_164459_create_real_added_order_cards_tables.php', 'CreateRealAddedOrdersTable' => $baseDir . '/database/migrations/2018_12_24_164457_create_real_added_orders_table.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index e4ed751d..f7c0f4b9 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -733,6 +733,7 @@ class ComposerStaticInite79258a3e34ad3e251999111d9f334d9 'CreateBlocsTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164210_create_blocs_table.php', 'CreateCardsTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164218_create_cards_table.php', 'CreateFailedJobsTable' => __DIR__ . '/../..' . '/database/migrations/0000_00_00_000000_create_failed_jobs_table.php', + 'CreateFlowPoolTables' => __DIR__ . '/../..' . '/database/migrations/2019_01_24_175246_create_flow_pool_tables.php', 'CreateJobsTable' => __DIR__ . '/../..' . '/database/migrations/0000_00_00_000000_create_jobs_table.php', 'CreateRealAddedOrderCardsTables' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164459_create_real_added_order_cards_tables.php', 'CreateRealAddedOrdersTable' => __DIR__ . '/../..' . '/database/migrations/2018_12_24_164457_create_real_added_orders_table.php', diff --git a/vendor/dipper/foundation/src/Database/DatabaseServiceProvider.php b/vendor/dipper/foundation/src/Database/DatabaseServiceProvider.php index a9141149..b9161fc0 100644 --- a/vendor/dipper/foundation/src/Database/DatabaseServiceProvider.php +++ b/vendor/dipper/foundation/src/Database/DatabaseServiceProvider.php @@ -29,7 +29,7 @@ class DatabaseServiceProvider extends ServiceProvider $connection->setQueryGrammar(new PostgresGrammar); return $connection; }); - + DB::extend('mysql', function ($config, $name) { $factory = new ConnectionFactory($this->app); $connection = $factory->make($config, $name); @@ -45,7 +45,9 @@ class DatabaseServiceProvider extends ServiceProvider QueryBuilder::macro('createNotExist', $this->macroCreateNotExist()); QueryBuilder::macro('updateBatch', $this->macroUpdateBatch()); SchemaBlueprint::macro('comment', $this->macroSchemaComment()); + SchemaBlueprint::macro('partition', $this->macroSchemaPartition()); SchemaBlueprint::macro('setIncrement', $this->macroSchemaSetIncrement()); + SchemaBlueprint::macro('addPartition', $this->macroSchemaAddPartition()); } /** @@ -59,17 +61,17 @@ class DatabaseServiceProvider extends ServiceProvider if (empty($values)) { return false; } - + if (!is_array(reset($values))) { $values = [$values]; } else { foreach ($values as $key => $value) { ksort($value); - + $values[$key] = $value; } } - + return $this->connection->affectingStatement( $this->grammar->compileReplace($this, $values), $this->cleanBindings(Arr::flatten($values, 1)) @@ -88,17 +90,17 @@ class DatabaseServiceProvider extends ServiceProvider if (empty($values)) { return false; } - + if (!is_array(reset($values))) { $values = [$values]; } else { foreach ($values as $key => $value) { ksort($value); - + $values[$key] = $value; } } - + return $this->connection->affectingStatement( $this->grammar->compileInsertUpdate($this, $values), $this->cleanBindings(Arr::flatten($values, 1)) @@ -146,21 +148,21 @@ class DatabaseServiceProvider extends ServiceProvider if (empty($values)) { return false; } - + if (empty($where)) { return false; } - + if (!is_array(reset($values))) { $values = [$values]; } else { foreach ($values as $key => $value) { ksort($value); - + $values[$key] = $value; } } - + return $this->connection->affectingStatement( $this->grammar->compileCreateNotExist($this, $values, $where), $this->cleanBindings(Arr::flatten($values, 1)) @@ -181,7 +183,7 @@ class DatabaseServiceProvider extends ServiceProvider if (empty($values)) { return false; } - + if (!is_array(reset($values))) { $values = [$values]; } else { @@ -210,7 +212,7 @@ class DatabaseServiceProvider extends ServiceProvider if (empty($values)) { return false; } - + if (!is_array(reset($values))) { $values = [$values]; } else { @@ -249,4 +251,31 @@ class DatabaseServiceProvider extends ServiceProvider return $this->addCommand('setIncrement', compact('number')); }; } + + /** + * 分区表 + * + * @return void + */ + public function macroSchemaPartition() + { + return function ($column, $type) { + $this->partition = [ + 'type' => $type, + 'column' => $column, + ]; + }; + } + + /** + * 添加分区 + * + * @return void + */ + public function macroSchemaAddPartition() + { + return function ($table, $type, array $conditions) { + return $this->addCommand('addPartition', compact('table', 'type', 'conditions')); + }; + } } diff --git a/vendor/dipper/foundation/src/Database/MySqlGrammar.php b/vendor/dipper/foundation/src/Database/MySqlGrammar.php index 3c74feca..dfcd0b43 100644 --- a/vendor/dipper/foundation/src/Database/MySqlGrammar.php +++ b/vendor/dipper/foundation/src/Database/MySqlGrammar.php @@ -53,7 +53,7 @@ class MySqlGrammar extends Grammar // simply makes creating the SQL easier for us since we can utilize the same // basic routine regardless of an amount of records given to us to insert. $table = $this->wrapTable($query->from); - + if (!is_array(reset($values))) { $values = [$values]; } diff --git a/vendor/dipper/foundation/src/Database/PostgresSchemaGrammar.php b/vendor/dipper/foundation/src/Database/PostgresSchemaGrammar.php index eca279a5..9dd1a342 100644 --- a/vendor/dipper/foundation/src/Database/PostgresSchemaGrammar.php +++ b/vendor/dipper/foundation/src/Database/PostgresSchemaGrammar.php @@ -21,11 +21,18 @@ class PostgresSchemaGrammar extends Grammar { $sql = []; + $partition = ''; + + if ($blueprint->partition) { + $partition = sprintf('partition by %s (%s)', $blueprint->partition['type'], $blueprint->partition['column']); + } + $sql[] = sprintf( - '%s table %s (%s)', + '%s table %s (%s) %s', $blueprint->temporary ? 'create temporary' : 'create', $this->wrapTable($blueprint), - implode(', ', $this->getColumns($blueprint)) + implode(', ', $this->getColumns($blueprint)), + $partition ); $comments = collect($blueprint->getAddedColumns())->where('comment', '<>', '')->all(); @@ -88,10 +95,35 @@ class PostgresSchemaGrammar extends Grammar if ($column) { $index = $this->wrap("{$blueprint->getTable()}_{$column->name}_seq"); - return sprintf("select setval('%s', %d, true);", $index, $command->number); + return sprintf("select setval('%s', %d, true)", $index, $command->number); } } + /** + * Compile a add partition table command. + * + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * @return string + */ + public function compileAddPartition(Blueprint $blueprint, Fluent $command) + { + if ($command->type === 'list') { + $sql = sprintf('IN (%s)', implode(',', array_map([$this, 'wrapString'], $command->conditions))); + } + + if ($command->type === 'range') { + $sql = sprintf("FROM ('%s') TO ('%s')", $command->conditions[0], $command->conditions[1]); + } + + return sprintf( + "create table %s partition of %s FOR VALUES %s", + $command->table, + $this->wrapTable($blueprint), + $sql + ); + } + /** * Convert an array of column names into a delimited string. * @@ -108,4 +140,20 @@ class PostgresSchemaGrammar extends Grammar return $this->wrap($column); }, $columns)); } + + + /** + * Wrap a single string in keyword identifiers. + * + * @param string $value + * @return string + */ + protected function wrapString($value) + { + if ($value !== '*') { + return "'".str_replace("'", "''''", $value)."'"; + } + + return $value; + } }