ReadRecordService.php 19 KB

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