<?php

namespace App\Http\Controllers\Channel\OfficialAccount;

use App\Modules\OfficialAccount\Models\CustomMsgSwitchs;

use App\Modules\OfficialAccount\Services\CustomMsgService;

use App\Http\Controllers\Channel\BaseController as ChannelBaseController;
use App\Modules\OfficialAccount\Services\CustomSendStatsService;
use App\Http\Controllers\Channel\OfficialAccount\Transformers\CustomSendStatsTransformers;
use Illuminate\Http\Request;

class CustomSendStatsController extends ChannelBaseController
{

     /**
     * @apiDefine OfficialAccount 公众号
     */

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/customSendDayStatsByChannelAndFrom 获取每日智能推送分析数据
     * @apiGroup OfficialAccount
     * @apiName customSendDayStatsByChannelAndFrom
     * @apiParam   {String}  from 智能分析类型  not_pay未支付提醒   recovery_push三天回本   hot_push热门推送   point_push书籍推荐.
     * @apiSuccess {String}  distribution_channel_id 渠道号.
     * @apiSuccess {String}  date 日期.
     * @apiSuccess {String}  push_user_num 推送次数.
     * @apiSuccess {String}  click_num 点击次数.
     * @apiSuccess {String}  amount 充值金额.
     * @apiSuccess {String}  success_pay_num 充值笔数.
     * @apiSuccess {String}  success_pay_rate 充值率.
     * @apiSuccess {String}  click_rate 点击率.
     * @apiSuccess {String}  from 智能分析类型  not_pay未支付提醒   recovery_push三天回本   hot_push热门推送   point_push书籍推荐..
     * @apiSuccessExample {json} Success-Response:
     *
     *      {
     *          "code": 0,
     *          "msg": "",
     *          "data": {
     *              "list": [
     *                  {
     *                      "id": 1,
     *                      "distribution_channel_id": 2,
     *                      "date": "2018-01-17",
     *                      "push_user_num": 1211,
     *                      "click_num": 211,
     *                      "click_rate": 0.000,
     *                      "amount": "2312.00",
     *                      "success_pay_num": 123,
     *                      "success_pay_rate": "0.5100",
     *                      "from": "not_pay"
     *                  },
     *                  {
     *                      "id": 2,
     *                      "distribution_channel_id": 2,
     *                      "date": "2018-01-16",
     *                      "push_user_num": 1312,
     *                      "click_num": 421,
     *                      "click_rate": 0.000,
     *                      "amount": "16421.00",
     *                      "success_pay_num": 3212,
     *                      "success_pay_rate": "0.2100",
     *                      "from": "not_pay"
     *                  }
     *              ],
     *              "meta": {
     *                  "total": 2,
     *                  "per_page": 15,
     *                  "current_page": 1,
     *                  "last_page": 1,
     *                  "next_page_url": "",
     *                  "prev_page_url": ""
     *              }
     *          }
     *      }   
     */
    function  customSendDayStatsByChannelAndFrom(Request $request) {


        $distribution_channel_id = $this->getChannelId();

        $from = $request->has('from') ? $request->input('from') : '';
        if(empty($from)) {
            return response()->error("PARAM_EMPTY");
        }

        \Log::info('日推送智能分析 渠道ID:'.$distribution_channel_id);
        \Log::info('日推送智能分析 类型:'.$from);

        $customMsgService = CustomSendStatsService::customSendDayStatsByChannelAndFrom($distribution_channel_id,$from);

        if (!empty($customMsgService)) {
            
            return response()->pagination(new CustomSendStatsTransformers(), $customMsgService);
        }else{

            $customMsgService['distribution_channel_id'] = $distribution_channel_id;
            $customMsgService['date'] = date("Y-m-d");
            $customMsgService['push_user_num'] = 0;
            $customMsgService['click_num'] = 0;
            $customMsgService['amount'] = 0;
            $customMsgService['success_pay_num'] = 0;
            $customMsgService['success_pay_rate'] = 0;
            $customMsgService['click_rate'] = 0;
            $customMsgService['from'] = $from;
            return response()->item(new CustomSendStatsTransformers(), $customMsgService);
        }
        
    }

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/customSendStatsByChannelAndFrom 获取智能推送分析总数据
     * @apiGroup OfficialAccount
     * @apiName customSendStatsByChannelAndFrom
     * @apiParam   {String}  from 智能分析类型  not_pay未支付提醒   recovery_push三天回本   hot_push热门推送   point_push书籍推荐.
     * @apiSuccess {String}  distribution_channel_id 渠道号.
     * @apiSuccess {String}  date 日期.
     * @apiSuccess {String}  push_user_num 推送次数.
     * @apiSuccess {String}  click_num 点击次数.
     * @apiSuccess {String}  amount 充值金额.
     * @apiSuccess {String}  success_pay_num 充值笔数.
     * @apiSuccess {String}  success_pay_rate 充值率.
     * @apiSuccess {String}  click_rate 点击率.
     * @apiSuccess {String}  from 智能分析类型  not_pay未支付提醒   recovery_push三天回本   hot_push热门推送   point_push书籍推荐..
     * @apiSuccessExample {json} Success-Response:
     *
     *      {
     *          "code": 0,
     *          "msg": "",
     *          "data": {
     *              "id": 1,
     *              "distribution_channel_id": 2,
     *              "date": "2018-01-18",
     *              "push_user_num": 4123,
     *              "click_num": 543,
     *              "amount": "123141.00",
     *              "success_pay_num": 2132,
     *              "success_pay_rate": "0.3100",
     *              "click_rate": "0.0000",
     *              "from": "not_pay"
     *          }
     *      }  
     */
    function  customSendStatsByChannelAndFrom(Request $request) {


        $distribution_channel_id = $this->getChannelId();

        $from = $request->has('from') ? $request->input('from') : '';
        if(empty($from)) {
            return response()->error("PARAM_EMPTY");
        }

        \Log::info('日推送智能分析 渠道ID:'.$distribution_channel_id);
        \Log::info('日推送智能分析 类型:'.$from);

        $customMsgService = CustomSendStatsService::customSendStatsByChannelAndFrom($distribution_channel_id,$from);

        if (!empty($customMsgService)) {
            
            return response()->item(new CustomSendStatsTransformers(), $customMsgService);
        }else{

            $customMsgService['distribution_channel_id'] = $distribution_channel_id;
            $customMsgService['date'] = date("Y-m-d");
            $customMsgService['push_user_num'] = 0;
            $customMsgService['click_num'] = 0;
            $customMsgService['amount'] = 0;
            $customMsgService['success_pay_num'] = 0;
            $customMsgService['success_pay_rate'] = 0;
            $customMsgService['click_rate'] = 0;
            $customMsgService['from'] = $from;
            return response()->item(new CustomSendStatsTransformers(), $customMsgService);
        }
        
    }
    
    /**
     * 获取智能推送列表
     * @param Request $request
     */
    function  customSendStatsFromTypes(Request $request) {
    	$customMsgSwitchs = CustomMsgSwitchs::getCustomMsgSwitchs();

    	return response()->success($customMsgSwitchs);
    }
    
}