<?php
/**
 * User: Mike
 * Email: m@9026.com
 * Date: 2017/1/12
 * Time: 17:52
 */

namespace App\Repositories\Album\Criteria;




use App\Models\AlbumProductModel;
use App\Repositories\Base\Criteria;
use App\Repositories\Contracts\RepositoryInterface as Repository;

class PriceWhere extends Criteria {

    private $search = [];
    private $store_id;
    private $agent_id;

    /**
     * MultiWhere constructor.
     * @param array $search
     *
     */
    public function __construct(array $search,int $store_id,int $agent_id)
    {
        $this->search = $search;
        $this->store_id = $store_id;
        $this->agent_id = $agent_id;
    }

    /**
     * @param $model
     * @param RepositoryInterface $repository
     * @return mixed
     */
    public function apply($model, Repository $repository)
    {
        if(isset($this->search['keyword']) && $this->search['keyword']) {
            $keyword = '%'.$this->search['keyword'].'%';
            $model = $model->where([['name','like',$keyword],['store_id',$this->store_id],['agent_id',$this->agent_id]])
                ->orwhere([['id','like',$keyword],['store_id',$this->store_id],['agent_id',$this->agent_id]]);
        } else if(isset($this->search['updated_at']) && $this->search['updated_at']) {
            $model = $model->where('updated_at',$this->search['updated_at']);
        } else {
            $model = $model->where([['store_id',$this->store_id],['agent_id',$this->agent_id]]);
        }
        if (isset($this->search['cat_id']) && $this->search['cat_id'] > 0) {
            $model = $model->where([['cat_id',$this->search['cat_id']]]);
        }
        return $model;
    }

}