CommonService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\AgentInfo;
  4. use App\Models\AgentConfig;
  5. use App\Models\CoachConfig;
  6. use Exception;
  7. class CommonService
  8. {
  9. /**
  10. * 获取代理商配置
  11. *
  12. * @param int $agentId 代理商编号
  13. * @return array
  14. * @throws Exception
  15. */
  16. public function getAgentConfig($agentId)
  17. {
  18. // 检查代理商是否存在
  19. $agent = AgentInfo::find($agentId);
  20. if (!$agent) {
  21. throw new Exception('代理商不存在');
  22. }
  23. // 获取代理商配置
  24. $config = AgentConfig::where('agent_id', $agentId)->first();
  25. if (!$config) {
  26. throw new Exception('代理商配置不存在');
  27. }
  28. return [
  29. 'agent_id' => $config->agent_id,
  30. 'min_distance' => $config->min_distance, // 最小路程
  31. 'min_fee' => $config->min_fee, // 最小路费金额
  32. 'per_km_fee' => $config->per_km_fee, // 每公里路程单价
  33. 'commission_rate' => $config->commission_rate, // 佣金比例
  34. 'created_at' => $config->created_at,
  35. 'updated_at' => $config->updated_at
  36. ];
  37. }
  38. /**
  39. * 获取技师配置
  40. *
  41. * @param int $coachId 技师编号
  42. * @return array
  43. * @throws Exception
  44. */
  45. public function getCoachConfig($coachId)
  46. {
  47. $config = CoachConfig::where('coach_id', $coachId)->first();
  48. if (!$config) {
  49. throw new Exception('技师配置不存在');
  50. }
  51. return [
  52. 'coach_id' => $config->coach_id,
  53. 'delivery_fee_enabled' => $config->delivery_fee_enabled, // 是否收取路费
  54. 'delivery_fee_type' => $config->delivery_fee_type, // 路费类型:单程/往返
  55. 'created_at' => $config->created_at,
  56. 'updated_at' => $config->updated_at
  57. ];
  58. }
  59. /**
  60. * 根据代理商ID计算路费
  61. */
  62. public function calculateDeliveryFee($coachId, $agentId, $distance)
  63. {
  64. // 获取技师配置
  65. $coachConfig = $this->getCoachConfig($coachId);
  66. // 如果技师不收取路费
  67. if (!$coachConfig['delivery_fee_enabled']) {
  68. return 0;
  69. }
  70. // 获取代理商配置
  71. $agentConfig = $this->getAgentConfig($agentId);
  72. // 计算单程路费
  73. $onewayFee = $this->calculateOnewayFee($distance, $agentConfig);
  74. // 根据技师配置返回单程或往返路费
  75. return $coachConfig['delivery_fee_type'] == 'round_trip' ? $onewayFee * 2 : $onewayFee;
  76. }
  77. /**
  78. * 根据位置计算路费
  79. */
  80. public function calculateDeliveryFeeByLocation($coachId, $latitude, $longitude, $distance)
  81. {
  82. // 根据经纬度查询代理商
  83. $agent = AgentInfo::whereRaw("ST_Contains(business_area_polygon, ST_GeomFromText('POINT($longitude $latitude)'))")
  84. ->first();
  85. if (!$agent) {
  86. throw new Exception('该区域暂无代理商');
  87. }
  88. return $this->calculateDeliveryFee($coachId, $agent->id, $distance);
  89. }
  90. /**
  91. * 计算单程路费
  92. */
  93. private function calculateOnewayFee($distance, $agentConfig)
  94. {
  95. // 如果路程小于等于最小路程
  96. if ($distance <= $agentConfig['min_distance']) {
  97. return $agentConfig['min_fee'];
  98. }
  99. // 超出的路程
  100. $extraDistance = $distance - $agentConfig['min_distance'];
  101. // 计算超出部分的费用
  102. $extraFee = $extraDistance * $agentConfig['per_km_fee'];
  103. // 返回总费用
  104. return $agentConfig['min_fee'] + $extraFee;
  105. }
  106. }