<?php
/**
 *   挖掘线索
 *  @author  system
 *  @version    1.0
 *  @date 2018-11-19 08:23:08
 *
 */
namespace App\Repositories\Company;

use App\Repositories\Base\Repository;


class InfoRepository extends Repository {

    public function model() {
        return \App\Models\CompanyInfoModel::class;
    }

    public function searchCompany(array $search,array $orderby=['id'=>'desc'],$pagesize=10)
    {
        $currentQuery = $this->model;
        /*企业名称*/
        if(isset($search['companyName']) && ! empty($search['companyName'])) {
            $keywords = str_replace(',','|',$search['companyName']);
            $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
                $query->where('companyName'  , 'REGEXP', $keywords);
            });
        }
        /*所在地区*/
        if(isset($search['district']) && ! empty($search['district'])) {
            $district = $search['district'];
            if (is_array($district)) $district = join('|',$district) ;

            $currentQuery = $currentQuery->where(function ($query) use ($district) {
                $query->where('district','REGEXP', $district);
            });
        }
        /*企业类型*/
        if(isset($search['entType']) && ! empty($search['entType'])) {
            $entType = $search['entType'];
            if (is_array($entType)) $entType = join('|',$entType) ;

            $currentQuery = $currentQuery->where(function ($query) use ($entType) {
                $query->where('entType','REGEXP',$entType);
            });
        }
        /*经营状态*/
        if(isset($search['openStatus']) && ! empty($search['openStatus'])) {
            $openStatus = $search['openStatus'];
            if (is_array($openStatus)) $openStatus = join('|',$openStatus) ;

            $currentQuery = $currentQuery->where(function ($query) use ($openStatus) {
                $query->where('openStatus','REGEXP',$openStatus);
            });
        }
        /*所属行业*/
        if(isset($search['industry']) && ! empty($search['industry'])) {
            $industry = $search['industry'];
            if (is_array($industry)) $industry = join('|',$industry) ;

            $currentQuery = $currentQuery->where(function ($query) use ($industry) {
                $query->where('industry','REGEXP',$industry);
            });
        }
        /*注册资本*/
        if(isset($search['regCapital']) && ! empty($search['regCapital'])) {
            $regCapital = explode('-',$search['regCapital']);

            $currentQuery = $currentQuery->where(function ($query) use ($regCapital) {
                if(!$regCapital[1]){
                    $query->where('regCapital','>=',(int)$regCapital[0]);
                }else{
                    $query->where('regCapital','>=',(int)$regCapital[0])
                          ->where('regCapital','<',(int)$regCapital[1]);
                }
            });
        }

        if($orderby && is_array($orderby)){
            foreach ($orderby AS $field => $value){
                $currentQuery = $currentQuery->orderBy($field, $value);
            }
        }
        $currentQuery = $currentQuery->paginate($pagesize);
        return $currentQuery;
    }
    
}
