<?php

namespace App\Modules\OfficialAccount\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class SmartPushMsgs extends Model
{
    protected $tables = 'smart_push_msgs';
    protected $fillable = ['appid', 'name','category_type', 'distribution_channel_id','content','sex','status','user_num','created_at','updated_at','description','book_name','chapter_name'];

    
    /**
     * 根据渠道获取自定义智能推送内容列表
     */
    static function smartPushMsgsBydistributionChannelId($distribtion_channel_id)
    {
    	return self::where('distribution_channel_id', $distribtion_channel_id)->where('status', 1)->get();
    }
    
    /**
     * 根据渠道、分类获取自定义智能推送内容列表
     */
    static function smartPushByDistributionChannelIdAndCategorySex($distribtion_channel_id,$category_type,$sex)
    {
    	return self::where('distribution_channel_id', $distribtion_channel_id)->where('category_type', $category_type)->where('sex', $sex)->first();
    }
    
    /**
     * 根据渠道、分类获取自定义智能推送内容列表
     */
    static function checkSexAllEdit($distribtion_channel_id,$category_type)
    {
    	$man = self::smartPushByDistributionChannelIdAndCategorySex($distribtion_channel_id,$category_type,'a');
    	$women = self::smartPushByDistributionChannelIdAndCategorySex($distribtion_channel_id,$category_type,'b');
    	if(empty($man) || empty($women)){
    		return false;
    	}
    	return true;
    }
    
  
    /**
     * 根据id获取自定义智能推送内容
     */
    static function smartPushMsgsById($id)
    {
        return self::where('id', $id)->first();
    }
    
    /**
     * 更新自定义智能推送内容状态
     */
    static function updateSmartPushMsgStatus($id,$status)
    {
    	return self::where('id', $id)->update(['status'=>$status,'updated_at'=>date('Y-m-d H:i:s')]);
    }

}