ReadRecordService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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\BaseService;
  10. use App\Modules\User\Models\ReadRecordFromRedis;
  11. use Redis;
  12. use Hashids;
  13. use App\Modules\Book\Services\BookConfigService;
  14. use App\Modules\Book\Models\Chapter;
  15. use DB;
  16. /**
  17. * @method static delReadRecordStatic(int $uid, array $bids) 删除最近阅读记录
  18. * @method static getBookReadRecordStatic(int $uid, int $bid) 获取单本书籍的最近阅读记录
  19. */
  20. class ReadRecordService
  21. {
  22. #region 以后按照这种方式写静态方法
  23. use BaseService;
  24. public function getBookReadRecord(int $uid, int $bid)
  25. {
  26. $record = Redis::hGet('book_read:' . $uid, $bid);
  27. if ($record) {
  28. $cid = explode('_', $record)[0];
  29. $name = self::cid2ChapterName($cid);
  30. return ['record_chapter_id' => $cid, 'record_chapter_name' => $name];
  31. }
  32. return false;
  33. }
  34. /**
  35. * 删除最近阅读记录
  36. * @param int $uid
  37. * @param array $bids
  38. */
  39. public function delReadRecord(int $uid, array $bids)
  40. {
  41. $key = 'book_read:' . $uid;
  42. $last_record = explode('_', Redis::hGet($key, 'last_read'));
  43. $last_record_bid = $last_record[0];
  44. $is_del_last = false;
  45. foreach ($bids as $bid) {
  46. if ($bid == $last_record_bid) {
  47. $is_del_last = true;
  48. }
  49. Redis::hDel($key, $bid);
  50. }
  51. if ($is_del_last) {
  52. $this->reSetLastRecord($uid);
  53. }
  54. }
  55. /**
  56. * 重置用户最近阅读记录
  57. * @param int $uid
  58. */
  59. public function reSetLastRecord($uid)
  60. {
  61. $key = 'book_read:' . $uid;
  62. $alls = Redis::hGetAll($key);
  63. $has_record = false;
  64. $last_timestamp = 0;
  65. $last_record = '';
  66. foreach ($alls as $k => $v) {
  67. if (is_numeric($k)) {
  68. $has_record = true;
  69. $record = explode('_', $v);
  70. $timestamp = $record[1];
  71. if ($last_timestamp < $timestamp) {
  72. $last_record = $k . '_' . $v;
  73. $last_timestamp = $timestamp;
  74. }
  75. }
  76. }
  77. if ($last_record) {
  78. Redis::hSet($key, 'last_read', $last_record);
  79. }
  80. if (!$has_record) {
  81. Redis::hDel($key, 'last_read');
  82. }
  83. }
  84. #endregion
  85. //阅读记录数
  86. const RECORD_COUNT = 50;
  87. 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'];
  88. /**
  89. * 获取
  90. * @param $uid
  91. * @return array
  92. */
  93. public static function getReadRecord_($uid)
  94. {
  95. $read_bids = Redis::hgetall('book_read:' . $uid);
  96. $res = [];
  97. $i = 0;
  98. foreach ($read_bids as $key => $v) {
  99. $record = explode('_', $v);
  100. $latest_read_cid = $record[0];
  101. $book_name = $record[1];
  102. $chapter_name = $record[2];
  103. $latest_read_time = $record[count($record) - 1];
  104. $res[$i] = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int) $latest_read_cid, 'time' => (int) $latest_read_time, 'chapter_name' => $chapter_name];
  105. $i++;
  106. }
  107. usort($res, function ($a, $b) {
  108. if ($a['time'] >= $b['time']) return -1;
  109. return 1;
  110. });
  111. return $res;
  112. }
  113. /**
  114. * 获取 升级版
  115. * @param $uid
  116. * @return array
  117. */
  118. public static function getReadRecord($uid, $is_need_check_db = false)
  119. {
  120. if ($is_need_check_db) {
  121. self::resetRecordFromDB($uid);
  122. }
  123. self::delTheLastRecord($uid);
  124. $read_bids = Redis::hgetall('book_read:' . $uid);
  125. $res = [];
  126. $i = 0;
  127. //self::delBookNameAndChapter($uid);
  128. foreach ($read_bids as $key => $v) {
  129. if (in_array($key, self::$not_uid_key)) {
  130. continue;
  131. }
  132. $record = explode('_', $v);
  133. $latest_read_cid = $record[0];
  134. $latest_read_time = $record[count($record) - 1];
  135. $book_name = self::bid2BookName($key);
  136. $chapter_name = self::cid2ChapterName($latest_read_cid);
  137. $res[$i] = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int) $latest_read_cid, 'time' => (int) $latest_read_time, 'chapter_name' => $chapter_name];
  138. $i++;
  139. }
  140. usort($res, function ($a, $b) {
  141. if ($a['time'] >= $b['time']) return -1;
  142. return 1;
  143. });
  144. return $res;
  145. }
  146. /**
  147. * 新增
  148. * @param $uid
  149. * @param $bid
  150. * @param $cid
  151. * @param $book_name
  152. * @param $chapter_name
  153. */
  154. public static function addReadRecord_($param)
  155. {
  156. $uid = $param['uid'];
  157. $bid = $param['bid'];
  158. $cid = $param['cid'];
  159. $book_name = $param['book_name'];
  160. $chapter_name = $param['chapter_name'];
  161. Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time());
  162. //Redis::hset('book_read:'.$uid, $bid, $cid."_".time());
  163. Redis::hset('book_read:' . $uid, $bid, "{$cid}_{$book_name}_{$chapter_name}_" . time());
  164. }
  165. /**
  166. * 添加阅读记录升级版
  167. * @param array $param
  168. */
  169. public static function addReadRecord($param)
  170. {
  171. $uid = $param['uid'];
  172. $bid = $param['bid'];
  173. $cid = $param['cid'];
  174. $book_name = isset($param['book_name']) ? $param['book_name'] : '';
  175. $chapter_name = isset($param['chapter_name']) ? $param['chapter_name'] : '';
  176. $book_key = 'wap:string:book:' . $bid;
  177. $chapter_key = 'wap:string:chapter:' . $cid;
  178. if ($book_name) {
  179. Redis::setex($book_key, 3600, $book_name);
  180. }
  181. if ($chapter_name) {
  182. Redis::setex($chapter_key, 3600, $chapter_name);
  183. }
  184. Redis::hmset('book_read:' . $uid, 'last_read', "{$bid}_{$cid}_" . time(), $bid, "{$cid}_" . time(), 'next_push_hour', 8);
  185. $num = random_int(1, 100);
  186. if ($num <= 3) {
  187. self::delTheLastRecord($uid);
  188. }
  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. 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. * 获取当前的send_order_id
  395. * @param int $uid
  396. * @return int
  397. */
  398. public static function getSendOrderId(int $uid)
  399. {
  400. try {
  401. $send_order_id = Redis::hget('book_read:' . $uid, 'send_order_id');
  402. if ($send_order_id)
  403. return (int) $send_order_id;
  404. } catch (\Exception $e) { }
  405. return 0;
  406. }
  407. /**
  408. * 设置内部派单
  409. * @param $uid
  410. * @param $inner_order_id
  411. */
  412. public static function setInnerSendOrderId($uid, $inner_order_id)
  413. {
  414. try {
  415. Redis::hset('book_read:' . $uid, 'inner_send_order_id', $inner_order_id);
  416. } catch (\Exception $e) { }
  417. }
  418. /**
  419. * 获取内部派单
  420. * @param $uid
  421. * @return string
  422. */
  423. public static function getInnerSendOrderId($uid)
  424. {
  425. try {
  426. $inner_send_order_id = Redis::hget('book_read:' . $uid, 'inner_send_order_id');
  427. if ($inner_send_order_id) {
  428. return $inner_send_order_id;
  429. }
  430. return '';
  431. } catch (\Exception $e) { }
  432. return '';
  433. }
  434. /**
  435. * 签到日期
  436. * @param int $uid
  437. * @return mixed
  438. */
  439. public static function getSignDay(int $uid)
  440. {
  441. try {
  442. return Redis::hget('book_read:' . $uid, 'sign_day');
  443. } catch (\Exception $e) { }
  444. return -1;
  445. }
  446. public static function setSignDay(int $uid)
  447. {
  448. return Redis::hset('book_read:' . $uid, 'sign_day', date('Y-m-d'));
  449. }
  450. /**
  451. * 签到次数和日期
  452. * @param int $uid
  453. */
  454. public static function sign(int $uid, bool $is_incr)
  455. {
  456. try {
  457. if ($is_incr) {
  458. Redis::hincrby('book_read:' . $uid, 'sign_counts', 1);
  459. } else {
  460. self::setSignCount($uid, 1);
  461. }
  462. self::setSignDay($uid);
  463. } catch (\Exception $e) {
  464. \Log::info('sign_ept:' . $e->getMessage());
  465. }
  466. return;
  467. }
  468. /**
  469. * @param int $uid
  470. * @return int
  471. */
  472. public static function getSignCount(int $uid)
  473. {
  474. try {
  475. $count = Redis::hget('book_read:' . $uid, 'sign_counts');
  476. if ($count) {
  477. return $count;
  478. }
  479. } catch (\Exception $e) { }
  480. return 0;
  481. }
  482. /**
  483. * 获取简单签到次数
  484. * @param int $uid
  485. * @return int
  486. */
  487. public static function getSignCountSimple(int $uid)
  488. {
  489. try {
  490. $count = Redis::hget('book_read:' . $uid, 'sign_counts');
  491. if ($count) {
  492. return (int) $count;
  493. }
  494. return 0;
  495. } catch (\Exception $e) { }
  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. 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. return '';
  538. }
  539. public static function setByField(int $uid, $field, $value)
  540. {
  541. if (!in_array($field, self::$not_uid_key)) {
  542. return false;
  543. }
  544. try {
  545. return Redis::hset('book_read:' . $uid, $field, $value);
  546. } catch (\Exception $e) { }
  547. return '';
  548. }
  549. public static function setByMultiField(int $uid, $kv)
  550. {
  551. $keys = array_keys($kv);
  552. foreach ($keys as $field) {
  553. if (!in_array($field, self::$not_uid_key)) {
  554. return false;
  555. }
  556. }
  557. try {
  558. return Redis::hmset('book_read:' . $uid, $kv);
  559. } catch (\Exception $e) { }
  560. return '';
  561. }
  562. private static function resetRecordFromDB($uid)
  563. {
  564. if (self::getByField($uid, 'last_read')) {
  565. return;
  566. }
  567. $record = ReadRecordFromRedis::where('uid', $uid)->select('field', 'value')->get();
  568. if ($record->isNotEmpty()) {
  569. foreach ($record as $item) {
  570. if (!in_array($item->field, self::$not_uid_key) || $item->field == 'last_read') {
  571. Redis::hset('book_read:' . $uid, $item->field, $item->value);
  572. }
  573. }
  574. }
  575. }
  576. //删除多余的阅读纪律
  577. public static function delTheLastRecord($uid)
  578. {
  579. $length = Redis::hlen('book_read:' . $uid);
  580. if ($length <= self::RECORD_COUNT + count(self::$not_uid_key)) {
  581. return;
  582. }
  583. $read_bids = Redis::hgetall('book_read:' . $uid);
  584. $i = 0;
  585. foreach ($read_bids as $key => $v) {
  586. if (in_array($key, self::$not_uid_key)) {
  587. continue;
  588. }
  589. $record = explode('_', $v);
  590. $latest_read_cid = $record[0];
  591. $latest_read_time = $record[count($record) - 1];
  592. $res[$i++] = ['bid' => $key, 'cid' => (int) $latest_read_cid, 'time' => (int) $latest_read_time];
  593. }
  594. usort($res, function ($a, $b) {
  595. if ($a['time'] >= $b['time']) return -1;
  596. return 1;
  597. });
  598. $j = 0;
  599. foreach ($res as $v) {
  600. if ($j++ >= self::RECORD_COUNT) {
  601. Redis::hdel('book_read:' . $uid, $v['bid']);
  602. }
  603. }
  604. }
  605. }