BookRelation ) { $this->BookRelation = model('BookRelation'); } } public static function instance() { if(self::$self == NULL){ self::$self = new self(); } return self::$self; } /** * 猜你喜欢 ---在批量书籍 有拆分关系的书籍 * @param $books * @param int $limit * @return array */ public function filterLikeBookRelation( $books, $limit=0 ){ // || count($books) <= $limit return $books; if ( empty( $books ) ) { return $books; } $res = []; $filterStr = ''; /**@return getRecentlyRead */ if (Cookie::has('user_id') || Cookie::has('token') ) { $recent = model('UserRecentlyRead')->getRecentlyRead(0, 20, null, true); }else{ return $books; } if ( !empty($recent['data']) ) { $filterStr .= implode( array_unique( array_column($recent['data'], 'book_id') ), ',' ); $filterStr .= implode( array_unique( array_column($recent['data'], 'relation_id') ), ',' ); } if ( $filterStr == '' ) { return $books; } $c = 0;//统计过滤后书籍的数量 foreach ($books as $key => $value) { if ( $c == $limit ) { break; } if ( strchr( $filterStr, (string)$value['book_id'] ) === false ) { $res[] = $value; $c++; }else{ $filterRes[] = $value; } } //如果书籍不够limit 补全 // $o = $limit - $c; // while( $o > 0 ){ // $res[] = array_shift($filterRes); // $o--; // } unset($books); unset($filterStr); unset($recent); return $res; } /** * 过滤推荐书架 (在阅读记录里面是否有拆分书) * @param $shelf 推荐书架里面的书 * @param $filterArr 阅读记录里面的 ['book_id'=>[1111,2222,]] * @return array */ public function filterShelfBooks( $shelf, $filterArr ){ return $shelf; if ( empty($shelf) ) { return $shelf; } $bookIds = []; $filterStr = ''; if ( is_array( $filterArr ) ) { $bookIds = array_keys($filterArr); $filterStr = implode( array_values($filterArr), ',' ); } $res = []; foreach ($shelf as $key => $value) { if ( strchr( $filterStr, (string)$value['book_id'] ) === false || in_array( $value['book_id'], $bookIds ) ) { $res[] = $value; } } unset($shelf); unset($bookIds); unset($filterStr); return $res; } /** * 添加书籍时 更新书籍互斥关系 支持批量插入 * @param $args ['book_id'=>[11111,111222,33333]] * @return array * @throws \Exception */ public function insertBookRelation( $args ){ $res = [ 'code'=>200, 'msg'=>'error' ]; if ( empty($args) ) { return $args; } $bookIds = array_column($args,'book_id'); $insert = []; //得到已经存在的互斥关系 不在进行插入 $oldBookIds = []; $oldBooks = $this->BookRelation->where('book_id','in',$bookIds)->select(); if ( !empty($oldBooks) ) { foreach ($oldBooks as $k => $v) { $oldBookIds[] = $v['book_id'].'_'.$v['relation_id']; } } foreach ($args as $key => $value) { if ( isset($value['relation_id']) && is_array($value['relation_id'])) { foreach ($value['relation_id'] as $k => $v) { $temp = $value['book_id'].'_'.$v; if ( !in_array( $temp, $oldBookIds )) { $one = ['book_id'=>$value['book_id'],'relation_id'=>$v]; $insert[] = $one; } } } } if ( !empty($insert) ) { $this->BookRelation->saveAll($insert,false); $res['msg']='ok'; } return $res; } /** * 在数据库中获取一本书籍 互斥书(互斥关系的书籍 子级书 和父级的子级书) * @param $bookId * @return array|string */ public function getBookRelationById( $bookId ){ if ( $bookId == false ) { return []; } $relationIds = []; $pid = 0; //获取此书的 子级书 和 父级书 $bookSons = $this->BookRelation->table('book_relation') ->where(['book_id'=>$bookId]) ->whereor(['relation_id'=>$bookId]) ->select(); if ( !empty($bookSons) ) { foreach ($bookSons as $key => $v) { if ($v['relation_id'] == $bookId) { $relationIds[] = $v['book_id']; } $relationIds[] = $v['relation_id']; } } // if ( $pid ) { // $bookBrother = $this->BookRelation->field('relation_id') // ->where(['book_id'=>$pid]) // ->where('relation_id','<>',$bookId) // ->select(); // if ( !empty($bookBrother) ) { // $brotherIds = array_column( $bookBrother, 'relation_id' ); // $relationIds = array_merge( $relationIds, $brotherIds ); // } // } //$relationIds = implode( ',', array_unique($relationIds)); return $relationIds; } /** * @param $bookIds * @return string */ public function getBookRelationBatch( $bookIds ){ if ( empty($bookIds) ){ return ''; } $relation = []; $son = $this->BookRelation->field('relation_id')->where(['book_id'=>['in',$bookIds]])->select(); $father = $this->BookRelation->field('book_id')->where(['relation_id'=>['in',$bookIds]])->select(); if ( !empty($father) ){ $relation = array_unique( array_column( $father, 'book_id' ) ); // $brother = $this->BookRelation->field('relation_id')->where(['book_id'=>['in',$relation]])->select(); } if ( !empty($son) ){ $relation = array_unique( array_merge( $relation, array_column( $son, 'relation_id')) ); } // if ( !empty($brother) ){ // $relation =array_unique( array_merge( $relation, array_column( $brother, 'relation_id')) ); // } return implode( ',', $relation ); } }