ParkingController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Http\Controllers\App;
  3. use App\Filters\ParkingFilter;
  4. use App\Http\Requests\ParkingRequest;
  5. use App\Http\Resources\App\ParkingResource;
  6. use App\Models\AdminMerchant;
  7. use App\Models\AdminUserArea;
  8. use App\Models\Area;
  9. use App\Models\Parking;
  10. use App\Utils\Admin;
  11. use App\Utils\GaodeMaps;
  12. use Illuminate\Http\Request;
  13. use App\Http\Controllers\Controller;
  14. use Illuminate\Support\Facades\Log;
  15. class ParkingController extends AppBaseController
  16. {
  17. /**
  18. * index 停车点列表
  19. *
  20. * @param Request $request
  21. * @param ParkingFilter $filter
  22. * @return \Illuminate\Http\JsonResponse
  23. * @author Fx
  24. *
  25. */
  26. public function index(Request $request, ParkingFilter $filter)
  27. {
  28. $admin_id = Admin::user()->id;
  29. $parking = Parking::query()->where(AdminMerchant::getMerchantWhere())->filter($filter)->orderByDesc('id');
  30. if (!Admin::isAdministrator()) {
  31. $area_ids = self::$areaIds;
  32. if (count($area_ids) !== 0) {
  33. $parking = $parking->whereIn('area_id', $area_ids);
  34. } else {
  35. $area_id = AdminUserArea::query()->where('admin_id', $admin_id)->first('area_id');
  36. $area_id = $area_id->area_id ?? 0;
  37. $parking = $parking->where('area_id', $area_id);
  38. }
  39. }
  40. //暂时没有写分页
  41. $parking = $parking->get();
  42. $result_array = ParkingResource::collection($parking)->toResponse($request)->getData(true);
  43. return $this->ok([
  44. 'parking_area' => array_column($result_array, 'parking_area'),
  45. 'point' => array_column($result_array, 'point'),
  46. ]);
  47. //return $this->ok(ParkingResource::collection($parking));
  48. //
  49. }
  50. /**
  51. * store 添加停车点
  52. *
  53. * @param ParkingRequest $request
  54. * @param Parking $model
  55. * @return \Illuminate\Http\JsonResponse
  56. * @author Fx
  57. *
  58. */
  59. public function store(ParkingRequest $request, Parking $model)
  60. {
  61. //
  62. $inputs = $request->validated();
  63. $admin_id = Admin::user()->id;
  64. $area_ids = self::$areaIds;
  65. $area_id = $inputs['area_id'];
  66. if (!in_array($area_id, $area_ids)) {
  67. return $this->error('暂无权限');
  68. }
  69. $parking_fence = object_array(json_decode($inputs['parking_fence']));
  70. $parking_centre = GetCenterFromDegrees($parking_fence);
  71. if (!$parking_centre) {
  72. return $this->error('参数错误');
  73. }
  74. $inputs['parking_radius'] = round(maxDistance($parking_centre, $parking_fence) + 1, 4);
  75. $inputs['parking_centre'] = json_encode($parking_centre);
  76. $inputs['description'] = GaodeMaps::getAddress([$parking_centre['lng'], $parking_centre['lat']]) ?? '';
  77. $res = $model->create($inputs);
  78. return $this->created(ParkingResource::make($res));
  79. }
  80. public function show(Parking $parking)
  81. {
  82. $data = [
  83. 'id' => $parking->id,
  84. 'name' => $parking->name,
  85. 'status' => $parking->status
  86. ];
  87. return $this->ok($data);
  88. }
  89. /**
  90. * update 更新停车点
  91. * @param ParkingRequest $request
  92. * @param Parking $parking
  93. * @return \Illuminate\Http\JsonResponse
  94. * @author Fx
  95. *
  96. */
  97. public function update(ParkingRequest $request, Parking $parking)
  98. {
  99. //
  100. $inputs = $request->validated();
  101. // 判断权限
  102. $admin_id = Admin::user()->id;
  103. $area_ids = self::$areaIds;
  104. $area_id = $parking->area_id;
  105. if (!in_array($area_id, $area_ids)) {
  106. return $this->error('暂无权限');
  107. }
  108. // if (!Admin::isAdministrator()) {
  109. //
  110. // }
  111. unset($inputs['parking_fence']);
  112. $parking->update($inputs);
  113. $parking->description = GaodeMaps::getAddress(json_decode($parking->parking_centre)) ?? '';
  114. $parking->save();
  115. return $this->ok(ParkingResource::make($parking));
  116. }
  117. /**
  118. * destroy 删除停车点
  119. *
  120. * @param Parking $parking
  121. * @return \Illuminate\Http\JsonResponse
  122. * @author Fx
  123. *
  124. */
  125. public function destroy(Parking $parking)
  126. {
  127. // 判断权限
  128. $admin_id = Admin::user()->id;
  129. $area_ids = self::$areaIds;
  130. $area_id = $parking->area_id;
  131. if (!in_array($area_id, $area_ids)) {
  132. return $this->error('暂无权限');
  133. }
  134. $parking->delete();
  135. return $this->noContent();
  136. }
  137. /**
  138. * updateStatus 更新停车点status
  139. *
  140. * @param Request $request
  141. * @return \Illuminate\Http\JsonResponse
  142. * @author Fx
  143. *
  144. */
  145. public function updateStatus(Request $request)
  146. {
  147. $status = $request->get('status');
  148. $parking_id = $request->get('id') ?? '';
  149. if (empty($parking_id)) return $this->error('参数错误');
  150. // 判断权限
  151. $admin_id = Admin::user()->id;
  152. $area_ids = self::$areaIds;
  153. $parking = Parking::query()->where(AdminMerchant::getMerchantWhere())->find($parking_id);
  154. if (empty($parking)) return $this->error('找不到停车区,请检查参数');
  155. $area_id = $parking->area_id;
  156. if (!in_array($area_id, $area_ids)) {
  157. return $this->error('暂无权限');
  158. }
  159. $parking->status = $status;
  160. $bool = $parking->save();
  161. if ($bool) {
  162. return $this->ok('修改成功');
  163. } else {
  164. return $this->error("修改失败,请联系管理员");
  165. }
  166. }
  167. /**
  168. * UniqueParkingName 检查停车点名称是否唯一
  169. *
  170. * @param Request $request
  171. * @return \Illuminate\Http\JsonResponse
  172. * @author Fx
  173. *
  174. */
  175. public function UniqueParkingName(Request $request)
  176. {
  177. $name = $request->get('name') ?? '';
  178. if (empty($name)) return $this->error('参数错误');
  179. $parking = Parking::query()->where(AdminMerchant::getMerchantWhere())->where('name', $name)->first();
  180. if (empty($parking)) {
  181. return $this->ok(['status' => 1]);
  182. } else {
  183. return $this->ok(['status' => 0]);
  184. }
  185. }
  186. }