CpExceptBookService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhao
  5. * Date: 2017/11/17
  6. * Time: 下午4:44
  7. */
  8. namespace App\Modules\Book\Services;
  9. use App\Modules\Book\Models\BookConfig;
  10. use App\Modules\Book\Models\CpExceptBook;
  11. use DB;
  12. class CpExceptBookService
  13. {
  14. /**
  15. * 获取列表
  16. * @param array $where
  17. */
  18. public static function getList(array $where=[]){
  19. $res = CpExceptBook::join('cp_users','cp_users.id','=','cp_except_book.cp_user_id')
  20. ->select('cp_except_book.id','cp_except_book.bid','cp_except_book.book_name','cp_users.username');
  21. if(isset($where['username']) && !empty($where['username'])){
  22. $res->where('cp_users.username',$where['username']);
  23. }
  24. return $res->paginate(20);
  25. }
  26. /**
  27. * 根据cp和书名获取图书信息
  28. * @param string $cp
  29. * @param string $name
  30. * @return mixed
  31. */
  32. public static function getBookByCp(string $cp,string $name){
  33. return BookConfig::join('books','book_configs.bid','=','books.id')->select('books.name','books.id')
  34. ->where('book_configs.cp_source',$cp)
  35. ->where('books.name','like','%'.$name.'%')
  36. ->whereNotExists(function ($query){
  37. $query->select('bid')->from('cp_except_book')->whereRaw('book_configs.bid=cp_except_book.bid');
  38. })
  39. ->get();
  40. }
  41. /**
  42. * 添加
  43. * @param array $info
  44. * @return mixed
  45. */
  46. public static function create(array $info){
  47. return CpExceptBook::create($info);
  48. }
  49. /**
  50. * 获取派出的bid
  51. * @param $id
  52. * @return array
  53. */
  54. public static function getCpBidList(int $id):array {
  55. if($id == 1){
  56. return CpExceptBook::select('bid')->get()->pluck('bid')->all();
  57. }
  58. return CpExceptBook::where('cp_user_id',$id)->select('bid')->get()->pluck('bid')->all();
  59. }
  60. /**
  61. * 是否已经加入排除
  62. * @param int $cp_user_id
  63. * @param int $bid
  64. * @return bool
  65. */
  66. public static function isHadExcept(int $cp_user_id,int $bid){
  67. $exist = CpExceptBook::where('cp_user_id',$cp_user_id)->where('bid',$bid)->first();
  68. if($exist)
  69. return true;
  70. else
  71. return false;
  72. }
  73. /***
  74. * 获取所有cp
  75. * @return mixed
  76. */
  77. public static function getAllcp(){
  78. return DB::table('cp_users')->select('username','id')->get();
  79. }
  80. public static function deleteExcept(int $id){
  81. CpExceptBook::where('id',$id)->delete();
  82. }
  83. public static function getBookCpList($oldname='',$newname=''){
  84. $where = [];
  85. if($oldname){
  86. $where[] = ['books.name','like','%'.trim($oldname).'%'];
  87. }
  88. if($newname){
  89. $where[] = ['book_configs.book_name','like','%'.trim($newname).'%'];
  90. }
  91. return BookConfig::join('books','books.id','=','book_configs.bid')
  92. ->select('books.id','books.ly_bid','books.name as oldname','book_configs.book_name as newname','book_configs.cp_source','book_configs.is_on_shelf')
  93. ->where($where)
  94. ->paginate(20);
  95. }
  96. public static function editBookCp($bid,$cpid,$cpname){
  97. BookConfig::where('bid',$bid)->update(['cp_source'=>$cpname]);
  98. DB::table('cp_subs')->where('bid',$bid)->update([
  99. 'cp_user_id'=>$cpid,
  100. 'cp_source'=>$cpname,
  101. 'updated_at'=>date('Y-m-d H:i:s')
  102. ]);
  103. }
  104. public static function cpDataStats($where,$whereIn,$group,$is_all=false){
  105. $result = DB::table('book_order_statistical')->join('book_configs','book_order_statistical.bid','book_configs.bid')
  106. ->where($where);
  107. if($whereIn){
  108. $result->where('book_configs.bid',$whereIn);
  109. }
  110. if($group){
  111. $result->select(
  112. 'book_configs.bid',
  113. 'book_configs.book_name',
  114. 'book_configs.cp_source',
  115. 'book_order_statistical.day',
  116. 'book_order_statistical.charge_balance',
  117. DB::raw('left(book_order_statistical.day,7) as month'),
  118. DB::raw('left(book_order_statistical.day,4) as year'));
  119. foreach ($group as $v){
  120. $result->groupBy($v);
  121. }
  122. //return $result;
  123. }else{
  124. $result->select(
  125. 'book_configs.bid',
  126. 'book_configs.book_name',
  127. 'book_configs.cp_source',
  128. 'book_order_statistical.day',
  129. 'book_order_statistical.charge_balance',
  130. DB::raw('left(book_order_statistical.day,7) as month'),
  131. DB::raw('left(book_order_statistical.day,4) as year')
  132. );
  133. if($is_all){
  134. return $result->limit(10000)->get();
  135. }else{
  136. return $result->paginate(20);
  137. }
  138. }
  139. }
  140. }