ReadRecordService.php 17 KB

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