IndexController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Http\Controllers\App;
  3. use App\Filters\AreaFilter;
  4. use App\Filters\BikeFilter;
  5. use App\Handlers\BikeStatusInfoSyncHandler;
  6. use App\Http\Resources\App\BikeResource;
  7. use App\Models\Area;
  8. use App\Models\Bike;
  9. use App\Models\LocationsLog;
  10. use App\Models\Parking;
  11. use Illuminate\Http\Request;
  12. use App\Http\Controllers\Controller;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. class IndexController extends AppBaseController
  16. {
  17. /**
  18. * index 首页 获取区域
  19. *
  20. * @return \Illuminate\Http\JsonResponse
  21. * @author Fx
  22. *
  23. */
  24. public function index()
  25. {
  26. $area = Area::query()->whereIn('id', self::$areaIds)->pluck('name', 'id');
  27. $wx_area = Area::query()->whereIn('id', self::$areaIds)->get()->toArray();
  28. $wx_data = [];
  29. if (!empty($wx_area)) {
  30. foreach ($wx_area as $v) {
  31. $wx_data[] = ['areaID' => $v['id'], 'text' => $v['name']];
  32. }
  33. }
  34. $bikeStates = Bike::$bikeStatesMaps;
  35. $wx_bikeStates = [];
  36. foreach ($bikeStates as $k => $v) {
  37. $wx_bikeStates[] = ['areaID' => $k, 'text' => $v];
  38. }
  39. $data = [
  40. 'area' => $area,
  41. 'wx_area' => $wx_data,
  42. 'bike_states' => $bikeStates,
  43. 'wx_bikeStates' => $wx_bikeStates,
  44. ];
  45. return $this->ok($data);
  46. }
  47. /**
  48. * getBikes 首页搜索车辆
  49. *
  50. * @param BikeFilter $filter
  51. * @return \Illuminate\Http\JsonResponse
  52. * @author Fx
  53. *
  54. */
  55. public function getBikes(BikeFilter $filter)
  56. {
  57. $bike = Bike::query()
  58. ->whereIn('put_area_id', self::$areaIds)
  59. ->whereNotNull('box_no')
  60. ->filter($filter)
  61. ->orderByDesc('id')
  62. ->get();
  63. // Log::info($bike);
  64. $bike_trouble = []; //故障
  65. $bike_trouble_off_line = []; //故障
  66. $bike_low_power = []; //低电量
  67. $bike_riding = []; //骑行中
  68. $bike_off_line = []; //未投放 下线
  69. $bike_not_riding = []; //未骑
  70. $bike_not_in_parking = []; // 不在停车区
  71. $bike_not_link = []; //离线车辆
  72. $bike_worker_riding = [];//运维骑行中
  73. foreach ($bike as $v) {
  74. if (app()->redis->hexists(BikeStatusInfoSyncHandler::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $v->bike_no)) {
  75. $bike_worker_riding[] = [
  76. 'bike_id' => $v->id,
  77. 'bike_no' => $v->bike_no,
  78. 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
  79. ];
  80. } elseif (!(bool)$v->is_link) {
  81. // 离线
  82. $bike_not_link[] = [
  83. 'bike_id' => $v->id,
  84. 'bike_no' => $v->bike_no,
  85. 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
  86. ];
  87. } elseif ((bool)$v->is_trouble) {
  88. // 故障上线
  89. if ((bool)$v->put_status) {
  90. $bike_trouble[] = [
  91. 'bike_id' => $v->id,
  92. 'bike_no' => $v->bike_no,
  93. 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
  94. ];
  95. } else {
  96. // 故障下线
  97. $bike_trouble_off_line[] = [
  98. 'bike_id' => $v->id,
  99. 'bike_no' => $v->bike_no,
  100. 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
  101. ];
  102. }
  103. } elseif (!(bool)$v->is_low_battery_power) {
  104. // 低电量
  105. $bike_low_power[] = [
  106. 'bike_id' => $v->id,
  107. 'bike_no' => $v->bike_no,
  108. 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
  109. ];
  110. } elseif (!(bool)$v->put_status) {
  111. // 下线 未投放
  112. $bike_off_line[] = [
  113. 'bike_id' => $v->id,
  114. 'bike_no' => $v->bike_no,
  115. 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
  116. ];
  117. } elseif ((bool)$v->is_riding) {
  118. //骑行中
  119. $bike_riding[] = [
  120. 'bike_id' => $v->id,
  121. 'bike_no' => $v->bike_no,
  122. 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
  123. ];
  124. } elseif (!(bool)$v->is_in_parking) {
  125. // 不在停车区
  126. $bike_not_in_parking[] = [
  127. 'bike_id' => $v->id,
  128. 'bike_no' => $v->bike_no,
  129. 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
  130. ];
  131. } else {
  132. // 未骑行
  133. $bike_not_riding[] = [
  134. 'bike_id' => $v->id,
  135. 'bike_no' => $v->bike_no,
  136. 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
  137. //'last_location_app' => $v->last_location ? LocationsLog::getNewestLocationByBikeNo($v->bike_no) : [113.801689, 34.815298],
  138. ];
  139. }
  140. }
  141. $data = [
  142. 'bike_trouble' => $bike_trouble, //故障
  143. 'bike_low_power' => $bike_low_power, //低电量
  144. 'bike_riding' => $bike_riding,
  145. 'bike_off_line' => $bike_off_line,
  146. 'bike_not_riding' => $bike_not_riding,
  147. 'bike_not_in_parking' => $bike_not_in_parking,
  148. 'bike_not_link' => $bike_not_link, // 离线
  149. 'bike_trouble_off_line' => $bike_trouble_off_line, //故障下线
  150. 'bike_worker_riding' => $bike_worker_riding,
  151. 'area_id' => self::$areaIds
  152. ];
  153. return $this->ok($data);
  154. }
  155. /**
  156. * searchBikes 首页搜索
  157. *
  158. * @param BikeFilter $filter
  159. * @return \Illuminate\Http\JsonResponse
  160. * @author Fx
  161. *
  162. */
  163. public function searchBikes(BikeFilter $filter)
  164. {
  165. $bike = Bike::query()
  166. ->whereIn('put_area_id', self::$areaIds)
  167. ->filter($filter)
  168. ->orderByDesc('id')
  169. ->get();
  170. return $this->ok(BikeResource::collection($bike));
  171. }
  172. /**
  173. * getAreas 还车点 骑行区
  174. *
  175. * @param Request $request
  176. * @return \Illuminate\Http\JsonResponse
  177. * @author Fx
  178. *
  179. */
  180. public function getAreas(Request $request, AreaFilter $areaFilter)
  181. {
  182. $areaIds = self::$areaIds;
  183. // 骑行区
  184. $ridding_area = Area::query()->filter($areaFilter)->whereIn('id', $areaIds);
  185. // 停车点
  186. $parking_area = Parking::query()->whereIn('area_id', $areaIds)->where('type', Parking::TYPE_STOP_BIKE)->where('status', Parking::STATUS_OK); //2为停车区
  187. //禁停区
  188. $no_parking_area = Parking::query()->whereIn('area_id', $areaIds)->where('type', Parking::TYPE_NO_STOP_BIKE)->where('status', Parking::STATUS_OK); //1为禁停
  189. $area_id = $request->get('put_area_id') ?? '';
  190. if (!empty($area_id)) {
  191. $ridding_area = $ridding_area->where('id', $area_id);
  192. $parking_area = $parking_area->where('area_id', $area_id);
  193. $no_parking_area = $no_parking_area->where('area_id', $area_id);
  194. }
  195. $ridding_area = $ridding_area->get();
  196. $parking_area = $parking_area->orderByDesc('id')->get();
  197. $no_parking_area = $no_parking_area->orderByDesc('id')->get();
  198. foreach ($ridding_area as &$v) {
  199. $v->area_fence_app = json_decode($v->area_fence);
  200. $v->area_centre_app = json_decode($v->area_centre);
  201. //$ridding_area_db = DB::table('areas')->find($v->id);
  202. $v->area_fence_wx = json_decode($v->wx_area_fence);
  203. $v->area_centre_wx = json_decode($v->wx_area_centre);
  204. // Log::info(json_decode($v->area_fence));
  205. }
  206. foreach ($parking_area as &$v1) {
  207. $v1->parking_fence_app = json_decode($v1->parking_fence);
  208. $v1->parking_centre_app = json_decode($v1->parking_centre);
  209. $v1->parking_fence_wx = json_decode($v1->wx_parking_fence);
  210. $v1->parking_centre_wx = json_decode($v1->wx_parking_centre);
  211. }
  212. foreach ($no_parking_area as &$v2) {
  213. $v2->parking_fence_app = json_decode($v2->parking_fence);
  214. $v2->parking_centre_app = json_decode($v2->parking_centre);
  215. $v2->parking_fence_wx = json_decode($v2->wx_parking_fence);
  216. $v2->parking_centre_wx = json_decode($v2->wx_parking_centre);
  217. }
  218. $data = [
  219. 'ridding_area' => $ridding_area,
  220. 'parking_area' => $parking_area,
  221. 'no_parking_area' => $no_parking_area
  222. ];
  223. return $this->ok($data);
  224. }
  225. }