ReadRecordService.php 20 KB

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