ReadRecordService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/11/21
  6. * Time: 10:42
  7. */
  8. namespace App\Modules\User\Services;
  9. use App\Modules\User\Models\ReadRecordFromRedis;
  10. use Redis;
  11. use Hashids;
  12. use App\Modules\Book\Services\BookConfigService;
  13. use App\Modules\Book\Models\Chapter;
  14. use DB;
  15. class ReadRecordService
  16. {
  17. public static function getBookReadRecordStatic(int $uid, int $bid)
  18. {
  19. $record = Redis::hGet('book_read:' . $uid, $bid);
  20. if ($record) {
  21. $cid = explode('_', $record)[0];
  22. $name = self::cid2ChapterName($cid);
  23. return ['record_chapter_id' => $cid, 'record_chapter_name' => $name];
  24. }
  25. return false;
  26. }
  27. /**
  28. * 删除最近阅读记录
  29. * @param int $uid
  30. * @param array $bids
  31. */
  32. public static function delReadRecordStatic(int $uid, array $bids)
  33. {
  34. $key = 'book_read:' . $uid;
  35. $last_record = explode('_', Redis::hGet($key, 'last_read'));
  36. $last_record_bid = $last_record[0];
  37. $is_del_last = false;
  38. foreach ($bids as $bid) {
  39. if ($bid == $last_record_bid) {
  40. $is_del_last = true;
  41. }
  42. Redis::hDel($key, $bid);
  43. }
  44. if ($is_del_last) {
  45. self::reSetLastRecord($uid);
  46. }
  47. }
  48. /**
  49. * 重置用户最近阅读记录
  50. * @param int $uid
  51. */
  52. public static function reSetLastRecord($uid)
  53. {
  54. $key = 'book_read:' . $uid;
  55. $alls = Redis::hGetAll($key);
  56. $has_record = false;
  57. $last_timestamp = 0;
  58. $last_record = '';
  59. foreach ($alls as $k => $v) {
  60. if (is_numeric($k)) {
  61. $has_record = true;
  62. $record = explode('_', $v);
  63. $timestamp = $record[1];
  64. if ($last_timestamp < $timestamp) {
  65. $last_record = $k . '_' . $v;
  66. $last_timestamp = $timestamp;
  67. }
  68. }
  69. }
  70. if ($last_record) {
  71. Redis::hSet($key, 'last_read', $last_record);
  72. }
  73. if (!$has_record) {
  74. Redis::hDel($key, 'last_read');
  75. }
  76. }
  77. //阅读记录数
  78. const RECORD_COUNT = 50;
  79. private static $not_uid_key = ['last_read', 'send_order_id', 'sign_count', 'sign_counts', 'sign_info', 'sign_day', 'smart_push', 'inner_send_order_id', 'gxhp', 'property', 'bind_phone_status', 'ua', 'sign_version', 'new_outer', 'new_inner', 'new_total', 'next_push_hour', 'person_account_id'];
  80. /**
  81. * 获取
  82. * @param $uid
  83. * @return array
  84. */
  85. public static function getReadRecord_($uid)
  86. {
  87. $read_bids = Redis::hgetall('book_read:' . $uid);
  88. $res = [];
  89. $i = 0;
  90. foreach ($read_bids as $key => $v) {
  91. $record = explode('_', $v);
  92. $latest_read_cid = $record[0];
  93. $book_name = $record[1];
  94. $chapter_name = $record[2];
  95. $latest_read_time = $record[count($record) - 1];
  96. $res[$i] = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int) $latest_read_cid, 'time' => (int) $latest_read_time, 'chapter_name' => $chapter_name];
  97. $i++;
  98. }
  99. usort($res, function ($a, $b) {
  100. if ($a['time'] >= $b['time']) return -1;
  101. return 1;
  102. });
  103. return $res;
  104. }
  105. /**
  106. * 获取 升级版
  107. * @param $uid
  108. * @return array
  109. */
  110. public static function getReadRecord($uid, $is_need_check_db = false)
  111. {
  112. if ($is_need_check_db) {
  113. self::resetRecordFromDB($uid);
  114. }
  115. self::delTheLastRecord($uid);
  116. $read_bids = Redis::hgetall('book_read:' . $uid);
  117. $res = [];
  118. $i = 0;
  119. foreach ($read_bids as $key => $v) {
  120. if (in_array($key, self::$not_uid_key)) {
  121. continue;
  122. }
  123. $record = explode('_', $v);
  124. $latest_read_cid = $record[0];
  125. $latest_read_time = $record[count($record) - 1];
  126. $book_name = self::bid2BookName($key);
  127. $chapter_name = self::cid2ChapterName($latest_read_cid);
  128. $res[$i] = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int) $latest_read_cid, 'time' => (int) $latest_read_time, 'chapter_name' => $chapter_name];
  129. $i++;
  130. }
  131. usort($res, function ($a, $b) {
  132. if ($a['time'] >= $b['time']) return -1;
  133. return 1;
  134. });
  135. return collect($res)->sortBy('time', 'desc')->values()->all();
  136. }
  137. /**
  138. * 新增
  139. * @param $uid
  140. * @param $bid
  141. * @param $cid
  142. * @param $book_name
  143. * @param $chapter_name
  144. */
  145. public static function addReadRecord_($param)
  146. {
  147. $uid = $param['uid'];
  148. $bid = $param['bid'];
  149. $cid = $param['cid'];
  150. $book_name = $param['book_name'];
  151. $chapter_name = $param['chapter_name'];
  152. Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time());
  153. //Redis::hset('book_read:'.$uid, $bid, $cid."_".time());
  154. Redis::hset('book_read:' . $uid, $bid, "{$cid}_{$book_name}_{$chapter_name}_" . time());
  155. }
  156. /**
  157. * 添加阅读记录升级版
  158. * @param array $param
  159. */
  160. public static function addReadRecord($param)
  161. {
  162. $uid = $param['uid'];
  163. $bid = $param['bid'];
  164. $cid = $param['cid'];
  165. $book_name = isset($param['book_name']) ? $param['book_name'] : '';
  166. $chapter_name = isset($param['chapter_name']) ? $param['chapter_name'] : '';
  167. $book_key = 'wap:string:book:' . $bid;
  168. $chapter_key = 'wap:string:chapter:' . $cid;
  169. if ($book_name) {
  170. Redis::setex($book_key, 3600, $book_name);
  171. }
  172. if ($chapter_name) {
  173. Redis::setex($chapter_key, 3600, $chapter_name);
  174. }
  175. Redis::hmset('book_read:' . $uid, 'last_read', "{$bid}_{$cid}_" . time(), $bid, "{$cid}_" . time(), 'next_push_hour', 8);
  176. $num = random_int(1, 100);
  177. if ($num <= 3) {
  178. self::delTheLastRecord($uid);
  179. }
  180. }
  181. /**
  182. * 获取最近一条阅读记录
  183. * @param $uid
  184. */
  185. public static function getFirstReadRecord_($uid)
  186. {
  187. $all = self::getReadRecord($uid);
  188. if (empty($all)) return [];
  189. $first = $all[0];
  190. if (!$first) return [];
  191. if (!isset($first['bid'])) return [];
  192. try {
  193. //$bid = Hashids::encode($first['bid']);
  194. $bid = $first['bid'];
  195. $book_info = BookConfigService::getBookById($bid);
  196. $cid = $first['cid'];
  197. $book_name = $first['book_name'];
  198. $res = [
  199. 'url' => '/reader?bid=' . $bid . '&cid=' . $cid,
  200. 'book_name' => $book_name,
  201. 'cover' => $book_info->cover,
  202. 'channel_name' => $book_info->channel_name,
  203. ];
  204. } catch (\Exception $e) {
  205. $res = [];
  206. }
  207. return $res;
  208. }
  209. /**
  210. * 获取最近一条阅读记录(升级版)
  211. * @param $uid
  212. * @return array
  213. */
  214. public static function getFirstReadRecord($uid)
  215. {
  216. self::delBookBase($uid);
  217. $record = Redis::hget('book_read:' . $uid, 'last_read');
  218. if ($record) {
  219. $record_arr = explode('_', $record);
  220. $bid = $record_arr[0];
  221. $bid = Hashids::encode($bid);
  222. $cid = $record_arr[1];
  223. $time = $record_arr[2];
  224. $book_info = BookConfigService::getBookById($bid);
  225. $book_name = isset($book_info->book_name) ? $book_info->book_name : '';
  226. $cover = isset($book_info->cover) ? $book_info->cover : '';
  227. $channel_name = isset($book_info->channel_name) ? $book_info->channel_name : '';
  228. $res = [
  229. 'url' => '/reader?bid=' . $bid . '&cid=' . $cid,
  230. 'book_name' => $book_name,
  231. 'cover' => $cover,
  232. 'channel_name' => $channel_name,
  233. 'time' => $time
  234. ];
  235. return $res;
  236. }
  237. return [];
  238. }
  239. /**
  240. * 获取简单阅读记录
  241. * @param $uid
  242. * @return int
  243. */
  244. public static function getSimpleFirstReadRecord($uid)
  245. {
  246. try {
  247. $record = Redis::hget('book_read:' . $uid, 'last_read');
  248. if ($record) {
  249. $record_arr = explode('_', $record);
  250. $bid = $record_arr[0];
  251. return (int) $bid;
  252. }
  253. } catch (\Exception $e) {
  254. }
  255. return 0;
  256. }
  257. /**
  258. * 获取客服消息点击数
  259. * @param $uid
  260. */
  261. public static function getCustomerMsgClickNum($channel_id, $from, $date)
  262. {
  263. $key = "fromcustomermsgenter:distribution_channel_id:" . $channel_id . 'from:' . $from;
  264. return Redis::hget($key, $date);
  265. }
  266. /**
  267. * 获取某本书的阅读记录
  268. */
  269. public static function getRecordByUidBid($uid, $bid)
  270. {
  271. return Redis::hget('book_read:' . $uid, $bid);
  272. }
  273. /**
  274. * 根据bid获取书名
  275. * @param $bid
  276. * @return bool|null|string
  277. */
  278. public static function bid2BookName($bid)
  279. {
  280. $book_key = 'wap:string:book:' . $bid;
  281. $book_name = Redis::get($book_key);
  282. Redis::EXPIRE($book_key, 3600);
  283. if (!$book_name) {
  284. $book_name = '';
  285. $book_info = BookConfigService::getBookById($bid);
  286. if ($book_info && isset($book_info->book_name)) {
  287. $book_name = $book_info->book_name;
  288. }
  289. }
  290. }
  291. /**
  292. * 根据cid获取章节名
  293. * @param $cid
  294. * @return bool|null|string
  295. */
  296. public static function cid2ChapterName($cid)
  297. {
  298. $chapter_key = 'wap:string:chapter:' . $cid;
  299. $chapter_name = Redis::get($chapter_key);
  300. Redis::EXPIRE($chapter_key, 3600);
  301. if (!$chapter_name) {
  302. $chapter_name = '';
  303. $chapter_info = Chapter::getChapterNameById($cid);
  304. if ($chapter_info && isset($chapter_info->name)) {
  305. $chapter_name = $chapter_info->name;
  306. }
  307. }
  308. return $chapter_name;
  309. }
  310. /**
  311. * 删除阅读记录中的书名和章节名
  312. * @param $uid
  313. * @param $record
  314. */
  315. public static function delBookNameAndChapter($uid)
  316. {
  317. $base_record = Redis::hget('book_base:' . $uid, 'last_read');
  318. if ($base_record) {
  319. $record_arr = explode('_', $base_record);
  320. $c = count($record_arr);
  321. if ($c > 3) {
  322. $bid = $record_arr[0];
  323. $cid = $record_arr[1];
  324. $time = $record_arr[$c - 1];
  325. Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_" . $time);
  326. }
  327. }
  328. $records = Redis::hgetall('book_read:' . $uid);
  329. foreach ($records as $key => $v) {
  330. $record = explode('_', $v);
  331. $count = count($record);
  332. if ($count > 3) {
  333. $latest_read_cid = $record[0];
  334. $book_name = $record[1];
  335. $chapter_name = $record[2];
  336. $latest_read_time = $record[$count - 1];
  337. Redis::hset('book_read:' . $uid, $key, "{$latest_read_cid}_" . $latest_read_time);
  338. $book_key = 'wap:string:book:' . $key;
  339. $chapter_key = 'wap:string:chapter:' . $latest_read_cid;
  340. Redis::set($book_key, $book_name);
  341. Redis::set($chapter_key, $chapter_name);
  342. }
  343. }
  344. }
  345. public static function delBookBase($uid)
  346. {
  347. $base_record = Redis::hget('book_base:' . $uid, 'last_read');
  348. if ($base_record) {
  349. Redis::del('book_base:' . $uid);
  350. Redis::hset('book_read:' . $uid, 'last_read', $base_record);
  351. }
  352. }
  353. /**
  354. * 获取简单阅读记录只有bid
  355. * @param int $uid
  356. * @return array
  357. */
  358. public static function getSimpleReadRecord(int $uid): array
  359. {
  360. $read_bids = Redis::hgetall('book_read:' . $uid);
  361. $res = [];
  362. if (!$read_bids) {
  363. return $res;
  364. }
  365. foreach ($read_bids as $key => $v) {
  366. if (in_array($key, self::$not_uid_key)) {
  367. continue;
  368. }
  369. array_push($res, $key);
  370. }
  371. return $res;
  372. }
  373. public static function ReadRecordStatistical(int $uid, int $distribution_channel_id, string $from)
  374. {
  375. try {
  376. DB::table('temp_read_active')->insert([
  377. 'uid' => $uid,
  378. 'distribution_channel_id' => $distribution_channel_id,
  379. 'from' => $from,
  380. 'created_at' => date('Y-m-d H:i:s'),
  381. 'updated_at' => date('Y-m-d H:i:s'),
  382. ]);
  383. } catch (\Exception $e) {
  384. }
  385. }
  386. /**
  387. * 获取当前的send_order_id
  388. * @param int $uid
  389. * @return int
  390. */
  391. public static function getSendOrderId(int $uid)
  392. {
  393. try {
  394. $send_order_id = Redis::hget('book_read:' . $uid, 'send_order_id');
  395. if ($send_order_id)
  396. return (int) $send_order_id;
  397. } catch (\Exception $e) {
  398. }
  399. return 0;
  400. }
  401. /**
  402. * 设置内部派单
  403. * @param $uid
  404. * @param $inner_order_id
  405. */
  406. public static function setInnerSendOrderId($uid, $inner_order_id)
  407. {
  408. try {
  409. Redis::hset('book_read:' . $uid, 'inner_send_order_id', $inner_order_id);
  410. } catch (\Exception $e) {
  411. }
  412. }
  413. /**
  414. * 获取内部派单
  415. * @param $uid
  416. * @return string
  417. */
  418. public static function getInnerSendOrderId($uid)
  419. {
  420. try {
  421. $inner_send_order_id = Redis::hget('book_read:' . $uid, 'inner_send_order_id');
  422. if ($inner_send_order_id) {
  423. return $inner_send_order_id;
  424. }
  425. return '';
  426. } catch (\Exception $e) {
  427. }
  428. return '';
  429. }
  430. /**
  431. * 签到日期
  432. * @param int $uid
  433. * @return mixed
  434. */
  435. public static function getSignDay(int $uid)
  436. {
  437. try {
  438. return Redis::hget('book_read:' . $uid, 'sign_day');
  439. } catch (\Exception $e) {
  440. }
  441. return -1;
  442. }
  443. public static function setSignDay(int $uid)
  444. {
  445. return Redis::hset('book_read:' . $uid, 'sign_day', date('Y-m-d'));
  446. }
  447. /**
  448. * 签到次数和日期
  449. * @param int $uid
  450. */
  451. public static function sign(int $uid, bool $is_incr)
  452. {
  453. try {
  454. if ($is_incr) {
  455. Redis::hincrby('book_read:' . $uid, 'sign_counts', 1);
  456. } else {
  457. self::setSignCount($uid, 1);
  458. }
  459. self::setSignDay($uid);
  460. } catch (\Exception $e) {
  461. \Log::info('sign_ept:' . $e->getMessage());
  462. }
  463. return;
  464. }
  465. /**
  466. * @param int $uid
  467. * @return int
  468. */
  469. public static function getSignCount(int $uid)
  470. {
  471. try {
  472. $count = Redis::hget('book_read:' . $uid, 'sign_counts');
  473. if ($count) {
  474. return $count;
  475. }
  476. } catch (\Exception $e) {
  477. }
  478. return 0;
  479. }
  480. /**
  481. * 获取简单签到次数
  482. * @param int $uid
  483. * @return int
  484. */
  485. public static function getSignCountSimple(int $uid)
  486. {
  487. try {
  488. $count = Redis::hget('book_read:' . $uid, 'sign_counts');
  489. if ($count) {
  490. return (int) $count;
  491. }
  492. return 0;
  493. } catch (\Exception $e) {
  494. }
  495. return 0;
  496. }
  497. public static function setSignCount(int $uid, int $count)
  498. {
  499. Redis::hset('book_read:' . $uid, 'sign_counts', $count);
  500. }
  501. public static function setSignInfo(int $uid, string $info)
  502. {
  503. Redis::hset('book_read:' . $uid, 'sign_info', $info);
  504. }
  505. public static function setSmartPush($uid, $bid)
  506. {
  507. $old = self::getSmartPush($uid);
  508. if ($old && !in_array($bid, $old)) {
  509. array_push($old, $bid);
  510. $bid_str = implode(',', $old);
  511. return Redis::hset('book_read:' . $uid, 'smart_push', $bid_str);
  512. } else {
  513. return Redis::hset('book_read:' . $uid, 'smart_push', $bid);
  514. }
  515. }
  516. public static function getSmartPush(int $uid): array
  517. {
  518. $res = Redis::hget('book_read:' . $uid, 'smart_push');
  519. if ($res) {
  520. return explode(',', $res);
  521. }
  522. return [];
  523. }
  524. public static function getByField(int $uid, $field)
  525. {
  526. try {
  527. return Redis::hget('book_read:' . $uid, $field);
  528. } catch (\Exception $e) {
  529. }
  530. return '';
  531. }
  532. public static function getByMultiField(int $uid, ...$field)
  533. {
  534. try {
  535. return Redis::hmget('book_read:' . $uid, $field);
  536. } catch (\Exception $e) {
  537. }
  538. return '';
  539. }
  540. public static function setByField(int $uid, $field, $value)
  541. {
  542. if (!in_array($field, self::$not_uid_key)) {
  543. return false;
  544. }
  545. try {
  546. return Redis::hset('book_read:' . $uid, $field, $value);
  547. } catch (\Exception $e) {
  548. }
  549. return '';
  550. }
  551. public static function setByMultiField(int $uid, $kv)
  552. {
  553. $keys = array_keys($kv);
  554. foreach ($keys as $field) {
  555. if (!in_array($field, self::$not_uid_key)) {
  556. return false;
  557. }
  558. }
  559. try {
  560. return Redis::hmset('book_read:' . $uid, $kv);
  561. } catch (\Exception $e) {
  562. }
  563. return '';
  564. }
  565. private static function resetRecordFromDB($uid)
  566. {
  567. if (self::getByField($uid, 'last_read')) {
  568. return;
  569. }
  570. $record = ReadRecordFromRedis::where('uid', $uid)->select('field', 'value')->get();
  571. if ($record->isNotEmpty()) {
  572. foreach ($record as $item) {
  573. if (!in_array($item->field, self::$not_uid_key) || $item->field == 'last_read') {
  574. Redis::hset('book_read:' . $uid, $item->field, $item->value);
  575. }
  576. }
  577. }
  578. }
  579. //删除多余的阅读纪律
  580. public static function delTheLastRecord($uid)
  581. {
  582. $length = Redis::hlen('book_read:' . $uid);
  583. if ($length <= self::RECORD_COUNT + count(self::$not_uid_key)) {
  584. return;
  585. }
  586. $read_bids = Redis::hgetall('book_read:' . $uid);
  587. $i = 0;
  588. foreach ($read_bids as $key => $v) {
  589. if (in_array($key, self::$not_uid_key)) {
  590. continue;
  591. }
  592. $record = explode('_', $v);
  593. $latest_read_cid = $record[0];
  594. $latest_read_time = $record[count($record) - 1];
  595. $res[$i++] = ['bid' => $key, 'cid' => (int) $latest_read_cid, 'time' => (int) $latest_read_time];
  596. }
  597. usort($res, function ($a, $b) {
  598. if ($a['time'] >= $b['time']) return -1;
  599. return 1;
  600. });
  601. $j = 0;
  602. foreach ($res as $v) {
  603. if ($j++ >= self::RECORD_COUNT) {
  604. Redis::hdel('book_read:' . $uid, $v['bid']);
  605. }
  606. }
  607. }
  608. }