<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/4
 * Time: 上午11:49
 */

namespace App\Modules\Book\Services;

use App\Modules\Book\Models\BookGifts;
use App\Modules\Book\Models\BookGiftsSend;
use App\Modules\BookGifts\Models\BookGiftsStatsByBook;
use App\Modules\BookGifts\Models\BookGiftsStatsByGift;
use App\Modules\User\Services\UserService;
use DB;
use Redis;

class BookGiftsService
{
    public static function getAllGifts() {
        $gifts = Redis::get('BookGifts:');
        if($gifts){
            $gifts = unserialize($gifts);
        }else{
            $gifts = BookGifts::getAllGifts();
            Redis::set('BookGifts:',serialize($gifts));
            Redis::expire('BookGifts:',86400);
        }
        return $gifts;
    }

    public static function getOneGift($id) {
        return BookGifts::find($id);
    }

    public static function sendGiftToBook($param) {
        DB::beginTransaction();
        try{

            $coin_paid = self::payCoin($param['cost'],$param['uid']);
            if($coin_paid) {
                $param['cost_reward'] =$coin_paid['decrease_reward'];
                $param['cost_recharge'] =$coin_paid['decrease_recharge'];
            }
            $BookGifts = BookGiftsSend::addGift($param);
            if(!$BookGifts || !$coin_paid) {
                DB::rollback();
                return false;
            };
        }catch (\Exception $e){
            DB::rollback();
            \Log::error('sendGiftForBook failed:'.$e->getMessage());
            return false;
        }
        DB::commit();
        return $BookGifts;
    }

    public static function getSendRecords($bid) {
        return BookGiftsSend::getSendRecords($bid);
    }

    public static function getSendRecordsV2($bid) {
        return BookGiftsSend::getSendRecordsV2($bid);
    }

    public static function getConsumeSendRecords($uid) {
        return BookGiftsSend::getConsumeSendRecords($uid);
    }

    protected static function payCoin($fee,$uid) {
        //DB::beginTransaction();
        $res = $result1 = $result2 = null;

        $user = UserService::getById($uid);
        if($user->balance < $fee){
            return false;
        }
        $decrease_recharge = 0;
        $decrease_reward =0;
        if ($user->charge_balance >= $fee) {
            $data['charge_balance'] = $fee;
            $charge_fee = $fee;
            $data['reward_balance'] = 0;

            $result1 = $user->decrement('balance', $fee);
            $result2 = $user->decrement('charge_balance', $fee);
            $decrease_recharge = $fee;
        } elseif ($user->charge_balance > 0) {

            $data['charge_balance'] = $user->charge_balance;
            $charge_fee = $user->charge_balance;
            $reword_fee = $data['reward_balance'] = $fee - $user->charge_balance;

            $result1 = $user->decrement('balance', $fee);
            $result2 = $user->decrement('charge_balance', $charge_fee);
            $result3 = $user->decrement('reward_balance', $reword_fee);
            $decrease_recharge = $charge_fee;
            $decrease_reward =$reword_fee;
        } else {
            $data['charge_balance'] = 0;
            $reword_fee = $data['reward_balance'] = $fee;

            $result1 = $user->decrement('balance', $fee);
            $result2 = $user->decrement('reward_balance', $reword_fee);
            $decrease_reward =$reword_fee;
        }

        if ($result2 && $result1) {
            //DB::commit();
            return compact('decrease_recharge','decrease_reward','fee');
            //return true;
        }
        //DB::rollback();
        return false;
    }

    public static function getGiftsSendStatisticByBook($start,$end){
        return BookGiftsSend::getGiftsSendStatisticByBook($start,$end);
    }
    public static function getGiftsSendStatisticByGift($start,$end){
        return BookGiftsSend::getGiftsSendStatisticByGift($start,$end);
    }

    public static function getGiftsSendDaillyStatsByBook($start='',$end='') {
        return BookGiftsStatsByBook::getStats($start,$end);
    }
    public static function getGiftsSendDaillyStatsByGift() {
        return BookGiftsStatsByGift::getStats();
    }


}