123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Services\Client;
- use App\Models\AgentInfo;
- use App\Models\AgentConfig;
- use App\Models\CoachConfig;
- use Exception;
- class CommonService
- {
-
- public function getAgentConfig($agentId)
- {
-
- $agent = AgentInfo::find($agentId);
- if (!$agent) {
- throw new Exception('代理商不存在');
- }
-
- $config = AgentConfig::where('agent_id', $agentId)->first();
- if (!$config) {
- throw new Exception('代理商配置不存在');
- }
- return [
- 'agent_id' => $config->agent_id,
- 'min_distance' => $config->min_distance,
- 'min_fee' => $config->min_fee,
- 'per_km_fee' => $config->per_km_fee,
- 'commission_rate' => $config->commission_rate,
- 'created_at' => $config->created_at,
- 'updated_at' => $config->updated_at
- ];
- }
-
- public function getCoachConfig($coachId)
- {
- $config = CoachConfig::where('coach_id', $coachId)->first();
- if (!$config) {
- throw new Exception('技师配置不存在');
- }
- return [
- 'coach_id' => $config->coach_id,
- 'delivery_fee_enabled' => $config->delivery_fee_enabled,
- 'delivery_fee_type' => $config->delivery_fee_type,
- 'created_at' => $config->created_at,
- 'updated_at' => $config->updated_at
- ];
- }
-
- public function calculateDeliveryFee($coachId, $agentId, $distance)
- {
-
- $coachConfig = $this->getCoachConfig($coachId);
-
-
- if (!$coachConfig['delivery_fee_enabled']) {
- return 0;
- }
-
- $agentConfig = $this->getAgentConfig($agentId);
-
-
- $onewayFee = $this->calculateOnewayFee($distance, $agentConfig);
-
- return $coachConfig['delivery_fee_type'] == 'round_trip' ? $onewayFee * 2 : $onewayFee;
- }
-
- public function calculateDeliveryFeeByLocation($coachId, $latitude, $longitude, $distance)
- {
-
- $agent = AgentInfo::whereRaw("ST_Contains(business_area_polygon, ST_GeomFromText('POINT($longitude $latitude)'))")
- ->first();
-
- if (!$agent) {
- throw new Exception('该区域暂无代理商');
- }
- return $this->calculateDeliveryFee($coachId, $agent->id, $distance);
- }
-
- private function calculateOnewayFee($distance, $agentConfig)
- {
-
- if ($distance <= $agentConfig['min_distance']) {
- return $agentConfig['min_fee'];
- }
-
- $extraDistance = $distance - $agentConfig['min_distance'];
-
-
- $extraFee = $extraDistance * $agentConfig['per_km_fee'];
-
-
- return $agentConfig['min_fee'] + $extraFee;
- }
- }
|