<?php

namespace App\Modules\Subscribe\Models;

use Illuminate\Database\Eloquent\Model;
use DB;
use App\Modules\User\Services\UserService;

class BookOrder extends Model
{
    protected $table = 'book_orders';
    protected $fillable = ['distribution_channel_id', 'bid', 'book_name', 'uid', 'u', 'fee', 'send_order_id', 'charge_balance', 'reward_balance'];

    /**
     * 获取订购记录
     * @param $uid
     * @return mixed
     */
    public static function getRecord($uid, $page_size = 15)
    {
        return self::where('uid', $uid)->select('id', 'bid', 'book_name', 'u', 'distribution_channel_id', 'fee', 'uid', 'created_at')->orderBy('created_at', 'desc')->paginate($page_size);
    }


    /**
     * 根据用户id获取用户的订购记录
     * @param $uid  用户id
     * @param string $startTime 开始时间
     * @param string $endTime 结束时间
     * @param bool $isAll 获取所有
     * @return mixed
     */
    public static function getRecordByUid($uid, $startTime = "", $endTime = "", $isAll = false)
    {
        $search_object = self::where('uid', $uid)->orderBy('updated_at', 'desc');
        if ($startTime) {
            $search_object->where('updated_at', '>=', $startTime);
        }
        if ($endTime) {
            $search_object->where('updated_at', '<=', $endTime . ' 23:59:59');
        }

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


    /**
     * 根据用户id获取用户的一段时间内的消费总额
     * @param $uid  用户id
     * @param string $startTime 开始时间
     * @param string $endTime 结束时间
     * @return mixed
     */
    public static function getBookTotalConsumeByUserIdAndTime($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');
    }

    /**
     * 获取订购记录
     * @param $uid
     * @return mixed
     */
    public static function getRecordByuidBid($uid, $bid)
    {
        return self::where('uid', $uid)->where('bid', $bid)->select('bid', 'book_name', 'u', 'distribution_channel_id', 'fee', 'uid')->first();
    }

    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 self::firstOrCreate($insert_data);
    }

    public static function addOrderRecodeAndDecrUserBalance($data, $uid)
    {
        DB::beginTransaction();
        $res = $result1 = $result2 = $result = null;
        try {
            $user = UserService::getById($uid);
            if ($user->charge_balance >= $data['fee']) {
                $data['charge_balance'] = $data['fee'];
                $data['reward_balance'] = 0;
                $res = BookOrder::create($data);
                //$result1 = $user->decrement('balance', $data['fee']);
                //$result2 = $user->decrement('charge_balance', $data['fee']);
                $user->balance -=   $data['fee'];
                $user->charge_balance -=   $data['fee'];
            } elseif ($user->charge_balance > 0) {

                $data['charge_balance'] = $user->charge_balance;
                $data['reward_balance'] = $data['fee'] - $user->charge_balance;
                $res = BookOrder::create($data);
                //$result1 = $user->decrement('balance', $data['fee']);
                //$result2 = $user->decrement('charge_balance', $user->charge_balance);
                //$result3 = $user->decrement('reward_balance', $data['reward_balance']);
                $user->balance -=   $data['fee'];
                $user->charge_balance =   0;
                $user->reward_balance -=   $data['reward_balance'];
            } else {
                $data['charge_balance'] = 0;
                $data['reward_balance'] = $data['fee'];
                $res = BookOrder::create($data);
                //$result1 = $user->decrement('balance', $data['fee']);
                //$result2 = $user->decrement('reward_balance', $data['fee']);
                $user->balance -=   $data['fee'];
                $user->reward_balance -=   $data['fee'];
            }
            if ($user->is_new == 1) {
                $user->is_new = 0;
            }
            $result = $user->save();

        } catch (\Exception $e) {
            \Log::info($e);
        }

        if ($res && $result) {
            DB::commit();
            return true;
        }
        DB::rollback();
        return false;
    }

    public static function getOrderInfos($id_begin, $id_end)
    {
        return self::select('uid', 'bid')->where('id', '>=', $id_begin)->where('id', '<', $id_end)->get();
    }

    public static function getOrderInfoCount()
    {
        return self::max('id');
    }
}
