ExamAccessLogModel.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * 模块访问记录-模型
  4. * @author laravel开发员
  5. * @since 2020/11/11
  6. * @package App\Models
  7. */
  8. namespace App\Models;
  9. use App\Services\RedisService;
  10. use Illuminate\Support\Facades\DB;
  11. class ExamAccessLogModel extends BaseModel
  12. {
  13. protected $table = 'exam_access_logs';
  14. /**
  15. * 模块访问日志
  16. * @param $date 日期
  17. * @param int $type 模块
  18. * @param int $scene 场景
  19. */
  20. public static function saveLog($date, $type=1, $scene=1, $userId=0)
  21. {
  22. if($type <= 0){
  23. return false;
  24. }
  25. $cacheKey = "caches:access:{$date}:{$type}_{$scene}";
  26. if(RedisService::get($cacheKey."_lock:{$userId}")){
  27. return false;
  28. }
  29. RedisService::set($cacheKey."_lock:{$userId}", date('Y-m-d H:i:s'), rand(3, 5));
  30. $checkId = RedisService::get($cacheKey);
  31. if(!$checkId){
  32. $checkId = self::where(['date'=> $date,'type'=>$type,'mark'=>1])
  33. ->value('id');
  34. }
  35. if($checkId){
  36. self::where(['id'=> $checkId])->update(["scene_count{$scene}"=>DB::raw("scene_count{$scene} + 1"),'update_time'=>time()]);
  37. }else{
  38. $id = self::insertGetId([
  39. 'date'=> $date,
  40. 'type'=> $type,
  41. "scene_count{$scene}"=> 1,
  42. 'create_time'=>time(),
  43. 'update_time'=>time(),
  44. 'status'=>1,
  45. ]);
  46. if($id){
  47. RedisService::set($cacheKey, $id, rand(300, 600));
  48. }
  49. }
  50. }
  51. }