diff --git a/app/Domains/Virtual/Services/OrderService.php b/app/Domains/Virtual/Services/OrderService.php index b9f8e082..2dbbf034 100644 --- a/app/Domains/Virtual/Services/OrderService.php +++ b/app/Domains/Virtual/Services/OrderService.php @@ -255,9 +255,8 @@ class OrderService extends Service } } - /** - * 转销售方式 - if (count($news)) { + // 用转销售方式创建 + if ($attributes['use_type'] === 1 && count($news)) { // 创建订单 $newOrderData = array_except($attributes, ['selected', 'sign']); $newOrderData['id'] = ++$maxId; @@ -298,16 +297,16 @@ class OrderService extends Service if (in_array($item['sim'], $news)) { $counts = $item['counts'] - 1; $selectedNews[$item['sim']] = [ - 'sim' => $item['sim'], - 'counts' => $counts - ]; + 'sim' => $item['sim'], + 'counts' => $counts + ]; continue; } $selectedNews[$item['sim']] = [ - 'sim' => $item['sim'], - 'counts' => $item['counts'], - ]; + 'sim' => $item['sim'], + 'counts' => $item['counts'], + ]; } $selectedNews = array_filter($selectedNews, function ($item) { @@ -316,44 +315,11 @@ 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); + $news = []; } - if (count($extras)) { + // 替卡方式 + if (count($news) || count($extras)) { $attributes['package_id'] = $attributes['package_id'] ?? $product->package_id; $having = "MAX(service_end_at) < '%s'"; @@ -366,31 +332,59 @@ class OrderService extends Service ->where('original_sim', 0) ->groupBy('sim'); - $replacement = DB::table(DB::raw("({$subQuery->toSql()}) as sub")) + $replacementQuery = 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($extras)) - ->get()->pluck('sim')->toArray(); + ->where('service_end_at', '<', Carbon::now()->subMonths(6)->endOfMonth()); - if (count($replacement) !== count($extras)) { - throw new NotAllowedException('可替换的卡量不足,不能进行转销售操作'); + if (count($news)) { + $replacement = $replacementQuery->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); } - $updates = []; - foreach ($extras as $key => $value) { - $updates[] = [ + if (count($extras)) { + $replacement = $replacementQuery->limit(count($extras))->get()->pluck('sim')->toArray(); + + if (count($replacement) !== count($extras)) { + throw new NotAllowedException('可替换的卡量不足,不能进行转销售操作'); + } + + $updates = []; + foreach ($extras as $key => $value) { + $updates[] = [ 'sim' => $value, 'original_sim' => $replacement[$key], ]; - } + } - $parameters = implode(',', array_map(function ($item) { - return "({$item['sim']}, {$item['original_sim']})"; - }, $updates)); + $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_olds AS ( + $sql = "WITH RECURSIVE as_table(sim, original_sim) AS (VALUES {$parameters}), update_olds AS ( 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 ) @@ -398,7 +392,8 @@ class OrderService extends Service FROM as_table WHERE virtual_order_cards_partition.sim = as_table.sim; "; - DB::statement($sql); + DB::statement($sql); + } } }