request->input('limit',10); $createTimeStart = $this->request->input('query_create_start_time'); $createTimeEnd = $this->request->input('query_create_end_time'); $list = $this ->siteModel ->where('is_del',SiteCode::SITE_NO_DEL) ->when($id = $this->request->input('query_id'),function ($query) use ($id) { $query->where('id',$id); }) ->when($name = $this->request->input('query_name'), function ($query) use ($name) { $query->where('name', 'like', "$name%"); }) ->when($cityId = $this->request->input('query_city_id') > 0, function ($query) use ($cityId) { $query->where('city_id', $cityId); }) ->when($status = $this->request->input('query_status'), function ($query) use ($status) { $query->where('status', $status); }) ->when($kitchenId = $this->request->input('query_kitchen_id'), function ($query) use ($kitchenId) { $query->where('kitchen_id', $kitchenId); }) ->when($driverId = $this->request->input('query_driver_id'), function ($query) use ($driverId) { $query->where('driver_id', $driverId); }) ->when(!empty($createTimeStart) && !empty($createTimeEnd), function ($query) use ($createTimeStart, $createTimeEnd) { $query->whereBetween('create_time', [$createTimeStart, $createTimeEnd]); }) ->paginate($limit)->toArray(); if (empty($list['data'])) return $this->return->success('success',$list); $driverIds = array_column($list['data'], 'delivered_id'); $kitchenIds = array_column($list['data'], 'kitchen_id'); $cityIds = array_column($list['data'], 'city_id'); $driverInfos = $this->adminUserModel->getDataByIds($driverIds); $cityInfos = $this->systemCityModel->getDataByIds($cityIds); $kitchenInfos = $this->kitchenModel->getDataByIds($kitchenIds); foreach ($list['data'] as &$one) { $one['driver_name'] = $driverInfos[$one['driver_id']]['name'] ?? ''; $one['city_name'] = $cityInfos[$one['city_id']]['title'] ?? ''; $one['kitchen_name'] = $kitchenInfos[$one['kitchen_id']]['name'] ?? ''; } return $this->return->success('success',$list); } /** * @return array */ public function add() { $name = $this->request->input('name'); $this->imageId = (int)$this->request->input('image_id'); $this->checkData(); $info = $this->siteModel->getInfoByName($name); if (!empty($info)) throw new ErrException('该地点已存在'); Db::transaction(function () use($name) { $this->updateOssObjects([$this->imageId]); $model = new Site(); $model->name = $name; $model->city_id = $this->cityId; $model->delivered_id = $this->driverId; $model->kitchen_id = $this->kitchenId; $model->address = $this->request->input('address'); $model->lng = $this->request->input('lng'); $model->lat = $this->request->input('lat'); $model->remark = $this->request->input('remark'); $model->status = $this->request->input('status'); $model->expected_delivery_time = $this->request->input('expected_delivery_time'); $model->expected_spend_time = $this->request->input('expected_spend_time'); $model->distance = SystemUtil::calculateDistance( ['lat' => $this->kitchenInfo->lat,'lng' => $this->kitchenInfo->lng], ['lat' => $this->request->input('lat'), 'lng' => $this->request->input('lng')] ); $model->image_id = $this->imageId; if (!$model->save()) throw new ErrException('添加失败'); }); return $this->return->success(); } /** * @return void */ private function checkData(): void { $this->cityId = (int)$this->request->input('city_id'); $this->driverId = (int)$this->request->input('driver_id'); $this->kitchenId = (int)$this->request->input('kitchen_id'); $this->cityInfo = $this->systemCityModel->getInfoByCityId($this->cityId); if ( empty($this->cityInfo) || $this->cityInfo->status == CityCode::STATUS_DISABLE ) throw new ErrException('该城市已被禁用'); $this->driverInfo = $this->adminUserModel->getAdminInfoById($this->driverId); if ( empty($this->driverInfo) || $this->driverInfo->status == UserCode::DISABLE || $this->driverInfo->role_id != RoleCode::DRIVER ) throw new ErrException('该司机已被禁用'); $this->kitchenInfo = $this->kitchenModel->getInfoById($this->kitchenId); if ( empty($this->kitchenInfo) || $this->kitchenInfo->status == SiteCode::KITCHEN_DISABLE ) throw new ErrException('该厨房已被禁用'); if ($this->kitchenInfo->city_id != $this->cityId) throw new ErrException('该厨房不属于该城市'); } /** * @return array */ public function edit(): array { $id = (int)$this->request->input('id'); $name = $this->request->input('name'); $status = (int)$this->request->input('status'); $info = $this->siteModel->getInfoById($id); $nameInfo = $this->siteModel->getInfoByName($name); if (empty($info)) throw new ErrException('数据不存在'); if ($info->id != $nameInfo->id) throw new ErrException('数据已存在'); if ($this->request->input('city_id') != $info->city_id) throw new ErrException('城市不可更改'); $this->cityInfo = $this->systemCityModel->getInfoByCityId($info->cityId); if ($status != SiteCode::SITE_DISABLE) { $this->checkDriver(); $this->checkKitchen(); } Db::transaction(function () use ($info,$name) { //是不是修改图片 if ($info->image_id != $this->imageId){ $this->updateOssObjects([$this->imageId]); $this->updateOssObjectsDisable([$info->image_id]); $info->image_id = $this->imageId; } if ( $info->kitchen_id != $this->kitchenId || $info->lng != $this->request->input('lng') || $info->lat != $this->request->input('lat') ){ $info->kitchen_id = $this->kitchenId; $info->address = $this->request->input('address'); $info->lng = $this->request->input('lng'); $info->lat = $this->request->input('lat'); $info->distance = SystemUtil::calculateDistance( ['lat' => $this->kitchenInfo->lat,'lng' => $this->kitchenInfo->lng], ['lat' => $this->request->input('lat'), 'lng' => $this->request->input('lng')] ); } $info->name = $name; // $info->city_id = $this->cityId; $info->delivered_id = $this->driverId; $info->remark = $this->request->input('remark'); $info->status = $this->request->input('status'); $info->expected_delivery_time = $this->request->input('expected_delivery_time'); $info->expected_spend_time = $this->request->input('expected_spend_time'); }); return $this->return->success(); } /** * @return void */ private function checkDriver(): void { $this->driverId = (int)$this->request->input('driver_id'); //todo 司机禁用或者删除的时候需要把点先清除 $driverInfo = $this->adminUserModel->getAdminInfoById($this->driverId); if ( empty($driverInfo) || $driverInfo->status == UserCode::DISABLE || $driverInfo->role_id != RoleCode::DRIVER ) throw new ErrException('该司机已被禁用'); } /** * @return void */ private function checkKitchen(): void { $this->kitchenId = (int)$this->request->input('kitchen_id'); //todo 厨房禁用或者删除的时候需要把点先清除 $this->kitchenInfo = $this->kitchenModel->getInfoById($this->kitchenId); if ( empty($this->kitchenInfo) || $this->kitchenInfo->status == SiteCode::KITCHEN_DISABLE ) throw new ErrException('该厨房已被禁用'); if ($this->kitchenInfo->city_id != $this->cityId) throw new ErrException('该厨房不属于该城市'); } /** * @return array */ public function del(): array { $id = (int)$this->request->input('id'); $info = $this->siteModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $info->is_del = UserCode::IS_DEL; if (!$info->save()) throw new ErrException('删除失败'); return $this->return->success(); } /** * 详情 * @return array */ public function info(): array { $id = (int)$this->request->input('id'); $info = $this->siteModel->getInfoById($id); if (empty($info)) throw new ErrException('数据不存在'); $res = $info->toArray(); $res['driver_name'] = $this->adminUserModel->where('id',$info->delivered_id)->value('chinese_name'); $res['kitchen_name'] = $this->kitchenModel->where('id',$info->kitchen_id)->value('title'); $res['city_name'] = $this->systemCityModel->where('id',$info->city_id)->value('name'); $res['image_url'] = $this->getOssObjectById($info->image_id); return $this->return->success('success',$res); } /** * 司机列表 * @return array */ public function driverList(): array { $limit = $this->request->input('limit', 10); $cityId = (int)$this->request->input('query_driver_city_id',0); $name = $this->request->input('query_driver_name'); // $where[] = [ // ['is_del', '=', UserCode::IS_NO_DEL], // ['status','=',UserCode::ENABLE], // ['role_id','=',RoleCode::DRIVER] // ]; $list = $this ->adminUserModel ->where('is_del',UserCode::IS_NO_DEL) ->where('status',UserCode::ENABLE) ->where('role_id',RoleCode::DRIVER) ->when($name, function ($query) use ($name) { $query->where('name', 'like', "$name%"); }) ->when($cityId > 0, function ($query) use ($cityId) { $query->whereIn('section_id', $this->adminSectionModel->getIdsByCityId($cityId)); }) ->paginate($limit,['chinese_name','id','mobile','status'])->toArray(); return $this->return->success('success',$list); } }