123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\common\model;
- use app\common\service\WaterBookService;
- use think\Model;
- use app\common\library\Redis;
- class NovelEndRecommend extends Model
- {
- // 表名
- protected $table = 'novel_end_recommend';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
-
- // 追加属性
- protected $append = [
- 'sex_text',
- 'status_text'
- ];
- public function getSexList()
- {
- return ['1' => __('Sex 1'),'2' => __('Sex 2')];
- }
- public function getStatusList()
- {
- return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
- }
- public function getSexTextAttr($value, $data)
- {
- $value = $value ? $value : $data['sex'];
- $list = $this->getSexList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : $data['status'];
- $list = $this->getStatusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- /**
- * 根据性别获取推荐书籍
- * @param bool $sex 1 = 男 , 2 = 女
- * @param $isWater
- * @return false|mixed|\PDOStatement|string|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getBooksBySex($sex = '1', $isWater = false){
- $redis = Redis::instance();
- $key = $isWater ? 'NERC:W:' . $sex : 'NERC:' . $sex;//清水和非清水书籍key
- $waterWhere = $isWater ? ' and classify_white_list=1' : '';
- if(!$redis->exists($key)){
- $obj = $this->alias('nr')->where(['nr.status'=>'normal','nr.sex'=>$sex]);
- $obj->join('book','book.id=book_id and state=1 '.$waterWhere,'inner');
- $source = $obj->order('weight', 'desc')
- ->field(['book_id','book_name','click_threshold','nr.status','nr.sex','nr.weight'])
- ->select();
- if(empty($source)){
- return null;
- }
- $resArr = [];
- $resStrArr = '';
- foreach($source as $item){
- $resArr[] = [
- 'book_id'=>$item['book_id'],
- 'book_name'=>$item['book_name'],
- 'click_threshold'=>$item['click_threshold'],
- ];
- // 构造按权重顺序的字符串数组,插入redis的set后顺序会变为为逆序
- $resStrArr = $item['book_id'] . ':' . $item['book_name'] . ':' . $item['click_threshold'];
- $redis->zadd($key, $item['weight'], $resStrArr);
- }
- $redis->expire($key,86400);
- return $resArr;
- }else{
- $data = array();
- $source = $redis->Zrevrange($key,0,-1);
- foreach($source as $item){
- $map = explode(':',$item);
- array_push($data,['book_id'=>array_shift($map),'book_name'=>array_shift($map),'click_threshold'=>array_shift($map)]);
- }
- return $data;
- }
- }
- }
|