ReadRecordService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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'];
  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());
  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. $cid = $record_arr[1];
  176. $book_info = BookConfigService::getBookById($bid);
  177. $book_name = isset($book_info->book_name)?$book_info->book_name:'';
  178. $cover = isset($book_info->cover)?$book_info->cover:'';
  179. $channel_name = isset($book_info->channel_name)?$book_info->channel_name:'';
  180. $res = [
  181. 'url' => '/reader?bid='.$bid.'&cid='.$cid,
  182. 'book_name'=>$book_name,
  183. 'cover' =>$cover,
  184. 'channel_name'=>$channel_name,
  185. ];
  186. return $res;
  187. }
  188. return [];
  189. }
  190. /**
  191. * 获取简单阅读记录
  192. * @param $uid
  193. * @return int
  194. */
  195. public static function getSimpleFirstReadRecord($uid){
  196. try{
  197. $record = Redis::hget('book_read:' . $uid, 'last_read');
  198. if($record){
  199. $record_arr = explode('_',$record);
  200. $bid = $record_arr[0];
  201. return (int)$bid;
  202. }
  203. }catch (\Exception $e){
  204. }
  205. return 0;
  206. }
  207. /**
  208. * 获取客服消息点击数
  209. * @param $uid
  210. */
  211. public static function getCustomerMsgClickNum($channel_id,$from,$date){
  212. $key = "fromcustomermsgenter:distribution_channel_id:".$channel_id.'from:'.$from;
  213. return Redis::hget($key,$date);
  214. }
  215. /**
  216. * 获取某本书的阅读记录
  217. */
  218. public static function getRecordByUidBid($uid,$bid){
  219. return Redis::hget('book_read:' . $uid, $bid);
  220. }
  221. /**
  222. * 根据bid获取书名
  223. * @param $bid
  224. * @return bool|null|string
  225. */
  226. public static function bid2BookName($bid){
  227. $book_name = null;
  228. if(is_null($book_name)){
  229. $book_key = 'wap:string:book:'.$bid;
  230. $book_name = Redis::get($book_key);
  231. Redis::EXPIRE($book_key,3600);
  232. if(!$book_name){
  233. $book_name = '';
  234. $book_info = BookConfigService::getBookById($bid);
  235. if($book_info && isset($book_info->book_name)){
  236. $book_name = $book_info->book_name;
  237. }
  238. }
  239. }
  240. return $book_name;
  241. }
  242. /**
  243. * 根据cid获取章节名
  244. * @param $cid
  245. * @return bool|null|string
  246. */
  247. public static function cid2ChapterName($cid){
  248. $chapter_name = null;
  249. if(is_null($chapter_name)){
  250. $chapter_key = 'wap:string:chapter:'.$cid;
  251. $chapter_name = Redis::get($chapter_key);
  252. Redis::EXPIRE($chapter_key,3600);
  253. if(!$chapter_name){
  254. $chapter_name = '';
  255. $chapter_info = Chapter::getChapterNameById($cid);
  256. if($chapter_info && isset($chapter_info->name)){
  257. $chapter_name = $chapter_info->name;
  258. }
  259. }
  260. }
  261. return $chapter_name;
  262. }
  263. /**
  264. * 删除阅读记录中的书名和章节名
  265. * @param $uid
  266. * @param $record
  267. */
  268. public static function delBookNameAndChapter($uid){
  269. //Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time()) ;
  270. $base_record = Redis::hget('book_base:' . $uid, 'last_read');
  271. if($base_record){
  272. $record_arr = explode('_',$base_record);
  273. $c = count($record_arr);
  274. if($c>3){
  275. $bid = $record_arr[0];
  276. $cid = $record_arr[1];
  277. $time = $record_arr[$c-1];
  278. Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_" . $time);
  279. }
  280. }
  281. $records = Redis::hgetall('book_read:' . $uid);
  282. foreach ($records as $key => $v) {
  283. $record = explode('_', $v);
  284. $count = count($record);
  285. if($count >3){
  286. $latest_read_cid = $record[0];
  287. $book_name = $record[1];
  288. $chapter_name = $record[2];
  289. $latest_read_time = $record[$count - 1];
  290. Redis::hset('book_read:' . $uid, $key, "{$latest_read_cid}_" . $latest_read_time);
  291. $book_key = 'wap:string:book:'.$key;
  292. $chapter_key = 'wap:string:chapter:'.$latest_read_cid;
  293. Redis::set($book_key,$book_name);
  294. Redis::set($chapter_key,$chapter_name);
  295. }
  296. }
  297. }
  298. public static function delBookBase($uid){
  299. $base_record = Redis::hget('book_base:' . $uid, 'last_read');
  300. if($base_record){
  301. Redis::del('book_base:' . $uid);
  302. Redis::hset('book_read:' . $uid, 'last_read', $base_record);
  303. }
  304. }
  305. /**
  306. * 获取简单阅读记录只有bid
  307. * @param int $uid
  308. * @return array
  309. */
  310. public static function getSimpleReadRecord(int $uid):array
  311. {
  312. $read_bids = Redis::hgetall('book_read:' . $uid);
  313. $res = [];
  314. if(!$read_bids) {
  315. return $res;
  316. }
  317. foreach ($read_bids as $key => $v) {
  318. if(in_array($key,self::$not_uid_key)){
  319. continue;
  320. }
  321. array_push($res,$key);
  322. }
  323. return $res;
  324. }
  325. public static function ReadRecordStatistical(int $uid,int $distribution_channel_id,string $from){
  326. try{
  327. DB::table('temp_read_active')->insert([
  328. 'uid'=>$uid,
  329. 'distribution_channel_id'=>$distribution_channel_id,
  330. 'from'=>$from,
  331. 'created_at'=>date('Y-m-d H:i:s'),
  332. 'updated_at'=>date('Y-m-d H:i:s'),
  333. ]);
  334. }catch (\Exception $e){
  335. }
  336. }
  337. /**
  338. * 获取当前的send_order_id
  339. * @param int $uid
  340. * @return int
  341. */
  342. public static function getSendOrderId(int $uid){
  343. try{
  344. $send_order_id = Redis::hget('book_read:' . $uid,'send_order_id');
  345. if($send_order_id)
  346. return (int)$send_order_id;
  347. }catch (\Exception $e){
  348. }
  349. return 0;
  350. }
  351. /**
  352. * 设置内部派单
  353. * @param $uid
  354. * @param $inner_order_id
  355. */
  356. public static function setInnerSendOrderId($uid,$inner_order_id){
  357. try{
  358. Redis::hset('book_read:' . $uid,'inner_send_order_id',$inner_order_id);
  359. }catch (\Exception $e){}
  360. }
  361. /**
  362. * 获取内部派单
  363. * @param $uid
  364. * @return string
  365. */
  366. public static function getInnerSendOrderId($uid){
  367. try{
  368. $inner_send_order_id = Redis::hget('book_read:' . $uid,'inner_send_order_id');
  369. if($inner_send_order_id){
  370. return $inner_send_order_id;
  371. }
  372. return '';
  373. }catch (\Exception $e){}
  374. return '';
  375. }
  376. /**
  377. * 签到日期
  378. * @param int $uid
  379. * @return mixed
  380. */
  381. public static function getSignDay(int $uid){
  382. try{
  383. return Redis::hget('book_read:' . $uid,'sign_day');
  384. }catch (\Exception $e){}
  385. return -1;
  386. }
  387. public static function setSignDay(int $uid){
  388. return Redis::hset('book_read:' . $uid,'sign_day',date('Y-m-d'));
  389. }
  390. /**
  391. * 签到次数和日期
  392. * @param int $uid
  393. */
  394. public static function sign(int $uid,bool $is_incr){
  395. try{
  396. if($is_incr){
  397. Redis::hincrby('book_read:' . $uid,'sign_counts',1);
  398. }else{
  399. self::setSignCount($uid,1);
  400. }
  401. self::setSignDay($uid);
  402. }catch (\Exception $e){
  403. \Log::info('sign_ept:'.$e->getMessage());
  404. }
  405. return;
  406. }
  407. /**
  408. * @param int $uid
  409. * @return int
  410. */
  411. public static function getSignCount(int $uid){
  412. try{
  413. $count = Redis::hget('book_read:' . $uid,'sign_counts');
  414. if($count){
  415. return $count;
  416. }
  417. }catch (\Exception $e){
  418. }
  419. return 0;
  420. }
  421. /**
  422. * 获取简单签到次数
  423. * @param int $uid
  424. * @return int
  425. */
  426. public static function getSignCountSimple(int $uid){
  427. try{
  428. $count = Redis::hget('book_read:' . $uid,'sign_counts');
  429. if($count){
  430. return (int)$count;
  431. }
  432. return 0;
  433. }catch (\Exception $e){
  434. }
  435. return 0;
  436. }
  437. public static function setSignCount(int $uid,int $count){
  438. Redis::hset('book_read:' . $uid,'sign_counts',$count);
  439. }
  440. public static function setSignInfo(int $uid,string $info){
  441. Redis::hset('book_read:' . $uid,'sign_info',$info);
  442. }
  443. public static function setSmartPush($uid,$bid){
  444. $old = self::getSmartPush($uid);
  445. if($old && !in_array($bid,$old)){
  446. array_push($old,$bid);
  447. $bid_str = implode(',',$old);
  448. return Redis::hset('book_read:' . $uid,'smart_push',$bid_str);
  449. }else{
  450. return Redis::hset('book_read:' . $uid,'smart_push',$bid);
  451. }
  452. }
  453. public static function getSmartPush(int $uid):array{
  454. $res = Redis::hget('book_read:' . $uid,'smart_push');
  455. if($res){
  456. return explode(',',$res);
  457. }
  458. return [];
  459. }
  460. public static function getByField(int $uid,$field){
  461. try{
  462. return Redis::hget('book_read:' . $uid,$field);
  463. }catch (\Exception $e){
  464. }
  465. return '';
  466. }
  467. public static function setByField(int $uid,$field,$value){
  468. if(!in_array($field,self::$not_uid_key)){
  469. return false;
  470. }
  471. try{
  472. return Redis::hset('book_read:' . $uid,$field,$value);
  473. }catch (\Exception $e){}
  474. return '';
  475. }
  476. private static function resetRecordFromDB($uid)
  477. {
  478. if(self::getByField($uid,'last_read')){
  479. return ;
  480. }
  481. $record = ReadRecordFromRedis::where('uid',$uid)->select('field','value')->get();
  482. if($record->isNotEmpty()){
  483. foreach ($record as $item){
  484. if(!in_array($item->field,self::$not_uid_key) || $item->field == 'last_read'){
  485. Redis::hset('book_read:'.$uid,$item->field,$item->value);
  486. }
  487. }
  488. }
  489. }
  490. //删除多余的阅读纪律
  491. public static function delTheLastRecord($uid){
  492. $length = Redis::hlen('book_read:'.$uid);
  493. if($length <= self::RECORD_COUNT+count(self::$not_uid_key)){
  494. return ;
  495. }
  496. $read_bids = Redis::hgetall('book_read:' . $uid);
  497. $i = 0;
  498. foreach ($read_bids as $key => $v) {
  499. if(in_array($key,self::$not_uid_key)){
  500. continue;
  501. }
  502. $record = explode('_', $v);
  503. $latest_read_cid = $record[0];
  504. $latest_read_time = $record[count($record) - 1];
  505. $res[$i++] = [ 'bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time];
  506. }
  507. usort($res, function ($a, $b) {
  508. if ($a['time'] >= $b['time']) return -1;
  509. return 1;
  510. });
  511. $j = 0;
  512. foreach ($res as $v){
  513. if($j++ >=self::RECORD_COUNT){
  514. Redis::hdel('book_read:'.$uid,$v['bid']);
  515. }
  516. }
  517. }
  518. }