| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 | <?phpnamespace App\Models\Subscribe;use App\Models\Book\Book;use App\Models\Book\Chapter;use Illuminate\Database\Eloquent\Model;class ChapterOrder extends Model{    private $uid;    public function __construct($uid)    {        $this->uid = $uid ? $uid : 0;        $this->setCurrentTable();    }    protected $connection = 'chapter_order_mysql';    protected $fillable = [        'distribution_channel_id', 'bid', 'cid', 'chapter_name', 'uid', 'u', 'fee', 'book_name', 'send_order_id', 'charge_balance', 'reward_balance'    ];    public function setCurrentTable()    {        $this->setTable('chapter_orders' . $this->uid % 512);    }    /**     * 判断是否章节订购     */    public function checkIsOrdered($bid, $cid)    {        return self::where('uid', $this->uid)            ->where('bid', $bid)            ->where('cid', $cid)            ->count();    }    /**     * 判断作品是否有章节订购     */    public function checkBookIsOrdered($bid)    {        return self::where('uid', $this->uid)            ->where('bid', $bid)            ->count();    }    public function getByUid($page_size)    {        $chapter_orders = self::where('uid', $this->uid)->orderBy('created_at', 'desc')->paginate($page_size);        $cids           = collect($chapter_orders->items())->pluck('cid');        $bids           = collect($chapter_orders->items())->pluck('bid');        $chapters       = Chapter::select('id', 'name')->whereIn('id', $cids)->get();        $books          = Book::select('id', 'name')->whereIn('id', $bids)->get();        foreach ($chapter_orders as $k => $v) {            $chapter = $chapters->where('id', $v->cid)->first();            $book    = $books->where('id', $v->bid)->first();            if ($chapter)                $v->chapter_name = $chapter->name;            if ($book)                $v->book_name = $book->name;            $chapter_orders[$k] = $v;        }        return $chapter_orders;    }    /**     * @param $uid     * @return array     */    public function getUserAllSubBooks($uid): array    {        if (empty($uid)) {            return [];        }        $result = self::select('bid')->where('uid', $uid)->groupBy('bid')->get();        return $result ? $result->toArray() : [];    }}
 |