<?php

namespace App\Modules\Subscribe\Models;

use Illuminate\Database\Eloquent\Model;

class ChapterOrder extends Model
{

    protected $connection = 'chapter_order_mysql';

    protected $fillable = [
        'distribution_channel_id', 'bid', 'cid', 'chapter_name', 'book_name', 'uid', 'u', 'fee', 'book_name', 'send_order_id', 'charge_balance', 'reward_balance'
    ];

    function  setCurrentTable($uid)
    {
        $this->setTable('chapter_orders' . $uid % 512);
    }

    //判断是否订购
    function checkIsOrdered($uid, $bid, $cid)
    {
        return self::where('uid', $uid)
            ->where('bid', $bid)
            ->where('cid', $cid)
            ->count();
    }

    function getByUid($uid, $page_size)
    {
        return self::where('uid', $uid)->orderBy('created_at', 'desc')->paginate($page_size);
    }

    public function getChapterOrderLists($uid, $page_size)
    {
        return $this->where('uid', $uid)->orderBy('created_at', 'desc')->paginate($page_size);
    }
    public static function getChapterTotalConsumeByUserIdAndTime($uid, $startTime = "", $endTime = "")
    {
        $search_object = self::where('uid', $uid);
        if ($startTime) {
            $search_object->where('updated_at', '>=', $startTime);
        }
        if ($endTime) {
            $search_object->where('updated_at', '<=', $endTime . ' 23:59:59');
        }
        return $search_object->sum('fee');
    }
}
