<?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\OrdersRechargeTransformer;
use App\Http\Controllers\Channel\Trade\Transformers\OrderStatTransformer;
use App\Http\Controllers\Channel\Trade\Transformers\OrderTransformer;
use App\Modules\SendOrder\Services\SendOrderForceDayStatService;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\SendOrder\Services\SendOrderStatService;
use App\Modules\Trade\Services\OrderStatService;
use App\Modules\Channel\Services\ChannelService;
use Illuminate\Http\Request;

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

    /**
     * @apiVersion 1.0.0
     * @api {GET} trade/getChannelTotalStat 获取账号下所有渠道订单统计数据
     * @apiGroup Trade
     * @apiName getChannelTotalStat
     * @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}  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 getChannelTotalStat(Request $request)
    {

        $channel_user_id = $this->getChannelUserId();

        if(!$channel_user_id) return response()->error('PARAM_ERROR');
        $distribution_channel_ids = ChannelService::getUserChannelIds($channel_user_id);

        $stats = OrderStatService::getChannelTotalStat($distribution_channel_ids);
        $stats->cost_sum = SendOrderService::getCostStats($distribution_channel_ids);
        return response()->item(new OrderStatTransformer(), $stats);
    }

    public function getSendOrdersRechargeStats(Request $request) {
        $channel_user_id = $this->getChannelUserId();

        if(!$channel_user_id) return response()->error('PARAM_ERROR');
        $distribution_channel_ids = ChannelService::getUserChannelIdsV2($channel_user_id);
        $stats = SendOrderStatService::getSendOrdersRechargeDetail($distribution_channel_ids);
        return response()->pagination(new OrdersRechargeTransformer(),$stats);
    }

    public function exportSendOrdersRechargeStats(){
        $channel_user_id = $this->getChannelUserId();

        if(!$channel_user_id) return response()->error('PARAM_ERROR');
        $distribution_channel_ids = ChannelService::getUserChannelIdsV2($channel_user_id);
        $stats = SendOrderStatService::exportSendOrdersRechargeDetail($distribution_channel_ids);
        saveExcelData($stats['header'],$stats['data'],'');
    }
}