perf: 优化签到数据结构

This commit is contained in:
kuaifan 2022-12-13 09:24:30 +08:00
parent 154b145e33
commit 4efbc7db25
9 changed files with 194 additions and 54 deletions

View File

@ -88,15 +88,25 @@ class PublicController extends AbstractController
return 'key error'; return 'key error';
} }
// //
$nowDate = date("Y-m-d");
$nowTime = date("H:i:s");
$macs = explode(",", $mac); $macs = explode(",", $mac);
foreach ($macs as $item) { foreach ($macs as $mac) {
$item = strtoupper($item); $mac = strtoupper($mac);
if (Base::isMac($item) && $UserCheckinMac = UserCheckinMac::whereMac($item)->first()) { if (Base::isMac($mac) && $UserCheckinMac = UserCheckinMac::whereMac($mac)->first()) {
UserCheckinRecord::createInstance([ $array = [
'userid' => $UserCheckinMac->userid, 'userid' => $UserCheckinMac->userid,
'mac' => $UserCheckinMac->mac, 'mac' => $UserCheckinMac->mac,
'time' => $time, 'date' => $nowDate,
])->save(); ];
$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'; return 'success';

View File

@ -856,6 +856,7 @@ class SystemController extends AbstractController
$headings[] = '首次签到结果'; $headings[] = '首次签到结果';
$headings[] = '最后签到时间'; $headings[] = '最后签到时间';
$headings[] = '最后签到结果'; $headings[] = '最后签到结果';
$headings[] = '参数数据';
// //
$sheets = []; $sheets = [];
$startD = Carbon::parse($date[0])->startOfDay(); $startD = Carbon::parse($date[0])->startOfDay();
@ -863,22 +864,25 @@ class SystemController extends AbstractController
$users = User::whereIn('userid', $userid)->take(20)->get(); $users = User::whereIn('userid', $userid)->take(20)->get();
/** @var User $user */ /** @var User $user */
foreach ($users as $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); $nickname = Base::filterEmoji($user->nickname);
$styles = ["A1:G1" => ["font" => ["bold" => true]]]; $styles = ["A1:H1" => ["font" => ["bold" => true]]];
$datas = []; $datas = [];
$startT = $startD->timestamp; $startT = $startD->timestamp;
$endT = $endD->timestamp; $endT = $endD->timestamp;
$index = 1; $index = 1;
while ($startT < $endT) { while ($startT < $endT) {
$index++; $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)]; $firstBetween = [Carbon::createFromTimestamp($startT), Carbon::createFromTimestamp($startT + $secondEnd - 1)];
$lastBetween = [Carbon::createFromTimestamp($startT + $secondStart + 1), Carbon::createFromTimestamp($startT + 86400)]; $lastBetween = [Carbon::createFromTimestamp($startT + $secondStart + 1), Carbon::createFromTimestamp($startT + 86400)];
$firstRecord = $records->whereBetween("created_at", $firstBetween)->first(); $firstRecord = $sameCollect?->whereBetween("datetime", $firstBetween)->first();
$lastRecord = $records->whereBetween("created_at", $lastBetween)->last(); $lastRecord = $sameCollect?->whereBetween("datetime", $lastBetween)->last();
$firstTimestamp = $firstRecord ? Carbon::parse($firstRecord->created_at)->timestamp : 0; $firstTimestamp = $firstRecord['timestamp'] ?: 0;
$lastTimestamp = $lastRecord ? Carbon::parse($lastRecord->created_at)->timestamp : 0; $lastTimestamp = $lastRecord['timestamp'] ?: 0;
if (Base::time() < $startT + $secondStart) { if (Base::time() < $startT + $secondStart) {
$firstResult = "-"; $firstResult = "-";
} else { } else {
@ -906,14 +910,18 @@ class SystemController extends AbstractController
} }
$firstTimestamp = $firstTimestamp ? date("H:i", $firstTimestamp) : "-"; $firstTimestamp = $firstTimestamp ? date("H:i", $firstTimestamp) : "-";
$lastTimestamp = $lastTimestamp ? date("H:i", $lastTimestamp) : "-"; $lastTimestamp = $lastTimestamp ? date("H:i", $lastTimestamp) : "-";
$section = array_map(function($item) {
return $item[0] . "-" . ($item[1] ?: "None");
}, $sameRecord?->atSection() ?: []);
$datas[] = [ $datas[] = [
"{$nickname} (ID: {$user->userid})", "{$nickname} (ID: {$user->userid})",
date("Y-m-d", $startT), $sameDate,
implode("-", $time), implode("-", $time),
$firstTimestamp, $firstTimestamp,
$firstResult, $firstResult,
$lastTimestamp, $lastTimestamp,
$lastResult, $lastResult,
implode(", ", $section),
]; ];
$startT += 86400; $startT += 86400;
} }

View File

@ -1412,25 +1412,18 @@ class UsersController extends AbstractController
$start = Carbon::parse(date("Y-m-01 00:00:00", strtotime($ym))); $start = Carbon::parse(date("Y-m-01 00:00:00", strtotime($ym)));
$end = (clone $start)->addMonth()->subSecond(); $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 = []; $array = [];
$startT = $start->timestamp; $startT = $start->timestamp;
$endT = $end->timestamp; $endT = $end->timestamp;
while ($startT < $endT) { while ($startT < $endT) {
$between = [Carbon::createFromTimestamp($startT), Carbon::createFromTimestamp($startT + 86400)]; $sameDate = date("Y-m-d", $startT);
$firstRecord = $records->whereBetween("created_at", $between)->first(); $sameRecord = isset($records[$sameDate]) ? $records[$sameDate] : null;
$lastRecord = $records->whereBetween("created_at", $between)->last(); if ($sameRecord) {
$firstTimestamp = $firstRecord ? Carbon::parse($firstRecord->created_at)->toDateTimeString() : ''; $array[] = [
$lastTimestamp = $lastRecord ? Carbon::parse($lastRecord->created_at)->toDateTimeString() : ''; 'date' => $sameDate,
if ($firstTimestamp) { 'section' => $sameRecord->atSection(),
$data = [
'time' => $firstTimestamp,
'all' => [$firstTimestamp],
]; ];
if ($lastTimestamp) {
$data['all'][] = $lastTimestamp;
}
$array[] = $data;
} }
$startT += 86400; $startT += 86400;
} }

View File

@ -11,16 +11,20 @@ use App\Module\Base;
* @property int $id * @property int $id
* @property int|null $userid 会员id * @property int|null $userid 会员id
* @property string|null $mac MAC地址 * @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 $created_at
* @property \Illuminate\Support\Carbon|null $updated_at * @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord newQuery() * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord query() * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord query()
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereCreatedAt($value) * @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 whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereMac($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 whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereUserid($value) * @method static \Illuminate\Database\Eloquent\Builder|UserCheckinRecord whereUserid($value)
* @mixin \Eloquent * @mixin \Eloquent
@ -28,4 +32,65 @@ use App\Module\Base;
class UserCheckinRecord extends AbstractModel 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;
}
} }

View File

@ -11,7 +11,7 @@ namespace App\Models;
* @property string|null $top_at 置顶时间 * @property string|null $top_at 置顶时间
* @property int|null $mark_unread 是否标记为未读0否1是 * @property int|null $mark_unread 是否标记为未读0否1是
* @property int|null $inviter 邀请人 * @property int|null $inviter 邀请人
* @property int|null $important 是否不可移出(项目、任务、部门人员) * @property int|null $important 是否不可移出(项目、任务人员)
* @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at * @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogUser newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogUser newModelQuery()

View File

@ -13,6 +13,9 @@ class CreateUserCheckinMacsTable extends Migration
*/ */
public function up() public function up()
{ {
if (Schema::hasTable('user_checkin_macs'))
return;
Schema::create('user_checkin_macs', function (Blueprint $table) { Schema::create('user_checkin_macs', function (Blueprint $table) {
$table->bigIncrements('id'); $table->bigIncrements('id');
$table->bigInteger('userid')->nullable()->default(0)->comment('会员id'); $table->bigInteger('userid')->nullable()->default(0)->comment('会员id');

View File

@ -13,6 +13,9 @@ class CreateUserCheckinRecordsTable extends Migration
*/ */
public function up() public function up()
{ {
if (Schema::hasTable('user_checkin_records'))
return;
Schema::create('user_checkin_records', function (Blueprint $table) { Schema::create('user_checkin_records', function (Blueprint $table) {
$table->bigIncrements('id'); $table->bigIncrements('id');
$table->bigInteger('userid')->nullable()->default(0)->comment('会员id'); $table->bigInteger('userid')->nullable()->default(0)->comment('会员id');

View File

@ -0,0 +1,74 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddDateTimesUserCheckinRecords extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$isAdd = false;
Schema::table('user_checkin_records', function (Blueprint $table) use (&$isAdd) {
if (!Schema::hasColumn('user_checkin_records', 'date')) {
$isAdd = true;
$table->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()
{
// ... 退回去意义不大
}
}

View File

@ -29,7 +29,7 @@
<div slot="content" v-html="getTimes(data.date)"></div> <div slot="content" v-html="getTimes(data.date)"></div>
<template v-if="doCheck(data.date)">{{$L('今天')}}</template> <template v-if="doCheck(data.date)">{{$L('今天')}}</template>
<template v-else>{{data.date | getCD}}</template> <template v-else>{{data.date | getCD}}</template>
<span :class="{'ui-state-down': true }">{{$L('已签到(*)次', getGold(data.date))}}</span> <span :class="{'ui-state-down': true }">{{$L('已签到')}}</span>
</Tooltip> </Tooltip>
</td> </td>
<template v-if="(!isCheck(data.date) && (doCheck(data.date) && !hasCheckin))"> <template v-if="(!isCheck(data.date) && (doCheck(data.date) && !hasCheckin))">
@ -122,26 +122,13 @@ export default {
monthClass(type) { monthClass(type) {
return type != 'cur'; return type != 'cur';
}, },
getGold(thisDay) {
for (let i in this.checkin) {
if (this.checkin.hasOwnProperty(i)) {
var d = new Date(this.checkin[i].time.replace(/-/g, '/'));
var _ymd = d.getFullYear() + '/' + (d.getMonth() + 1) + '/' + d.getDate();
if (new Date(thisDay).getTime() == new Date(_ymd).getTime()) {
return this.checkin[i].all.length;
}
}
}
},
getTimes(thisDay) { getTimes(thisDay) {
for (let i in this.checkin) { for (let i in this.checkin) {
if (this.checkin.hasOwnProperty(i)) { if (this.checkin.hasOwnProperty(i)) {
var d = new Date(this.checkin[i].time.replace(/-/g, '/')); if (new Date(thisDay).getTime() == $A.Date(this.checkin[i].date).getTime()) {
var _ymd = d.getFullYear() + '/' + (d.getMonth() + 1) + '/' + d.getDate(); return this.checkin[i].section.map(item => {
return `${item[0]} - ${item[1] || 'None'}`
if (new Date(thisDay).getTime() == new Date(_ymd).getTime()) { }).join('<br/>');
return this.checkin[i].all.join('<br/>');
} }
} }
} }
@ -244,17 +231,14 @@ export default {
return !((arr[0] == '') && (arr[1] == '') && (arr[2] == '') && (arr[3] == '') && (arr[4] == '') && (arr[5] == '') && (arr[6] == '')); return !((arr[0] == '') && (arr[1] == '') && (arr[2] == '') && (arr[3] == '') && (arr[4] == '') && (arr[5] == '') && (arr[6] == ''));
}, },
isCheck(index) { isCheck(index) {
const todayDate = new Date();
for (let i in this.checkin) { for (let i in this.checkin) {
let todayDate = new Date(); if ($A.Date(todayDate.getFullYear() + '/' + todayDate.getMonth() + '/' + todayDate.getDate()).getTime() == $A.Date(this.checkin[i].date).getTime()) {
let today = todayDate.getFullYear() + '/' + (todayDate.getMonth() + 1) + '/' + todayDate.getDate();
let d = new Date(this.checkin[i].time.replace(/-/g, '/'));
let _ymd = d.getFullYear() + '/' + (d.getMonth() + 1) + '/' + d.getDate();
if (new Date(today).getTime() == new Date(_ymd).getTime()) {
// //
this.hasCheckin = true; this.hasCheckin = true;
} }
if (new Date(index).getTime() == new Date(_ymd).getTime()) { if (new Date(index).getTime() == $A.Date(this.checkin[i].date).getTime()) {
//console.log('') //console.log('')
return true; return true;
} }