<?php
/**
维护公众号交互的用户表
 */

namespace App\Console\Commands\Tool;

use Log;
use Illuminate\Console\Command;
use App\Modules\Promotion\Services\PromotionService;
use App\Modules\Promotion\Models\TempTitleImageStatistic;
use DB;
use Redis;

class TitleImageStatistics extends Command
{
    /**
     * 执行命令  TitleImageStatistics
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'TitleImageStatistics';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '标题图片统计';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        ini_set('memory_limit', '1024M');
        print_r('memory_used:'.memory_get_usage()."\n");
        print_r("======标题图片统计【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
        $headlines = PromotionService::getFullHeadline();
        $images = PromotionService::getFullMessageRandomDocumentCovers(3);
        $calculate_time = date('Y-m-d H:i:s');
        foreach($headlines as $key=>$headline){
        	\Log::info('headlines_key:'.$key);
        	foreach($images as $image){
        		try{
        			$title_image_key = $headline['id'].'_'.$image['id'];
        			//         		\Log::info('TitleImageStatistics_title_image_key:'.$title_image_key);
        			$title_image_statistics_key = 'smart_push_from_title_image:'.$title_image_key;
        			$send_num = Redis::hget($title_image_statistics_key,'send_num');
        			
        			if($send_num > 0){
        				\Log::info('TitleImageStatistics_insert_title_image_key:'.$title_image_key);
        				$title_image_send_uv_key = 'smart_push_from_title_image_send_uv:'.$title_image_key;
        				$title_image_click_uv_key = 'smart_push_from_title_image_click_uv:'.$title_image_key;
        				$send_uv = Redis::SCARD($title_image_send_uv_key);
        				$click_uv = Redis::SCARD($title_image_click_uv_key);
        				$click_num = Redis::hget($title_image_statistics_key,'click_num');
        			
        				$insert_data = [
	        				'title_id'=>$headline['id'],
	        				'image_id'=>$image['id'],
	        				'send_uv'=>isset($send_uv)?$send_uv:0,
	        				'click_uv'=>isset($click_uv)?$click_uv:0,
	        				'send_num'=>isset($send_num)?$send_num:0,
	        				'click_num'=>isset($click_num)?$click_num:0,
	        				'calculate_time'=>$calculate_time,
        				];
        				\Log::info($insert_data);
        				 
        				TempTitleImageStatistic::create($insert_data);
        			}
        		}catch(\Exception $e){
        			\Log::info('TitleImageStatistics_ept:'.$e->getMessage());
        		}
        	
        	}
        }
        
        
        print_r("======标题图片统计 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
        print_r('memory_used:'.memory_get_usage()."\n");
    }
}