Files
hyperf_service/app/Service/Admin/System/SiteService.php
2024-11-12 11:02:38 +08:00

187 lines
5.0 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\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\Model\AdminSection;
use App\Model\AdminUser;
use App\Model\Kitchen;
use App\Model\Site;
use App\Model\SystemCity;
use App\Service\Admin\BaseService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
class SiteService extends BaseService
{
/**
* @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 AdminSection
*/
#[Inject]
protected readonly AdminSection $adminSectionModel;
/**
* @var int
*/
protected int $cityId = 0;
/**
* @var int
*/
protected int $driverId = 0;
/**
* @var int
*/
protected int $kitchenId = 0;
/**
* @var int
*/
protected int $imageId = 0;
protected mixed $kitchenInfo;
protected mixed $driverInfo;
protected mixed $cityInfo;
public function handle()
{
return $this->return->success();
}
public function add()
{
$this->checkData();
Db::transaction(function () {
$model = new Site();
$model->name = $this->request->input('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 = 0;
if (!$model->save()) throw new ErrException('添加失败');
});
return $this->return->success();
}
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 (
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('该厨房已被禁用');
}
public function edit()
{
return $this->return->success();
}
public function del()
{
return $this->return->success();
}
public function info()
{
return $this->return->success();
}
/**
* @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');
// $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);
}
}