ReadRecordService.php 19 KB

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