<?php

namespace App\Http\Controllers\Wechat\Api;

use App\Http\Requests;
use App\Modules\OfficialAccount\Services\WechatTemplateService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use EasyWeChat\Foundation\Application;
use App\Libs\Classes\WxSign;
use App\Http\Controllers\WechatBaseApisController;
use Illuminate\Support\Facades\Redis;

/**
 * 微信内部中间件相关api接口
 * @author zhoulingjie
 *
 */
class WechatInnerApisController extends WechatBaseApisController
{
	 public function __construct($_param)
	 {
	 	parent::__construct($_param);
	 }
	 
	 /**
	  * 得到事件的推送内容
	  * 
	  */
	 public function get_event_content($event,$content,$gzh_app_id,$openid)
	 {
 	    $data = array();
 		$data['event'] = strtolower($event);
 		$data['content'] = $content;
 		$data['appid'] = $gzh_app_id;
 		$data['openid'] = $openid;
 		return $this->do_api_post($data,'get_event_content');
	 }
	 
	 /**
	  * 得到强关用户信息
	  */
	 function get_force_wx_user($gzh_app_id,$openid)
	 {
	 	$data = array();
	 	$data['appid'] = $gzh_app_id;
	 	$data['openid'] = $openid;
	 	return $this->do_api_post($data,'get_force_wx_user');
	 }
	 
	 /**
	  * 得到默认登录用户信息
	  */
	 function get_login_wx_user($gzh_app_id,$union_id)
	 {
	 	$data = array();
	 	$data['appid'] = $gzh_app_id;
	 	$data['unionid'] = $union_id;
	 	return $this->do_api_post($data,'get_login_wx_user');
	 }
	 
	 /**
	  * 得到公众号信息
	  */
	 function get_official_account($gzh_app_id)
	 {
	 	// 从redis取
	 	$redis_offcial_account_key = env('redis_offcial_account_key');
	 	if(!empty($redis_offcial_account_key)){
	 		$redis_offcial_account_key = $redis_offcial_account_key.$gzh_app_id;
// 	 		v('get_official_account_from_redis:'.$gzh_app_id.' redis_offcial_account_key:'.$redis_offcial_account_key);
	 		$official_account = Redis::hGet($redis_offcial_account_key,'official_account_info');
	 		$official_account = objectToArray(json_decode($official_account));
// 	 		v('official_account:'.json_encode($official_account));
	 		if(empty($official_account)){
	 			v('get_official_account_from_redis_api:'.$gzh_app_id);
	 			$data = array();
	 			$data['appid'] = $gzh_app_id;
	 			return $this->do_api_post($data,'get_official_account');
	 		}
	 		return $official_account;
	 	}else{
	 		v('get_official_account_from_api:'.$gzh_app_id);
		 	$data = array();
		 	$data['appid'] = $gzh_app_id;
		 	return $this->do_api_post($data,'get_official_account');
	 	}

	 }
	 
	 /**
	  * 得到基础模板消息列表
	  */
	 function get_wechat_public_templates($gzh_app_id)
	 {
	 	$data = array();
	 	$data['appid'] = $gzh_app_id;
	 	return $this->do_api_post($data,'get_wechat_public_templates');
	 }
	 
	 /**
	  * 得到公众号对应的模板消息
	  */
	 function get_template_official_info($gzh_app_id,$common_template_id)
	 {
	 	$data = array();
	 	$data['appid'] = $gzh_app_id;
	 	$data['common_template_id'] = $common_template_id;
	 	return $this->do_api_post($data,'get_template_official_info');
	 }
	 
	 
	 /**
	  * 得到公众号对应的菜单设置列表
	  */
	 function get_official_account_menus($gzh_app_id)
	 {
	 	$data = array();
	 	$data['appid'] = $gzh_app_id;
	 	return $this->do_api_post($data,'get_official_account_menus');
	 }
	 
	 /**
	  * 和强关公众号交互时间更新
	  */
	 function update_user_interaction_time($gzh_app_id,$openid,$distribution_channel_id)
	 {
	 	$data = array();
	 	$data['appid'] = $gzh_app_id;
	 	$data['openid'] = $openid;
	 	$data['distribution_channel_id'] = $distribution_channel_id;
	 	$data['last_interactive_time'] = date('Y-m-d H:i:s');
	 	return $this->do_api_post($data,'update_user_interaction_time');
	 }
	 
	 /**
	  * 更新模板消息任务状态
	  */
	 function update_template_task_status($task_id,$status,$msg)
	 {
	 	// 从redis取
	 	$redis_task_key = env('redis_task_key');
	 	if(!empty($redis_task_key)){
	 		$redis_task_key = $redis_task_key.$task_id;
// 	 		v('update_template_task_status_from_redis:'.$task_id.' redis_task_key:'.$redis_task_key);
	 		$template_task = Redis::hGet($redis_task_key,'wechat_msg');
	 		$template_task = objectToArray(json_decode($template_task));
// 	 		v('update_template_task_status_template_task:'.json_encode($template_task));
	 		$task_status = isset($template_task['status']) ? $template_task['status']:'';
	 		v('update_template_task_status_task_id:'.$task_id.' $task_status:'.$task_status);
	 		if($task_status == 1 || $task_status == 8){
	 			v('update_template_task_status_exe_innner:'.$task_id);
	 			$data = array();
	 			$data['id'] = $task_id;
	 			$data['status'] = $status;
	 			$data['msg'] = $msg;
                WechatTemplateService::updateWechatTemplateStatus($data);
	 			//return $this->do_api_post($data,'update_template_task_status');
	 		}else{
	 			v('update_template_task_status_ignore:'.$task_id.' status:'.$task_status);
	 		}
	 	
	 		return false;
	 	}else{
 			v('update_template_task_status_inner:'.$task_id);
 			$data = array();
 			$data['id'] = $task_id;
 			$data['status'] = $status;
 			$data['msg'] = $msg;
 			return $this->do_api_post($data,'update_template_task_status');
	 	}

	 }
	 
	 /**
	  * 关注
	  */
	 function subscribe_wx_user($gzh_app_id,$openid)
	 {
	 	$data = array();
	 	$data['appid'] = $gzh_app_id;
	 	$data['openid'] = $openid;
	 	return $this->do_api_post($data,'subscribe_wx_user');
	 }
	 
	 /**
	  * 取消关注
	  */
	 function unsubscribe_wx_user($gzh_app_id,$openid)
	 {
	 	$data = array();
	 	$data['appid'] = $gzh_app_id;
	 	$data['openid'] = $openid;
	 	return $this->do_api_post($data,'unsubscribe_wx_user');
	 }
	 
	 /**
	 * 更新用户uid
	 */
	 function update_force_wx_user($openid,$uid)
	 {
	 	$data = array();
	 	$data['uid'] = $uid;
	 	$data['openid'] = $openid;
	 	return $this->do_api_post($data,'update_force_wx_user');
	 }
	 
	 /**
	  * 取消授权
	  */
	 function cancel_oauth_official_account($gzh_app_id)
	 {
	 	$data = array();
	 	$data['appid'] = $gzh_app_id;
	 	return $this->do_api_post($data,'cancel_oauth_official_account');
	 }
	 
	 /**
	  * 保存默认登录用户信息
	  */
	 function save_login_wx_user($data)
	 {
	 	return $this->do_api_post($data,'save_login_wx_user');
	 }
	 
	 /**
	  * 保存强关登录用户信息
	  */
	 function save_force_wx_user($data)
	 {
	 	return $this->do_api_post($data,'save_force_wx_user');
	 }
	 
	 /**
	  * 保存公众号信息
	  */
	 function save_official_account($data)
	 {
	 	return $this->do_api_post($data,'save_official_account');
	 }
	 
	 /**
	  * 保存公众号对应模板消息
	  */
	 function save_template_official_info($data)
	 {
	 	return $this->do_api_post($data,'save_template_official_info');
	 }
	 
	 /**
	  * 更新公众号对应模板消息
	  */
	 function update_template_official_info($data)
	 {
	 	return $this->do_api_post($data,'update_template_official_info');
	 }

	 /**
	  * 得到模板消息任务
	  */
	 function get_template_task($task_id)
	 {
	 	// 从redis取
	 	$redis_task_key = env('redis_task_key');
	 	if(!empty($redis_task_key)){
	 		$redis_task_key = $redis_task_key.$task_id;
	 		$template_task = Redis::hGet($redis_task_key,'wechat_msg');
	 		$template_task = objectToArray(json_decode($template_task));
	 		if(empty($template_task)){
	 			v('get_template_task_from_redis_api:'.$task_id);
	 			$data = array();
	 			$data['id'] = $task_id;
	 			return $this->do_api_post($data,'get_template_task');
	 		}
	 		return $template_task;
	 	}else{
	 		$data = array();
	 		$data['id'] = $task_id;
	 		return $this->do_api_post($data,'get_template_task');
	 	}

	 }
	 
	 /**
	  * 得到素材强关的映射关系（distribution_channel_id，openid => uid）
	  */
	 function get_material_force_subscribe_mapping($distribution_channel_id,$openid)
	 {
	 	$data = array();
	 	$data['distribution_channel_id'] = $distribution_channel_id;
	 	$data['openid'] = $openid;
	 	return $this->do_api_post($data,'get_material_force_subscribe_mapping');
	 }
	 
}