This commit is contained in:
邓皓元 2019-04-24 14:13:23 +08:00
parent a4f3b877ca
commit 8a31259822
13 changed files with 124 additions and 37 deletions

View File

@ -153,7 +153,6 @@ class AddedOrderSync extends Command
->whereIn('account_no', array_keys($this->business_type))
->where('create_time', '>=', $starttime->timestamp)
->where('create_time', '<=', $endtime->timestamp)
->where('r_id', 112370)
->orderBy('create_time')->get()->collect()->toArray();
$array = [];

View File

@ -329,4 +329,20 @@ class OrderController extends Controller
return res($order, '排单成功');
}
/**
* 套餐升级.
*
* @return \Illuminate\Http\Response
*/
public function upgrade()
{
$attributes = $this->request->all();
$order = $this->orderService->upgrade($attributes);
return res($order, '修改成功');
}
}

View File

@ -924,6 +924,24 @@ class OrderService extends Service
return $order;
}
/**
* 套餐升级
*/
public function upgrade(array $attributes = [])
{
$rule = [
'new_package_id' => ['required'],
'selected' => ['required', 'array'],
];
$message = [
'new_package_id.required' => '请选择新的套餐',
'selected.required' => '请选择卡',
];
Validator::validate($attributes, $rule, $message);
}
protected function upsertOrderCards($array, $node)
{
$table = $this->tables[$node['type']];

View File

@ -26,7 +26,7 @@ class CreateRealOrderCardsTable extends Migration
$table->integer('package_id')->unsigned()->default(0)->comment('套餐ID');
$table->integer('counts')->unsigned()->default(0)->comment('数量');
$table->integer('unit_price')->unsigned()->default(0)->comment('单价');
$table->integer('virtual_order_id')->unsigned()->default(0)->comment('VD 订单ID');
$table->integer('virtual_order_id')->unsigned()->default(0)->comment('RD 订单ID');
$table->timestamp('refunded_at')->nullable()->comment('退货时间');
$table->timestamps();
$table->softDeletes();
@ -45,22 +45,32 @@ class CreateRealOrderCardsTable extends Migration
Schema::table('real_order_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id']);
$table->unique(['sim', 'deleted_at']);
$table->comment("RD销售订单关联表");
});
Schema::table('real_order_renewal_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->comment("RD续费订单关联表");
});
Schema::table('real_order_renewal_package_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->comment("RD续费包订单关联表");
});
Schema::table('real_order_flows_package_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->comment("RD加油包订单关联表");
});
Schema::table('real_order_optional_package_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->comment("RD可选包订单关联表");
});
Schema::table('real_order_additional_package_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->comment("RD附加包订单关联表");
});
}

View File

@ -21,7 +21,7 @@ class CreateVirtualOrdersTable extends Migration
$table->increments('id')->comment('订单ID');
$table->string('sn', 32)->comment('订单编号');
$table->tinyInteger('source')->unsigned()->default(0)->comment('订单来源0:用户订单 1:后台订单)');
$table->tinyInteger('type')->unsigned()->default(0)->comment('订单类型0:基础订单 1:套餐续费 2续费包 3:加油包 4:可选包 5:附加包');
$table->tinyInteger('type')->unsigned()->default(0)->comment('订单类型0:基础订单 1:套餐续费 2续费包 3:加油包 4:可选包 5:附加包 6:组合包 7:套餐升级');
$table->integer('company_id')->unsigned()->default(0)->comment("企业ID");
$table->integer('package_id')->unsigned()->default(0)->comment('套餐ID');
$table->string('transaction_no', 64)->default('')->comment('交易流水号');

View File

@ -49,24 +49,28 @@ class CreateVirtualOrderCardsTables extends Migration
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->index(['type', 'company_id', 'package_id', 'unit_price']);
$table->index(['service_start_at', 'service_end_at']);
$table->comment("VD销售订单关联表");
});
Schema::table('virtual_order_renewal_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->index(['type', 'company_id', 'package_id', 'unit_price']);
$table->index(['service_start_at', 'service_end_at']);
$table->comment("VD续费订单关联表");
});
Schema::table('virtual_order_renewal_package_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->index(['type', 'company_id', 'package_id', 'unit_price']);
$table->index(['service_start_at', 'service_end_at']);
$table->comment("VD续费包订单关联表");
});
Schema::table('virtual_order_flows_package_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->index(['type', 'company_id', 'package_id', 'unit_price']);
$table->index(['service_start_at', 'service_end_at']);
$table->comment("VD加油包订单关联表");
});
DB::unprepared(File::get(__DIR__ . '/create_virtual_order_cards_func.pgsql'));

View File

@ -19,6 +19,7 @@ class AddRealOrderGroupPackageCardsPartition extends Migration
Schema::table('real_order_group_package_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->comment("RD组合包订单关联表");
});
}

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddVirtualOrderUpgradeCardsPartition extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('virtual_order_cards_partition', function (Blueprint $table) {
$table->addPartition('virtual_order_upgrade_cards', 'list', [7]);
});
Schema::table('virtual_order_upgrade_cards', function (Blueprint $table) {
$table->unique(['sim', 'order_id', 'deleted_at']);
$table->index(['type', 'company_id', 'package_id', 'unit_price']);
$table->index(['service_start_at', 'service_end_at']);
$table->comment("VD升级订单关联表");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('virtual_order_upgrade_cards');
}
}

View File

@ -25,39 +25,38 @@ CREATE OR REPLACE FUNCTION GET_TIMELINES(INT8[])
AS $$
DECLARE
activate_cards JSONB := '[]';
packages JSONB := '[]';
query TEXT;
package_row JSONB := '[]';
orders JSON[] := '{}';
order_row RECORD;
temp_service_start_at TIMESTAMP;
temp_service_end_at TIMESTAMP;
activated_at TIMESTAMP;
BEGIN
RAISE NOTICE 't1';
SELECT array_to_json(array_agg(row_to_json(t))) INTO activate_cards
FROM (SELECT cards.sim, cards.virtual_activated_at FROM vd.cards WHERE cards.sim = ANY ($1)) t;
RAISE NOTICE 't2';
query := 'SELECT
virtual_order_cards_partition.id,
virtual_order_cards_partition.type,
virtual_order_cards_partition.sim,
virtual_order_cards_partition.package_id,
virtual_order_cards_partition.created_at,
virtual_packages.service_months,
virtual_packages.effect_months,
virtual_packages.delay_months,
virtual_order_cards_partition.counts,
virtual_order_cards_partition.service_start_at,
virtual_order_cards_partition.service_end_at
FROM vd.virtual_order_cards_partition JOIN vd.virtual_packages ON virtual_order_cards_partition.package_id = virtual_packages."id"
WHERE virtual_order_cards_partition.sim = ANY($1)
FROM (SELECT cards.sim, cards.virtual_activated_at FROM vd.cards WHERE cards.sim = ANY ($1)) t;
SELECT array_to_json(array_agg(row_to_json(t))) INTO packages
FROM (SELECT virtual_packages.id, virtual_packages.service_months, virtual_packages.effect_months, virtual_packages.effect_months FROM vd.virtual_packages) t;
query := 'SELECT id, type, sim, package_id, created_at, counts, service_start_at, service_end_at
FROM vd.virtual_order_cards_partition
WHERE sim = ANY($1)
ORDER BY sim ASC, created_at ASC';
SELECT array_agg(row_to_json(t)) INTO orders
FROM EXECUTE query;
RAISE NOTICE '%s', orders;
FOR order_row IN EXECUTE query USING $1
LOOP
RAISE NOTICE 't3';
SELECT value->>'virtual_activated_at' INTO activated_at
FROM json_array_elements(activate_cards::JSON)
WHERE value->>'sim' = order_row.sim::TEXT;
RAISE NOTICE 't4';
FROM json_array_elements(activate_cards::JSON)
WHERE value->>'sim' = order_row.sim::TEXT;
IF NOT FOUND THEN
temp_service_start_at := NULL;
@ -66,22 +65,26 @@ BEGIN
temp_service_start_at := NULL;
temp_service_end_at := NULL;
ELSE
SELECT value INTO package_row
FROM json_array_elements(packages::JSON)
WHERE value->>'id' = order_row.package_id::TEXT;
-- 服务时间
CASE (order_row."type")
WHEN 0 THEN
temp_service_start_at := TO_CHAR(activated_at, 'YYYY-MM-01 00:00:00');
temp_service_end_at := temp_service_start_at + (order_row.service_months || ' month')::INTERVAL +
(order_row.delay_months || ' month')::INTERVAL - '1 second'::INTERVAL;
temp_service_end_at := temp_service_start_at + (package_row->>'service_months' || ' month')::INTERVAL +
(package_row->>'delay_months' || ' month')::INTERVAL - '1 second'::INTERVAL;
WHEN 1, 2 THEN
IF (temp_service_end_at > order_row.created_at) THEN
temp_service_start_at := TO_CHAR(temp_service_end_at + '1 month'::INTERVAL, 'YYYY-MM-01 00:00:00');
ELSE
temp_service_start_at := TO_CHAR(order_row.created_at + (order_row.effect_months || ' month')::INTERVAL,
temp_service_start_at := TO_CHAR(order_row.created_at + (package_row->>'effect_months' || ' month')::INTERVAL,
'YYYY-MM-01 00:00:00');
END IF;
temp_service_end_at :=
temp_service_start_at + order_row.counts * (order_row.service_months || ' month')::INTERVAL +
(order_row.delay_months || ' month')::INTERVAL - '1 second'::INTERVAL;
temp_service_start_at + order_row.counts * (package_row->>'service_months' || ' month')::INTERVAL +
(package_row->>'delay_months' || ' month')::INTERVAL - '1 second'::INTERVAL;
ELSE
-- 先购买了加油包后再激活的
IF (order_row.created_at < activated_at) THEN
@ -92,15 +95,15 @@ BEGIN
ELSE
-- 延时生效
temp_service_start_at :=
TO_CHAR(activated_at + (order_row.effect_months || ' month')::INTERVAL, 'YYYY-MM-01 00:00:00');
TO_CHAR(activated_at + (package_row->>'effect_months' || ' month')::INTERVAL, 'YYYY-MM-01 00:00:00');
END IF;
ELSE
-- 延时生效
temp_service_start_at := TO_CHAR(order_row.created_at + (order_row.effect_months || ' month')::INTERVAL,
temp_service_start_at := TO_CHAR(order_row.created_at + (package_row->>'effect_months' || ' month')::INTERVAL,
'YYYY-MM-01 00:00:00');
END IF;
temp_service_end_at := temp_service_start_at + (order_row.service_months || ' month')::INTERVAL +
(order_row.delay_months || ' month')::INTERVAL - '1 second'::INTERVAL;
temp_service_end_at := temp_service_start_at + (package_row->>'service_months' || ' month')::INTERVAL +
(package_row->>'delay_months' || ' month')::INTERVAL - '1 second'::INTERVAL;
END CASE;
END IF;
@ -112,7 +115,6 @@ BEGIN
service_end_at := COALESCE(order_row.service_end_at::TIMESTAMP, temp_service_end_at::TIMESTAMP);
RETURN NEXT;
END LOOP;
RAISE NOTICE 't5';
RETURN;
END;
$$ LANGUAGE plpgsql;

View File

@ -10,5 +10,3 @@ use Illuminate\Database\Schema\Blueprint;
use App\Domains\Virtual\Services\OrderService;
require_once realpath(dirname(__FILE__) . '/TestCase.php');
var_dump(preg_match('/[1-2][0-9]{3}[0-1][0-9]/', '201901'));

View File

@ -5,7 +5,7 @@ use App\Domains\Card\Services\CardService;
require_once realpath(dirname(__FILE__) . '/TestCase.php');
$simArray = [
'1440418967511'
'1440418268370'
];
$res = CardService::getMongoCardsInfo($simArray);

View File

@ -9,6 +9,7 @@ return array(
'AccountSeeder' => $baseDir . '/database/seeds/AccountSeeder.php',
'AddBusinessTypeToRealOrders' => $baseDir . '/database/migrations/2019_04_16_110601_add_business_type_to_real_orders.php',
'AddRealOrderGroupPackageCardsPartition' => $baseDir . '/database/migrations/2019_04_17_161638_add_real_order_group_package_cards_partition.php',
'AddVirtualOrderUpgradeCardsPartition' => $baseDir . '/database/migrations/2019_04_23_102650_add_virtual_order_upgrade_cards_partition.php',
'ArithmeticError' => $vendorDir . '/symfony/polyfill-php70/Resources/stubs/ArithmeticError.php',
'AssertionError' => $vendorDir . '/symfony/polyfill-php70/Resources/stubs/AssertionError.php',
'CompanyAccountSeeder' => $baseDir . '/database/seeds/CompanyAccountSeeder.php',

View File

@ -728,6 +728,7 @@ class ComposerStaticInite79258a3e34ad3e251999111d9f334d9
'AccountSeeder' => __DIR__ . '/../..' . '/database/seeds/AccountSeeder.php',
'AddBusinessTypeToRealOrders' => __DIR__ . '/../..' . '/database/migrations/2019_04_16_110601_add_business_type_to_real_orders.php',
'AddRealOrderGroupPackageCardsPartition' => __DIR__ . '/../..' . '/database/migrations/2019_04_17_161638_add_real_order_group_package_cards_partition.php',
'AddVirtualOrderUpgradeCardsPartition' => __DIR__ . '/../..' . '/database/migrations/2019_04_23_102650_add_virtual_order_upgrade_cards_partition.php',
'ArithmeticError' => __DIR__ . '/..' . '/symfony/polyfill-php70/Resources/stubs/ArithmeticError.php',
'AssertionError' => __DIR__ . '/..' . '/symfony/polyfill-php70/Resources/stubs/AssertionError.php',
'CompanyAccountSeeder' => __DIR__ . '/../..' . '/database/seeds/CompanyAccountSeeder.php',