<?php

namespace App\Http\Controllers\Wap\Web;

use App\Modules\Activity\Services\ActivityService;
use Illuminate\Http\Request;
use App\Http\Controllers\Wap\BaseController;
use Redis;
use DB;

class WelfareController extends BaseController
{

    //书币赠送
    public function giving(Request $request){

        $uid = $this->uid;
        $distribution_channel_id = $this->distribution_channel_id;
        $info = '';
        $token = trim($request->input('token'));
        if(!$token)
        {
            return response('参数有误');
        }

        $giving = DB::table('book_coin_givings')->where('token',$token)->first();
        if(!$giving || $giving->distribution_channel_id != $distribution_channel_id)
        {
            return response('参数有误');
        }

        //判断是否结束
        if(time() > strtotime($giving->end_time))
        {
            $info = '活动已结束';
        }else{
            $has_got = DB::table('book_coin_giving_users')->where('giving_id',$giving->id)->where('uid',$uid)->first();

            if($has_got)
            {
                $info = '书币已领过，去看书吧';
            }else{
                $ntime = date("Y-m-d H:i:s");
                $data = [
                    'uid'=>$uid,
                    'giving_id'=>$giving->id,
                    'created_at'=>$ntime,
                    'updated_at'=>$ntime
                ];
                DB::table('book_coin_giving_users')->insert($data);
                DB::update("update users set balance = balance + {$giving->amount},reward_balance =reward_balance+{$giving->amount} where id ={$uid} ");
                $info = "成功领取{$giving->amount}书币！";
            }
        }

        return view('wap.welfare.giving', compact('info'));
    }
}
