BookRelationService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * user wudd
  4. * date 20191011
  5. * description 拆分书籍关系
  6. */
  7. namespace app\common\service;
  8. use app\common\library\Redis;
  9. use app\common\model\BookRelation;
  10. use think\Db;
  11. use think\Cookie;
  12. class BookRelationService
  13. {
  14. /**
  15. * @var BookRelation
  16. */
  17. private $BookRelation;
  18. private static $self;
  19. public function __construct(){
  20. if ( !$this->BookRelation ) {
  21. $this->BookRelation = model('BookRelation');
  22. }
  23. }
  24. public static function instance()
  25. {
  26. if(self::$self == NULL){
  27. self::$self = new self();
  28. }
  29. return self::$self;
  30. }
  31. /**
  32. * 猜你喜欢 ---在批量书籍 有拆分关系的书籍
  33. * @param $books
  34. * @param int $limit
  35. * @return array
  36. */
  37. public function filterLikeBookRelation( $books, $limit=0 ){
  38. // || count($books) <= $limit
  39. return $books;
  40. if ( empty( $books ) ) {
  41. return $books;
  42. }
  43. $res = [];
  44. $filterStr = '';
  45. /**@return getRecentlyRead */
  46. if (Cookie::has('user_id') || Cookie::has('token') ) {
  47. $recent = model('UserRecentlyRead')->getRecentlyRead(0, 20, null, true);
  48. }else{
  49. return $books;
  50. }
  51. if ( !empty($recent['data']) ) {
  52. $filterStr .= implode( array_unique( array_column($recent['data'], 'book_id') ), ',' );
  53. $filterStr .= implode( array_unique( array_column($recent['data'], 'relation_id') ), ',' );
  54. }
  55. if ( $filterStr == '' ) {
  56. return $books;
  57. }
  58. $c = 0;//统计过滤后书籍的数量
  59. foreach ($books as $key => $value) {
  60. if ( $c == $limit ) {
  61. break;
  62. }
  63. if ( strchr( $filterStr, (string)$value['book_id'] ) === false ) {
  64. $res[] = $value;
  65. $c++;
  66. }else{
  67. $filterRes[] = $value;
  68. }
  69. }
  70. //如果书籍不够limit 补全
  71. // $o = $limit - $c;
  72. // while( $o > 0 ){
  73. // $res[] = array_shift($filterRes);
  74. // $o--;
  75. // }
  76. unset($books);
  77. unset($filterStr);
  78. unset($recent);
  79. return $res;
  80. }
  81. /**
  82. * 过滤推荐书架 (在阅读记录里面是否有拆分书)
  83. * @param $shelf 推荐书架里面的书
  84. * @param $filterArr 阅读记录里面的 ['book_id'=>[1111,2222,]]
  85. * @return array
  86. */
  87. public function filterShelfBooks( $shelf, $filterArr ){
  88. return $shelf;
  89. if ( empty($shelf) ) {
  90. return $shelf;
  91. }
  92. $bookIds = [];
  93. $filterStr = '';
  94. if ( is_array( $filterArr ) ) {
  95. $bookIds = array_keys($filterArr);
  96. $filterStr = implode( array_values($filterArr), ',' );
  97. }
  98. $res = [];
  99. foreach ($shelf as $key => $value) {
  100. if ( strchr( $filterStr, (string)$value['book_id'] ) === false || in_array( $value['book_id'], $bookIds ) ) {
  101. $res[] = $value;
  102. }
  103. }
  104. unset($shelf);
  105. unset($bookIds);
  106. unset($filterStr);
  107. return $res;
  108. }
  109. /**
  110. * 添加书籍时 更新书籍互斥关系 支持批量插入
  111. * @param $args ['book_id'=>[11111,111222,33333]]
  112. * @return array
  113. * @throws \Exception
  114. */
  115. public function insertBookRelation( $args ){
  116. $res = [
  117. 'code'=>200,
  118. 'msg'=>'error'
  119. ];
  120. if ( empty($args) ) {
  121. return $args;
  122. }
  123. $bookIds = array_column($args,'book_id');
  124. $insert = [];
  125. //得到已经存在的互斥关系 不在进行插入
  126. $oldBookIds = [];
  127. $oldBooks = $this->BookRelation->where('book_id','in',$bookIds)->select();
  128. if ( !empty($oldBooks) ) {
  129. foreach ($oldBooks as $k => $v) {
  130. $oldBookIds[] = $v['book_id'].'_'.$v['relation_id'];
  131. }
  132. }
  133. foreach ($args as $key => $value) {
  134. if ( isset($value['relation_id']) && is_array($value['relation_id'])) {
  135. foreach ($value['relation_id'] as $k => $v) {
  136. $temp = $value['book_id'].'_'.$v;
  137. if ( !in_array( $temp, $oldBookIds )) {
  138. $one = ['book_id'=>$value['book_id'],'relation_id'=>$v];
  139. $insert[] = $one;
  140. }
  141. }
  142. }
  143. }
  144. if ( !empty($insert) ) {
  145. $this->BookRelation->saveAll($insert,false);
  146. $res['msg']='ok';
  147. }
  148. return $res;
  149. }
  150. /**
  151. * 在数据库中获取一本书籍 互斥书(互斥关系的书籍 子级书 和父级的子级书)
  152. * @param $bookId
  153. * @return array|string
  154. */
  155. public function getBookRelationById( $bookId ){
  156. if ( $bookId == false ) {
  157. return [];
  158. }
  159. $relationIds = [];
  160. $pid = 0;
  161. //获取此书的 子级书 和 父级书
  162. $bookSons = $this->BookRelation->table('book_relation')
  163. ->where(['book_id'=>$bookId])
  164. ->whereor(['relation_id'=>$bookId])
  165. ->select();
  166. if ( !empty($bookSons) ) {
  167. foreach ($bookSons as $key => $v) {
  168. if ($v['relation_id'] == $bookId) {
  169. $relationIds[] = $v['book_id'];
  170. }
  171. $relationIds[] = $v['relation_id'];
  172. }
  173. }
  174. // if ( $pid ) {
  175. // $bookBrother = $this->BookRelation->field('relation_id')
  176. // ->where(['book_id'=>$pid])
  177. // ->where('relation_id','<>',$bookId)
  178. // ->select();
  179. // if ( !empty($bookBrother) ) {
  180. // $brotherIds = array_column( $bookBrother, 'relation_id' );
  181. // $relationIds = array_merge( $relationIds, $brotherIds );
  182. // }
  183. // }
  184. //$relationIds = implode( ',', array_unique($relationIds));
  185. return $relationIds;
  186. }
  187. /**
  188. * @param $bookIds
  189. * @return string
  190. */
  191. public function getBookRelationBatch( $bookIds ){
  192. if ( empty($bookIds) ){
  193. return '';
  194. }
  195. $relation = [];
  196. $son = $this->BookRelation->field('relation_id')->where(['book_id'=>['in',$bookIds]])->select();
  197. $father = $this->BookRelation->field('book_id')->where(['relation_id'=>['in',$bookIds]])->select();
  198. if ( !empty($father) ){
  199. $relation = array_unique( array_column( $father, 'book_id' ) );
  200. // $brother = $this->BookRelation->field('relation_id')->where(['book_id'=>['in',$relation]])->select();
  201. }
  202. if ( !empty($son) ){
  203. $relation = array_unique( array_merge( $relation, array_column( $son, 'relation_id')) );
  204. }
  205. // if ( !empty($brother) ){
  206. // $relation =array_unique( array_merge( $relation, array_column( $brother, 'relation_id')) );
  207. // }
  208. return implode( ',', $relation );
  209. }
  210. }