| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- /**
- * 模块访问记录-模型
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Models
- */
- namespace App\Models;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- class ExamAccessLogModel extends BaseModel
- {
- protected $table = 'exam_access_logs';
- /**
- * 模块访问日志
- * @param $date 日期
- * @param int $type 模块
- * @param int $scene 场景
- */
- public static function saveLog($date, $type=1, $scene=1, $userId=0)
- {
- if($type <= 0){
- return false;
- }
- $cacheKey = "caches:access:{$date}:{$type}_{$scene}";
- if(RedisService::get($cacheKey."_lock:{$userId}")){
- return false;
- }
- RedisService::set($cacheKey."_lock:{$userId}", date('Y-m-d H:i:s'), rand(3, 5));
- $checkId = RedisService::get($cacheKey);
- if(!$checkId){
- $checkId = self::where(['date'=> $date,'type'=>$type,'mark'=>1])
- ->value('id');
- }
- if($checkId){
- self::where(['id'=> $checkId])->update(["scene_count{$scene}"=>DB::raw("scene_count{$scene} + 1"),'update_time'=>time()]);
- }else{
- $id = self::insertGetId([
- 'date'=> $date,
- 'type'=> $type,
- "scene_count{$scene}"=> 1,
- 'create_time'=>time(),
- 'update_time'=>time(),
- 'status'=>1,
- ]);
- if($id){
- RedisService::set($cacheKey, $id, rand(300, 600));
- }
- }
- }
- }
|