SuperiorNewBookService.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\SuperiorNewBooks;
  10. use DB;
  11. class SuperiorNewBookService
  12. {
  13. public static function getSuperiorCount($pid){
  14. return SuperiorNewBooks::getSuperiorTotal($pid);
  15. }
  16. public static function getLastSuperiorItem($pid) {
  17. return SuperiorNewBooks::getLastSuperiorItem($pid);
  18. }
  19. public static function addSuperiorItem($param) {
  20. $count = self::getSuperiorCount($param->pid);
  21. $sign = ['bid'=>$param->bid];
  22. $data = array(
  23. 'category_id'=>$param->category_id,
  24. 'pid'=>$param->pid,
  25. 'status'=>1,
  26. 'recommend_index'=>$param->recommend_index,
  27. 'updated_at'=>date('Y-m-d H:i:s')
  28. );
  29. //var_dump($data);
  30. \Log::info('superior_count:'.$count);
  31. $book = SuperiorNewBooks::where('bid',$param->bid)->first();
  32. //if(!$book) {
  33. if($count<20){
  34. SuperiorNewBooks::updateOrCreate($sign,$data);
  35. }else{
  36. $book_to_add = SuperiorNewBooks::where('bid',$param->bid)->first();
  37. if(!$book_to_add || ($book_to_add && $book_to_add->status==0)) {
  38. $last_item = self::getLastSuperiorItem($param->pid);
  39. $last_item->status = 0;
  40. $last_item->save();
  41. }
  42. SuperiorNewBooks::updateOrCreate($sign,$data);
  43. }
  44. //}
  45. }
  46. public static function getSuperiorList($pid='',$search=''){
  47. return SuperiorNewBooks::getSuperiorList($pid,$search);
  48. }
  49. public static function getSupriorBidList(){
  50. $books = SuperiorNewBooks::getSuperiorList('');
  51. $res = [];
  52. foreach ($books as $book){
  53. $res[] = $book->bid;
  54. }
  55. return $res;
  56. }
  57. public static function deleteOne($bid){
  58. return SuperiorNewBooks::where('bid',$bid)->update(['status'=>0]);
  59. }
  60. public static function setTop($bid){
  61. $item = SuperiorNewBooks::where('bid',$bid)->first();
  62. $max = SuperiorNewBooks::select(DB::raw('max(order_index) as max_index'))
  63. ->where('pid',$item->pid)
  64. ->first();
  65. $max_index = intval($max->max_index)+1;
  66. $item->order_index = $max_index;
  67. $item->save();
  68. }
  69. }