<?php

namespace App\Services\Subscribe;

use App\Models\Subscribe\ChapterOrder;
use App\Models\User\User;
use App\Modules\User\Services\UserService;
use Illuminate\Support\Facades\DB;

class ChapterOrderService
{
    //判断是否订购
    public static function checkIsOrdered($uid, $bid, $cid)
    {
        return (new ChapterOrder($uid))->checkIsOrdered($bid, $cid);
    }

    //判断本书是否订购过
    public static function checkBookIsOrdered($uid, $bid)
    {
        return (new ChapterOrder($uid))->checkBookIsOrdered($bid);
    }

    /**
     * 根据uid章节订购信息
     * @return
     */
    public function getByUid($uid, $page_size)
    {
        return (new ChapterOrder($uid))->getByUid($page_size);
    }

    /**
     * 获取用户订阅消费总额
     * @param $uid
     * @return mixed
     */
    public function getUserCostSum($uid)
    {
        $chapter_model = new ChapterOrder($uid);
        return $chapter_model->where('uid', $uid)->sum('fee');
    }


    /**
     * 根据用户id获取用户的订购记录
     * @param        $uid  用户id
     * @param string $startTime 开始时间
     * @param string $endTime 结束时间
     * @param bool   $isAll 获取所有
     * @return mixed
     */
    public static function getRecordByUid($uid, $startTime = "", $endTime = "", $isAll = false)
    {
        $chapter_model = new ChapterOrder($uid);

        $chapter = $chapter_model->where('uid', $uid)->orderBy('updated_at', 'desc');
        if ($startTime) {
            $chapter->where('updated_at', '>=', $startTime);
        }
        if ($endTime) {
            $chapter->where('updated_at', '<=', $endTime . ' 23:59:59');
        }

        if ($isAll) {
            return $chapter->get();
        } else {
            return $chapter->paginate();
        }
    }


    /**
     * 获取章节消费总额
     * @param $uid
     * @return mixed
     */
    public static function getChapterTotalConsume($uid)
    {
        $chapter_model = new ChapterOrder($uid);
        return $chapter_model->where('uid', $uid)->sum('fee');
    }


    /**
     * 获取章节消费总额
     * @param $uid
     * @return mixed
     */
    public static function getChapterTotalConsumeByUserIdAndTime($uid, $startTime = "", $endTime = "")
    {
        $chapter_model = new ChapterOrder($uid);

        $chapter = $chapter_model->where('uid', $uid);
        if ($startTime) {
            $chapter->where('updated_at', '>=', $startTime);
        }
        if ($endTime) {
            $chapter->where('updated_at', '<=', $endTime . ' 23:59:59');
        }
        return $chapter->sum('fee');
    }


    /**
     * 获取订购章节数
     * @param $uid
     * @return mixed
     */
    public static function getUserRecordCount($uid)
    {
        $chapter_model = new ChapterOrder($uid);
        return $chapter_model->where('uid', $uid)->count();
    }

    /**
     * 根据uid章节订购信息
     * @return
     */
    public static function addChapterOrder($data)
    {
        $chapterOrder                           = new ChapterOrder($data['uid']);
        $chapterOrder->uid                      = $data['uid'];
        $chapterOrder->bid                      = $data['bid'];
        $chapterOrder->cid                      = $data['cid'];
        $chapterOrder->fee                      = $data['fee'];
        $chapterOrder->chapter_name             = $data['chapter_name'];
        $chapterOrder->book_name                = $data['book_name'];
        $chapterOrder->send_order_id            = $data['send_order_id'];
        $chapterOrder->distribution_channel_id  = $data['distribution_channel_id'];
        $chapterOrder->charge_balance           = $data['charge_balance'];
        $chapterOrder->reward_balance           = $data['reward_balance'];
        return $chapterOrder->save();
    }

    public static function getRecentChapterOrderCount(int $uid)
    {
        $chapterOrder = new ChapterOrder($uid);
        $count        = $chapterOrder->where('uid', $uid)->where('created_at', '>=', date('Y-m-d', strtotime('-1 month')))->count();
        return $count;
    }
}
