BookConfigService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. public static function getBookLists(array $where, $order = [],$is_external_shelf=true)
  60. {
  61. if (empty($where)) {
  62. return (object)array();
  63. }
  64. if ($order) {
  65. $res = BookConfig::getBookLists($where,$order,$is_external_shelf);
  66. } else{
  67. $res = BookConfig::getBookLists($where,[],$is_external_shelf);
  68. }
  69. return $res;
  70. }
  71. /**
  72. *
  73. * 根据条件获取图书
  74. * @param array $where ['key'=>'根据关键词查询','category_id'=>'根据分类id查询','is_on_shelf'=>上下架查询,'channel_name'=>'频道查询(男频女频)']
  75. * @param array $order 排序 默认是bid排序
  76. * @param int $page_size
  77. * @return mixed
  78. */
  79. public static function getBooks(array $where, array $order = [], $page_size = 15)
  80. {
  81. return BookConfig::getBooks($where, $order, $page_size);
  82. }
  83. /**
  84. * 获取阅读完的推荐
  85. * @param $category_id
  86. * @param int $num
  87. * @return mixed
  88. */
  89. public static function getRecommendBooks($bid, $category_id, $num = 4)
  90. {
  91. return BookConfig::getRecommendBooks($bid, $category_id, $num);
  92. }
  93. /**
  94. * 获取阅读完的推荐(快应用)
  95. * @param $category_id
  96. * @param int $num
  97. * @return mixed
  98. */
  99. public static function getQuickAppRecommendBooks($bid, $category_id, $num = 4)
  100. {
  101. return BookConfig::getQuickAppRecommendBooks($bid, $category_id, $num);
  102. }
  103. public static function getSimpleBooksByIds(array $ids)
  104. {
  105. $str = implode(',', $ids);
  106. $field = 'bid,' . $str;
  107. return BookConfig::whereIn('bid', $ids)->select('bid', 'book_name')->orderBy(DB::raw('field(' . $field . ')'))->get();
  108. }
  109. public static function findBookKeywords(bool $is_all = false)
  110. {
  111. $sql = BookKeyword::where('status', 1)->orderBy('sequence');
  112. if ($is_all) {
  113. return $sql->get();
  114. } else {
  115. return $sql->paginate(10);
  116. }
  117. }
  118. public static function saveUserSearchLog(string $words, int $uid)
  119. {
  120. QappUserSearchBookLog::create([
  121. 'uid' => $uid,
  122. 'words' => $words,
  123. ]);
  124. }
  125. /**
  126. * @return FreeBookConfig
  127. */
  128. public static function findFreeBookConfig(int $sex)
  129. {
  130. return FreeBookConfig::where(['sex' => $sex, 'is_enabled' => 1])
  131. ->where('end_time', '>=', now())
  132. ->orderBy('end_time')
  133. ->first();
  134. }
  135. /**
  136. * 查找限免书籍
  137. * @return array
  138. */
  139. public static function findFreeBooks(int $sex)
  140. {
  141. $config = self::findFreeBookConfig($sex);
  142. \Log::info('return_empty_free_books:'.$sex);
  143. \Log::info($config);
  144. if ($config) {
  145. $free_books = FreeBook::where('config_id', $config->id)
  146. ->where('is_enabled', 1)
  147. ->get();
  148. $bids = $free_books->pluck('bid')->all();
  149. $book_configs = BookConfig::whereIn('bid', $bids)
  150. ->where('is_on_shelf', 2)
  151. ->select('bid', 'book_name', 'cover')
  152. ->get();
  153. $books = Book::whereIn('id', $bids)->select('id', 'intro')->get();
  154. \Log::info('return_empty_data:');
  155. \Log::info($books);
  156. $book_list = $book_configs->transform(function ($item) use ($books) {
  157. $book = $books->where('id', $item->bid)->first();
  158. return [
  159. 'book_id' => Hashids::encode($item->bid),
  160. 'cover_url' => $item->cover,
  161. 'book_name' => $item->book_name,
  162. 'intro' => $book->intro,
  163. ];
  164. })->all();
  165. return [
  166. 'title' => $config->name,
  167. 'end_time' => $config->end_time,
  168. 'list' => $book_list,
  169. ];
  170. }
  171. return [];
  172. }
  173. /**
  174. * 判断书籍是否限免
  175. * @return bool
  176. */
  177. public static function judgeBookIsFree(int $bid)
  178. {
  179. $ids = [];
  180. foreach ([1, 2] as $sex) {
  181. $config = self::findFreeBookConfig($sex);
  182. if ($config) {
  183. $ids[] = $config->id;
  184. }
  185. }
  186. return FreeBook::where('bid', $bid)
  187. ->whereIn('config_id', $ids)
  188. ->where('is_enabled', 1)->select('id')->first();
  189. }
  190. public static function getByBidNoFilter($bid){
  191. return FreeBook::join('free_book_config','free_book_config.id','=','free_books.config_id')
  192. ->where('bid',$bid)
  193. ->select('free_books.id','end_time')
  194. ->where('end_time','<',date('Y-m-d H:i:s'))
  195. ->orderBy('free_book_config.end_time','desc')
  196. ->first();
  197. }
  198. public static function chargeStats($id,$amount,$uid){
  199. if(!Redis::Sismember('qapp:free:virtual:uids'.$id,$uid)){
  200. return ;
  201. }
  202. $now = date('Y-m-d');
  203. $amount = $amount*100;
  204. Redis::hincrby('qapp:book:free:charge:'.$id,$now,$amount);
  205. #Redis::sadd('qapp:free:charge'.$now,$id);
  206. Redis::sadd('qapp:free:actuality' . $now, $id);
  207. Redis::sadd('qapp:free:charge:uids'.$now.$id,$uid);
  208. }
  209. public static function getBookByField($bids,$field){
  210. if(!$bids || !$field) return null;
  211. return BookConfig::join('books','books.id','=','book_configs.bid')->
  212. whereIn('bid',$bids)->select($field)->get();
  213. }
  214. }