master
cody 3 months ago
parent 75efd86990
commit 24c0f74893

@ -140,32 +140,19 @@ class OtherController extends CommonController
$sortName = $all['sort_name'] ?? 'distance';
$sortType = $all['sort_type'] ?? 'asc';
// 使用简单稳定的距离计算公式
// 使用简的距离计算公式
$distanceFormula = "
(6371 * acos(
GREATEST(-1, LEAST(1,
cos(radians(?)) *
cos(radians(CAST(company_latitude AS DECIMAL(10,8)))) *
cos(radians(CAST(company_longitude AS DECIMAL(10,8))) - radians(?)) +
sin(radians(?)) *
sin(radians(CAST(company_latitude AS DECIMAL(10,8))))
))
cos(radians(?)) *
cos(radians(CAST(company_latitude AS DECIMAL(10,8)))) *
cos(radians(CAST(company_longitude AS DECIMAL(10,8))) - radians(?)) +
sin(radians(?)) *
sin(radians(CAST(company_latitude AS DECIMAL(10,8))))
)) AS distance
";
$query = Company::with([
'users' => function ($query) {
$query->with([
'courseSigns' => function ($query) {
$query->with('course')->orderBy('fee_status', 'desc');
}
]);
}
])
->whereHas('users', function ($query) {
$query->where('is_schoolmate', 1);
})
->select('*')
// 先构建基础查询
$query = Company::select('*')
->selectRaw($distanceFormula, [$latitude, $longitude, $latitude])
->where(function ($query) use ($all) {
if (isset($all['company_name'])) {
@ -179,7 +166,19 @@ class OtherController extends CommonController
->whereRaw('company_longitude REGEXP \'^-?[0-9]+\.?[0-9]*$\'')
->whereRaw('company_latitude REGEXP \'^-?[0-9]+\.?[0-9]*$\'')
->whereRaw('CAST(company_longitude AS DECIMAL(10,8)) BETWEEN -180 AND 180')
->whereRaw('CAST(company_latitude AS DECIMAL(10,8)) BETWEEN -90 AND 90');
->whereRaw('CAST(company_latitude AS DECIMAL(10,8)) BETWEEN -90 AND 90')
->whereHas('users', function ($query) {
$query->where('is_schoolmate', 1);
})
->with([
'users' => function ($query) {
$query->with([
'courseSigns' => function ($query) {
$query->with('course')->orderBy('fee_status', 'desc');
}
]);
}
]);
// 根据排序字段进行排序
if ($sortName === 'distance') {
@ -191,6 +190,16 @@ class OtherController extends CommonController
$result = $query->paginate($pageSize, ['*'], 'page', $page);
// 添加调试信息
\Log::info('Distance calculation debug', [
'input_lat' => $latitude,
'input_lon' => $longitude,
'target_lat' => '31.326810',
'target_lon' => '120.421618',
'expected_distance' => 13.22,
'actual_distance' => $result->first() ? $result->first()->distance : 'N/A'
]);
return $this->success($result);
}

Loading…
Cancel
Save