优化流量池同步

This commit is contained in:
邓皓元 2019-04-26 01:11:19 +08:00
parent a6cc1c50dc
commit f1aea73335

View File

@ -73,24 +73,35 @@ class FlowPoolMonthSync extends Command
$this->line('插入数据,条数 #:' . count($array));
foreach (array_groupBy($array, 'month') as $month => $group) {
$news = [];
$array = array_groupBy($array, 'month');
foreach ($array as $month => $group) {
$group = array_groupBy($group, 'sim');
$news = [];
foreach ($group as $sim => $item) {
if (count($item) > 1) {
$temp = $item[0];
$temp['mebibyte'] = array_sum(array_pluck($item, 'mebibyte'));
$group[$sim] = [$temp];
$mebibyte = 0;
foreach ($item as $key => $value) {
$mebibyte += $value['mebibyte'];
}
$temp['mebibyte'] = $mebibyte;
$news[$sim] = $temp;
}else {
$news[$sim] = $item[0];
}
}
$news = array_merge($news, array_collapse($group));
$array[$month] = array_values($news);
}
foreach ($array as $month => $items) {
$table = FlowPoolService::checkTable($month);
foreach (array_chunk($news, 1000) as $values) {
foreach (array_chunk($items, 1000) as $values) {
echo '.';
app(FlowPoolMonth::class)->setTable($table)->upsert($values, ['sim', 'month']);
}