分组计算人数
This commit is contained in:
parent
7aa0e9d262
commit
d870c56aff
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Stats\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
@ -63,33 +63,36 @@ class OrderService extends Service
|
||||
->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);
|
||||
|
@ -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();
|
||||
|
@ -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
|
||||
|
@ -1,35 +1,17 @@
|
||||
<?php
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Dipper\Excel\Excel;
|
||||
use Dipper\Excel\Reader;
|
||||
use Dipper\Excel\Writer;
|
||||
use Dipper\Excel\QueuedWriter;
|
||||
use App\Domains\Export\Services\ExportService;
|
||||
use App\Domains\Stats\Exports\CompanyReportExport;
|
||||
use App\Domains\Stats\Services\OrderService;
|
||||
|
||||
|
||||
require_once realpath(dirname(__FILE__) . '/TestCase.php');
|
||||
|
||||
$conditions = [
|
||||
'type' => [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());
|
||||
|
35
tests/ExportManyTest.php
Normal file
35
tests/ExportManyTest.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Dipper\Excel\Excel;
|
||||
use Dipper\Excel\Reader;
|
||||
use Dipper\Excel\Writer;
|
||||
use Dipper\Excel\QueuedWriter;
|
||||
use App\Domains\Export\Services\ExportService;
|
||||
use App\Domains\Stats\Exports\CompanyReportExport;
|
||||
|
||||
require_once realpath(dirname(__FILE__) . '/TestCase.php');
|
||||
|
||||
$conditions = [
|
||||
'type' => [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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user