Files
hyperf_service/app/Service/Admin/System/KitchenService.php
2024-11-20 17:23:15 +08:00

189 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\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use App\Constants\Common\SiteCode;
use App\Exception\ErrException;
use App\Model\Kitchen;
use App\Model\Site;
use App\Model\SystemCity;
use App\Service\Admin\BaseService;
use App\Service\ServiceTrait\Common\SiteTrait;
use Exception;
use Hyperf\Di\Annotation\Inject;
class KitchenService extends BaseService
{
use SiteTrait;
/**
* @var Kitchen
*/
#[Inject]
protected Kitchen $kitchenModel;
/**
* @var Site
*/
#[Inject]
protected Site $siteModel;
/**
* @var SystemCity
*/
#[Inject]
protected SystemCity $systemCityModel;
/**
* @var array 查询条件
*/
private array $where = [];
/**
* @return array
*/
public function handle(): array
{
$limit = (int)$this->request->input('limit', 10);
$this->getWhere();
if (empty($this->where)) {
$data = $this->kitchenModel->orderBy('id','desc')->paginate($limit)->toArray();
}else{
$data = $this->kitchenModel->orderBy('id','desc')->where($this->where)->paginate($limit)->toArray();
}
$cityId = array_unique(array_column($data['data'],'city_id'));
$cityName = $this->systemCityModel->getCityNameByIds($cityId);
foreach ($data['data'] as &$one) {
$one['city_name'] = $cityName[$one['city_id']] ?? '';
}
return $this->return->success('success',$data);
}
/**
* @return void
*/
private function getWhere(): void
{
$this->where[] = ['is_del','=',SiteCode::KITCHEN_NO_DEL];
if ($this->request->input('city_id',0) > 0) {
$this->where[] = ['city_id', '=', $this->request->input('city_id')];
}
}
/**
* @return array
*/
public function add(): array
{
$name = $this->request->input('name');
$info = $this->kitchenModel->getInfoByName($name);
if (!empty($info)) throw new ErrException('数据已存在');
$model = new Kitchen();
$model->name = $this->request->input('name');
$model->city_id = $this->request->input('city_id');
$model->address = $this->request->input('address');
$model->lng = $this->request->input('lng');
$model->lat = $this->request->input('lat');
$model->status = $this->request->input('status');
if (!$model->save()) throw new ErrException('添加失败');
// if ($model->status == SiteCode::KITCHEN_ENABLE) {
// $this->setSiteCache(SiteCode::KITCHEN_REDIS_PREFIX.$model->id,$model->lng,$model->lat);
// }
return $this->return->success();
}
/**
* @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->kitchenModel->getInfoById($id);
if (empty($info)) throw new ErrException('数据不存在');
$name = $this->kitchenModel->getInfoByName($name);
if ($name->id != $info->id) throw new ErrException('数据已存在');
if ($info->status == SiteCode::KITCHEN_ENABLE && $status == SiteCode::KITCHEN_DISABLE) {
$this->siteModel->disableStatusByKitchenId($info->id);
}
$info->name = $this->request->input('name');
$info->address = $this->request->input('address');
$info->lng = $this->request->input('lng');
$info->lat = $this->request->input('lat');
$info->status = $status;
if (!$info->save()) throw new ErrException('修改失败');
return $this->return->success();
}
/**
* @return array
* @throws Exception
*/
public function del(): array
{
$id = (int)$this->request->input('id');
$info = $this->kitchenModel->getInfoById($id);
if (empty($info)) throw new ErrException('数据不存在');
$this->siteModel->disableStatusByKitchenId($info->id);
$info->is_del = SiteCode::SITE_DEL;
if (!$info->save()) throw new ErrException('删除失败');
return $this->return->success();
}
/**
* @return array
*/
public function info(): array
{
$data = $this->kitchenModel
->getInfoById((int)$this->request->input('id'));
if (empty($data)) throw new ErrException('数据不存在');
$res = [
'id' => $data->id,
'city_id' => $data->city_id,
'name' => $data->name,
'address' => $data->address,
'lng' => $data->lng,
'lat' => $data->lat,
'status' => $data->status,
'city_name' => $this->systemCityModel->where('id',$data->city_id)->value('title')
];
return $this->return->success('success', $res);
}
}