2020-05-20 01:50:34 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\User;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use League\Csv\Writer;
|
|
|
|
|
|
|
|
class CheckinCsvExporter
|
|
|
|
{
|
|
|
|
/** @var User Target user */
|
|
|
|
private $user;
|
|
|
|
/** @var string Output filename */
|
|
|
|
private $filename;
|
|
|
|
/** @var string Output charset */
|
|
|
|
private $charset;
|
|
|
|
|
|
|
|
public function __construct(User $user, string $filename, string $charset)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
$this->filename = $filename;
|
|
|
|
$this->charset = $charset;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute()
|
|
|
|
{
|
|
|
|
$csv = Writer::createFromPath($this->filename, 'wb');
|
|
|
|
$csv->setNewline("\r\n");
|
|
|
|
if ($this->charset === 'SJIS-win') {
|
|
|
|
$csv->addStreamFilter('convert.mbstring.encoding.UTF-8:SJIS-win');
|
|
|
|
}
|
|
|
|
|
2020-08-23 12:28:21 +09:00
|
|
|
$header = ['日時', 'ノート', 'オカズリンク'];
|
2020-05-20 01:50:34 +09:00
|
|
|
for ($i = 1; $i <= 32; $i++) {
|
|
|
|
$header[] = "タグ{$i}";
|
|
|
|
}
|
|
|
|
$csv->insertOne($header);
|
|
|
|
|
|
|
|
DB::transaction(function () use ($csv) {
|
|
|
|
// TODO: そんなに読み取り整合性を保つ努力はしていないのと、chunkの件数これでいいか分からない
|
|
|
|
$this->user->ejaculations()->with('tags')->orderBy('ejaculated_date')
|
|
|
|
->chunk(1000, function ($ejaculations) use ($csv) {
|
|
|
|
foreach ($ejaculations as $ejaculation) {
|
|
|
|
$record = [
|
2020-05-21 22:28:34 +09:00
|
|
|
$ejaculation->ejaculated_date->format('Y/m/d H:i'),
|
2020-05-20 01:50:34 +09:00
|
|
|
$ejaculation->note,
|
|
|
|
$ejaculation->link,
|
|
|
|
];
|
2020-05-20 21:42:22 +09:00
|
|
|
foreach ($ejaculation->tags->take(32) as $tag) {
|
2020-05-20 01:50:34 +09:00
|
|
|
$record[] = $tag->name;
|
|
|
|
}
|
|
|
|
$csv->insertOne($record);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|