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

namespace App\Http\Controllers\Manage\Trade;

use App\Http\Controllers\Manage\BaseController;
use App\Http\Controllers\Manage\Trade\Transformers\OrderStatTransformer;
use App\Modules\Trade\Services\OrderStatService;
use App\Modules\Channel\Services\ChannelService;
use Illuminate\Http\Request;

class OrderStatsController extends BaseController
{

    /**
     * @apiDefine Trade 交易订单
     */

    /**
     * @apiVersion 1.0.0
     * @api {GET} trade/orderStats 获取渠道统计列表
     * @apiGroup Trade
     * @apiName orderStats
     * @apiParam   {Number}  distribution_channel_id 渠道id.
     * @apiParam   {Number}  phone 手机号码.
     * @apiSuccess {Number}  distribution_channel_id 渠道id.
     * @apiSuccess {Number}  distribution_channel_name 渠道名称.
     * @apiSuccess {Number}  phone 渠道手机号码.
     * @apiSuccess {Number}  pay_success_user_num 总付费用户数.
     * @apiSuccess {Number}  total_recharge_amount 总付费总额.
     * @apiSuccess {String}  unpaid_num 未付费订单数.
     * @apiSuccess {String}  paid_num 付费订单数.
     * @apiSuccess {String}  promotion_total_uv 推广uv数.
     * @apiSuccess {String}  promotion_total_pv 推广pv数.
     * @apiSuccess {String}  send_order_num 派单数.
     * @apiSuccess {String}  register_user_num 注册用户数.
     * @apiSuccess {String}  force_user_num 关注用户数.
     * @apiSuccess {String}  ticket_recharge_pay_user_num 普通充值用户数
     * @apiSuccess {Number}  year_recharge_user_num 包年VIP充值用户数
     * @apiSuccess {String}  created_at 创建时间
     * @apiSuccess {String}  updated_at 更新时间
     */
    function index(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? (int)$request->input('distribution_channel_id') : null;
        $phone = $request->has('phone') ? (int)$request->input('phone') : '';

        $stats = OrderStatService::getList($distribution_channel_id,$phone);
        return response()->pagination(new OrderStatTransformer(), $stats);
    }

    function export(Request $request)
    {
        $distribution_channel_id = $request->has('distribution_channel_id') ? (int)$request->input('distribution_channel_id') : null;
        $phone = $request->has('phone') ? (int)$request->input('phone') : '';

        $stats = OrderStatService::getList($distribution_channel_id,$phone,true);

        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . "渠道汇总统计列表" . date("YmdHis") . ".csv");
        echo iconv("UTF-8","GBK","\"渠道ID\",\"渠道名称\",\"累计派单数\",\"累计新增点击数\",\"累计新增人数\",\"新增关注人数\",\"累计注册人数\",\"普通付费人数\",\"包年VIP人数\",\"订单成功率\",\"累计充值总额\",\"公众号数量\",\"公众号名称\"\r\n");
        if($stats)
        {
            foreach($stats as $stat){

                echo("\"" . $stat->distribution_channel_id .  "\",");
                echo("\"" . iconv('utf-8','gbk',$stat->nickname) .  "\",");
                echo("\"" . (int)$stat->send_order_num .  "\",");
                echo("\"" . (int)$stat->promotion_total_pv .  "\",");
                echo("\"" . (int)$stat->promotion_total_uv .  "\",");
                echo("\"" . (int)$stat->force_user_num .  "\",");
                echo("\"" . (int)$stat->register_user_num .  "\",");
                echo("\"" . (int)$stat->ticket_recharge_pay_user_num .  "\",");
                echo("\"" . (int)$stat->year_pay_user_num .  "\",");
                $per = (float)$stat->unpaid_num+(float)$stat->paid_num > 0 ?  round(((float)$stat->paid_num/((float)$stat->unpaid_num+(float)$stat->paid_num))*100,2) : 0;
                echo("\"" .  $per.  "%\",");
                echo("\"" .  (float)$stat->total_recharge_amount .  "\",");
                echo("\"" .  (int)$stat->official_account_num .  "\",");
                echo("\"" .  iconv('utf-8','gbk',$stat->official_account_names) .  "\"\r\n");
            }
        }
        exit();
    }

}