ReadRecordService.php 16 KB

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