diff --git a/app/Extend/SystemUtil.php b/app/Extend/SystemUtil.php index 681c662..f98971c 100644 --- a/app/Extend/SystemUtil.php +++ b/app/Extend/SystemUtil.php @@ -57,4 +57,39 @@ class SystemUtil { return filter_var($realIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); } + + /** + * 计算距离 + * @param array $locationOneArr + * @param array $locationTwoArr + * @param int $unit + * @return float|int + */ + static function calculateDistance(array $locationOneArr, array $locationTwoArr, int $unit = 1): float|int + { + // 地球平均半径,单位:公里 + $earthRadius = 6371; + + // 提取经纬度 + $lat1 = deg2rad($locationOneArr['lat']); + $lon1 = deg2rad($locationOneArr['lon']); + $lat2 = deg2rad($locationTwoArr['lat']); + $lon2 = deg2rad($locationTwoArr['lon']); + + // 计算距离 + $dLat = $lat2 - $lat1; + $dLon = $lon2 - $lon1; + + $a = sin($dLat / 2) * sin($dLat / 2) + cos($lat1) * cos($lat2) * sin($dLon / 2) * sin($dLon / 2); + $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); + + // 根据单位转换距离 + $distance = $earthRadius * $c; + return match ($unit) { + 1 => $distance, //公里 + 2 => $distance * 1000, //英尺 + 3 => $distance * 3280.84, //米 + default => 0, + }; + } } \ No newline at end of file diff --git a/app/Service/Admin/System/SiteService.php b/app/Service/Admin/System/SiteService.php index a81a2ee..9cd0e34 100644 --- a/app/Service/Admin/System/SiteService.php +++ b/app/Service/Admin/System/SiteService.php @@ -10,23 +10,26 @@ declare(strict_types=1); namespace App\Service\Admin\System; -use App\Constants\Admin\AuthCode; use App\Constants\Admin\UserCode; use App\Constants\Common\CityCode; use App\Constants\Common\RoleCode; use App\Constants\Common\SiteCode; use App\Exception\ErrException; +use App\Extend\SystemUtil; use App\Model\AdminSection; use App\Model\AdminUser; use App\Model\Kitchen; use App\Model\Site; use App\Model\SystemCity; use App\Service\Admin\BaseService; +use App\Service\ServiceTrait\Common\OssTrait; use Hyperf\DbConnection\Db; use Hyperf\Di\Annotation\Inject; class SiteService extends BaseService { + use OssTrait; + /** * @var Site */ @@ -76,23 +79,91 @@ class SiteService extends BaseService */ protected int $imageId = 0; + /** + * @var mixed + */ protected mixed $kitchenInfo; + /** + * @var mixed + */ protected mixed $driverInfo; + /** + * @var mixed + */ protected mixed $cityInfo; - public function handle() + /** + * @return array + */ + public function handle(): array { - return $this->return->success(); + $limit = (int)$this->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(); - Db::transaction(function () { + $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 = $this->request->input('name'); + $model->name = $name; $model->city_id = $this->cityId; $model->delivered_id = $this->driverId; $model->kitchen_id = $this->kitchenId; @@ -103,7 +174,11 @@ class SiteService extends BaseService $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 = 0; + $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('添加失败'); }); @@ -111,12 +186,14 @@ class SiteService extends BaseService 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->imageId = (int)$this->request->input('image_id'); $this->cityInfo = $this->systemCityModel->getInfoByCityId($this->cityId); if ( @@ -136,32 +213,144 @@ class SiteService extends BaseService empty($this->kitchenInfo) || $this->kitchenInfo->status == SiteCode::KITCHEN_DISABLE ) throw new ErrException('该厨房已被禁用'); + if ($this->kitchenInfo->city_id != $this->cityId) throw new ErrException('该厨房不属于该城市'); } - public function edit() + /** + * @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(); } - public function del() + /** + * @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(); } - public function info() + /** + * 详情 + * @return array + */ + public function info(): array { - return $this->return->success(); + $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('city_id',0); - $name = $this->request->input('name'); + $cityId = (int)$this->request->input('query_driver_city_id',0); + $name = $this->request->input('query_driver_name'); // $where[] = [ // ['is_del', '=', UserCode::IS_NO_DEL],