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 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)->sortByDesc('time')->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 $param
  159. * @return bool
  160. * @throws \Exception
  161. */
  162. public static function addReadRecord($param)
  163. {
  164. // 第一章不计入阅读记录
  165. $sequence = (int)getProp($param, 'sequence');
  166. // if ($sequence <= 1) {
  167. // return false;
  168. // }
  169. $uid = $param['uid'];
  170. $bid = $param['bid'];
  171. $cid = $param['cid'];
  172. $book_name = getProp($param, 'book_name');
  173. $chapter_name = getProp($param, 'chapter_name');
  174. $book_key = 'wap:string:book:' . $bid;
  175. $chapter_key = 'wap:string:chapter:' . $cid;
  176. if ($book_name) {
  177. Redis::setex($book_key, 3600, $book_name);
  178. }
  179. if ($chapter_name) {
  180. Redis::setex($chapter_key, 3600, $chapter_name);
  181. }
  182. Redis::hmset('book_read:' . $uid, 'last_read', "{$bid}_{$cid}_" . time(), $bid, "{$cid}_" . time(), 'next_push_hour', 8);
  183. $num = random_int(1, 100);
  184. if ($num <= 3) {
  185. self::delTheLastRecord($uid);
  186. }
  187. return true;
  188. }
  189. /**
  190. * 获取最近一条阅读记录
  191. * @param $uid
  192. */
  193. public static function getFirstReadRecord_($uid)
  194. {
  195. $all = self::getReadRecord($uid);
  196. if (empty($all)) return [];
  197. $first = $all[0];
  198. if (!$first) return [];
  199. if (!isset($first['bid'])) return [];
  200. try {
  201. //$bid = Hashids::encode($first['bid']);
  202. $bid = $first['bid'];
  203. $book_info = BookConfigService::getBookById($bid);
  204. $cid = $first['cid'];
  205. $book_name = $first['book_name'];
  206. $res = [
  207. 'url' => '/reader?bid=' . $bid . '&cid=' . $cid,
  208. 'book_name' => $book_name,
  209. 'cover' => $book_info->cover,
  210. 'channel_name' => $book_info->channel_name,
  211. ];
  212. } catch (\Exception $e) {
  213. $res = [];
  214. }
  215. return $res;
  216. }
  217. /**
  218. * 获取最近一条阅读记录(升级版)
  219. * @param $uid
  220. * @return array
  221. */
  222. public static function getFirstReadRecord($uid)
  223. {
  224. self::delBookBase($uid);
  225. $record = Redis::hget('book_read:' . $uid, 'last_read');
  226. if ($record) {
  227. $record_arr = explode('_', $record);
  228. $bid = $record_arr[0];
  229. $bid = Hashids::encode($bid);
  230. $cid = $record_arr[1];
  231. $time = $record_arr[2];
  232. $book_info = BookConfigService::getBookById($bid);
  233. $book_name = isset($book_info->book_name) ? $book_info->book_name : '';
  234. $cover = isset($book_info->cover) ? $book_info->cover : '';
  235. $channel_name = isset($book_info->channel_name) ? $book_info->channel_name : '';
  236. $res = [
  237. 'url' => '/reader?bid=' . $bid . '&cid=' . $cid,
  238. 'book_name' => $book_name,
  239. 'cover' => $cover,
  240. 'channel_name' => $channel_name,
  241. 'time' => $time
  242. ];
  243. return $res;
  244. }
  245. return [];
  246. }
  247. /**
  248. * 获取简单阅读记录
  249. * @param $uid
  250. * @return int
  251. */
  252. public static function getSimpleFirstReadRecord($uid)
  253. {
  254. try {
  255. $record = Redis::hget('book_read:' . $uid, 'last_read');
  256. if ($record) {
  257. $record_arr = explode('_', $record);
  258. $bid = $record_arr[0];
  259. return (int)$bid;
  260. }
  261. } catch (\Exception $e) {
  262. }
  263. return 0;
  264. }
  265. /**
  266. * 获取客服消息点击数
  267. * @param $uid
  268. */
  269. public static function getCustomerMsgClickNum($channel_id, $from, $date)
  270. {
  271. $key = "fromcustomermsgenter:distribution_channel_id:" . $channel_id . 'from:' . $from;
  272. return Redis::hget($key, $date);
  273. }
  274. /**
  275. * 获取某本书的阅读记录
  276. */
  277. public static function getRecordByUidBid($uid, $bid)
  278. {
  279. return Redis::hget('book_read:' . $uid, $bid);
  280. }
  281. /**
  282. * 根据bid获取书名
  283. * @param $bid
  284. * @return bool|null|string
  285. */
  286. public static function bid2BookName($bid)
  287. {
  288. $book_key = 'wap:string:book:' . $bid;
  289. $book_name = Redis::get($book_key);
  290. Redis::EXPIRE($book_key, 3600);
  291. if (!$book_name) {
  292. $book_name = '';
  293. $book_info = BookConfigService::getBookById($bid);
  294. if ($book_info && isset($book_info->book_name)) {
  295. $book_name = $book_info->book_name;
  296. }
  297. }
  298. }
  299. /**
  300. * 根据cid获取章节名
  301. * @param $cid
  302. * @return bool|null|string
  303. */
  304. public static function cid2ChapterName($cid)
  305. {
  306. $chapter_key = 'wap:string:chapter:' . $cid;
  307. $chapter_name = Redis::get($chapter_key);
  308. Redis::EXPIRE($chapter_key, 3600);
  309. if (!$chapter_name) {
  310. $chapter_name = '';
  311. $chapter_info = Chapter::getChapterNameById($cid);
  312. if ($chapter_info && isset($chapter_info->name)) {
  313. $chapter_name = $chapter_info->name;
  314. }
  315. }
  316. return $chapter_name;
  317. }
  318. /**
  319. * 删除阅读记录中的书名和章节名
  320. * @param $uid
  321. * @param $record
  322. */
  323. public static function delBookNameAndChapter($uid)
  324. {
  325. $base_record = Redis::hget('book_base:' . $uid, 'last_read');
  326. if ($base_record) {
  327. $record_arr = explode('_', $base_record);
  328. $c = count($record_arr);
  329. if ($c > 3) {
  330. $bid = $record_arr[0];
  331. $cid = $record_arr[1];
  332. $time = $record_arr[$c - 1];
  333. Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_" . $time);
  334. }
  335. }
  336. $records = Redis::hgetall('book_read:' . $uid);
  337. foreach ($records as $key => $v) {
  338. $record = explode('_', $v);
  339. $count = count($record);
  340. if ($count > 3) {
  341. $latest_read_cid = $record[0];
  342. $book_name = $record[1];
  343. $chapter_name = $record[2];
  344. $latest_read_time = $record[$count - 1];
  345. Redis::hset('book_read:' . $uid, $key, "{$latest_read_cid}_" . $latest_read_time);
  346. $book_key = 'wap:string:book:' . $key;
  347. $chapter_key = 'wap:string:chapter:' . $latest_read_cid;
  348. Redis::set($book_key, $book_name);
  349. Redis::set($chapter_key, $chapter_name);
  350. }
  351. }
  352. }
  353. public static function delBookBase($uid)
  354. {
  355. $base_record = Redis::hget('book_base:' . $uid, 'last_read');
  356. if ($base_record) {
  357. Redis::del('book_base:' . $uid);
  358. Redis::hset('book_read:' . $uid, 'last_read', $base_record);
  359. }
  360. }
  361. /**
  362. * 获取简单阅读记录只有bid
  363. * @param int $uid
  364. * @return array
  365. */
  366. public static function getSimpleReadRecord(int $uid): array
  367. {
  368. $read_bids = Redis::hgetall('book_read:' . $uid);
  369. $res = [];
  370. if (!$read_bids) {
  371. return $res;
  372. }
  373. foreach ($read_bids as $key => $v) {
  374. if (in_array($key, self::$not_uid_key)) {
  375. continue;
  376. }
  377. array_push($res, $key);
  378. }
  379. return $res;
  380. }
  381. public static function ReadRecordStatistical(int $uid, int $distribution_channel_id, string $from)
  382. {
  383. try {
  384. DB::table('temp_read_active')->insert([
  385. 'uid' => $uid,
  386. 'distribution_channel_id' => $distribution_channel_id,
  387. 'from' => $from,
  388. 'created_at' => date('Y-m-d H:i:s'),
  389. 'updated_at' => date('Y-m-d H:i:s'),
  390. ]);
  391. } catch (\Exception $e) {
  392. }
  393. }
  394. /**
  395. * 获取当前的send_order_id
  396. * @param int $uid
  397. * @return int
  398. */
  399. public static function getSendOrderId(int $uid)
  400. {
  401. try {
  402. $send_order_id = Redis::hget('book_read:' . $uid, 'send_order_id');
  403. if ($send_order_id)
  404. return (int)$send_order_id;
  405. } catch (\Exception $e) {
  406. }
  407. return 0;
  408. }
  409. /**
  410. * 设置内部派单
  411. * @param $uid
  412. * @param $inner_order_id
  413. */
  414. public static function setInnerSendOrderId($uid, $inner_order_id)
  415. {
  416. try {
  417. Redis::hset('book_read:' . $uid, 'inner_send_order_id', $inner_order_id);
  418. } catch (\Exception $e) {
  419. }
  420. }
  421. /**
  422. * 获取内部派单
  423. * @param $uid
  424. * @return string
  425. */
  426. public static function getInnerSendOrderId($uid)
  427. {
  428. try {
  429. $inner_send_order_id = Redis::hget('book_read:' . $uid, 'inner_send_order_id');
  430. if ($inner_send_order_id) {
  431. return $inner_send_order_id;
  432. }
  433. return '';
  434. } catch (\Exception $e) {
  435. }
  436. return '';
  437. }
  438. /**
  439. * 签到日期
  440. * @param int $uid
  441. * @return mixed
  442. */
  443. public static function getSignDay(int $uid)
  444. {
  445. try {
  446. return Redis::hget('book_read:' . $uid, 'sign_day');
  447. } catch (\Exception $e) {
  448. }
  449. return -1;
  450. }
  451. public static function setSignDay(int $uid)
  452. {
  453. return Redis::hset('book_read:' . $uid, 'sign_day', date('Y-m-d'));
  454. }
  455. /**
  456. * 签到次数和日期
  457. * @param int $uid
  458. */
  459. public static function sign(int $uid, bool $is_incr)
  460. {
  461. try {
  462. if ($is_incr) {
  463. Redis::hincrby('book_read:' . $uid, 'sign_counts', 1);
  464. } else {
  465. self::setSignCount($uid, 1);
  466. }
  467. self::setSignDay($uid);
  468. } catch (\Exception $e) {
  469. \Log::info('sign_ept:' . $e->getMessage());
  470. }
  471. return;
  472. }
  473. /**
  474. * @param int $uid
  475. * @return int
  476. */
  477. public static function getSignCount(int $uid)
  478. {
  479. try {
  480. $count = Redis::hget('book_read:' . $uid, 'sign_counts');
  481. if ($count) {
  482. return $count;
  483. }
  484. } catch (\Exception $e) {
  485. }
  486. return 0;
  487. }
  488. /**
  489. * 获取简单签到次数
  490. * @param int $uid
  491. * @return int
  492. */
  493. public static function getSignCountSimple(int $uid)
  494. {
  495. try {
  496. $count = Redis::hget('book_read:' . $uid, 'sign_counts');
  497. if ($count) {
  498. return (int)$count;
  499. }
  500. return 0;
  501. } catch (\Exception $e) {
  502. }
  503. return 0;
  504. }
  505. public static function setSignCount(int $uid, int $count)
  506. {
  507. Redis::hset('book_read:' . $uid, 'sign_counts', $count);
  508. }
  509. public static function setSignInfo(int $uid, string $info)
  510. {
  511. Redis::hset('book_read:' . $uid, 'sign_info', $info);
  512. }
  513. public static function setSmartPush($uid, $bid)
  514. {
  515. $old = self::getSmartPush($uid);
  516. if ($old && !in_array($bid, $old)) {
  517. array_push($old, $bid);
  518. $bid_str = implode(',', $old);
  519. return Redis::hset('book_read:' . $uid, 'smart_push', $bid_str);
  520. } else {
  521. return Redis::hset('book_read:' . $uid, 'smart_push', $bid);
  522. }
  523. }
  524. public static function getSmartPush(int $uid): array
  525. {
  526. $res = Redis::hget('book_read:' . $uid, 'smart_push');
  527. if ($res) {
  528. return explode(',', $res);
  529. }
  530. return [];
  531. }
  532. public static function getByField(int $uid, $field)
  533. {
  534. try {
  535. return Redis::hget('book_read:' . $uid, $field);
  536. } catch (\Exception $e) {
  537. }
  538. return '';
  539. }
  540. public static function getByMultiField(int $uid, ...$field)
  541. {
  542. try {
  543. return Redis::hmget('book_read:' . $uid, $field);
  544. } catch (\Exception $e) {
  545. }
  546. return '';
  547. }
  548. public static function setByField(int $uid, $field, $value)
  549. {
  550. if (!in_array($field, self::$not_uid_key)) {
  551. return false;
  552. }
  553. try {
  554. return Redis::hset('book_read:' . $uid, $field, $value);
  555. } catch (\Exception $e) {
  556. }
  557. return '';
  558. }
  559. public static function setByMultiField(int $uid, $kv)
  560. {
  561. $keys = array_keys($kv);
  562. foreach ($keys as $field) {
  563. if (!in_array($field, self::$not_uid_key)) {
  564. return false;
  565. }
  566. }
  567. try {
  568. return Redis::hmset('book_read:' . $uid, $kv);
  569. } catch (\Exception $e) {
  570. }
  571. return '';
  572. }
  573. private static function resetRecordFromDB($uid)
  574. {
  575. if (self::getByField($uid, 'last_read')) {
  576. return;
  577. }
  578. $record = ReadRecordFromRedis::where('uid', $uid)->select('field', 'value')->get();
  579. if ($record->isNotEmpty()) {
  580. foreach ($record as $item) {
  581. if (!in_array($item->field, self::$not_uid_key) || $item->field == 'last_read') {
  582. Redis::hset('book_read:' . $uid, $item->field, $item->value);
  583. }
  584. }
  585. }
  586. }
  587. //删除多余的阅读纪律
  588. public static function delTheLastRecord($uid)
  589. {
  590. $length = Redis::hlen('book_read:' . $uid);
  591. if ($length <= self::RECORD_COUNT + count(self::$not_uid_key)) {
  592. return;
  593. }
  594. $read_bids = Redis::hgetall('book_read:' . $uid);
  595. $i = 0;
  596. foreach ($read_bids as $key => $v) {
  597. if (in_array($key, self::$not_uid_key)) {
  598. continue;
  599. }
  600. $record = explode('_', $v);
  601. $latest_read_cid = $record[0];
  602. $latest_read_time = $record[count($record) - 1];
  603. $res[$i++] = ['bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time];
  604. }
  605. usort($res, function ($a, $b) {
  606. if ($a['time'] >= $b['time']) return -1;
  607. return 1;
  608. });
  609. $j = 0;
  610. foreach ($res as $v) {
  611. if ($j++ >= self::RECORD_COUNT) {
  612. Redis::hdel('book_read:' . $uid, $v['bid']);
  613. }
  614. }
  615. }
  616. }