From a35fd9569a8c902cc648c0018a9fcd9d916eaae5 Mon Sep 17 00:00:00 2001 From: denghy Date: Thu, 18 Apr 2019 09:12:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E5=8E=9F=E6=9C=89=E5=8D=A1=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Domains/Virtual/Services/OrderService.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/app/Domains/Virtual/Services/OrderService.php b/app/Domains/Virtual/Services/OrderService.php index 83d9c7b9..b9f8e082 100644 --- a/app/Domains/Virtual/Services/OrderService.php +++ b/app/Domains/Virtual/Services/OrderService.php @@ -255,6 +255,8 @@ class OrderService extends Service } } + /** + * 转销售方式 if (count($news)) { // 创建订单 $newOrderData = array_except($attributes, ['selected', 'sign']); @@ -315,6 +317,41 @@ class OrderService extends Service $attributes['counts'] = array_sum(array_pluck($selectedNews, 'counts')); $attributes['selected'] = array_values($selectedNews); } + */ + + /** 用原有卡替换方式 */ + if (count($news)) { + $replacement = DB::table(DB::raw("({$subQuery->toSql()}) as sub")) + ->mergeBindings($subQuery->getQuery()) + ->select(['sim']) + ->orderBy('service_end_at') + ->where('service_end_at', '<', Carbon::now()->subMonths(6)->endOfMonth()) + ->limit(count($news)) + ->get()->pluck('sim')->toArray(); + + if (count($replacement) !== count($news)) { + throw new NotAllowedException('可替换的卡量不足,不能进行转销售操作'); + } + + $updates = []; + foreach ($news as $key => $value) { + $updates[] = [ + 'sim' => $value, + 'original_sim' => $replacement[$key], + ]; + } + + $parameters = implode(',', array_map(function ($item) { + return "({$item['sim']}, {$item['original_sim']})"; + }, $updates)); + + $sql = "WITH RECURSIVE as_table(sim, original_sim) AS (VALUES {$parameters}) + UPDATE virtual_order_cards_partition SET sim = as_table.sim, original_sim = as_table.original_sim + FROM as_table WHERE virtual_order_cards_partition.sim = as_table.original_sim + "; + + DB::statement($sql); + } if (count($extras)) { $attributes['package_id'] = $attributes['package_id'] ?? $product->package_id;