TestBookService.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Modules\Book\Services;
  3. use App\Modules\Book\Models\BookConfig;
  4. use App\Modules\Book\Models\NewBookTest;
  5. use Redis;
  6. class TestBookService
  7. {
  8. const gu_yan = [
  9. '穿越重生',
  10. '经商种田',
  11. '古代虐情',
  12. ];
  13. /**
  14. * @param $sex 1:男 2:女
  15. * @param array $filter_bids
  16. */
  17. static function getTestBook($sex,$user_type='ALL')
  18. {
  19. $current_test_book = NewBookTest::where('status', 1)->select('id', 'bid', 'plan_push_user_num')->where('sex', $sex)->where('user_type',$user_type)->orderby('test_weight', 'desc')->orderby('id', 'asc')->first();
  20. //当前如果有测试中图书 直接返回
  21. if ($current_test_book) return $current_test_book;
  22. $to_test_book = NewBookTest::where('status', 0)->select('id', 'bid', 'plan_push_user_num')->where('sex', $sex)->where('user_type',$user_type)->orderby('test_weight', 'desc')->orderby('id', 'asc')->first();
  23. if ($to_test_book) {
  24. //当前如果无测试中图书 但有等待测试图书 修改为测试中
  25. $current_time = date('Y-m-d H:i:s');
  26. if (NewBookTest::where('id', $to_test_book->id)->update(['test_begin_time' => $current_time, 'status' => 1])) {
  27. return $to_test_book;
  28. }
  29. }
  30. return null;
  31. }
  32. static function finishTest($bid)
  33. {
  34. $book_config_res = BookConfig::where('bid', $bid)->where('test_status', 1)->update(['test_status' => 2, 'test_update_time' => date('Y-m-d H:i:s')]);
  35. $test_res = NewBookTest::where('bid', $bid)->where('status', 1)->update(['status' => 2, 'test_end_time' => date('Y-m-d H:i:s')]);
  36. return true;
  37. }
  38. static function record($test_id, $uid)
  39. {
  40. $current_time = time();
  41. return Redis::zadd('NewBookTestUserDetail:' . $test_id, $current_time, $uid);
  42. }
  43. }