<?php

namespace App\Modules\Ali\Models;

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

class AliSendOrder extends Model
{
    protected $table = 'ali_send_orders';
    protected $fillable = [
        'name',
        'promotion_type',
        'distribution_channel_id',
        'fan_num',
        'cost',
        'is_enable',
        'pre_send_date',
        'star_num',
        'remark',
        'send_time'
    ];
 
    /**
     * 创建推广派单
     */
    static function createOrder($data)
    {
        return self::firstOrCreate($data);
    }

    /**
     * 删除派单
     * @param $id 派单id
     * @param $distribution_channel_id 渠道id
     * @return
     */
    static function removeSendOrder($id, $distribution_channel_id)
    {
        return self::where('id', $id)->where('distribution_channel_id', $distribution_channel_id)->update(['is_enable' => 0]);
    }
    
    /**
     * 获取派单信息
     */
    static function search($params, $isAll = false)
    {
    	$search_object = self::where('is_enable', 1)->orderBy('id', 'desc');
 
    	if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) {
    		$search_object->where('ali_send_orders.distribution_channel_id', $params['distribution_channel_id']);
    	}
    
    	if (isset($params['name']) && $params['name']) {
    		$search_object->where('ali_send_orders.name', 'like', '%' . $params['name'] . '%');
    	}
    
    	if (isset($params['start_time']) && $params['start_time']) {
    		$search_object->where('ali_send_orders.created_at', '>=', $params['start_time']);
    	}
    
    	if (isset($params['end_time']) && $params['end_time']) {
    		$search_object->where('ali_send_orders.created_at', '<=', $params['end_time']);
    	}
    
    	if (isset($params['send_time_start_time']) && $params['send_time_start_time']) {
    		$search_object->where('ali_send_orders.send_time', '>=', $params['send_time_start_time']);
    	}
    
    	if (isset($params['send_time_end_time']) && $params['send_time_end_time']) {
    		$search_object->where('ali_send_orders.send_time', '<=', $params['send_time_end_time']);
    	}
    
    	if (isset($params['pre_send_date_end']) && $params['pre_send_date_end']) {
    		$search_object->where('ali_send_orders.pre_send_date', '<=', $params['pre_send_date_end']);
    	}
    
    	if (isset($params['pre_send_date_start']) && $params['pre_send_date_start']) {
    		$search_object->where('ali_send_orders.pre_send_date', '>=', $params['pre_send_date_start']);
    	}
    
    	if (isset($params['start_send_time']) && $params['start_send_time']) {
    		$search_object->where('ali_send_orders.send_time', '>=', $params['start_send_time']);
    	}
    
    	if (isset($params['end_send_time']) && $params['end_send_time']) {
    		$search_object->where('ali_send_orders.send_time', '<=', $params['end_send_time']);
    	}
    
    	if (isset($params['promotion_type']) && $params['promotion_type']) {
    		$search_object->where('ali_send_orders.promotion_type', $params['promotion_type']);
    	}
    
    	if (isset($params['id']) && $params['id']) {
    		$search_object->where('ali_send_orders.id', $params['id']);
    	}

    	if ($isAll) {
    		return $search_object->get();
    	} else {
    		return $search_object->paginate();
    	}
    }
    
   
}
