From 4efbc7db259965b01a0734d027a313949b23e1f5 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Tue, 13 Dec 2022 09:24:30 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E7=AD=BE=E5=88=B0?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/PublicController.php | 22 ++++-- app/Http/Controllers/Api/SystemController.php | 22 ++++-- app/Http/Controllers/Api/UsersController.php | 21 ++---- app/Models/UserCheckinRecord.php | 69 ++++++++++++++++- app/Models/WebSocketDialogUser.php | 2 +- ..._214403_create_user_checkin_macs_table.php | 3 + ...4409_create_user_checkin_records_table.php | 3 + ...19_add_date_times_user_checkin_records.php | 74 +++++++++++++++++++ .../manage/components/CheckinCalendar.vue | 32 ++------ 9 files changed, 194 insertions(+), 54 deletions(-) create mode 100644 database/migrations/2022_12_13_092619_add_date_times_user_checkin_records.php diff --git a/app/Http/Controllers/Api/PublicController.php b/app/Http/Controllers/Api/PublicController.php index a04ac794d..0bd1c097c 100755 --- a/app/Http/Controllers/Api/PublicController.php +++ b/app/Http/Controllers/Api/PublicController.php @@ -88,15 +88,25 @@ class PublicController extends AbstractController return 'key error'; } // + $nowDate = date("Y-m-d"); + $nowTime = date("H:i:s"); $macs = explode(",", $mac); - foreach ($macs as $item) { - $item = strtoupper($item); - if (Base::isMac($item) && $UserCheckinMac = UserCheckinMac::whereMac($item)->first()) { - UserCheckinRecord::createInstance([ + foreach ($macs as $mac) { + $mac = strtoupper($mac); + if (Base::isMac($mac) && $UserCheckinMac = UserCheckinMac::whereMac($mac)->first()) { + $array = [ 'userid' => $UserCheckinMac->userid, 'mac' => $UserCheckinMac->mac, - 'time' => $time, - ])->save(); + 'date' => $nowDate, + ]; + $record = UserCheckinRecord::where($array)->first(); + if (empty($record)) { + $record = UserCheckinRecord::createInstance($array); + $record->save(); + } + $record->times = Base::array2json(array_merge($record->times, [$nowTime])); + $record->report_time = $time; + $record->save(); } } return 'success'; diff --git a/app/Http/Controllers/Api/SystemController.php b/app/Http/Controllers/Api/SystemController.php index 5180a7d40..cbbd913b2 100755 --- a/app/Http/Controllers/Api/SystemController.php +++ b/app/Http/Controllers/Api/SystemController.php @@ -856,6 +856,7 @@ class SystemController extends AbstractController $headings[] = '首次签到结果'; $headings[] = '最后签到时间'; $headings[] = '最后签到结果'; + $headings[] = '参数数据'; // $sheets = []; $startD = Carbon::parse($date[0])->startOfDay(); @@ -863,22 +864,25 @@ class SystemController extends AbstractController $users = User::whereIn('userid', $userid)->take(20)->get(); /** @var User $user */ foreach ($users as $user) { - $records = UserCheckinRecord::whereUserid($user->userid)->whereBetween("created_at", [$startD, $endD])->orderBy('id')->get(); + $records = UserCheckinRecord::whereUserid($user->userid)->whereBetween("created_at", [$startD, $endD])->orderBy('id')->get()->keyBy('date'); // $nickname = Base::filterEmoji($user->nickname); - $styles = ["A1:G1" => ["font" => ["bold" => true]]]; + $styles = ["A1:H1" => ["font" => ["bold" => true]]]; $datas = []; $startT = $startD->timestamp; $endT = $endD->timestamp; $index = 1; while ($startT < $endT) { $index++; + $sameDate = date("Y-m-d", $startT); + $sameRecord = isset($records[$sameDate]) ? $records[$sameDate] : null; + $sameCollect = $sameRecord?->atCollect(); $firstBetween = [Carbon::createFromTimestamp($startT), Carbon::createFromTimestamp($startT + $secondEnd - 1)]; $lastBetween = [Carbon::createFromTimestamp($startT + $secondStart + 1), Carbon::createFromTimestamp($startT + 86400)]; - $firstRecord = $records->whereBetween("created_at", $firstBetween)->first(); - $lastRecord = $records->whereBetween("created_at", $lastBetween)->last(); - $firstTimestamp = $firstRecord ? Carbon::parse($firstRecord->created_at)->timestamp : 0; - $lastTimestamp = $lastRecord ? Carbon::parse($lastRecord->created_at)->timestamp : 0; + $firstRecord = $sameCollect?->whereBetween("datetime", $firstBetween)->first(); + $lastRecord = $sameCollect?->whereBetween("datetime", $lastBetween)->last(); + $firstTimestamp = $firstRecord['timestamp'] ?: 0; + $lastTimestamp = $lastRecord['timestamp'] ?: 0; if (Base::time() < $startT + $secondStart) { $firstResult = "-"; } else { @@ -906,14 +910,18 @@ class SystemController extends AbstractController } $firstTimestamp = $firstTimestamp ? date("H:i", $firstTimestamp) : "-"; $lastTimestamp = $lastTimestamp ? date("H:i", $lastTimestamp) : "-"; + $section = array_map(function($item) { + return $item[0] . "-" . ($item[1] ?: "None"); + }, $sameRecord?->atSection() ?: []); $datas[] = [ "{$nickname} (ID: {$user->userid})", - date("Y-m-d", $startT), + $sameDate, implode("-", $time), $firstTimestamp, $firstResult, $lastTimestamp, $lastResult, + implode(", ", $section), ]; $startT += 86400; } diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index fc4eed49e..3d3a5e964 100755 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -1412,25 +1412,18 @@ class UsersController extends AbstractController $start = Carbon::parse(date("Y-m-01 00:00:00", strtotime($ym))); $end = (clone $start)->addMonth()->subSecond(); // - $records = UserCheckinRecord::whereUserid($user->userid)->whereBetween('created_at', [$start, $end])->orderBy('id')->get(); + $records = UserCheckinRecord::whereUserid($user->userid)->whereBetween('created_at', [$start, $end])->orderBy('id')->get()->keyBy('date'); $array = []; $startT = $start->timestamp; $endT = $end->timestamp; while ($startT < $endT) { - $between = [Carbon::createFromTimestamp($startT), Carbon::createFromTimestamp($startT + 86400)]; - $firstRecord = $records->whereBetween("created_at", $between)->first(); - $lastRecord = $records->whereBetween("created_at", $between)->last(); - $firstTimestamp = $firstRecord ? Carbon::parse($firstRecord->created_at)->toDateTimeString() : ''; - $lastTimestamp = $lastRecord ? Carbon::parse($lastRecord->created_at)->toDateTimeString() : ''; - if ($firstTimestamp) { - $data = [ - 'time' => $firstTimestamp, - 'all' => [$firstTimestamp], + $sameDate = date("Y-m-d", $startT); + $sameRecord = isset($records[$sameDate]) ? $records[$sameDate] : null; + if ($sameRecord) { + $array[] = [ + 'date' => $sameDate, + 'section' => $sameRecord->atSection(), ]; - if ($lastTimestamp) { - $data['all'][] = $lastTimestamp; - } - $array[] = $data; } $startT += 86400; } diff --git a/app/Models/UserCheckinRecord.php b/app/Models/UserCheckinRecord.php index 1592e30e7..17e5c01c3 100644 --- a/app/Models/UserCheckinRecord.php +++ b/app/Models/UserCheckinRecord.php @@ -11,16 +11,20 @@ use App\Module\Base; * @property int $id * @property int|null $userid 会员id * @property string|null $mac MAC地址 - * @property int|null $time 上报的时间戳 + * @property string|null $date 签到日期 + * @property array $times 签到时间 + * @property int|null $report_time 上报的时间戳 * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord newQuery() * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord query() * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereDate($value) * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereMac($value) - * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereTime($value) + * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereReportTime($value) + * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereTimes($value) * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereUpdatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereUserid($value) * @mixin \Eloquent @@ -28,4 +32,65 @@ use App\Module\Base; class UserCheckinRecord extends AbstractModel { + /** + * 签到记录 + * @param $value + * @return array + */ + public function getTimesAttribute($value) + { + if (is_array($value)) { + return $value; + } + return Base::json2array($value); + } + + /** + * 时间收集 + * @return \Illuminate\Support\Collection + */ + public function atCollect() + { + $sameTimes = array_map(function($time) { + return [ + "datetime" => "{$this->date} {$time}", + "timestamp" => strtotime("{$this->date} {$time}") + ]; + }, $this->times); + return collect($sameTimes); + } + + /** + * 签到时段 + * @param int $diff 多长未签到算失效(秒) + * @return array + */ + public function atSection($diff = 3600) + { + $start = ""; + $end = ""; + $array = []; + foreach ($this->times as $time) { + $time = preg_replace("/:00$/", "", $time); + if (empty($start)) { + $start = $time; + continue; + } + if (empty($end)) { + $end = $time; + continue; + } + if (strtotime("2022-01-01 {$time}") - strtotime("2022-01-01 {$end}") > $diff) { + $array[] = [$start, $end]; + $start = $time; + $end = ""; + continue; + } + $end = $time; + } + if ($start) { + $array[] = [$start, $end]; + } + return $array; + } } diff --git a/app/Models/WebSocketDialogUser.php b/app/Models/WebSocketDialogUser.php index 1f48cf039..acf7c1177 100644 --- a/app/Models/WebSocketDialogUser.php +++ b/app/Models/WebSocketDialogUser.php @@ -11,7 +11,7 @@ namespace App\Models; * @property string|null $top_at 置顶时间 * @property int|null $mark_unread 是否标记为未读:0否,1是 * @property int|null $inviter 邀请人 - * @property int|null $important 是否不可移出(项目、任务、部门人员) + * @property int|null $important 是否不可移出(项目、任务人员) * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogUser newModelQuery() diff --git a/database/migrations/2022_12_11_214403_create_user_checkin_macs_table.php b/database/migrations/2022_12_11_214403_create_user_checkin_macs_table.php index d27c57cf2..815118e7d 100644 --- a/database/migrations/2022_12_11_214403_create_user_checkin_macs_table.php +++ b/database/migrations/2022_12_11_214403_create_user_checkin_macs_table.php @@ -13,6 +13,9 @@ class CreateUserCheckinMacsTable extends Migration */ public function up() { + if (Schema::hasTable('user_checkin_macs')) + return; + Schema::create('user_checkin_macs', function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('userid')->nullable()->default(0)->comment('会员id'); diff --git a/database/migrations/2022_12_11_214409_create_user_checkin_records_table.php b/database/migrations/2022_12_11_214409_create_user_checkin_records_table.php index a1fcc2db4..20a582e51 100644 --- a/database/migrations/2022_12_11_214409_create_user_checkin_records_table.php +++ b/database/migrations/2022_12_11_214409_create_user_checkin_records_table.php @@ -13,6 +13,9 @@ class CreateUserCheckinRecordsTable extends Migration */ public function up() { + if (Schema::hasTable('user_checkin_records')) + return; + Schema::create('user_checkin_records', function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('userid')->nullable()->default(0)->comment('会员id'); diff --git a/database/migrations/2022_12_13_092619_add_date_times_user_checkin_records.php b/database/migrations/2022_12_13_092619_add_date_times_user_checkin_records.php new file mode 100644 index 000000000..d574677ba --- /dev/null +++ b/database/migrations/2022_12_13_092619_add_date_times_user_checkin_records.php @@ -0,0 +1,74 @@ +string('date', 20)->nullable()->default('')->after('mac')->comment('签到日期'); + $table->text('times')->nullable()->after('date')->comment('签到时间'); + $table->renameColumn('time', 'report_time'); + } + }); + if ($isAdd) { + $userids = \App\Models\UserCheckinRecord::select('userid')->distinct()->get()->pluck('userid'); + foreach ($userids as $userid) { + $list = \App\Models\UserCheckinRecord::whereUserid($userid)->orderBy('created_at')->get(); + $ids = []; + $date = ""; + $array = []; + foreach ($list as $item) { + $ids[] = $item->id; + $created_at = \Carbon\Carbon::parse($item->created_at); + if ($created_at->toDateString() != $date) { + $date = $created_at->toDateString(); + if ($array) { + $record = \App\Models\UserCheckinRecord::createInstance($array); + $record->save(); + } + $array = [ + 'userid' => $item->userid, + 'mac' => $item->mac, + 'date' => $date, + 'times' => [], + 'report_time' => $item->report_time, + 'created_at' => $item->created_at, + ]; + } + if ($array) { + $array['times'][] = $created_at->toTimeString(); + } + } + if ($array) { + \App\Models\UserCheckinRecord::whereIn('id', $ids)->delete(); + } + if ($array) { + $record = \App\Models\UserCheckinRecord::createInstance($array); + $record->save(); + } + } + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // ... 退回去意义不大 + } +} diff --git a/resources/assets/js/pages/manage/components/CheckinCalendar.vue b/resources/assets/js/pages/manage/components/CheckinCalendar.vue index 9efa07842..02e84f80e 100644 --- a/resources/assets/js/pages/manage/components/CheckinCalendar.vue +++ b/resources/assets/js/pages/manage/components/CheckinCalendar.vue @@ -29,7 +29,7 @@
- {{$L('已签到(*)次', getGold(data.date))}} + {{$L('已签到')}}