__('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 = 女 * @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',$limit=3,$isWater=false){ $redis = Redis::instance(); $key = $isWater ? 'SRB:W:'.$sex :'SRB:'.$sex; $waterWhere = $isWater ? ' and classify_white_list=1' : ''; if(!$redis->exists($key)){ $obj = $this->alias('sr')->where(['sr.status'=>'normal','sr.sex'=>$sex]); $obj->join('book','book.id=book_id and book.state=1'.$waterWhere,'inner'); $source = $obj->select(); if(empty($source)){ return null; } foreach($source as $item){ $redis->sadd($key,$item['book_id'].':'.$item['book_name']); } $redis->expire($key,900); if(count($source) >= $limit){ shuffle($source); return array_slice($source,0,$limit); } return $source; }else{ $data = array(); $source = $redis->srandmember($key,$limit); foreach($source as $item){ $map = explode(':',$item); array_push($data,['book_id'=>array_shift($map),'book_name'=>implode($map)]); } return $data; } } }