NovelEndRecommend.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\common\model;
  3. use app\common\service\WaterBookService;
  4. use think\Model;
  5. use app\common\library\Redis;
  6. class NovelEndRecommend extends Model
  7. {
  8. // 表名
  9. protected $table = 'novel_end_recommend';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'int';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. // 追加属性
  16. protected $append = [
  17. 'sex_text',
  18. 'status_text'
  19. ];
  20. public function getSexList()
  21. {
  22. return ['1' => __('Sex 1'),'2' => __('Sex 2')];
  23. }
  24. public function getStatusList()
  25. {
  26. return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
  27. }
  28. public function getSexTextAttr($value, $data)
  29. {
  30. $value = $value ? $value : $data['sex'];
  31. $list = $this->getSexList();
  32. return isset($list[$value]) ? $list[$value] : '';
  33. }
  34. public function getStatusTextAttr($value, $data)
  35. {
  36. $value = $value ? $value : $data['status'];
  37. $list = $this->getStatusList();
  38. return isset($list[$value]) ? $list[$value] : '';
  39. }
  40. /**
  41. * 根据性别获取推荐书籍
  42. * @param bool $sex 1 = 男 , 2 = 女
  43. * @param $isWater
  44. * @return false|mixed|\PDOStatement|string|\think\Collection
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. public function getBooksBySex($sex = '1', $isWater = false){
  50. $redis = Redis::instance();
  51. $key = $isWater ? 'NERC:W:' . $sex : 'NERC:' . $sex;//清水和非清水书籍key
  52. $waterWhere = $isWater ? ' and classify_white_list=1' : '';
  53. if(!$redis->exists($key)){
  54. $obj = $this->alias('nr')->where(['nr.status'=>'normal','nr.sex'=>$sex]);
  55. $obj->join('book','book.id=book_id and state=1 '.$waterWhere,'inner');
  56. $source = $obj->order('weight', 'desc')
  57. ->field(['book_id','book_name','click_threshold','nr.status','nr.sex','nr.weight'])
  58. ->select();
  59. if(empty($source)){
  60. return null;
  61. }
  62. $resArr = [];
  63. $resStrArr = '';
  64. foreach($source as $item){
  65. $resArr[] = [
  66. 'book_id'=>$item['book_id'],
  67. 'book_name'=>$item['book_name'],
  68. 'click_threshold'=>$item['click_threshold'],
  69. ];
  70. // 构造按权重顺序的字符串数组,插入redis的set后顺序会变为为逆序
  71. $resStrArr = $item['book_id'] . ':' . $item['book_name'] . ':' . $item['click_threshold'];
  72. $redis->zadd($key, $item['weight'], $resStrArr);
  73. }
  74. $redis->expire($key,86400);
  75. return $resArr;
  76. }else{
  77. $data = array();
  78. $source = $redis->Zrevrange($key,0,-1);
  79. foreach($source as $item){
  80. $map = explode(':',$item);
  81. array_push($data,['book_id'=>array_shift($map),'book_name'=>array_shift($map),'click_threshold'=>array_shift($map)]);
  82. }
  83. return $data;
  84. }
  85. }
  86. }