ParkingController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Filters\ApplyParkingFilter;
  4. use App\Filters\ParkingFilter;
  5. use App\Http\Requests\ParkingRequest;
  6. use App\Http\Resources\ApplyParkingResource;
  7. use App\Http\Resources\ParkingResource;
  8. use App\Models\AdminMerchant;
  9. use App\Models\AdminUser;
  10. use App\Models\AdminUserArea;
  11. use App\Models\ApplyParking;
  12. use App\Models\Area;
  13. use App\Models\Parking;
  14. use App\Utils\Admin;
  15. use App\Utils\GaodeMaps;
  16. use Illuminate\Http\Request;
  17. use App\Http\Controllers\Controller;
  18. use Illuminate\Support\Facades\Log;
  19. class ParkingController extends Controller
  20. {
  21. /**
  22. * index 停车点列表
  23. *
  24. * @param Request $request
  25. * @param ParkingFilter $filter
  26. * @return \Illuminate\Http\JsonResponse
  27. * @author Fx
  28. *
  29. */
  30. public function index(Request $request, ParkingFilter $filter)
  31. {
  32. $admin_id = Admin::user()->id;
  33. $parking = Parking::query()->filter($filter)->orderByDesc('id');
  34. if (!Admin::isAdministrator()) {
  35. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  36. if (count($area_ids) !== 0) {
  37. $parking = $parking->whereIn('area_id', $area_ids);
  38. } else {
  39. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  40. $area_id = $area_id->area_id ?? 0;
  41. $parking = $parking->where('area_id', $area_id);
  42. }
  43. }
  44. //暂时没有写分页
  45. $parking = $parking->get();
  46. return $this->ok(ParkingResource::collection($parking));
  47. //
  48. }
  49. /**
  50. * Show the form for creating a new resource.
  51. *
  52. * @return \Illuminate\Http\Response
  53. */
  54. public function create()
  55. {
  56. //
  57. }
  58. /**
  59. * store 添加停车点
  60. *
  61. * @param ParkingRequest $request
  62. * @param Parking $model
  63. * @return \Illuminate\Http\JsonResponse
  64. * @author Fx
  65. *
  66. */
  67. public function store(ParkingRequest $request, Parking $model)
  68. {
  69. //
  70. $inputs = $request->validated();
  71. $parking_fence = object_array($inputs['parking_fence']);
  72. $parking_centre = GetCenterFromDegrees($parking_fence);
  73. $inputs['parking_radius'] = round(maxDistance($parking_centre, $parking_fence) + config('systemConfig.parking_radius_fushe'), 4);
  74. $inputs['parking_centre'] = json_encode($parking_centre);
  75. $inputs['description'] = GaodeMaps::getAddress([$parking_centre['lng'], $parking_centre['lat']]) ?? '';
  76. $res = $model->create($inputs);
  77. return $this->created(ParkingResource::make($res));
  78. }
  79. /**
  80. * Display the specified resource.
  81. *
  82. * @param int $id
  83. * @return \Illuminate\Http\Response
  84. */
  85. public function show($id)
  86. {
  87. //
  88. }
  89. /**
  90. * edit 编辑停车点
  91. *
  92. * @param Parking $parking
  93. * @return \Illuminate\Http\JsonResponse
  94. * @author Fx
  95. *
  96. */
  97. public function edit(Parking $parking)
  98. {
  99. //
  100. return $this->ok(ParkingResource::make($parking));
  101. }
  102. /**
  103. * update 更新停车点
  104. *
  105. * @param ParkingRequest $request
  106. * @param Parking $parking
  107. * @return \Illuminate\Http\JsonResponse
  108. * @author Fx
  109. *
  110. */
  111. public function update(ParkingRequest $request, Parking $parking)
  112. {
  113. //
  114. $inputs = $request->validated();
  115. unset($inputs['parking_fence']);
  116. $parking->update($inputs);
  117. $parking->description = GaodeMaps::getAddress(json_decode($parking->parking_centre)) ?? '';
  118. $parking->save();
  119. return $this->ok(ParkingResource::make($parking));
  120. }
  121. /**
  122. * updateStatus 禁用启用停车点
  123. *
  124. * @param Request $request
  125. * @return \Illuminate\Http\JsonResponse
  126. * @author Fx
  127. *
  128. */
  129. public function updateStatus(Request $request)
  130. {
  131. $status = $request->get('status');
  132. $parking_id = $request->get('id') ?? '';
  133. if (empty($parking_id)) return $this->error('参数错误');
  134. $park = Parking::where('id', $parking_id)->first();
  135. $park->status = $status;
  136. $bool = $park->save();
  137. if ($bool) {
  138. return $this->created();
  139. } else {
  140. return $this->error("修改失败,请联系管理员");
  141. }
  142. }
  143. /**
  144. * Remove the specified resource from storage.
  145. *
  146. * @param int $id
  147. * @return \Illuminate\Http\Response
  148. */
  149. public function destroy(Parking $parking)
  150. {
  151. $parking->delete();
  152. return $this->noContent();
  153. }
  154. /**
  155. * 申请增加还车点 列表
  156. * @param ApplyParkingFilter $filter
  157. * @return \Illuminate\Http\JsonResponse
  158. */
  159. public function applyAddParking(ApplyParkingFilter $filter){
  160. $admin_id = Admin::user()->id;
  161. $applyModel = ApplyParking::query()
  162. ->filter($filter)
  163. ->where(AdminMerchant::getMerchantWhere());
  164. if (!Admin::isAdministrator()) {
  165. $area_ids = AdminUser::getAreaIdsByAdminId($admin_id);
  166. if (count($area_ids) !== 0) {
  167. $applyModel->whereIn('area_id', $area_ids);
  168. } else {
  169. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  170. $area_id = $area_id->area_id ?? 0;
  171. $applyModel->where('area_id', $area_id);
  172. }
  173. }
  174. $apply = $applyModel->paginate();
  175. return $this->ok(ApplyParkingResource::collection($apply));
  176. }
  177. }