ChapterOrder.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Models\Subscribe;
  3. use App\Models\Book\Book;
  4. use App\Models\Book\Chapter;
  5. use Illuminate\Database\Eloquent\Model;
  6. class ChapterOrder extends Model
  7. {
  8. private $uid;
  9. public function __construct($uid)
  10. {
  11. $this->uid = $uid ? $uid : 0;
  12. $this->setCurrentTable();
  13. }
  14. protected $connection = 'chapter_order_mysql';
  15. protected $fillable = [
  16. 'distribution_channel_id', 'bid', 'cid', 'chapter_name', 'uid', 'u', 'fee', 'book_name', 'send_order_id', 'charge_balance', 'reward_balance'
  17. ];
  18. public function setCurrentTable()
  19. {
  20. $this->setTable('chapter_orders' . $this->uid % 512);
  21. }
  22. /**
  23. * 判断是否章节订购
  24. */
  25. public function checkIsOrdered($bid, $cid)
  26. {
  27. return self::where('uid', $this->uid)
  28. ->where('bid', $bid)
  29. ->where('cid', $cid)
  30. ->count();
  31. }
  32. /**
  33. * 判断作品是否有章节订购
  34. */
  35. public function checkBookIsOrdered($bid)
  36. {
  37. return self::where('uid', $this->uid)
  38. ->where('bid', $bid)
  39. ->count();
  40. }
  41. public function getByUid($page_size)
  42. {
  43. $chapter_orders = self::where('uid', $this->uid)->orderBy('created_at', 'desc')->paginate($page_size);
  44. $cids = collect($chapter_orders->items())->pluck('cid');
  45. $bids = collect($chapter_orders->items())->pluck('bid');
  46. $chapters = Chapter::select('id', 'name')->whereIn('id', $cids)->get();
  47. $books = Book::select('id', 'name')->whereIn('id', $bids)->get();
  48. foreach ($chapter_orders as $k => $v) {
  49. $chapter = $chapters->where('id', $v->cid)->first();
  50. $book = $books->where('id', $v->bid)->first();
  51. if ($chapter)
  52. $v->chapter_name = $chapter->name;
  53. if ($book)
  54. $v->book_name = $book->name;
  55. $chapter_orders[$k] = $v;
  56. }
  57. return $chapter_orders;
  58. }
  59. /**
  60. * @param $uid
  61. * @return array
  62. */
  63. public function getUserAllSubBooks($uid): array
  64. {
  65. if (empty($uid)) {
  66. return [];
  67. }
  68. $result = self::select('bid')->where('uid', $uid)->groupBy('bid')->get();
  69. return $result ? $result->toArray() : [];
  70. }
  71. }