BookConfigService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/4
  6. * Time: 下午1:43
  7. */
  8. namespace App\Modules\Book\Services;
  9. use App\Modules\Book\Models\BookCombinationConfig;
  10. use App\Modules\Book\Models\BookConfig;
  11. use App\Modules\Product\Services\ProductService;
  12. use App\Modules\Book\Models\Book;
  13. use App\Modules\Book\Models\BookKeyword;
  14. use App\Modules\Book\Models\FreeBook;
  15. use App\Modules\Book\Models\FreeBookConfig;
  16. use App\Modules\Book\Models\QappUserSearchBookLog;
  17. use Redis;
  18. use DB;
  19. use Vinkla\Hashids\Facades\Hashids;
  20. class BookConfigService
  21. {
  22. /**
  23. * 根据id获取图书
  24. * @param $bid
  25. * @return mixed
  26. */
  27. public static function getBookById($bid)
  28. {
  29. return BookConfig::getBookById($bid);
  30. }
  31. /**
  32. * 获取图书组合信息
  33. * @param $bid
  34. * @return array
  35. */
  36. public static function getCombinationInfo($bid)
  37. {
  38. $info = BookCombinationConfig::where('bid',$bid)->where('is_enable',1)->select('bid_group')->first();
  39. if($info) return explode(',',$info->bid_group);
  40. return [];
  41. }
  42. /**
  43. * 根据bid数组获取多本图书
  44. * @param $where
  45. * @param null $order
  46. * @return mixed
  47. */
  48. public static function getBooksByIds(array $where, $order = [],$is_external_shelf=true)
  49. {
  50. if (empty($where)) {
  51. return (object)array();
  52. }
  53. if ($order)
  54. $res = BookConfig::getBooksByIds($where,$order,$is_external_shelf);
  55. else
  56. $res = BookConfig::getBooksByIds($where,[],$is_external_shelf);
  57. return $res;
  58. }
  59. /**
  60. *
  61. * 根据条件获取图书
  62. * @param array $where ['key'=>'根据关键词查询','category_id'=>'根据分类id查询','is_on_shelf'=>上下架查询,'channel_name'=>'频道查询(男频女频)']
  63. * @param array $order 排序 默认是bid排序
  64. * @param int $page_size
  65. * @return mixed
  66. */
  67. public static function getBooks(array $where, array $order = [], $page_size = 15)
  68. {
  69. return BookConfig::getBooks($where, $order, $page_size);
  70. }
  71. /**
  72. * 获取阅读完的推荐
  73. * @param $category_id
  74. * @param int $num
  75. * @return mixed
  76. */
  77. public static function getRecommendBooks($bid, $category_id, $num = 4)
  78. {
  79. return BookConfig::getRecommendBooks($bid, $category_id, $num);
  80. }
  81. /**
  82. * 获取阅读完的推荐(快应用)
  83. * @param $category_id
  84. * @param int $num
  85. * @return mixed
  86. */
  87. public static function getQuickAppRecommendBooks($bid, $category_id, $num = 4)
  88. {
  89. return BookConfig::getQuickAppRecommendBooks($bid, $category_id, $num);
  90. }
  91. public static function getSimpleBooksByIds(array $ids)
  92. {
  93. $str = implode(',', $ids);
  94. $field = 'bid,' . $str;
  95. return BookConfig::whereIn('bid', $ids)->select('bid', 'book_name')->orderBy(DB::raw('field(' . $field . ')'))->get();
  96. }
  97. public static function findBookKeywords(bool $is_all = false)
  98. {
  99. $sql = BookKeyword::where('status', 1)->orderBy('sequence');
  100. if ($is_all) {
  101. return $sql->get();
  102. } else {
  103. return $sql->paginate(10);
  104. }
  105. }
  106. public static function saveUserSearchLog(string $words, int $uid)
  107. {
  108. QappUserSearchBookLog::create([
  109. 'uid' => $uid,
  110. 'words' => $words,
  111. ]);
  112. }
  113. /**
  114. * @return FreeBookConfig
  115. */
  116. public static function findFreeBookConfig(int $sex)
  117. {
  118. return FreeBookConfig::where(['sex' => $sex, 'is_enabled' => 1])
  119. ->where('end_time', '>=', now())
  120. ->orderBy('end_time')
  121. ->first();
  122. }
  123. /**
  124. * 查找限免书籍
  125. * @return array
  126. */
  127. public static function findFreeBooks(int $sex)
  128. {
  129. $config = self::findFreeBookConfig($sex);
  130. if ($config) {
  131. $free_books = FreeBook::where('config_id', $config->id)
  132. ->where('is_enabled', 1)
  133. ->get();
  134. $bids = $free_books->pluck('bid')->all();
  135. $book_configs = BookConfig::whereIn('bid', $bids)
  136. ->where('is_on_shelf', 2)
  137. ->select('bid', 'book_name', 'cover')
  138. ->get();
  139. $books = Book::whereIn('id', $bids)->select('id', 'intro')->get();
  140. $book_list = $book_configs->transform(function ($item) use ($books) {
  141. $book = $books->where('id', $item->bid)->first();
  142. return [
  143. 'book_id' => Hashids::encode($item->bid),
  144. 'cover_url' => $item->cover,
  145. 'book_name' => $item->book_name,
  146. 'intro' => $book->intro,
  147. ];
  148. })->all();
  149. return [
  150. 'title' => $config->name,
  151. 'end_time' => $config->end_time,
  152. 'list' => $book_list,
  153. ];
  154. }
  155. return [];
  156. }
  157. /**
  158. * 判断书籍是否限免
  159. * @return bool
  160. */
  161. public static function judgeBookIsFree(int $bid)
  162. {
  163. $ids = [];
  164. foreach ([1, 2] as $sex) {
  165. $config = self::findFreeBookConfig($sex);
  166. if ($config) {
  167. $ids[] = $config->id;
  168. }
  169. }
  170. return FreeBook::where('bid', $bid)
  171. ->whereIn('config_id', $ids)
  172. ->where('is_enabled', 1)->select('id')->first();
  173. }
  174. public static function getByBidNoFilter($bid){
  175. return FreeBook::join('free_book_config','free_book_config.id','=','free_books.config_id')
  176. ->where('bid',$bid)
  177. ->select('free_books.id','end_time')
  178. ->where('end_time','<',date('Y-m-d H:i:s'))
  179. ->orderBy('free_book_config.end_time','desc')
  180. ->first();
  181. }
  182. public static function chargeStats($id,$amount,$uid){
  183. if(!Redis::Sismember('qapp:free:virtual:uids'.$id,$uid)){
  184. return ;
  185. }
  186. $now = date('Y-m-d');
  187. $amount = $amount*100;
  188. Redis::hincrby('qapp:book:free:charge:'.$id,$now,$amount);
  189. #Redis::sadd('qapp:free:charge'.$now,$id);
  190. Redis::sadd('qapp:free:actuality' . $now, $id);
  191. Redis::sadd('qapp:free:charge:uids'.$now.$id,$uid);
  192. }
  193. public static function getBookByField($bids,$field){
  194. if(!$bids || !$field) return null;
  195. return BookConfig::whereIn('bid',$bids)->select($field)->get();
  196. }
  197. }