<?php

namespace App\Http\Controllers\Wechat\Template;

use App\Http\Models\OfficialAccount;

use App\Http\Requests;
use App\Http\Controllers\WechatController;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use EasyWeChat\Foundation\Application;
use App\Http\Models\WechatUser;
use App\Http\Models\WechatTemplateSendInfo;
use App\Http\Models\WechatTemplateTask;
use App\Http\Models\WechatTemplateOfficialInfo;


/**
 * 模板消息定时发送类
 * @author zhoulingjie
 *
 */
class TemplateWorksController extends WechatController
{
    
    public function __construct($template_info)
    {
    	$this->template_info = $template_info;
//     	v('$template_info');v($this->template_info);
    	$this->gzh_app_id = isset($this->template_info['appid'])?$this->template_info['appid']:'';
    	if(!empty($this->gzh_app_id)){
    		parent::__construct($this->gzh_app_id);
    	}else{
    		v('template_work_appid is null');
    	}
    	$this->error_msg = '';
    }
    
    /**
     * 执行模板消息
     */
    public function do_work()
    {
    	if(empty($this->gzh_app_id)) return false;
    	v('do_work_start2');
    	
    	// 检查表数据
    	$openid = $this->template_info['openid'];
    	$appid = $this->template_info['appid'];
    	$url = $this->template_info['url'];
    	$task_id = $this->template_info['task_id'];
    	$type = $this->template_info['type'];
    	$template_id = $this->template_info['template_id'];
    	$template_content = $this->template_info['template_content'];
    	if(empty($openid) || empty($task_id) || empty($template_id)){
    		v('invalid_null_task_id:'.$task_id.' openid:'.$openid);
    		return false;
    	}
    	
    	v('template_start:openid:'.$openid);
    	
    	$template_task = $this->WechatApi->get_template_task($task_id);
    	$status = isset($template_task['status']) ? $template_task['status']:'';
//     	v('task_id:'.$task_id.' status:'.$status);
    	if($status == 4){
    		v('task_id_close:'.$task_id);
    		return false;
    	}
    
    	$this->send_one_template($openid,$template_id,$url,$template_content);
    	
    	if($type == 'last_task' && $status != 3){
    		v('update_tamplate_task_status:'.$task_id);
    		$status = 3;
    		$this->WechatApi->update_template_task_status($task_id, $status, $this->error_msg);
    	}
    	
    	// TODO  休眠200ms，外面的调度相当于for循环，直接不停止cpu会飙高，sleep的方式有待考证
    	usleep(200000);
    	v('update_tamplate_task_end:'.$task_id);

    }
    
    function send_one_template($openid,$templateId,$url,$data){
    	try{
    		v('send_to_openid:'.$openid.' templateId:'.$templateId.' url:'.$url);
    		$data = objectToArray(json_decode($data));
    		$send_data = array();
    		foreach($data  as $_data){
    			foreach($_data as $key=>$one_data){
    				$send_data[$key] = $one_data;
    			}
    		}
//     		v($send_data);
    		$result = $this->app->notice->uses($templateId)->withUrl($url)->andData($send_data)->andReceiver($openid)->send();
//     		v('send_one_template_res:'.$openid.' res:'.json_encode($result));
    	}
    	// 加上\ 全局抓取
    	catch(\Exception $e){
    		v('send_openid_ept:'.$openid.' info:'.$e->getMessage());
    		$this->error_msg = $e->getMessage();
    	}
    }
  
    
}