123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- namespace App\Http\Controllers\App;
- use App\Filters\AreaFilter;
- use App\Filters\BikeFilter;
- use App\Handlers\BikeStatusInfoSyncHandler;
- use App\Http\Resources\App\BikeResource;
- use App\Models\Area;
- use App\Models\Bike;
- use App\Models\LocationsLog;
- use App\Models\Parking;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class IndexController extends AppBaseController
- {
- /**
- * index 首页 获取区域
- *
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function index()
- {
- $area = Area::query()->whereIn('id', self::$areaIds)->pluck('name', 'id');
- $wx_area = Area::query()->whereIn('id', self::$areaIds)->get()->toArray();
- $wx_data = [];
- if (!empty($wx_area)) {
- foreach ($wx_area as $v) {
- $wx_data[] = ['areaID' => $v['id'], 'text' => $v['name']];
- }
- }
- $bikeStates = Bike::$bikeStatesMaps;
- $wx_bikeStates = [];
- foreach ($bikeStates as $k => $v) {
- $wx_bikeStates[] = ['areaID' => $k, 'text' => $v];
- }
- $data = [
- 'area' => $area,
- 'wx_area' => $wx_data,
- 'bike_states' => $bikeStates,
- 'wx_bikeStates' => $wx_bikeStates,
- ];
- return $this->ok($data);
- }
- /**
- * getBikes 首页搜索车辆
- *
- * @param BikeFilter $filter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function getBikes(BikeFilter $filter)
- {
- $bike = Bike::query()
- ->whereIn('put_area_id', self::$areaIds)
- ->whereNotNull('box_no')
- ->filter($filter)
- ->orderByDesc('id')
- ->get();
- // Log::info($bike);
- $bike_trouble = []; //故障
- $bike_trouble_off_line = []; //故障
- $bike_low_power = []; //低电量
- $bike_riding = []; //骑行中
- $bike_off_line = []; //未投放 下线
- $bike_not_riding = []; //未骑
- $bike_not_in_parking = []; // 不在停车区
- $bike_not_link = []; //离线车辆
- $bike_worker_riding = [];//运维骑行中
- foreach ($bike as $v) {
- if (app()->redis->hexists(BikeStatusInfoSyncHandler::REDIS_RIDE_BIKE_WORKER_ORDERS_TAG, $v->bike_no)) {
- $bike_worker_riding[] = [
- 'bike_id' => $v->id,
- 'bike_no' => $v->bike_no,
- 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
- ];
- } elseif (!(bool)$v->is_link) {
- // 离线
- $bike_not_link[] = [
- 'bike_id' => $v->id,
- 'bike_no' => $v->bike_no,
- 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
- ];
- } elseif ((bool)$v->is_trouble) {
- // 故障上线
- if ((bool)$v->put_status) {
- $bike_trouble[] = [
- 'bike_id' => $v->id,
- 'bike_no' => $v->bike_no,
- 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
- ];
- } else {
- // 故障下线
- $bike_trouble_off_line[] = [
- 'bike_id' => $v->id,
- 'bike_no' => $v->bike_no,
- 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
- ];
- }
- } elseif (!(bool)$v->is_low_battery_power) {
- // 低电量
- $bike_low_power[] = [
- 'bike_id' => $v->id,
- 'bike_no' => $v->bike_no,
- 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
- ];
- } elseif (!(bool)$v->put_status) {
- // 下线 未投放
- $bike_off_line[] = [
- 'bike_id' => $v->id,
- 'bike_no' => $v->bike_no,
- 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
- ];
- } elseif ((bool)$v->is_riding) {
- //骑行中
- $bike_riding[] = [
- 'bike_id' => $v->id,
- 'bike_no' => $v->bike_no,
- 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
- ];
- } elseif (!(bool)$v->is_in_parking) {
- // 不在停车区
- $bike_not_in_parking[] = [
- 'bike_id' => $v->id,
- 'bike_no' => $v->bike_no,
- 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
- ];
- } else {
- // 未骑行
- $bike_not_riding[] = [
- 'bike_id' => $v->id,
- 'bike_no' => $v->bike_no,
- 'last_location_app' => $v->last_location ? [json_decode($v->last_location)->lng, json_decode($v->last_location)->lat] : [113.801689, 34.815298],
- //'last_location_app' => $v->last_location ? LocationsLog::getNewestLocationByBikeNo($v->bike_no) : [113.801689, 34.815298],
- ];
- }
- }
- $data = [
- 'bike_trouble' => $bike_trouble, //故障
- 'bike_low_power' => $bike_low_power, //低电量
- 'bike_riding' => $bike_riding,
- 'bike_off_line' => $bike_off_line,
- 'bike_not_riding' => $bike_not_riding,
- 'bike_not_in_parking' => $bike_not_in_parking,
- 'bike_not_link' => $bike_not_link, // 离线
- 'bike_trouble_off_line' => $bike_trouble_off_line, //故障下线
- 'bike_worker_riding' => $bike_worker_riding,
- 'area_id' => self::$areaIds
- ];
- return $this->ok($data);
- }
- /**
- * searchBikes 首页搜索
- *
- * @param BikeFilter $filter
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function searchBikes(BikeFilter $filter)
- {
- $bike = Bike::query()
- ->whereIn('put_area_id', self::$areaIds)
- ->filter($filter)
- ->orderByDesc('id')
- ->get();
- return $this->ok(BikeResource::collection($bike));
- }
- /**
- * getAreas 还车点 骑行区
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * @author Fx
- *
- */
- public function getAreas(Request $request, AreaFilter $areaFilter)
- {
- $areaIds = self::$areaIds;
- // 骑行区
- $ridding_area = Area::query()->filter($areaFilter)->whereIn('id', $areaIds);
- // 停车点
- $parking_area = Parking::query()->whereIn('area_id', $areaIds)->where('type', Parking::TYPE_STOP_BIKE)->where('status', Parking::STATUS_OK); //2为停车区
- //禁停区
- $no_parking_area = Parking::query()->whereIn('area_id', $areaIds)->where('type', Parking::TYPE_NO_STOP_BIKE)->where('status', Parking::STATUS_OK); //1为禁停
- $area_id = $request->get('put_area_id') ?? '';
- if (!empty($area_id)) {
- $ridding_area = $ridding_area->where('id', $area_id);
- $parking_area = $parking_area->where('area_id', $area_id);
- $no_parking_area = $no_parking_area->where('area_id', $area_id);
- }
- $ridding_area = $ridding_area->get();
- $parking_area = $parking_area->orderByDesc('id')->get();
- $no_parking_area = $no_parking_area->orderByDesc('id')->get();
- foreach ($ridding_area as &$v) {
- $v->area_fence_app = json_decode($v->area_fence);
- $v->area_centre_app = json_decode($v->area_centre);
- //$ridding_area_db = DB::table('areas')->find($v->id);
- $v->area_fence_wx = json_decode($v->wx_area_fence);
- $v->area_centre_wx = json_decode($v->wx_area_centre);
- // Log::info(json_decode($v->area_fence));
- }
- foreach ($parking_area as &$v1) {
- $v1->parking_fence_app = json_decode($v1->parking_fence);
- $v1->parking_centre_app = json_decode($v1->parking_centre);
- $v1->parking_fence_wx = json_decode($v1->wx_parking_fence);
- $v1->parking_centre_wx = json_decode($v1->wx_parking_centre);
- }
- foreach ($no_parking_area as &$v2) {
- $v2->parking_fence_app = json_decode($v2->parking_fence);
- $v2->parking_centre_app = json_decode($v2->parking_centre);
- $v2->parking_fence_wx = json_decode($v2->wx_parking_fence);
- $v2->parking_centre_wx = json_decode($v2->wx_parking_centre);
- }
- $data = [
- 'ridding_area' => $ridding_area,
- 'parking_area' => $parking_area,
- 'no_parking_area' => $no_parking_area
- ];
- return $this->ok($data);
- }
- }
|