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

namespace App\Http\Controllers\Manage\SendOrder;

use App\Http\Controllers\Channel\BaseController;
use App\Http\Controllers\Manage\SendOrder\Transformers\SendOrderBreakevenStatsTransformer;
use App\Modules\Channel\Services\ChannelService;
use App\Modules\Statistic\Services\SendOrderBreakevenStatsService;
use Illuminate\Http\Request;


class SendOrderBreakevenStatsController extends BaseController
{

    /**
     * @apiDefine sendOrder 派单
     */

    /**
     * @apiVersion 1.0.0
     * @api {GET} sendOrder/getSendOrderBreakevenStats 获取30日回本率数据
     * @apiGroup sendOrder
     * @apiName getSendOrderBreakevenStats
     *
     * @apiParam   {Number}  [distribution_channel_id] 分销渠道ID
     * @apiParam   {String}  [start_date] 开始日期
     * @apiParam   {String}  [end_date] 结束日期
     *
     * @apiSuccess {Number}  id  id.
     * @apiSuccess {Number}  distribution_channel_id 渠道id.
     * @apiSuccess {Number}  distribution_channel_name 渠道名称.
     * @apiSuccess {Number}  official_account_id 服务号id.
     * @apiSuccess {String}  official_account_name 服务号名称.
     * @apiSuccess {String}  date  日期
     * @apiSuccess {Number}  force_user_num 粉丝数
     * @apiSuccess {Number}  total_force_user_num 总粉丝数
     * @apiSuccess {Number}  unsubscribe_in_one_day_user_num 24小时取关用户数
     * @apiSuccess {String}  created_at 创建时间
     * @apiSuccess {String}  updated_at 更新时间
     * @apiSuccess {Number}  recharge_amount_in_one_day 24小时充值
     * @apiSuccess {Number}  recharge_amount_in_three_days 72小时充值
     * @apiSuccess {Number}  recharge_amount_in_one_month 30天充值
     * @apiSuccess {Number}  recharge_amount_in_two_months 60天充值
     * @apiSuccess {Number}  recharge_amount_in_three_months 90天充值
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *            {
     *              "id": 2,
     *              "distribution_channel_id": 2,
     *              "distribution_channel_name":"渠道名称",
     *              "official_account_id": 1233,
     *              "official_account_name": "杭州掌维",
     *              "date": "2018-2-9",
     *              "force_user_num": 11,
     *              "total_force_user_num": 111,
     *              "unsubscribe_in_one_day_user_num": 5,
     *              "created_at": "2017-12-01 10:20:04",
     *              "updated_at": "2017-12-01 10:20:04",
     *              "recharge_amount_in_one_day": 9,
     *              "recharge_amount_in_three_days": 6,
     *              "recharge_amount_in_one_month": 23,
     *              "recharge_amount_in_two_months":11,
     *              "recharge_amount_in_three_months": 112
     *            }
     *        ]
     *     }
     */
    function getSendOrderBreakevenStats(Request $request)
    {
        $params = [];
        $request->has('end_date') && $request->input('end_date') && $params['end_date'] = $request->input('end_date');
        $request->has('start_date') && $request->input('start_date') && $params['start_date'] = $request->input('start_date');
        $request->has('distribution_channel_id') && $request->input('distribution_channel_id') && $params['distribution_channel_id'] = (int)$request->input('distribution_channel_id');
        $data = SendOrderBreakevenStatsService::getInfos($params, false);
        foreach ($data as $item) {
            $item->distribution_channel_name = ChannelService::getChannelNicknameById($item->distribution_channel_id);
        }
        return response()->pagination(new SendOrderBreakevenStatsTransformer(), $data);
    }


    /**
     * @apiVersion 1.0.0
     * @api {GET} sendOrder/exportSendOrderBreakevenStats 导出30日回本率数据
     * @apiGroup sendOrder
     * @apiName exportSendOrderBreakevenStats
     *
     * @apiParam   {Number}  [distribution_channel_id] 分销渠道ID
     * @apiParam   {String}  [start_date] 开始日期
     * @apiParam   {String}  [end_date] 结束日期
     * @apiSuccess {Number}  id  id.
     * @apiSuccess {Number}  distribution_channel_id 渠道id.
     * @apiSuccess {Number}  official_account_id 服务号id.
     * @apiSuccess {String}  official_account_name 服务号名称.
     * @apiSuccess {String}  date  日期
     * @apiSuccess {Number}  force_user_num 粉丝数
     * @apiSuccess {Number}  total_force_user_num 总粉丝数
     * @apiSuccess {Number}  unsubscribe_in_one_day_user_num 24小时取关用户数
     * @apiSuccess {String}  created_at 创建时间
     * @apiSuccess {String}  updated_at 更新时间
     * @apiSuccess {Number}  recharge_amount_in_one_day 24小时充值
     * @apiSuccess {Number}  recharge_amount_in_three_days 72小时充值
     * @apiSuccess {Number}  recharge_amount_in_one_month 30天充值
     * @apiSuccess {Number}  recharge_amount_in_two_months 60天充值
     * @apiSuccess {Number}  recharge_amount_in_three_months 90天充值
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *            {
     *              "id": 2,
     *              "distribution_channel_id": 2,
     *              "official_account_id": 1233,
     *              "official_account_name": "杭州掌维",
     *              "date": "2018-2-9",
     *              "force_user_num": 11,
     *              "total_force_user_num": 111,
     *              "unsubscribe_in_one_day_user_num": 5,
     *              "created_at": "2017-12-01 10:20:04",
     *              "updated_at": "2017-12-01 10:20:04",
     *              "recharge_amount_in_one_day": 9,
     *              "recharge_amount_in_three_days": 6,
     *              "recharge_amount_in_one_month": 23,
     *              "recharge_amount_in_two_months":11,
     *              "recharge_amount_in_three_months": 112
     *            }
     *        ]
     *     }
     */
    function exportSendOrderBreakevenStats(Request $request)
    {
        $params = [];
        $request->has('end_date') && $request->input('end_date') && $params['end_date'] = $request->input('end_date');
        $request->has('start_date') && $request->input('start_date') && $params['start_date'] = $request->input('start_date');
        $request->has('distribution_channel_id') && $request->input('distribution_channel_id') && $params['distribution_channel_id'] = (int)$request->input('distribution_channel_id');
        $data = SendOrderBreakevenStatsService::getInfos($params, true);
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . "数据统计" . date("YmdHis") . ".csv");

        echo("\"" . mb_convert_encoding("\"日期\",\"渠道id\",\"渠道名称\",\"服务号id\",\"服务号名称\",\"粉丝数\",\"总粉丝数\",\"24小时取关用户数\",\"创建时间\",\"更新时间\",\"24小时充值\",\"72小时充值\",\"30天充值\",\"60天充值\",\"90天充值\"\r\n", "GBK", "UTF-8") . "\",");
        if ($data) {
            foreach ($data as $item) {

                $official_account_name = $item->official_account_name;
                $distribution_channel_name = ChannelService::getChannelNicknameById($item->distribution_channel_id);

                echo("\"" . mb_convert_encoding(date('Y-m-d', strtotime($item->date)), "GBK", "UTF-8") . "\",");
                echo("\"" . $item->distribution_channel_id . "\",");
                echo("\"" . mb_convert_encoding($distribution_channel_name ? $distribution_channel_name : "未知", "GBK", "UTF-8") . "\",");
                echo("\"" . $item->official_account_id . "\",");
                echo("\"" . mb_convert_encoding($official_account_name ? $official_account_name : "未知", "GBK", "UTF-8") . "\",");
                echo("\"" . $item->force_user_num . "\",");
                echo("\"" . $item->total_force_user_num . "\",");
                echo("\"" . $item->unsubscribe_in_one_day_user_num . "\",");
                echo("\"" . mb_convert_encoding(date('Y-m-d H:i:s', strtotime($item->created_at)), "GBK", "UTF-8") . "\",");
                echo("\"" . mb_convert_encoding(date('Y-m-d H:i:s', strtotime($item->updated_at)), "GBK", "UTF-8") . "\",");
                echo("\"" . $item->recharge_amount_in_one_day . "\",");
                echo("\"" . $item->recharge_amount_in_three_days . "\",");
                echo("\"" . $item->recharge_amount_in_one_month . "\",");
                echo("\"" . $item->recharge_amount_in_two_months . "\",");
                echo("\"" . $item->recharge_amount_in_three_months . "\"\r\n");
            }
        }
        exit();
    }
}