<?php
namespace App\Modules\OfficialAccount\Services;


use App\Modules\OfficialAccount\Models\DistributionSelfDefineContent;
use Redis;

class MsgService
{

    /**
    * 关注回复
    */
    static function getReply($distribution_channel_id)
    {
        //关注回复
        return Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
    }

    /**
     * 设置回复
     */
    static function setReply($params)
    {
        return self::setReplyToCache($params);
    }

    //关注回复
    static function setReplyToCache($params)
    {
        $distribution_channel_id = $params['distribution_channel_id'];
        
        $content = $params['content'];
        $is_pic = $params['is_pic'];
        $start_time = $params['start_time'];
        $end_time = $params['end_time'];
        //$modes = [1=>'default',2=>'pic',3=>'text'];
        $mode = $params['mode'];
        $status = $params['status'];
        $key = isset($params['key']) && $params['key'] ? $params['key']:'0'; 
        $action = $params['action']; 
        \Log::info('setReplyToCache_param:');
        \Log::info($params);
        
        // 按时间
        $history_key = 'content_mode_'.$mode;
        
        $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);

        $replays = $replays?$replays:[];
        \Log::info('setReplyToCache_origin_$replays:');
        \Log::info($replays);
        
        $history_contents = isset($replays[$history_key]) && !empty($replays[$history_key]) ?$replays[$history_key]:'';
        \Log::info('$history_contents_origin');\Log::info($history_contents);
        $history_contents = object_to_array(json_decode($history_contents));
        \Log::info('$history_contents_decode');\Log::info($history_contents);
        $history_content_count = count($history_contents);
        if($action == 'add'){
        	$key = $history_content_count;
        }
        
        $history_contents[$key]['start_time'] = $start_time;
        $history_contents[$key]['end_time'] = $end_time;
        $history_contents[$key]['content'] = $content;
        $history_contents[$key]['is_pic'] = $mode==2?1:0;
        $history_contents[$key]['status'] = $status;
        \Log::info('$history_contents_before_encode');\Log::info($history_contents);
        // 一定要原始对象基础上改
        $replays[$mode] = $content;
        $replays['current'] = $mode;
        $replays[$history_key] = json_encode($history_contents);

        \Log::info('setReplyToCache_after_data:'.$distribution_channel_id);
        \Log::info($replays);
        Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays);//设置内容
    }

    /**
     * 获取当前回复
     * 微信交互使用
     */
    static function getCurrentReply($distribution_channel_id)
    {
    	
        //关注回复
        $replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
        $now = date('Y-m-d H:i:s');
//         $now = date('Y-m-d',strtotime('2018-11-13'));
        $current = isset($replays['current'])?$replays['current']:'1';
        
        // 获取当前时间区间的回复
        $current_reply = null;
        if($current != 1)
        {
        	$history_contents = isset($replays['content_mode_'.$current])?$replays['content_mode_'.$current]:'';
        	$history_contents = object_to_array(json_decode($history_contents));
        	\Log::info('$history_contents2');\Log::info($history_contents);
        	if(!empty($history_contents)){
        		// 先看历史时间段有没有匹配的
        		foreach($history_contents as $key=>$history_content){
        			$start_time = isset($history_content['start_time'])?$history_content['start_time']:'';
        			$end_time = isset($history_content['end_time'])?$history_content['end_time']:'';
        			$status = isset($history_content['status'])?$history_content['status']:'0';
        			\Log::info('start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now.' status:'.$status);
        			if($start_time <= $now && $now <= $end_time && $status == 1){
        				\Log::info('getCurrentReply:distribution_channel_id:'.$distribution_channel_id.' get_this:'.$key.' start_time:'.$start_time.' end_time:'.$end_time.' now:'.$now);
        				
        				$obj = new \stdClass();
        				$obj->distribution_channel_id = $distribution_channel_id;
        				$obj->content = $history_content['content'];
        				$obj->is_pic = $history_content['is_pic'];
        				$obj->status = 1;
        				return $obj;
        			}
        		}

        	}
        }
        return $current_reply;
    }
    
    /**
     * 初始化修复redis格式
     */
    static function initOriginReply($distribution_channel_id)
    {
    	//关注回复
    	$replays = Redis::hgetAll('site_sub_reply:'.$distribution_channel_id);
    	$origin_content = [];
    	if(!empty($replays)){
    		\Log::info('initOriginReply_not_null:'.$distribution_channel_id);
    		
    		// 第0个默认是原版的内容
    		if(! isset($replays['content_mode_2'])){
    			\Log::info('initOriginReply:'.$distribution_channel_id.' mode:2');
    			$replays['2'] = isset($replays['2'])?$replays['2']:'';
    			$origin_content[0]['start_time'] = '2018-01-01';
    			$origin_content[0]['end_time'] = '2030-01-01';
    			$origin_content[0]['content'] = json_decode($replays['2']);
    			$origin_content[0]['status'] = 1;
    			$origin_content[0]['is_pic'] = 1;
    			$replays['content_mode_2'] = json_encode($origin_content);
    			Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays);
    		}
    		if(! isset($replays['content_mode_3'])){
    			\Log::info('initOriginReply:'.$distribution_channel_id.' mode:3');
    			$replays['3'] = isset($replays['3']) ? $replays['3']:'';
    			\Log::info($replays['3']);\Log::info(json_decode($replays['3']));
    			$origin_content[0]['start_time'] = '2018-01-01';
    			$origin_content[0]['end_time'] = '2030-01-01';
    			$origin_content[0]['content'] = $replays['3'];
    			$origin_content[0]['status'] = 1;
    			$origin_content[0]['is_pic'] = 0;
    			$replays['content_mode_3'] = json_encode($origin_content);
    			Redis::hmset('site_sub_reply:'.$distribution_channel_id,$replays);
    		}
    		
    	}else{
    		\Log::info('initOriginReply_is_null:'.$distribution_channel_id);
    	}
    	 
    }

}