diff --git a/app/Domains/Stats/Http/Controllers/OrderController.php b/app/Domains/Stats/Http/Controllers/OrderController.php index 36fee12a..48bb57e0 100644 --- a/app/Domains/Stats/Http/Controllers/OrderController.php +++ b/app/Domains/Stats/Http/Controllers/OrderController.php @@ -1,4 +1,5 @@ groupBy(['company_id', 'package_id', 'unit_price', 'pay_channel'])->paginate($conditions['limit']); $order_ids = []; + $groups = []; - foreach ($orders as $order) { - $order_ids = array_merge($order_ids, str_to_array($order->order_id)); + foreach ($orders as $i => $order) { + $order->gid = $i; + $o_ids = str_to_array($order->order_id); + + foreach ($o_ids as $v) { + $groups[$v] = "($i, $v)"; + } + + $order_ids = array_merge($order_ids, $o_ids); } $order_ids = array_unique($order_ids); - $members = $repository->select([ - 'order_id', + $values = implode(',', $groups); + $join = DB::raw("(VALUES $values) as t(gid, o_id)"); + + $members = $repository->join($join, 'o_id', '=', 'order_id')->select([ + 'gid', DB::raw('SUM(counts) as counts'), DB::raw('COUNT(distinct sim) as members'), - ])->withConditions(['order_id' => $order_ids])->groupBy('order_id')->get()->keyBy('order_id'); + ])->withConditions(['order_id' => $order_ids])->groupBy('gid')->get(); $orders->map(function ($item) use ($companies, $packages, $members) { $item->company_name = $companies[$item->company_id]; $item->package_name = $packages[$item->package_id]; - $order_ids = str_to_array($item->order_id); - - $item->members = 0; - $item->counts = 0; - - foreach ($order_ids as $id) { - $item->members += $members[$id]['members'] ?? 0; - $item->counts += $members[$id]['counts'] ?? 0; - } - + $item->members = $members[$item->gid]['members'] ?? 0; + $item->counts = $members[$item->gid]['counts'] ?? 0; $item->custom_price = sprintf('%.02f', $item->unit_price * $item->counts / 100); $item->unit_price = sprintf('%.02f', $item->unit_price / 100); diff --git a/app/Domains/Virtual/Commands/Schedule/AutoActivate.php b/app/Domains/Virtual/Commands/Schedule/AutoActivate.php index 16b2d2df..aaa64b7b 100644 --- a/app/Domains/Virtual/Commands/Schedule/AutoActivate.php +++ b/app/Domains/Virtual/Commands/Schedule/AutoActivate.php @@ -26,29 +26,13 @@ class AutoActivate extends Command $this->info('自动激活脚本'); - // $simArray = app(OrderCardPartitionRepository::class)->select('sim') - // ->where('created_at', '>=', $time->copy()->startOfMonth()) - // ->where('created_at', '<=', $time->copy()->endOfMonth()) - // ->whereNull('service_start_at') - // ->get()->pluck('sim')->toArray(); + $simArray = sprintf("SELECT sim FROM virtual_order_cards WHERE created_at >= '%s' AND created_at <= '%s' AND service_start_at IS NULL ORDER BY created_at DESC", $time->copy()->startOfMonth(), $time->copy()->endOfMonth()); - // $this->info(microtime(true)); + $updateSql = "UPDATE cards SET virtual_activated_at='%s' WHERE sim IN (%s) AND virtual_activated_at IS NULL"; + DB::statement(sprintf($updateSql, $datetime->startOfMonth(), $simArray)); - // foreach (array_chunk($simArray, 1000) as $values) { - // Card::query()->whereIn('sim', $values)->whereNull('virtual_activated_at')->update(['virtual_activated_at' => $datetime->startOfMonth()]); - // $values = implode(',', array_keys($values)); - // DB::statement("select fix_timelines('{{$values}}'::INT8[]);"); - // } - - $sql = "WITH sim_array AS ( - SELECT sim FROM virtual_order_cards WHERE created_at >= '%s' AND created_at <= '%s' AND service_start_at IS NULL ORDER BY created_at DESC - ), activate_updates AS ( - UPDATE cards SET virtual_activated_at='%s' WHERE sim IN (SELECT * FROM sim_array) AND virtual_activated_at IS NULL - ) - SELECT fix_timelines(ARRAY(SELECT * FROM sim_array)); - "; - - DB::statement(sprintf($sql, $time->copy()->startOfMonth(), $time->copy()->endOfMonth(), $datetime->startOfMonth())); + $fixSql = "SELECT fix_timelines(ARRAY(%s))"; + DB::statement(sprintf($fixSql, $simArray)); app(CardRepository::class)->forgetCached(); app(OrderCardPartitionRepository::class)->forgetCached(); diff --git a/database/migrations/create_virtual_order_cards_func.pgsql b/database/migrations/create_virtual_order_cards_func.pgsql index 25e1dfce..801fd085 100644 --- a/database/migrations/create_virtual_order_cards_func.pgsql +++ b/database/migrations/create_virtual_order_cards_func.pgsql @@ -48,7 +48,7 @@ BEGIN virtual_order_cards_partition.service_start_at, virtual_order_cards_partition.service_end_at FROM vd.virtual_order_cards_partition - WHERE virtual_order_cards_partition.sim = ANY ($1) AND deleted_at IS NOT NULL AND refunded_at IS NOT NULL + WHERE virtual_order_cards_partition.sim = ANY ($1) AND deleted_at IS NULL AND refunded_at IS NULL ORDER BY virtual_order_cards_partition.sim ASC, virtual_order_cards_partition.created_at ASC) t; IF orders IS NOT NULL THEN diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 175cd3e0..69fd6c6d 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -1,35 +1,17 @@ [2, 3], - 'company_name' => '福建车媒通网络科技有限公司', - 'month' => '2016-01' -]; +$service = app(OrderService::class); -$start = Carbon::parse('2016-01')->startOfMonth(); -$end = Carbon::parse('2019-07')->endOfMonth(); +$res = $service->index([ + 'company_name' => '福建省福信富通网络科技股份有限公司', + 'starttime' => '2016-01-01 00:00:00', + 'endtime' => '2016-12-31 23:59:59', + 'type' => 1, +]); -for ($i = 0; $i <= $end->diffInMonths($start); $i++) { - $month = $start->copy()->addMonths($i)->format('Y-m'); - $conditions['month'] = $month; - echo $month . PHP_EOL; - $export = new CompanyReportExport($conditions); - $excel = new Excel( - app()->make(Writer::class), - app()->make(QueuedWriter::class), - app()->make(Reader::class), - app()->make('filesystem') - ); - $excel->store($export, $export->filename, 'public'); - unset($export); -} +dd($res->toArray()); diff --git a/tests/ExportManyTest.php b/tests/ExportManyTest.php new file mode 100644 index 00000000..175cd3e0 --- /dev/null +++ b/tests/ExportManyTest.php @@ -0,0 +1,35 @@ + [2, 3], + 'company_name' => '福建车媒通网络科技有限公司', + 'month' => '2016-01' +]; + +$start = Carbon::parse('2016-01')->startOfMonth(); +$end = Carbon::parse('2019-07')->endOfMonth(); + +for ($i = 0; $i <= $end->diffInMonths($start); $i++) { + $month = $start->copy()->addMonths($i)->format('Y-m'); + $conditions['month'] = $month; + echo $month . PHP_EOL; + $export = new CompanyReportExport($conditions); + $excel = new Excel( + app()->make(Writer::class), + app()->make(QueuedWriter::class), + app()->make(Reader::class), + app()->make('filesystem') + ); + $excel->store($export, $export->filename, 'public'); + unset($export); +}