<?php
/**
 * zhoulj
 * 2018-03-07
 */

namespace App\Modules\OfficialAccount\Services;
use App\Modules\OfficialAccount\Models\CustomSendMsgs;
use App\Modules\OfficialAccount\Models\CustomSendDayStats;
use App\Modules\OfficialAccount\Models\WechatTemplateMsgs;
use App\Modules\Statistic\Services\SendStatistics;

class CustomTemplateSendStatService
{

    /**
     * 通过分销渠道ID和From获取每日智能推送分析
     */
    static function getCustomTemplateSendStat($stat_type,$start_date,$end_date,$get_all=false)
    {
    	$result = array();
    	\Log::info('$stat_type：'.$stat_type.' $start_date:'.$start_date.' $end_date:'.$end_date);
    	// 基础数据
    	switch($stat_type){
    		case "templateMessage":
    			$result = WechatTemplateMsgs::wechatTemplateMsgsPaginate($start_date,$end_date,$get_all);
    			break;
    		case "customerServiceNews":
    			$result = CustomSendMsgs::customSendMsgsPaginate($start_date,$end_date,$get_all);
    			break;
    		case "intelligentRecommendation":
    			$result = CustomSendDayStats::customSendDayStatsPaginate($start_date,$end_date,$get_all);
    			break;
    		default:
    			break;
    	}
    	
        return $result;
    }
    
    /**
     * 通过分销渠道ID和From获取每日智能推送分析
     */
    static function getMainCustomTemplateSendStat($stat_type,$start_date,$end_date,$get_all=false){
    	$base_datas = self::getCustomTemplateSendStat($stat_type,$start_date,$end_date,$get_all);
    	\Log::info('$base_datas');\Log::info($base_datas);
    	//         die();
    	// 目前只有智能推送有统计
    	foreach($base_datas as $base_data){
    		\Log::info('one_$base_data');
    		\Log::info($base_data);
    		// 付费等数据，统一从娃哥那边接口获取
    		// start_date,end_date,sites,stat_type
    		if($stat_type == 'intelligentRecommendation'){
    			$_stat_types = array();
    			$user_pay_stat = SendStatistics::getWechatMsgUseRecord($base_data->site_id,$stat_type,$start_date,$end_date);
    			\Log::info('user_pay_stat');\Log::info($user_pay_stat);
    			$base_data->uv = $user_pay_stat['uv'];
    			$base_data->total_order_num = $user_pay_stat['order_count'];
    			$base_data->suc_order_num = $user_pay_stat['success_order_count'];
    			$base_data->charge_amount = $user_pay_stat['success_order_sum'];
    			$base_data->not_first_charge_user_num = $user_pay_stat['second_pay_count'];
    		}else{
    			$base_data->uv = '';
    			$base_data->total_order_num = '';
    			$base_data->suc_order_num = '';
    			$base_data->charge_amount = '';
    			$base_data->not_first_charge_user_num = '';
    		}
    	}
    	
    	return $base_datas;
    }

}
