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

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


class SmartPushMsgService
{

    /**
    * 通过id获取自定义智能推送内容
    */
    static function smartPushMsgById($smartPushPrams)
    {

        return SmartPushMsgs::smartPushMsgsById($smartPushPrams['id']);

    }

    /**
    * 获取推送自定义智能推送内容消息列表
    */
    static function smartPushMsgBydistributionChannelId($smartPushPrams)
    {

        return SmartPushMsgs::smartPushMsgsBydistributionChannelId($smartPushPrams['distribution_channel_id']);

    }
    
    /**
     * 根据渠道和分类、性别获取推送自定义智能推送内容消息
     */
    static function smartPushByDistributionChannelIdAndCategorySex($smartPushPrams)
    {
    
    	return SmartPushMsgs::smartPushByDistributionChannelIdAndCategorySex($smartPushPrams['distribution_channel_id'],$smartPushPrams['category_type'],$smartPushPrams['sex']);
    
    }
    
    /**
     * 根据渠道和分类、性别获取推送自定义智能推送内容消息
     */
    static function checkSexAllEdit($smartPushPrams)
    {
    
    	return SmartPushMsgs::checkSexAllEdit($smartPushPrams['distribution_channel_id'],$smartPushPrams['category_type']);
    
    }
    

    
    /**
     * 更新状态
     */
    static function updateSmartPushMsgStatus($smartPushPrams)
    {
    
    	return SmartPushMsgs::updateSmartPushMsgStatus($smartPushPrams['id'],$smartPushPrams['status']);
    
    }

    /**
    * 保存或修改自定义智能推送内容
    */
    static function addSmartPushMsg($smartPushMsgPrams)
    {

    	try {
    		\Log::info($smartPushMsgPrams);
    		$smartPushMsg = SmartPushMsgs::smartPushByDistributionChannelIdAndCategorySex($smartPushMsgPrams['distribution_channel_id'],$smartPushMsgPrams['category_type'],$smartPushMsgPrams['sex']);
            \Log::info($smartPushMsg);
	        if(empty($smartPushMsg)) {
	        	\Log::info('addSmartPushMsg_create');
	            SmartPushMsgs::firstOrCreate($smartPushMsgPrams);
	            return 1;
	        }else{
	        	\Log::info('addSmartPushMsg_exist，channel_id:'.$smartPushMsgPrams['distribution_channel_id'].' category:'.$smartPushMsgPrams['category_type']);
	        	return 1;
	        }

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

    	} 

    }
    
    /**
     * 修改自定义智能推送内容
     */
    static function updateSmartPushMsg($smartPushMsgPrams)
    {
    
    	try {
    
    		$smartPushMsg = SmartPushMsgs::smartPushMsgsById($smartPushMsgPrams['id']);
    		\Log::info('updateSmartPushMsg_origin');\Log::info($smartPushMsg);
    		if(!empty($smartPushMsg)) {
    			$smartPushMsg->name = $smartPushMsgPrams['name'];
    			$smartPushMsg->category_type = $smartPushMsgPrams['category_type'];
    			$smartPushMsg->content = $smartPushMsgPrams['content'];
    			$smartPushMsg->sex = $smartPushMsgPrams['sex'];
    			$smartPushMsg->description = $smartPushMsgPrams['description'];
    			$smartPushMsg->book_name = $smartPushMsgPrams['book_name'];
    			$smartPushMsg->chapter_name = $smartPushMsgPrams['chapter_name'];
    			$smartPushMsg->distribution_channel_id = $smartPushMsgPrams['distribution_channel_id'];
    			$smartPushMsg->status = $smartPushMsgPrams['status'];
    			\Log::info('after_updateSmartPushMsg:');
    			\Log::info($smartPushMsg);
    			$smartPushMsg->save();
    
    			return 1;
    
    		}else{
    			\Log::info('updateSmartPushMsg_not_exist');
    			return 2;
    			 
    		}
    
    	} catch (\Exception $e) {
    		\Log::info("updateSmartPushMsg_ept：".$e);
    
    		return 0;
    
    	}
    
    }


}