<?php

namespace App\Modules\Subscribe\Services;

use App\Modules\Subscribe\Models\BookOrder;
use DB;

class BookOrderService
{

    /**
     * 获取订购记录
     * @param $uid
     * @return mixed
     */
    public static function getRecord($uid, $page_size = 15)
    {
        return BookOrder::getRecord($uid, $page_size);
    }

    /**
     * 获取订购记录
     * @param $uid
     * @return mixed
     */
    public static function getRecordByuidBid($uid, $bid)
    {
        return BookOrder::getRecordByuidBid($uid, $bid);
    }

    /**
     * 是否订阅短片
     * @param $uid
     * @return mixed
     */
    public static function isHasBookOrder($uid){
        $result =  BookOrder::where('uid',$uid)->select('id')->first();
        if($result){
            return true;
        }
        return false;
    }

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

    static function save_book_order($data)
    {
        $insert_data = array();
        $insert_data['uid'] = $data['uid'];
        $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
        $insert_data['fee'] = $data['fee'];
        $insert_data['bid'] = $data['bid'];
        $insert_data['book_name'] = $data['book_name'];
        $insert_data['send_order_id'] = $data['send_order_id'];
        $insert_data['charge_balance'] = $data['charge_balance'];
        $insert_data['reward_balance'] = $data['reward_balance'];

        return BookOrder::save_book_order($insert_data);
    }

    /**
     * data = [uid,fee,u,distribution_channel_id,bid,book_name,send_order_id]
     * 单本订购并扣余额
     * @param $uid
     * @return mixed
     */
    public static function addOrderRecodeAndDecrUserBalance(array $data, $uid)
    {
        return BookOrder::addOrderRecodeAndDecrUserBalance($data, $uid);
    }

    public static function getBookTotalConsume($uid)
    {
        return BookOrder::where('uid', $uid)->sum('fee');
    }

    /**
     * 根据用户id获取用户的一段时间内的消费总额
     * @param $uid  用户id
     * @param string $startTime 开始时间
     * @param string $endTime 结束时间
     * @return mixed
     */
    public static function getBookTotalConsumeByUserIdAndTime($uid, $startTime = "", $endTime = "")
    {
        return BookOrder::getBookTotalConsumeByUserIdAndTime($uid, $startTime, $endTime);
    }

    /**
     * @param $send_order_id
     * @param $start_time
     * @param $end_time
     * @return array ['total'=>133,'charge_balance'=>45,'reward_balance'=>$reward_balance]
     */
    public static function getSendorderTotal($send_order_id, $start_time, $end_time)
    {
        //$charge_balance = BookOrder::where('send_order_id', $send_order_id)->whereBetween('created_at', [$start_time, $end_time])->sum('charge_balance');
        //$reward_balance = BookOrder::where('send_order_id', $send_order_id)->whereBetween('created_at', [$start_time, $end_time])->sum('reward_balance');
        //$fee = BookOrder::where('send_order_id', $send_order_id)->whereBetween('created_at', [$start_time, $end_time])->sum('fee');
        $res = BookOrder::select(DB::raw('sum(charge_balance) as charge_balance_sum,sum(reward_balance) as reward_balance_sum,sum(fee) as fee_sum'))
            ->where('send_order_id', $send_order_id)
            ->whereBetween('created_at', [$start_time, $end_time])
            ->first();
        return [
            'total' => $res->fee_sum,
            'charge_balance' => $res->charge_balance_sum,
            'reward_balance' => $res->reward_balance_sum
        ];
        //return ['total' => $fee, 'charge_balance' => $charge_balance, 'reward_balance' => $reward_balance];
    }

    public static function getOrderInfos($id_begin, $id_end)
    {
        return BookOrder::getOrderInfos($id_begin, $id_end);
    }

    public static function getCount()
    {
        return BookOrder::getOrderInfoCount();
    }
}
