ReadRecordService.php 14 KB

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