<?php
/**
 * 微信接口中间转发service
 */

namespace App\Modules\WechatInner\Services;

use App\Modules\OfficialAccount\Services\ForceSubscribeService;
use App\Http\Controllers\Wechat\OfficialAccount\OfficialInteractiveEventController;
use App\Modules\OfficialAccount\Models\ForceSubscribeUsers;
use App\Modules\OfficialAccount\Models\TempForceSubscribeUsers;
use App\Modules\OfficialAccount\Models\OfficialAccount;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\User\Services\UserService;
use App\Modules\Subscribe\Services\OrderService;
use App\Modules\Subscribe\Services\YearOrderService;
use App\Modules\User\Models\YqMove;
use App\Libs\OSS;
use Illuminate\Http\Request;
use Redis;
use App\Jobs\SendTemplate;
use App\Jobs\SendStatisticsList;
use GuzzleHttp\Client;

class WechatInnerService
{

	//-----------强关用户---------------

	/**
	* 通过appid和openid获取强关用户
	*/
	static function get_event_data($post_data,$event)
    {
    	$params = [];
    	// 获取用户
        if($event == 'get_force_wx_user'){
        	$params['appid'] = $post_data['appid'];
        	$params['openid'] = $post_data['openid'];
        	$force_subscribe = ForceSubscribeService::forceSubscribeUsersByAppidAndOpenId($params);
        	if(count($force_subscribe) > 1){
        		$force_subscribe = $force_subscribe->toArray();
        	}
        	return $force_subscribe;
        	
        }elseif($event == 'update_user_interaction_time'){
        	$params['appid'] = $post_data['appid'];
        	$params['openid'] = $post_data['openid'];
        	$params['distribution_channel_id'] = $post_data['distribution_channel_id'];
        	$params['last_interactive_time'] = $post_data['last_interactive_time'];
        	
        	$resultStatus = ForceSubscribeService::forceSubscribeUsersLastTimeUpdate($params);
        	// 更新交互中间表
        	$tempResultStatus = ForceSubscribeService::tempForceSubscribeUsersLastTimeUpdate($params);
        	
        }
        // get_event_content
        elseif($event == 'get_event_content'){
        	$request = new Request();
        	foreach($post_data as $key=>$val){
        		$request->offsetSet($key, $val);
        	}
        	$inner_event = new OfficialInteractiveEventController();
        	$inner_event_data = $inner_event->officialInteractiveEventFeedback($request);
        	if(empty($inner_event_data)){
        		v('empty_inner:'.json_encode($post_data));
        		return null;
        	}
        	$inner_data = $inner_event_data->getData();
        	$inner_data = objectToArray($inner_data);
        	$last_data = isset($inner_data['data'])?$inner_data['data']:null;
        	v('inter_last_data');v($last_data);
        	return $last_data;
        }
        // 素材映射
        elseif($event == 'get_material_force_subscribe_mapping'){
        	
        }
        // 取关
        elseif($event == 'unsubscribe_wx_user'){
        	
        }
        
        return true;
    }

}