184 lines
4.9 KiB
PHP
184 lines
4.9 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\AdminException;
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* @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 AdminException('数据已存在');
|
|
|
|
$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 AdminException('添加失败');
|
|
|
|
// 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 AdminException('数据不存在');
|
|
|
|
$name = $this->kitchenModel->getInfoByName($name);
|
|
if ($name->id != $info->id) throw new AdminException('数据已存在');
|
|
|
|
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 AdminException('修改失败');
|
|
|
|
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 AdminException('数据不存在');
|
|
|
|
$this->siteModel->disableStatusByKitchenId($info->id);
|
|
|
|
$info->is_del = SiteCode::SITE_DEL;
|
|
|
|
if (!$info->save()) throw new AdminException('删除失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function info(): array
|
|
{
|
|
$data = $this
|
|
->kitchenModel
|
|
->where('id', $this->request->input('id'))
|
|
->where('is_del', SiteCode::KITCHEN_NO_DEL)
|
|
->first(['id','city_id','name','address','lng','lat','status']);
|
|
if (empty($data)) throw new AdminException('数据不存在');
|
|
|
|
$res = $data->toArray();
|
|
$res['city_name'] = $this->systemCityModel->where('id',$data->city_id)->value('title');
|
|
|
|
return $this->return->success('success', $res);
|
|
}
|
|
} |