|
|
|
@ -140,20 +140,8 @@ class OtherController extends CommonController
|
|
|
|
$sortName = $all['sort_name'] ?? 'distance';
|
|
|
|
$sortName = $all['sort_name'] ?? 'distance';
|
|
|
|
$sortType = $all['sort_type'] ?? 'asc';
|
|
|
|
$sortType = $all['sort_type'] ?? 'asc';
|
|
|
|
|
|
|
|
|
|
|
|
// 使用简化的距离计算公式,避免精度问题
|
|
|
|
// 先构建基础查询,不使用SQL计算距离
|
|
|
|
$distanceFormula = "
|
|
|
|
|
|
|
|
(6371 * 2 * asin(
|
|
|
|
|
|
|
|
sqrt(
|
|
|
|
|
|
|
|
pow(sin(radians((CAST(company_latitude AS DECIMAL(10,8)) - ?) / 2)), 2) +
|
|
|
|
|
|
|
|
cos(radians(?)) * cos(radians(CAST(company_latitude AS DECIMAL(10,8)))) *
|
|
|
|
|
|
|
|
pow(sin(radians((CAST(company_longitude AS DECIMAL(10,8)) - ?) / 2)), 2)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)) AS distance
|
|
|
|
|
|
|
|
";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 先构建基础查询
|
|
|
|
|
|
|
|
$query = Company::select('*')
|
|
|
|
$query = Company::select('*')
|
|
|
|
->selectRaw($distanceFormula, [$latitude, $latitude, $longitude])
|
|
|
|
|
|
|
|
->where(function ($query) use ($all) {
|
|
|
|
->where(function ($query) use ($all) {
|
|
|
|
if (isset($all['company_name'])) {
|
|
|
|
if (isset($all['company_name'])) {
|
|
|
|
$query->where('company_name', 'like', '%' . $all['company_name'] . '%');
|
|
|
|
$query->where('company_name', 'like', '%' . $all['company_name'] . '%');
|
|
|
|
@ -195,15 +183,38 @@ class OtherController extends CommonController
|
|
|
|
]);
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
// 根据排序字段进行排序
|
|
|
|
// 根据排序字段进行排序
|
|
|
|
if ($sortName === 'distance') {
|
|
|
|
if ($sortName !== 'distance') {
|
|
|
|
// 距离排序始终按升序(从近到远)
|
|
|
|
|
|
|
|
$query->orderBy('distance', 'asc');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$query->orderBy($sortName, $sortType);
|
|
|
|
$query->orderBy($sortName, $sortType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$result = $query->paginate($pageSize, ['*'], 'page', $page);
|
|
|
|
$result = $query->paginate($pageSize, ['*'], 'page', $page);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在PHP中计算距离
|
|
|
|
|
|
|
|
foreach ($result->items() as $company) {
|
|
|
|
|
|
|
|
$companyLat = floatval($company->company_latitude);
|
|
|
|
|
|
|
|
$companyLon = floatval($company->company_longitude);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 使用Haversine公式计算距离
|
|
|
|
|
|
|
|
$distance = 6371 * acos(
|
|
|
|
|
|
|
|
cos(deg2rad($latitude)) * cos(deg2rad($companyLat)) *
|
|
|
|
|
|
|
|
cos(deg2rad($companyLon) - deg2rad($longitude)) +
|
|
|
|
|
|
|
|
sin(deg2rad($latitude)) * sin(deg2rad($companyLat))
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$company->distance = $distance;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果按距离排序,在PHP中排序
|
|
|
|
|
|
|
|
if ($sortName === 'distance') {
|
|
|
|
|
|
|
|
$items = $result->items();
|
|
|
|
|
|
|
|
usort($items, function ($a, $b) {
|
|
|
|
|
|
|
|
return $a->distance <=> $b->distance;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重新设置分页数据
|
|
|
|
|
|
|
|
$result->setCollection(collect($items));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 添加调试信息
|
|
|
|
// 添加调试信息
|
|
|
|
\Log::info('Distance calculation debug', [
|
|
|
|
\Log::info('Distance calculation debug', [
|
|
|
|
'input_lat' => $latitude,
|
|
|
|
'input_lat' => $latitude,
|
|
|
|
@ -212,8 +223,7 @@ class OtherController extends CommonController
|
|
|
|
'target_lon' => '120.421618',
|
|
|
|
'target_lon' => '120.421618',
|
|
|
|
'expected_distance' => 13.22,
|
|
|
|
'expected_distance' => 13.22,
|
|
|
|
'actual_distance' => $result->first() ? $result->first()->distance : 'N/A',
|
|
|
|
'actual_distance' => $result->first() ? $result->first()->distance : 'N/A',
|
|
|
|
'formula' => $distanceFormula,
|
|
|
|
'calculation_method' => 'PHP Haversine'
|
|
|
|
'parameters' => [$latitude, $latitude, $longitude]
|
|
|
|
|
|
|
|
]);
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
return $this->success($result);
|
|
|
|
return $this->success($result);
|
|
|
|
|