<?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();
    }

    public static function chargeGiveGift($property,$price,$charge_type){
        if(!$property) $property = 'none';
        $gift_info = [
            1=>['name_desc'=>'黄金玫瑰','cost'=>200,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/3.png'],
            2=>['name_desc'=>'文学奖','cost'=>800,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/5.png'],
            3=>['name_desc'=>'锦囊','cost'=>500,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/4.png'],
            4=>['name_desc'=>'小皮鞭','cost'=>200,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/6.png'],
            5=>['name_desc'=>'催更票','cost'=>100,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/1.png'],
            6=>['name_desc'=>'点个赞','cost'=>50,'icon'=>'https://yqcdn.iycdm.com/wap/present/20181016/2.png'],
            7=>['name_desc'=>'文房四宝','cost'=>100,'icon'=>'https://cdn-novel.iycdm.com/h5/20181107/four_literature_tools.png'],
            8=>['name_desc'=>'至尊大神','cost'=>1800,'icon'=>'https://cdn-novel.iycdm.com/h5/20181107/literature_emprior.png'],
        ];
        if($property == 'none' || $property == 'low'){
            $gift = [];
            if($price <2){
                return [];
            }
            if($price <18 && $price >=2){
                $gift = [['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']];
            }elseif ($price <30){
                $gift = [['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']];
            }elseif($price < 50 ){
                $gift = [
                    ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
                ];
            }elseif ($price <100){
                $gift = [
                    ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
                ];
            }elseif ($price <200){
                $gift = [
                    ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
                ];
            }elseif ($price >=200){
                $gift = [
                    ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>8,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
                ];
            }
            if($charge_type == 'YEAR_ORDER'){
                $gift = [
                    ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>8,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
                ];
            }
            if($gift){
                foreach ($gift as &$item){
                    $item['name_desc'] = $gift_info[$item['gift_id']]['name_desc'];
                    $item['cost'] = $gift_info[$item['gift_id']]['cost'];
                    $item['icon'] = $gift_info[$item['gift_id']]['icon'];
                }
            }
            return $gift;
        }

        if($property == 'high' || $property == 'medium'){
            $gift = [];
            if($price < 18){
                return [];
            }
            if($price <30 && $price >=18){
                $gift = [
                    ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
                ];
            }elseif($price < 50 ){
                $gift = [
                    ['gift_id'=>6,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>'']
                ];
            }elseif ($price <100){
                $gift = [
                    ['gift_id'=>6,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>7,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
                ];
            }elseif ($price <200){
                $gift = [
                    ['gift_id'=>7,'num'=>3,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
                ];

            }elseif ($price >=200){
                $gift = [
                    ['gift_id'=>7,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>3,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>2,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>8,'num'=>1,'name_desc'=>'','cost'=>'','icon'=>'']
                ];
            }
            if($charge_type == 'YEAR_ORDER'){
                $gift = [
                    ['gift_id'=>6,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>7,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>1,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>3,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>2,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>''],
                    ['gift_id'=>8,'num'=>2,'name_desc'=>'','cost'=>'','icon'=>'']
                ];
            }
            if($gift){
                foreach ($gift as &$item){
                    $item['name_desc'] = $gift_info[$item['gift_id']]['name_desc'];
                    $item['cost'] = $gift_info[$item['gift_id']]['cost'];
                    $item['icon'] = $gift_info[$item['gift_id']]['icon'];
                }
            }
            return $gift;
        }

        return [];
    }
}