AgentService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\AgentInfo;
  4. use App\Models\AgentConfig;
  5. use App\Models\Project;
  6. use Exception;
  7. class AgentService
  8. {
  9. /**
  10. * 获取代理商配置
  11. */
  12. public function getAgentConfig($agentId)
  13. {
  14. // 检查代理商是否存在
  15. $agent = AgentInfo::where('id', $agentId)
  16. ->where('state', 'enable')
  17. ->firstOrFail();
  18. // 获取代理商配置
  19. $config = AgentConfig::where('agent_id', $agentId)->firstOrFail();
  20. return [
  21. 'min_distance' => $config->min_distance,
  22. 'min_fee' => $config->min_fee,
  23. 'per_km_fee' => $config->per_km_fee
  24. ];
  25. }
  26. /**
  27. * 获取代理商
  28. */
  29. public function getAgent($areaCode)
  30. {
  31. // 根据区域编码查询代理商
  32. $agent = AgentInfo::where('business_area', 'like', "%{$areaCode}%")
  33. ->where('state', 'enable')
  34. ->first();
  35. if (!$agent) {
  36. // 如果找不到,按照市、省、全国逐级查找
  37. $areaCodeParts = str_split($areaCode, 2);
  38. foreach ($areaCodeParts as $index => $part) {
  39. $searchCode = implode('', array_slice($areaCodeParts, 0, $index + 1));
  40. $agent = AgentInfo::where('business_area', 'like', "%{$searchCode}%")
  41. ->where('state', 'enable')
  42. ->first();
  43. if ($agent) {
  44. break;
  45. }
  46. }
  47. }
  48. return $agent;
  49. }
  50. /**
  51. * 获取代理商项目列表
  52. */
  53. public function getProjectList($categoryId, $agentId)
  54. {
  55. // 查询代理商
  56. $agent = AgentInfo::where('id', $agentId)
  57. ->where('state', 'enable')
  58. ->firstOrFail();
  59. // 查询项目列表
  60. $query = Project::where('state', 'enable');
  61. if ($categoryId) {
  62. $query->where('category_id', $categoryId);
  63. }
  64. $projects = $query->whereHas('agents', function($q) use ($agentId) {
  65. $q->where('agent_id', $agentId);
  66. })
  67. ->with(['category:id,name'])
  68. ->orderBy('sort', 'desc')
  69. ->paginate(10);
  70. return $projects;
  71. }
  72. /**
  73. * 获取代理商项目详情
  74. */
  75. public function getProjectDetail($projectId, $agentId)
  76. {
  77. // 查询代理商
  78. $agent = AgentInfo::where('id', $agentId)
  79. ->where('state', 'enable')
  80. ->firstOrFail();
  81. // 查询项目
  82. $project = Project::where('id', $projectId)
  83. ->where('state', 'enable')
  84. ->whereHas('agents', function($q) use ($agentId) {
  85. $q->where('agent_id', $agentId);
  86. })
  87. ->with([
  88. 'category:id,name',
  89. 'agents' => function($q) use ($agentId) {
  90. $q->where('agent_id', $agentId);
  91. }
  92. ])
  93. ->firstOrFail();
  94. return $project;
  95. }
  96. }