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

namespace App\Http\Controllers\Manage\OfficialAccount;

use App\Http\Controllers\Manage\BaseController;
use App\Http\Controllers\Manage\OfficialAccount\Transformers\CustomTemplateSendStatTransformer;
use App\Modules\OfficialAccount\Services\CustomTemplateSendStatService;
use App\Modules\Statistic\Services\SendStatistics;
use Illuminate\Http\Request;

class CustomTemplateSendStatsController extends BaseController
{

    /**
     * @apiDefine OfficialAccount 客服模板消息发送统计
     */

    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/customTemplateSendStats 客服模板消息发送统计
     * @apiGroup OfficialAccount
     * @apiName customTemplateSendStats
     * @apiParam   {Number}  stat_type 类型(templateMessage,customerServiceNews,intelligentRecommendation).
     * @apiParam   {Number}  start_date 起始查询.
     * @apiParam   {Number}  start_date 截止查询.
     */
	//http://zzsy/api/OfficialAccount/customTemplateSendStats?start_date=2018-03-01&end_date=2018-03-08&stat_type=templateMessage
    function customTemplateSendStats(Request $request)
    {
        $stat_type = $request->has('stat_type') ? $request->input('stat_type') : 'templateMessage';
        $start_date = $request->has('start_date') ? $request->input('start_date') : date('Y-m-d',strtotime('-7 day'));
        $end_date = $request->has('end_date') ? $request->input('end_date') : date('Y-m-d',strtotime('+1 day'));
        \Log::info('customTemplateSendStats:stat_type.'.$stat_type.' start_date:'.$start_date.' end_date:'.$end_date);
       
        $base_datas = CustomTemplateSendStatService::getMainCustomTemplateSendStat($stat_type,$start_date,$end_date);
        \Log::info('$base_datas');\Log::info($base_datas);
        return response()->pagination(new CustomTemplateSendStatTransformer(), $base_datas);
    }
    
    /**
     * @apiVersion 1.0.0
     * @api {GET} OfficialAccount/exportCustomTemplateSendStats 导出-客服模板消息发送统计
     * @apiGroup OfficialAccount
     * @apiName exportCustomTemplateSendStats
     * @apiParam   {Number}  stat_type 类型(templateMessage,customerServiceNews,intelligentRecommendation).
     * @apiParam   {Number}  start_date 起始查询.
     * @apiParam   {Number}  start_date 截止查询.
     */
    //http://zzsy/api/OfficialAccount/exportCustomTemplateSendStats?stat_type=templateMessage&start_date=2017-03-01&end_date=2018-03-08
    function exportCustomTemplateSendStats(Request $request)
    {
        $stat_type = $request->has('stat_type') ? $request->input('stat_type') : 'templateMessage';
        $start_date = $request->has('start_date') ? $request->input('start_date') : date('Y-m-d',strtotime('-7 day'));
        $end_date = $request->has('end_date') ? $request->input('end_date') : date('Y-m-d',strtotime('+1 day'));
        \Log::info('customTemplateSendStats:stat_type.'.$stat_type.' start_date:'.$start_date.' end_date:'.$end_date);
       
        $CustomTemplateSendStats = CustomTemplateSendStatService::getMainCustomTemplateSendStat($stat_type,$start_date,$end_date,true);
        \Log::info('export_customTemplateSendStats_stats');\Log::info($CustomTemplateSendStats);
//         return response()->collection(new CustomTemplateSendStatTransformer(), $CustomTemplateSendStats);
    
    	header("Content-type:application/vnd.ms-excel");
    	header("Content-Disposition:attachment;filename=" . "客服模板消息发送统计" . date("YmdHis") . ".csv");
    	echo iconv("UTF-8","GBK","\"日期\",\"模板名称\",\"任务名称\",\"渠道id\",\"公众号名称\",\"标签\",\"发送时间\",\"成功发送人数\",\"UV\",\"总订单数\",\"成功订单数\",\"充值金额\",\"非首充用户数\"\r\n");
    	if($CustomTemplateSendStats)
    	{
    		foreach($CustomTemplateSendStats as $CustomTemplateSendStat){
    			echo("\"" . (isset($CustomTemplateSendStat->created_date) ? $CustomTemplateSendStat->created_date : "")  .  "\",");
    			echo("\"" . (iconv('utf-8','gbk',isset($CustomTemplateSendStat->template_name) ? $CustomTemplateSendStat->template_name : "")) .  "\",");
    			echo("\"" . (iconv('utf-8','gbk',isset($CustomTemplateSendStat->task_name) ? $CustomTemplateSendStat->task_name : "")) .  "\",");
    			echo("\"" . (isset($CustomTemplateSendStat->site_id) ? $CustomTemplateSendStat->site_id : "")  .  "\",");
    			echo("\"" . (iconv('utf-8','gbk',isset($CustomTemplateSendStat->official_account_name) ? $CustomTemplateSendStat->official_account_name : "")) .  "\",");
    			echo("\"" . (iconv('utf-8','gbk',isset($CustomTemplateSendStat->tag) ? $CustomTemplateSendStat->tag : "")) .  "\",");
    			echo("\"" .  (isset($CustomTemplateSendStat->send_time) ? $CustomTemplateSendStat->send_time : "")  .  "\",");
    			echo("\"" . (isset($CustomTemplateSendStat->suc_send_num) ? $CustomTemplateSendStat->suc_send_num : "")  .  "\",");
    			echo("\"" .  (isset($CustomTemplateSendStat->uv) ? $CustomTemplateSendStat->uv : "")  .  "\",");
    			echo("\"" .  (isset($CustomTemplateSendStat->total_order_num) ? $CustomTemplateSendStat->total_order_num : "")  .  "\",");
    			echo("\"" .  (isset($CustomTemplateSendStat->suc_order_num) ? $CustomTemplateSendStat->suc_order_num : "")  .  "\",");
    			echo("\"" . (isset($CustomTemplateSendStat->charge_amount) ? $CustomTemplateSendStat->charge_amount : "")  .  "\",");
    			echo("\"" . (isset($CustomTemplateSendStat->not_first_charge_user_num) ? $CustomTemplateSendStat->not_first_charge_user_num  : "") .  "\"\r\n");
    		}
    		
    	}
    	exit();
    }
    

}