<?php

namespace App\Modules\Activity\Models;

use Illuminate\Database\Eloquent\Model;

class Activity extends Model
{
	protected $connection = 'api_mysql';
	
    protected $table = 'activity';
    protected $fillable = ['id', 'name', 'start_time', 'end_time', 'activity_page', 'product_id', 'token', 'default_template_id', 'customer_msg', 'distribution_channel_id', 'setting','type','create_type','price_title','priority'];

    //查询活动信息
    static function search($params = [], $is_all = false)
    {
        $search_obj = self::join('products', 'products.id', '=', 'activity.product_id')
            ->select('activity.id', 'activity.name', 'activity.created_at', 'activity.start_time', 'activity.end_time', 'activity.activity_page', 'products.price', 'products.given');
        if (isset($params['activity_id']) && $params['activity_id']) $search_obj->where('activity.id', $params['activity_id']);
        if (isset($params['begin_time']) && $params['begin_time']) $search_obj->where('start_time', '>=', $params['begin_time']);
        if (isset($params['end_time']) && $params['end_time']) $search_obj->where('end_time', '<=', $params['end_time'] . ' 23:59:59');
        if (isset($params['distribution_channel_id'])) $search_obj->whereIn('activity.distribution_channel_id', $params['distribution_channel_id']);
        if ($is_all) {
            return $search_obj->where('token', '!=', '')->orderBy('activity.created_at', 'desc')->get();
        } else {
            return $search_obj->where('token', '!=', '')->orderBy('activity.created_at', 'desc')->paginate();
        }
    }

    //获取活动ID
    static function getActivityIds($params)
    {
        $search_obj = self::orderBy('id', 'desc');
        if (isset($params['name'])) {
            $name = trim($params['name']);
            $search_obj->where('name', 'like', "%{$name}%");
        }
        return $search_obj->pluck('id');
    }
}
