['mw.sn', 'mw.real_name', 'mw.account', 'mw.password', 'mw.mobile', 'mw.sex', 'mw.channel', 'mw.is_disable', 'mw.is_new_user', 'mw.create_time', 'mw.update_time'], ]; } public function queryWhere(){ $where = []; // 用户昵称 if (isset($this->params['nickname']) && !empty($this->params['nickname'])) { $where[] = ['mw.nickname|mw.worker_number', 'like','%' .$this->params['nickname'] . '%']; } if (isset($this->params['worker_number']) && !empty($this->params['worker_number'])) { $where[] = ['mw.worker_number', 'like','%' .$this->params['worker_number'] . '%']; } if (isset($this->params['worker_exp_type']) && !empty($this->params['worker_exp_type'])) { $where[] = ['mwr.maintain_exp_type', '=', $this->params['worker_exp_type']]; } if(isset($this->params['min_work_number']) && is_numeric($this->params['min_work_number'])){ $where[] =[ 'work_total','>=',$this->params['min_work_number']]; } if(isset($this->params['max_work_number']) && is_numeric($this->params['max_work_number'])){ $where[] =[ 'work_total','work_total <= ',$this->params['max_work_number']]; } return $where; } /** * @notes 获取列表 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author likeadmin * @date 2024/07/10 18:17 */ public function lists(): array { $customer_lon = 0; $customer_lat = 0; if(isset($this->params['order_id']) && $this->params['order_id']){ $lon_lat = (ServiceWork::where('id',$this->params['order_id'])->column("lon,lat,goods_category_id"))[0]; $customer_lon = $lon_lat['lon']; $customer_lat = $lon_lat['lat']; $goods_category_id = $lon_lat['goods_category_id']; // whereFindInSet('category_ids',$goods_category_id)-> } // 无人在0,0坐标上 if($customer_lon && $customer_lat){ // 查出所有的师傅 -> 计算距离 -> 按距离排序 -> 分页 $allList = MasterWorker::alias('mw') ->join('master_worker_register mwr', 'mwr.worker_id = mw.id') ->whereFindInSet('mw.category_ids',$goods_category_id) ->where($this->searchWhere) ->where($this->queryWhere()) ->field([ 'mw.*', 'mwr.maintain_exp_type', 'mwr.other_exp_type', ]) //->limit($this->limitOffset, $this->limitLength) ->order(['mw.id' => 'desc']) ->select() ->toArray(); $allList = array_column($allList, null, 'id'); $mastersDistances = $this->mastersDistance($allList,$customer_lon,$customer_lat); $list = $this->limitPage($allList,$mastersDistances,$this->params['page_no']??1,$this->params['page_size']??10); $this->count = count($allList); }else{ $list = MasterWorker::alias('mw') ->join('master_worker_register mwr', 'mwr.worker_id = mw.id') ->whereFindInSet('mw.category_ids',$goods_category_id) ->where($this->searchWhere) ->where($this->queryWhere()) ->field([ 'mw.*', 'mwr.maintain_exp_type', 'mwr.other_exp_type', ]) ->limit($this->limitOffset, $this->limitLength) ->order(['mw.id' => 'desc']) ->select() ->toArray(); $this->count = MasterWorker::alias('mw') ->join('master_worker_register mwr', 'mwr.worker_id = mw.id') ->field([ 'mw.*']) ->where($this->searchWhere)->where($this->queryWhere()) ->count(); } return $list; } /** * @notes 获取数量 * @return int * @author likeadmin * @date 2024/07/10 18:17 */ public function count(): int { /*return MasterWorker::alias('mw') ->join('master_worker_register mwr', 'mwr.worker_id = mw.id') ->field([ 'mw.*']) ->where($this->searchWhere)->where($this->queryWhere()) ->count();*/ return $this->count; } private function limitPage($allList,$data,$page,$itemsPerPage) { $startIndex = (($page??1) - 1) * $itemsPerPage; $currentPageData = array_slice($data, $startIndex, $itemsPerPage, true); $lon_lat_masters = []; foreach ($currentPageData as $key => $value){ $lon_lat_masters[$key] = $allList[$key]; $lon_lat_masters[$key]['user_distance'] = number_format($value,2); } return $lon_lat_masters?array_values($lon_lat_masters):[]; } private function mastersDistance($masters,$customer_lon,$customer_lat) { $nearbyMasters = []; foreach ($masters as $master) { $distance = $this->haversineDistance($customer_lat, $customer_lon, $master['lat'], $master['lon']); $nearbyMasters[$master['id']] = $distance; /*if ($distance <= 10000) { // 距离小于或等于10公里 $nearbyMasters[] = $master; }*/ } asort($nearbyMasters); return $nearbyMasters; } private function haversineDistance($lat1, $lon1, $lat2, $lon2) { $lat1 = deg2rad($lat1); $lon1 = deg2rad($lon1); $lat2 = deg2rad($lat2); $lon2 = deg2rad($lon2); $dlon = $lon2 - $lon1; $dlat = $lat2 - $lat1; $a = sin($dlat / 2)**2 + cos($lat1) * cos($lat2) * sin($dlon / 2)**2; $c = 2 * asin(sqrt($a)); $r = 6371; // 地球平均半径,单位为公里 $distance = $c * $r; return $distance; } }