ReadRecordService.php 14 KB

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