CompanySpecialBookService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2018/3/23
  6. * Time: 上午11:10
  7. */
  8. namespace App\Modules\Channel\Services;
  9. use App\Modules\Channel\Models\CompanySpecialBook;
  10. class CompanySpecialBookService
  11. {
  12. /**
  13. * 查询
  14. * @param $bid
  15. * @param $company_id
  16. * @return mixed
  17. */
  18. public static function findBookCompany($bid, $company_id) {
  19. return CompanySpecialBook::where('bid', $bid)->where('company_id', $company_id)->first();
  20. }
  21. /**
  22. * @param $bid
  23. * @param $company_id
  24. * @return mixed
  25. */
  26. public static function addBook($bid, $company_id) {
  27. $obj = self::findBookCompany($bid, $company_id);
  28. if(!$obj) {
  29. $data = ['bid' => $bid, 'company_id' => $company_id];
  30. return CompanySpecialBook::create($data);
  31. }
  32. return $obj;
  33. }
  34. /**
  35. * @param $bid
  36. * @param $company_id
  37. */
  38. public static function rmBook($bid, $company_id) {
  39. $obj = self::findBookCompany($bid, $company_id);
  40. if($obj) {
  41. $obj->delete();
  42. }
  43. }
  44. /**
  45. * 删除公司对应所有书籍信息
  46. * @param $company_id
  47. */
  48. public static function rmCompany($company_id) {
  49. CompanySpecialBook::where('company_id', $company_id)->delete();
  50. }
  51. /**
  52. * 删除所有
  53. * @param $bid
  54. */
  55. public static function rmAllBook($bid) {
  56. CompanySpecialBook::where('bid', $bid)->delete();
  57. }
  58. /**
  59. * 更具书籍获取公司
  60. * @param $bid
  61. * @return mixed
  62. */
  63. public static function findCompanyToBid($bid) {
  64. $sqlObj = CompanySpecialBook::select('companies.id', 'companies.name', 'companies.is_important', 'companies.created_at')
  65. ->leftJoin('companies', 'companies.id', '=', 'company_special_books.company_id')
  66. ->where('company_special_books.bid', $bid);
  67. return $sqlObj->get();
  68. }
  69. /**
  70. * 根据company_id获取图书
  71. * @param $company_id
  72. * @return array
  73. */
  74. public static function getBidByCompany($company_id){
  75. $data= CompanySpecialBook::where('company_id', $company_id)->select('bid')->get();
  76. $bids=[];
  77. foreach ($data as $item){
  78. $bids[]=$item->bid;
  79. }
  80. return $bids;
  81. }
  82. /**
  83. * 获取内部上架书籍ID
  84. * @return array|null
  85. */
  86. public static function getBidByFirst() {
  87. // $data = CompanySpecialBook::first();
  88. // if($data) {
  89. // return self::getBidByCompany($data->company_id);
  90. // }
  91. // return null;
  92. $data= CompanySpecialBook::groupBy('bid')->orderBy('bid')->get();
  93. $bids=[];
  94. foreach ($data as $item){
  95. $bids[]=$item->bid;
  96. }
  97. return $bids;
  98. }
  99. }