<?php

namespace App\Http\Controllers\Manage\Book;

use App\Http\Controllers\Manage\Book\Transformers\BookGiftDailyTransformer;
use App\Http\Controllers\Manage\Book\Transformers\BookGiftTransformer;
use App\Modules\Book\Services\BookCategoryService;
use App\Modules\Book\Services\BookConfigService;
use App\Modules\Book\Services\BookGiftsService;
use App\Modules\Book\Services\BookService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BookGiftsController extends Controller
{
    /**
     * 获取按书统计送礼数据
     * @param Request $request
     * @return mixed
     */
   public function getBookGiftsStatsByBook(Request $request){
        $time = $request->input('time','');
        if(!in_array($time,['last_week','last_month','today'])){
            return response()->error('PARAM_ERROR');
        }
        if($time=='last_week'){
            $start = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')));
            $end =  date('Y-m-d H:i:s',mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')));
        }
        if($time=='last_month'){
            $start = date('Y-m-01 00:00:00',strtotime('-1 month'));
            $end =  date('Y-m-d 23:59:59',strtotime(date('Y-m-01 H:i:s -1 day')));
        }
        if($time=='today'){
            $start = date('Y-m-d 00:00:00');
            $end =  date('Y-m-d 23:59:59');
        }
        $res = BookGiftsService::getGiftsSendStatisticByBook($start,$end);
        foreach ($res as $value){
            $res->book_name = (BookConfigService::getBookById($value->bid))->book_name;
        }
        return response()->pagination(new BookGiftTransformer(),$res);
   }

    /**
     * 获取按礼物分组统计送礼数据
     * @param Request $request
     * @return mixed
     */
    public function getBookGiftsStatsByGift(Request $request){
        $time = $request->input('time','');
        if(!in_array($time,['last_week','last_month','today'])){
            return response()->error('PARAM_ERROR');
        }
        if($time=='last_week'){
            $start = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')));
            $end =  date('Y-m-d H:i:s',mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')));
        }
        if($time=='last_month'){
            $start = date('Y-m-01 00:00:00',strtotime('-1 month'));
            $end =  date('Y-m-d 23:59:59',strtotime(date('Y-m-01 H:i:s -1 day')));
        }
        if($time=='today'){
            $start = date('Y-m-d 00:00:00');
            $end =  date('Y-m-d 23:59:59');
        }
        $res = BookGiftsService::getGiftsSendStatisticByGift($start,$end);

        return response()->pagination(new BookGiftTransformer(),$res);
    }

    /**
     * 获取按书分组每日送礼统计
     * @param Request $request
     * @return mixed
     */
    public function getBookGiftDailyStatsByBook(Request $request) {
        $start = $request->input('start_date','');
        $end = $request->input('end_date','');
        $res = BookGiftsService::getGiftsSendDaillyStatsByBook($start,$end);
        return response()->pagination(new BookGiftDailyTransformer(),$res);
    }

    /**
     * 获取按书分组每日送礼统计
     * @param Request $request
     * @return mixed
     */
    public function getBookGiftDailyStatsByGift(Request $request) {
        $res = BookGiftsService::getGiftsSendDaillyStatsByGift();
        return response()->pagination(new BookGiftDailyTransformer(),$res);
    }


}
