123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- /**
- * user wudd
- * date 20191011
- * description 拆分书籍关系
- */
- namespace app\common\service;
- use app\common\library\Redis;
- use app\common\model\BookRelation;
- use think\Db;
- use think\Cookie;
- class BookRelationService
- {
- /**
- * @var BookRelation
- */
- private $BookRelation;
- private static $self;
- public function __construct(){
- if ( !$this->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 );
- }
- }
|