AgentService.php 3.0 KB

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