<?php


namespace App\Traits;


trait ModelSearchWay
{
    private function isSearchLike($str)
    {
        if (substr($str, 0, 1) == "%" || substr($str, strlen($str) - 1, 1) == "%") {
            return true;
        }
        return false;
    }

    private function searchWay($query, $param, $column)
    {
        if ($this->isSearchLike($param)) {
            $query->where($column, 'like', $param);
        } else {
            $query->whereIn($column, array_filter(preg_split('/[,， ]+/is', $param)));
        }
        return $query;
    }

    private function sqlSearchWay($sql,$condition,$column)
    {
        if ($condition){
            if (strpos($condition, ',') || strpos($condition, '，') || strpos($condition, ' ')) {
                $arr = array_filter(preg_split('/[,， ]+/is', $condition));
                $sql .= ' and '.$column.' in (';
                foreach ($arr as $index => $arr){
                    if ($index != 0)$sql .= ',';
                    $sql .= "'".$arr."'";
                }
                $sql .= ') ';
                unset($condition);
            } else {
                $sql .= " and $column like '".$condition."' ";
            }
        }
        return $sql;
    }
}
