ReadRecordService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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', 'person_account_id','first_price','qkm_push'];
  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. if(!is_numeric($key)) continue;
  65. $record = explode('_', $v);
  66. $latest_read_cid = $record[0];
  67. $latest_read_time = $record[count($record) - 1];
  68. $book_name = self::bid2BookName($key);
  69. $chapter_name = self::cid2ChapterName($latest_read_cid);
  70. $res[$i] = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time, 'chapter_name' => $chapter_name];
  71. $i++;
  72. }
  73. usort($res, function ($a, $b) {
  74. if ($a['time'] >= $b['time']) return -1;
  75. return 1;
  76. });
  77. return $res;
  78. }
  79. /**
  80. * 新增
  81. * @param $uid
  82. * @param $bid
  83. * @param $cid
  84. * @param $book_name
  85. * @param $chapter_name
  86. */
  87. public static function addReadRecord_($param)
  88. {
  89. $uid = $param['uid'];
  90. $bid = $param['bid'];
  91. $cid = $param['cid'];
  92. $book_name = $param['book_name'];
  93. $chapter_name = $param['chapter_name'];
  94. Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time());
  95. //Redis::hset('book_read:'.$uid, $bid, $cid."_".time());
  96. Redis::hset('book_read:' . $uid, $bid, "{$cid}_{$book_name}_{$chapter_name}_" . time());
  97. }
  98. /**
  99. * 添加阅读记录升级版
  100. * @param $param
  101. */
  102. public static function addReadRecord($param)
  103. {
  104. $uid = $param['uid'];
  105. $bid = $param['bid'];
  106. $cid = $param['cid'];
  107. $book_name = isset($param['book_name'])?$param['book_name']:'';
  108. $chapter_name = isset($param['chapter_name'])?$param['chapter_name']:'';
  109. $book_key = 'wap:string:book:'.$bid;
  110. $chapter_key = 'wap:string:chapter:'.$cid;
  111. if($book_name){
  112. Redis::setex($book_key,3600,$book_name);
  113. }
  114. if($chapter_name){
  115. Redis::setex($chapter_key,3600,$chapter_name);
  116. }
  117. /*Redis::hset('book_read:' . $uid, 'last_read', "{$bid}_{$cid}_" . time());
  118. //Redis::hset('book_read:'.$uid, $bid, $cid."_".time());
  119. Redis::hset('book_read:' . $uid, $bid, "{$cid}_" . time());*/
  120. Redis::hmset('book_read:' . $uid,'last_read', "{$bid}_{$cid}_" . time(),$bid, "{$cid}_" . time(),'next_push_hour',8);
  121. $num = random_int(1,100);
  122. if($num <=3){
  123. self::delTheLastRecord($uid);
  124. }
  125. }
  126. /**
  127. * 删除
  128. * @param $uid
  129. * @param $bid
  130. */
  131. public static function delReadRecord($uid, $bid)
  132. {
  133. if (Redis::hexists('book_read:' . $uid, $bid)) {
  134. Redis::hdel('book_read:' . $uid, $bid);
  135. }
  136. }
  137. /**
  138. * 获取最近一条阅读记录
  139. * @param $uid
  140. */
  141. public static function getFirstReadRecord_($uid){
  142. $all = self::getReadRecord($uid);
  143. if(empty($all)) return [];
  144. $first = $all[0];
  145. if(!$first) return [];
  146. if(!isset($first['bid'])) return [];
  147. try{
  148. //$bid = Hashids::encode($first['bid']);
  149. $bid = $first['bid'];
  150. $book_info = BookConfigService::getBookById($bid);
  151. $cid = $first['cid'];
  152. $book_name = $first['book_name'];
  153. $res = [
  154. 'url' => '/reader?bid='.$bid.'&cid='.$cid,
  155. 'book_name'=>$book_name,
  156. 'cover' =>$book_info->cover,
  157. 'channel_name'=>$book_info->channel_name,
  158. ];
  159. }catch (\Exception $e){
  160. $res = [];
  161. }
  162. return $res;
  163. }
  164. /**
  165. * 获取最近一条阅读记录(升级版)
  166. * @param $uid
  167. * @return array
  168. */
  169. public static function getFirstReadRecord($uid){
  170. self::delBookBase($uid);
  171. //Redis::hget('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time());
  172. $record = Redis::hget('book_read:' . $uid, 'last_read');
  173. if($record){
  174. $record_arr = explode('_',$record);
  175. $bid = $record_arr[0];
  176. $book_info = BookConfigService::getBookById($bid);
  177. $cid = $record_arr[1];
  178. $time = $record_arr[2];
  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='.Hashids::encode($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. $key = (int)$key;
  325. $key && array_push($res,$key);
  326. }
  327. return $res;
  328. }
  329. public static function ReadRecordStatistical(int $uid,int $distribution_channel_id,string $from){
  330. try{
  331. DB::table('temp_read_active')->insert([
  332. 'uid'=>$uid,
  333. 'distribution_channel_id'=>$distribution_channel_id,
  334. 'from'=>$from,
  335. 'created_at'=>date('Y-m-d H:i:s'),
  336. 'updated_at'=>date('Y-m-d H:i:s'),
  337. ]);
  338. }catch (\Exception $e){
  339. }
  340. }
  341. /**
  342. * 获取当前的send_order_id
  343. * @param int $uid
  344. * @return int
  345. */
  346. public static function getSendOrderId(int $uid){
  347. try{
  348. $send_order_id = Redis::hget('book_read:' . $uid,'send_order_id');
  349. if($send_order_id)
  350. return (int)$send_order_id;
  351. }catch (\Exception $e){
  352. }
  353. return 0;
  354. }
  355. /**
  356. * 设置内部派单
  357. * @param $uid
  358. * @param $inner_order_id
  359. */
  360. public static function setInnerSendOrderId($uid,$inner_order_id){
  361. try{
  362. Redis::hset('book_read:' . $uid,'inner_send_order_id',$inner_order_id);
  363. }catch (\Exception $e){}
  364. }
  365. /**
  366. * 获取内部派单
  367. * @param $uid
  368. * @return string
  369. */
  370. public static function getInnerSendOrderId($uid){
  371. try{
  372. $inner_send_order_id = Redis::hget('book_read:' . $uid,'inner_send_order_id');
  373. if($inner_send_order_id){
  374. return $inner_send_order_id;
  375. }
  376. return '';
  377. }catch (\Exception $e){}
  378. return '';
  379. }
  380. /**
  381. * 签到日期
  382. * @param int $uid
  383. * @return mixed
  384. */
  385. public static function getSignDay(int $uid){
  386. try{
  387. return Redis::hget('book_read:' . $uid,'sign_day');
  388. }catch (\Exception $e){}
  389. return -1;
  390. }
  391. public static function setSignDay(int $uid){
  392. return Redis::hset('book_read:' . $uid,'sign_day',date('Y-m-d'));
  393. }
  394. /**
  395. * 签到次数和日期
  396. * @param int $uid
  397. */
  398. public static function sign(int $uid,bool $is_incr){
  399. try{
  400. if($is_incr){
  401. Redis::hincrby('book_read:' . $uid,'sign_counts',1);
  402. }else{
  403. self::setSignCount($uid,1);
  404. }
  405. self::setSignDay($uid);
  406. }catch (\Exception $e){
  407. \Log::info('sign_ept:'.$e->getMessage());
  408. }
  409. return;
  410. }
  411. /**
  412. * @param int $uid
  413. * @return int
  414. */
  415. public static function getSignCount(int $uid){
  416. try{
  417. $count = Redis::hget('book_read:' . $uid,'sign_counts');
  418. if($count){
  419. return $count;
  420. }
  421. }catch (\Exception $e){
  422. }
  423. return 0;
  424. }
  425. /**
  426. * 获取简单签到次数
  427. * @param int $uid
  428. * @return int
  429. */
  430. public static function getSignCountSimple(int $uid){
  431. try{
  432. $count = Redis::hget('book_read:' . $uid,'sign_counts');
  433. if($count){
  434. return (int)$count;
  435. }
  436. return 0;
  437. }catch (\Exception $e){
  438. }
  439. return 0;
  440. }
  441. public static function setSignCount(int $uid,int $count){
  442. Redis::hset('book_read:' . $uid,'sign_counts',$count);
  443. }
  444. public static function setSignInfo(int $uid,string $info){
  445. Redis::hset('book_read:' . $uid,'sign_info',$info);
  446. }
  447. public static function setSmartPush($uid,$bid){
  448. $old = self::getSmartPush($uid);
  449. if($old && !in_array($bid,$old)){
  450. array_push($old,$bid);
  451. $bid_str = implode(',',$old);
  452. return Redis::hset('book_read:' . $uid,'smart_push',$bid_str);
  453. }else{
  454. return Redis::hset('book_read:' . $uid,'smart_push',$bid);
  455. }
  456. }
  457. public static function getSmartPush(int $uid):array{
  458. $res = Redis::hget('book_read:' . $uid,'smart_push');
  459. if($res){
  460. return explode(',',$res);
  461. }
  462. return [];
  463. }
  464. public static function getByField(int $uid,$field){
  465. try{
  466. return Redis::hget('book_read:' . $uid,$field);
  467. }catch (\Exception $e){
  468. }
  469. return '';
  470. }
  471. public static function getByMultiField(int $uid,...$field){
  472. try{
  473. return Redis::hmget('book_read:' . $uid,$field);
  474. }catch (\Exception $e){
  475. }
  476. return '';
  477. }
  478. public static function setByField(int $uid,$field,$value){
  479. if(!in_array($field,self::$not_uid_key)){
  480. return false;
  481. }
  482. try{
  483. return Redis::hset('book_read:' . $uid,$field,$value);
  484. }catch (\Exception $e){}
  485. return '';
  486. }
  487. public static function setByMultiField(int $uid,$kv){
  488. $keys = array_keys($kv);
  489. foreach ($keys as $field){
  490. if(!in_array($field,self::$not_uid_key)){
  491. return false;
  492. }
  493. }
  494. try{
  495. return Redis::hmset('book_read:' . $uid,$kv);
  496. }catch (\Exception $e){}
  497. return '';
  498. }
  499. private static function resetRecordFromDB($uid)
  500. {
  501. if(self::getByField($uid,'last_read')){
  502. return ;
  503. }
  504. $record = ReadRecordFromRedis::where('uid',$uid)->select('field','value')->get();
  505. if($record->isNotEmpty()){
  506. foreach ($record as $item){
  507. if(!in_array($item->field,self::$not_uid_key) || $item->field == 'last_read'){
  508. Redis::hset('book_read:'.$uid,$item->field,$item->value);
  509. }
  510. }
  511. }
  512. }
  513. //删除多余的阅读纪律
  514. public static function delTheLastRecord($uid){
  515. $length = Redis::hlen('book_read:'.$uid);
  516. if($length <= self::RECORD_COUNT+count(self::$not_uid_key)){
  517. return ;
  518. }
  519. $read_bids = Redis::hgetall('book_read:' . $uid);
  520. $i = 0;
  521. foreach ($read_bids as $key => $v) {
  522. if(in_array($key,self::$not_uid_key)){
  523. continue;
  524. }
  525. $record = explode('_', $v);
  526. $latest_read_cid = $record[0];
  527. $latest_read_time = $record[count($record) - 1];
  528. $res[$i++] = [ 'bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time];
  529. }
  530. usort($res, function ($a, $b) {
  531. if ($a['time'] >= $b['time']) return -1;
  532. return 1;
  533. });
  534. $j = 0;
  535. foreach ($res as $v){
  536. if($j++ >=self::RECORD_COUNT){
  537. Redis::hdel('book_read:'.$uid,$v['bid']);
  538. }
  539. }
  540. }
  541. }