ReadRecordService.php 19 KB

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