feat : kitchen
This commit is contained in:
184
app/Service/Admin/System/KitchenService.php
Normal file
184
app/Service/Admin/System/KitchenService.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
41
app/Service/Admin/System/SiteService.php
Normal file
41
app/Service/Admin/System/SiteService.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?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\Service\Admin\BaseService;
|
||||
|
||||
class SiteService extends BaseService
|
||||
{
|
||||
public function handle()
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
public function edit()
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
public function del()
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
public function info()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
42
app/Service/ServiceTrait/Common/SiteTrait.php
Normal file
42
app/Service/ServiceTrait/Common/SiteTrait.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\ServiceTrait\Common;
|
||||
|
||||
use App\Cache\Redis\Common\CommonRedisKey;
|
||||
use App\Cache\Redis\RedisCache;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
trait SiteTrait
|
||||
{
|
||||
/**
|
||||
* @var RedisCache
|
||||
*/
|
||||
#[Inject]
|
||||
protected RedisCache $redisCache;
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @param string $lng
|
||||
* @param string $lat
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws \RedisException
|
||||
*/
|
||||
public function setSiteCache($value, string $lng = '0', string $lat = '0'): void
|
||||
{
|
||||
$key = CommonRedisKey::getActivateSiteList();
|
||||
$this->redisCache->zRem($key,$value,'system');
|
||||
$this->redisCache->geoAdd($key,$lng,$lat,$value,'system');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user