<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/6/20
 * Time: 下午4:53
 */

namespace App\Http\Controllers\Manage\OfficialAccount;


use App\Http\Controllers\Manage\Finance\BaseController;
use App\Http\Controllers\Manage\OfficialAccount\Transformers\OfficialAccountDaySubStatTransformer;
use App\Modules\Subscribe\Services\OfficialAccountDaySubStatService;
use Illuminate\Http\Request;

class DataController extends BaseController
{


    /**
     * @apiVersion 1.0.0
     * @apiDescription 每个服务号每月订阅数据
     * @api {GET} OfficialAccount/day/sub/stat
     * @apiGroup OfficialAccount
     * @apiName get_list
     * @apiParam   {String}  [appid] 服务号ID
     * @apiParam   {String}  [search_name] 搜索服务号名称
     * @apiParam   {String}  [type] month， day 按月，按天
     * @apiParam   {String}  [start_time] 开始时间2017-01-01.(可不传)
     * @apiParam   {String}  [end_time] 结束时间2017-02-01.(可不传)
     *
     * @apiSuccess   {String}  appid 服务号ID
     * @apiSuccess   {String}  official_account_name 服务号名称
     * @apiSuccess   {String}  date 日期.
     * @apiSuccess   {String}  month 日期.
     * @apiSuccess   {Number}  reward_balance 赠送币金额.
     * @apiSuccess   {Number}  charge_balance 充值消耗币金额.
     * @apiSuccess   {Number}  fee 总额金额.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *              {
     *                  "list":[
     *                             {
     *                              "appid": "wx6f0d9d775ef72b9a",
     *                              "official_account_name": "择天小说",
     *                              "month": "2018-05",
     *                              "date": "",
     *                              "reward_balance": "287950",
     *                              "charge_balance": "275964",
     *                              "fee": "563914"
     *                              }
     *                          ]
     *                   "meta":{
     *                      "total": 4,
     *                       "per_page": 15,
     *                       "current_page": 1,
     *                       "last_page": 1,
     *                       "next_page_url": "",
     *                       "prev_page_url": ""
     *                          }
     *               }
     *     }
     */
    function get_list(Request $request) {
        $appid = $request->has('appid') ? $request->input('appid') : '';
        $official_account_name = $request->has('search_name') ? $request->input('search_name') : '';

        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd',strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd',strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $params = [
            'appid'=>$appid,
            'official_account_name'=>$official_account_name,
            'start_date'=>$start_time,
            'end_date'=>$end_time,
        ];

        $type = $request->has('type') ? $request->input('type') : 'month';

        $data = [];
        if($type == 'day') {
            $data = OfficialAccountDaySubStatService::getDayListData($params);
        } else {
            $data = OfficialAccountDaySubStatService::getMonthListData($params);
        }

        return response()->pagination(new OfficialAccountDaySubStatTransformer(), $data);
    }
}