ReadRecordService.php 20 KB

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