ReadRecordService.php 20 KB

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