<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/30
 * Time: 17:24
 */

namespace App\Http\Controllers\Manage\Trade;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Manage\Trade\Transformers\SubstituteOrderTransformer;
use App\Modules\Subscribe\Services\SubstituteOrderService;
use DB;
use Illuminate\Http\Request;

class SubstituteOrdersController extends Controller
{

    /**
     * @apiDefine Trade 代付
     */

    /**
     * @apiVersion 1.0.0
     * @api {GET} trade/getSubstituteOrderInfos 获取订单列表
     * @apiGroup Trade
     * @apiName getSubstituteOrderInfos
     * @apiParam   {String}  [start_time] 开始时间
     * @apiParam   {String}  [end_time] 结束时间
     * @apiParam   {Number}  [distribution_channel_id] 分销渠道ID
     * @apiSuccess {String} date  日期.
     * @apiSuccess {Number}  num 代付人数
     * @apiSuccess {Number}  amount 代付金额
     * @apiSuccess {Number}  success_num 代付成功订单
     * @apiSuccess {Number}  click_substitute_order_pay_button_uv 充值页面代付按钮UV
     * @apiSuccess {Number}  show_substitute_order_page_uv '代付页面UV
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *            {
     *              "show_substitute_order_page_uv": 33,
     *              "click_substitute_order_pay_button_uv": 11222,
     *              "success_num": 122,
     *              "amount": 45,
     *              "num": 10,
     *              "date": '2018-10-01',
     *            }
     *        ]
     *     }
     */
    function getSubstituteOrderInfos(Request $request)
    {
        $end_time = $request->has('end_time') ? $request->input('end_time') : '';
        $start_time = $request->has('start_time') ? $request->input('start_time') : '';
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';

        if (!$start_time) {
            $start_time = date("2018-10-1");
        }

        if (!$end_time) {
            $end_time = date("Y-m-d");
        }

        $result = SubstituteOrderService::substituteStats(false,$start_time, $end_time, $distribution_channel_id);
        foreach ($result as $item) {
            $date = $item->date;
            $distribution_channel_id = $item->distribution_channel_id;
            $item->show_substitute_order_page_uv = SubstituteOrderService::getSubstitutePageUvPv($distribution_channel_id, $date);
            $item->click_substitute_order_pay_button_uv = SubstituteOrderService::getSubstituteButtonUvPv($distribution_channel_id, $date);
        }
        return response()->pagination(new SubstituteOrderTransformer(), $result);

    }

    /**
     * @apiVersion 1.0.0
     * @api {GET} trade/exportSubstituteOrderInfos 导出订单列表
     * @apiGroup Trade
     * @apiName exportSubstituteOrderInfos
     * @apiParam   {String}  [start_time] 开始时间
     * @apiParam   {String}  [end_time] 结束时间
     * @apiParam   {Number}  [distribution_channel_id] 分销渠道ID
     * @apiSuccess {String} date  日期.
     * @apiSuccess {Number}  num 代付人数
     * @apiSuccess {Number}  amount 代付金额
     * @apiSuccess {Number}  success_num 代付成功订单
     * @apiSuccess {Number}  click_substitute_order_pay_button_uv 充值页面代付按钮UV
     * @apiSuccess {Number}  show_substitute_order_page_uv '代付页面UV
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *            {
     *              "show_substitute_order_page_uv": 33,
     *              "click_substitute_order_pay_button_uv": 11222,
     *              "success_num": 122,
     *              "amount": 45,
     *              "num": 10,
     *              "date": '2018-10-01',
     *            }
     *        ]
     *     }
     */
    function exportSubstituteOrderInfos(Request $request)
    {
        $end_time = $request->has('end_time') ? $request->input('end_time') : '';
        $start_time = $request->has('start_time') ? $request->input('start_time') : '';
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';

        if (!$start_time) {
            $start_time = date("2018-10-1");
        }

        if (!$end_time) {
            $end_time = date("Y-m-d");
        }

        $result = SubstituteOrderService::substituteStats(true,$start_time, $end_time, $distribution_channel_id);
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . "代付信息" . date("YmdHis") . ".csv");
        echo("\"" . mb_convert_encoding("\"日期\",\"代付人数\",\"代付金额\",\"充值页面代付按钮UV\",\"代付页面UV\",\"代付订单\",\"代付成功订单\"\r\n", "GBK", "UTF-8") . "\",");

        foreach ($result as $item) {
            $date = $item->date;
            $distribution_channel_id = $item->distribution_channel_id;
            $item->show_substitute_order_page_uv = SubstituteOrderService::getSubstitutePageUvPv($distribution_channel_id, $date);
            $item->click_substitute_order_pay_button_uv = SubstituteOrderService::getSubstituteButtonUvPv($distribution_channel_id, $date);

            echo("\"" . mb_convert_encoding($item->date, "GBK", "UTF-8") . "\",");
            echo("\"" . mb_convert_encoding($item->users_num, "GBK", "UTF-8") . "\",");
            echo("\"" . mb_convert_encoding($item->amount, "GBK", "UTF-8") . "\",");
            echo("\"" . mb_convert_encoding($item->click_substitute_order_pay_button_uv, "GBK", "UTF-8") . "\",");
            echo("\"" . mb_convert_encoding($item->show_substitute_order_page_uv, "GBK", "UTF-8") . "\",");
            //代付订单
            echo("\"" . mb_convert_encoding($item->num, "GBK", "UTF-8") . "\",");
            echo("\"" . $item->success_num . "\"\r\n");
        }
        exit();
    }
}
