<?php
/**
 * Created by PhpStorm.
 * User: z_yang
 * Date: 2018/03/09
 * Time: 20:18
 */

namespace App\Modules\Statistic\Services;

use Redis;
use DB;
use App\Modules\Statistic\Services\WapVisitStatService;
class SendStatistics
{

    /**
     * 获取强关前一章和vip前一章的uv
     */
    public static function getBeforeForceSubAndBeforeVipUv($bid,$send_order_id,$date,$one_day=true){
        $info = WapVisitStatService::getBeforeForceSubAndBeforeVipUv($bid,$send_order_id,$date,$one_day);
        $before_vip = $info['beforevipuv'];
        $before_forcesub = $info['beforeforcesubuv'];
        return compact('before_vip','before_forcesub');
    }

    public static function getWechatMsgUseRecord($distribution_channel_id,$from,$start_date,$end_date,$allsum=false){

        if($allsum){
            $start_date = '2018-04-17';
        }
        $origin_start_time = $start_date;
        if(strtotime($start_date) > strtotime($end_date)){
            return [];
        }
        if($allsum){
            $uvpv_info = WapVisitStatService::getSitePvAndUv($distribution_channel_id,$from,null,null);
        }else{
            $uvpv_info = WapVisitStatService::getSitePvAndUv($distribution_channel_id,$from,$start_date,$end_date);
        }
        $pv = $uvpv_info['pv'];
        $uv = $uvpv_info['uv'];

        $order_num_sql_format = 'select count(*) as count from orders where distribution_channel_id =%s  and from_type ="%s" and created_at BETWEEN "%s" and "%s"';
        $success_order_num_sql_format = 'select count(*) as count,ifnull(sum(price),0) as sum,count(DISTINCT uid) as user_num from orders where distribution_channel_id =%s  and from_type ="%s" and created_at BETWEEN "%s" and "%s" and status = "PAID"';
        $second_pay_order_num_sql_format = 'select count(*) as count from orders a where distribution_channel_id =%s  and from_type ="%s" and created_at BETWEEN "%s" and "%s" and status = "PAID" 
          and exists (select uid from orders b WHERE created_at <="%s" and b.uid=a.uid and status = "PAID" limit 1)';

        $order_num_sql = sprintf($order_num_sql_format,$distribution_channel_id,$from,$origin_start_time,$end_date);
        if($allsum){
            $success_order_num_sql = sprintf($success_order_num_sql_format,$distribution_channel_id,$from,'2018-04-17 18:09:35',date('Y-m-d H:i:s'));
        }else{
            $success_order_num_sql = sprintf($success_order_num_sql_format,$distribution_channel_id,$from,$origin_start_time,$end_date);
        }

        $second_pay_order_num_sql = sprintf($second_pay_order_num_sql_format,$distribution_channel_id,$from,$origin_start_time,$end_date,$origin_start_time);
        $order_num_info = DB::select($order_num_sql);
        $success_order_info = DB::select($success_order_num_sql);
        $second_pay_info = DB::select($second_pay_order_num_sql);

        $order_count = $order_num_info[0]->count;
        $success_order_count = $success_order_info[0]->count;
        $success_order_sum = $success_order_info[0]->sum;
        $user_num = $success_order_info[0]->user_num;
        $second_pay_count = $second_pay_info[0]->count;

        return compact('uv','order_count','success_order_count','success_order_sum','second_pay_count','pv','user_num');
    }

}