<?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['company_name']) && ! empty($search['company_name'])) {
            $keywords = str_replace(',','|',$search['company_name']);
            $currentQuery = $currentQuery->where(function ($query) use ($keywords) {
                $query->where('company_name'  , '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['ent_type']) && ! empty($search['ent_type'])) {
            $ent_type = $search['ent_type'];
            if (is_array($ent_type)) $ent_type = join('|',$ent_type) ;

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

            $currentQuery = $currentQuery->where(function ($query) use ($open_status) {
                $query->where('open_status','REGEXP',$open_status);
            });
        }
        /*所属行业*/
        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['reg_capital']) && ! empty($search['reg_capital'])) {
            $reg_capital = explode('-',$search['reg_capital']);

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

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