<?php
/**
 * Created by sublime.
 * User: wosinC
 * Date: 2017/12/2
 * Time: 上午11:39
 */

namespace App\Modules\OfficialAccount\Services;

use App\Modules\OfficialAccount\Models\WechatTemplateMsgs;
use App\Modules\OfficialAccount\Models\CustomSendMsgs;
use App\Modules\OfficialAccount\Models\BatchCustomSendMsgs;
use Redis;


class WechatTemplateService
{

    /**
    * 更新模板消息或者客服消息状态（灵杰 wechat 调用）
    */
    static function updateWechatTemplateStatus($wechatTemplatePrams)
    {

    	try {

    		\Log::info('==========================更新 模板消息/客服消息推送状态'.$wechatTemplatePrams['id']);

            $task_id = $wechatTemplatePrams['id'];
            $taskidArray  = explode('_', $task_id);
            if (count($taskidArray)>1) {
                if ($taskidArray[0] == 'custom') {

                    \Log::info('updateWechatTemplateStatus：'.$task_id.' status:'.$wechatTemplatePrams['status']);
                    $customSendMsgs = CustomSendMsgs::customSendMsgsByTaskid($task_id);

                    if(!empty($customSendMsgs)) {
                        $customSendMsgs['status'] = $wechatTemplatePrams['status'];
                        $customSendMsgs->save();
                        $officialAccountArray = $customSendMsgs->toArray();
                        Redis::hset('send_wechat_msg:task_id:'.$customSendMsgs['task_id'], 'wechat_msg', json_encode($officialAccountArray));

                        // 如果是智能托管有批次号，更新下批次状态
                        if($customSendMsgs['trusteeship'] == 1 && $wechatTemplatePrams['status'] == 3){
                        	\Log::info('try_update_batch_custom_send_msgs_status:'.$customSendMsgs['id']);
                        	$batchCustomMsg = BatchCustomSendMsgs::batchCustomMsgById($customSendMsgs['batch_no']);
                        	if(isset($batchCustomMsg->status) && $batchCustomMsg->status !=3 ){
                        		\Log::info('update_batch_custom_send_msgs_status:'.$customSendMsgs['id'].' batch_no:'.$customSendMsgs['batch_no']);
                        		BatchCustomSendMsgs::updateBatchCustomSendMsgStatusById($customSendMsgs['batch_no'],3,0);
                        	}else{
                        		\Log::info('already_update_batch_custom_send_msgs_status:'.$customSendMsgs['id'].' batch_no:'.$customSendMsgs['batch_no']);
                        	}
                        }else{
                        	\Log::info('not_update_batch_custom_send_msgs_status:'.$customSendMsgs['id']);
                        }

                        return 1;
                    }else{
                        \Log::info('==========================客服消息 数据未找到'.$task_id);
                        return 2;
                    }
                }
                
            }else{

                \Log::info('==========================模板消息推送'.$task_id);
                $wechatTemplateMsgs = WechatTemplateMsgs::wechatTemplateMsgsById($wechatTemplatePrams['id']);

                if(!empty($wechatTemplateMsgs)) {
                    
                   
                    $wechatTemplateMsgs['status'] = $wechatTemplatePrams['status'];

                    $wechatTemplateMsgs->save();

                    $officialAccountArray = $wechatTemplateMsgs->toArray();
                    Redis::hset('send_wechat_msg:task_id:'.$wechatTemplateMsgs['id'], 'wechat_msg', json_encode($officialAccountArray));

//                    Redis::hset('send_wechat_msg:task_id:'.$wechatTemplateMsgs['id'], 'wechat_msg', '[{id:'.$wechatTemplateMsgs['id'].'},{appid:'.$wechatTemplateMsgs['appid'].'},{common_template_id:'.$wechatTemplateMsgs['common_template_id'].'},{name:'.$wechatTemplateMsgs['name'].'},{send_time:'.$wechatTemplateMsgs['send_time'].'},{template_content:'.$wechatTemplateMsgs['template_content'].'},{redirect_url:'.$wechatTemplateMsgs['redirect_url'].'},{distribution_channel_id:'.$wechatTemplateMsgs['distribution_channel_id'].'},{remark:'.$wechatTemplateMsgs['remark'].'},{subscribe_time:'.$wechatTemplateMsgs['subscribe_time'].'},{sex:'.$wechatTemplateMsgs['sex'].'},{balance:'.$wechatTemplateMsgs['balance'].'},{order_type:'.$wechatTemplateMsgs['order_type'].'},{category_id:'.$wechatTemplateMsgs['category_id'].'}]');


                    return 1;
                }else{
                    \Log::info('==========================模板消息 数据未找到'.$task_id);
                    return 2;
                }

            }

    		

    	} catch (\Exception $e) {
            \Log::info($e->getMessage());
    		return 0;
    	}

    }

}