<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/12/1
 * Time: 11:37
 */

namespace App\Http\Controllers\Channel\Trade;

use App\Http\Controllers\Channel\BaseController;
use App\Http\Controllers\Channel\Trade\Transformers\OrderDayStatTransformer;
use App\Modules\Trade\Services\OrderDayStatService;
use App\Modules\Channel\Services\ChannelService;
use Illuminate\Http\Request;

class OrderDayStatController extends BaseController
{
    /**
     * @apiDefine Trade 订单
     */

    /**
     * @apiVersion 1.0.0
     * @api {GET} trade/orderDayStats 获取日订单统计数据
     * @apiGroup Trade
     * @apiName orderDayStats
     * @apiParam {Number}  begin_time 开始时间.
     * @apiParam {Number}  end_time 结束时间.
     * @apiSuccess {Number}  date 日期.
     * @apiSuccess {String}  pay_success_user_num 总付费用户数
     * @apiSuccess {String}  ticket_recharge_user_num 普通充值用户数.
     * @apiSuccess {String}  year_recharge_user_num 包年VIP充值用户数.
     * @apiSuccess {String}  total_recharge_amount 总充值金额.
     * @apiSuccess {Number}  unpaid_num 未支付用户数
     * @apiSuccess {String}  paid_num 付费用用户数
     * @apiSuccess {String}  promotion_total_uv uv
     * @apiSuccess {String}  promotion_total_pv pv
     * @apiSuccess {String}  force_user_num 强关用户数
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *
     *         "data": [
     *            {
     *              "date": "2017-12-01",
     *              "pay_success_user_num": 1212,
     *              "ticket_recharge_user_num": 1212,
     *              "year_recharge_user_num": 1212,
     *              "total_recharge_amount": 1212,
     *              "unpaid_num": 0,
     *              "paid_num": 12,
     *              "promotion_total_uv": 1512094804,
     *              "promotion_total_pv": 1512094804,
     *              "force_user_num": 1512094804
     *            }
     *        ]
     *     }
     */
    function search(Request $request)
    {
        $begin_time = $request->has('begin_time') ? $request->input('begin_time') : '';
        $end_time = $request->has('end_time') ? $request->input('end_time') : '';
        if(!$begin_time || !$end_time){
            //return response()->error('PARAM_ERROR');
        }

        //if()
        $distribution_channel_id = $this->getChannelId();

        if(!$distribution_channel_id) return response()->error('PARAM_ERROR');
        $stats = OrderDayStatService::search(compact('distribution_channel_id','begin_time','end_time'));
        return response()->pagination(new OrderDayStatTransformer(), $stats);
    }
    
    /**
     * 账号级别的日统计
     * @param Request $request
     */
    function accountSearch(Request $request)
    {
    	$begin_time = $request->has('begin_time') ? $request->input('begin_time') : '';
    	$end_time = $request->has('end_time') ? $request->input('end_time') : '';
    	if(!$begin_time || !$end_time){
    		//return response()->error('PARAM_ERROR');
    	}
    	$channel_user_id = $this->getChannelUserId();
    	if(!$channel_user_id) return response()->error('PARAM_ERROR');
    	 
    	$channels = ChannelService::getByChannelUserId($channel_user_id);
    	$stats = [];
    	$distribution_channel_id  = [];
    	foreach($channels as $channel){
    		$distribution_channel_id[] = $channel->id;
    	}
    	\Log::info('AccountSearch:$channel_user_id:'.$channel_user_id.' $distribution_channel_id:'.json_encode($distribution_channel_id));
    	 
    	$stats = OrderDayStatService::AccountSearch(compact('distribution_channel_id','begin_time','end_time'));
    	 
    
    	return response()->pagination(new OrderDayStatTransformer(), $stats);
    }

    /**
     * @apiVersion 1.0.0
     * @api {GET} trade/exportOrderDayStats 导出日订单统计数据
     * @apiGroup Trade
     * @apiName exportOrderDayStats
     * @apiParam {Number}  begin_time 开始时间.
     * @apiParam {Number}  end_time 结束时间.
     * @apiSuccess {Number}  date 日期.
     * @apiSuccess {String}  pay_success_user_num 总付费用户数
     * @apiSuccess {String}  ticket_recharge_user_num 普通充值用户数.
     * @apiSuccess {String}  year_recharge_user_num 包年VIP充值用户数.
     * @apiSuccess {String}  total_recharge_amount 总充值金额.
     * @apiSuccess {Number}  unpaid_num 未支付用户数
     * @apiSuccess {String}  paid_num 付费用用户数
     * @apiSuccess {String}  promotion_total_uv uv
     * @apiSuccess {String}  promotion_total_pv pv
     * @apiSuccess {String}  force_user_num 强关用户数
     */
    function export(Request $request)
    {
        $begin_time = $request->has('begin_time') ? $request->input('begin_time') : '';
        $end_time = $request->has('end_time') ? $request->input('end_time') : '';
        $distribution_channel_id = $this->getChannelId();

        if(!$distribution_channel_id) return response()->error('PARAM_ERROR');
        $stats = OrderDayStatService::search(compact('distribution_channel_id','begin_time','end_time'),true);

        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . "订单列表" . date("YmdHis") . ".csv");
        echo iconv("UTF-8","GBK","\"日期\",\"充值总额\",\"普通充值用户数\",\"年费VIP人数\",\"成功订单数\",\"订单总数\",\"当日新增用户\",\"当日新用户关注\"\r\n");
        if($stats)
        {
            foreach($stats as $stat){
            
                echo("\"" . $stat->date .  "\",");
                echo("\"" . $stat->total_recharge_amount .  "\",");
                echo("\"" . $stat->ticket_recharge_user_num .  "\",");
                echo("\"" . $stat->year_recharge_user_num .  "\",");
                echo("\"" . $stat->paid_num .  "\",");
                echo("\"" . ($stat->unpaid_num+$stat->paid_num) .  "\",");
                echo("\"" . $stat->promotion_total_uv .  "\",");
                echo("\"" . $stat->force_user_num .  "\"\r\n");

            }
        }
        exit();
    }


}