ReadRecordService.php 19 KB

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