Files
hyperf_service/app/Service/Admin/System/SiteService.php
2025-02-26 10:30:55 +08:00

341 lines
12 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\System;
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
*/
#[Inject]
protected readonly Site $siteModel;
/**
* @var AdminUser
*/
#[Inject]
protected readonly AdminUser $adminUserModel;
/**
* @var SystemCity
*/
#[Inject]
protected readonly SystemCity $systemCityModel;
/**
* @var Kitchen
*/
#[Inject]
protected readonly Kitchen $kitchenModel;
/**
* @var int
*/
protected int $cityId = 0;
/**
* @var int
*/
protected int $driverId = 0;
/**
* @var int
*/
protected int $kitchenId = 0;
/**
* @var int
*/
protected int $imageId = 0;
/**
* @var mixed
*/
protected mixed $kitchenInfo;
/**
* @var mixed
*/
protected mixed $driverInfo;
/**
* @var mixed
*/
protected mixed $cityInfo;
/**
* @return array
*/
public function handle(): array
{
$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('delivered_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');
$imageIds = array_column($list['data'], 'image_id');
$driverInfos = $this->adminUserModel->getDataByIds($driverIds);
$cityInfos = $this->systemCityModel->getDataByIds($cityIds);
$kitchenInfos = $this->kitchenModel->getDataByIds($kitchenIds);
$imageInfos = $this->getOssObjects($imageIds);
foreach ($list['data'] as &$one) {
$one['driver_name'] = $driverInfos[$one['delivered_id']]['name'] ?? '';
$one['city_name'] = $cityInfos[$one['city_id']]['title'] ?? '';
$one['kitchen_name'] = $kitchenInfos[$one['kitchen_id']]['name'] ?? '';
$one['url'] = $imageInfos[$one['image_id']]['url'] ?? '';
}
return $this->return->success('success',$list);
}
/**
* @return array
*/
public function add(): array
{
$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->pid = $this->request->input('pid',0);
$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->getInfoById($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');
$this->cityId = (int)$this->request->input('city_id');
$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->cityId != $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);
}
}