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