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

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


class WechatKeywordMsgService
{

    /**
    * 通过id获取关键字
    */
    static function wechatKeywordMsgById($wechatKeywordPrams)
    {

        return WechatKeywordMsgs::wechatKeywordMsgsById($wechatKeywordPrams['id']);

    }

    /**
    * 获取推送关键字消息列表
    */
    static function wechatKeywordMsgBydistributionChannelId($wechatKeywordPrams)
    {

        return WechatKeywordMsgs::wechatKeywordMsgsBydistributionChannelId($wechatKeywordPrams['distribution_channel_id']);

    }
    
    /**
     * 更新状态
     */
    static function updateWechatKeywordMsgStatus($wechatKeywordPrams)
    {
    
    	return WechatKeywordMsgs::updateWechatKeywordMsgStatus($wechatKeywordPrams['id'],$wechatKeywordPrams['distribution_channel_id'],$wechatKeywordPrams['status']);
    
    }

    /**
    * 增加关键字
    */
    static function addWechatKeywordMsg($wechatKeywordMsgPrams)
    {

    	try {
    		\Log::info($wechatKeywordMsgPrams);
    		
    		$wechatKeywordMsg = WechatKeywordMsgs::wechatKeywordByDistributionChannelIdAndKeyword($wechatKeywordMsgPrams['distribution_channel_id'],$wechatKeywordMsgPrams['keyword'],1);

	        if(empty($wechatKeywordMsg)) {
	        	\Log::info('addWechatKeywordMsg_create');
	            
	            WechatKeywordMsgs::firstOrCreate($wechatKeywordMsgPrams);

	            return 1;

	        }else{
	        	\Log::info('addWechatKeywordMsg_exist');
	        	return 2;
	            
	        }

    	} catch (\Exception $e) {
            \Log::info("addWechatKeywordMsg_ept：".$e);
    		
    		return 0;

    	} 

    }
    
    /**
     * 修改关键字
     */
    static function updateWechatKeywordMsg($wechatKeywordMsgPrams)
    {
    
    	try {
    
    		$wechatKeywordMsg = WechatKeywordMsgs::wechatKeywordMsgsById($wechatKeywordMsgPrams['id']);
    		\Log::info('updateWechatKeywordMsg_origin');\Log::info($wechatKeywordMsg);
    		if(!empty($wechatKeywordMsg)) {

    			$wechatKeywordMsg->appids = $wechatKeywordMsgPrams['appids'];
    			$wechatKeywordMsg->bid = $wechatKeywordMsgPrams['bid'];
    			$wechatKeywordMsg->cid = $wechatKeywordMsgPrams['cid'];
    			$wechatKeywordMsg->book_name = $wechatKeywordMsgPrams['book_name'];
    			$wechatKeywordMsg->chapter_name = $wechatKeywordMsgPrams['chapter_name'];
    			$wechatKeywordMsg->send_title = $wechatKeywordMsgPrams['send_title'];
    			$wechatKeywordMsg->send_cover = $wechatKeywordMsgPrams['send_cover'];
    			$wechatKeywordMsg->keyword = $wechatKeywordMsgPrams['keyword'];
    			\Log::info('after_update:');
    			\Log::info($wechatKeywordMsg);
    			$wechatKeywordMsg->save();
    
    			return 1;
    
    		}else{
    			\Log::info('updateWechatKeywordMsg_not_exist');
    			return 2;
    			 
    		}
    
    	} catch (\Exception $e) {
    		\Log::info("updateWechatKeywordMsg_ept：".$e);
    
    		return 0;
    
    	}
    
    }


}