160 lines
4.2 KiB
PHP
160 lines
4.2 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\CityCache;
|
|
use App\Constants\Common\CityCode;
|
|
use App\Exception\ErrException;
|
|
use App\Model\SystemCity;
|
|
use App\Model\SystemCityConfig;
|
|
use App\Service\Admin\BaseService;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use function Swoole\Coroutine\Http\request;
|
|
|
|
class CityService extends BaseService
|
|
{
|
|
/**
|
|
* @var SystemCity
|
|
*/
|
|
#[Inject]
|
|
protected SystemCity $systemCityModel;
|
|
|
|
/**
|
|
* @var SystemCityConfig
|
|
*/
|
|
#[Inject]
|
|
protected SystemCityConfig $systemCityConfigModel;
|
|
|
|
/**
|
|
* @var CityCache
|
|
*/
|
|
#[Inject]
|
|
protected CityCache $cityCache;
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
* @throws \RedisException
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
$limit = (int)$this->request->input('limit', 10);
|
|
|
|
$list = $this->systemCityModel
|
|
->where('is_del', CityCode::IS_NOT_DELETE)
|
|
->paginate($limit,['id','title','status','city_id','province_id'])
|
|
->toArray();
|
|
|
|
$allInfo = array_column($this->cityCache->getCityList(),'title','id');
|
|
|
|
if (!empty($list['data'])) {
|
|
foreach ($list['data'] as &$v) {
|
|
$v['city_name'] = $allInfo[$v['city_id']];
|
|
$v['province_name'] = $allInfo[$v['province_id']];
|
|
}
|
|
}
|
|
|
|
return $this->return->success('success', ['list' => $list]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function add(): array
|
|
{
|
|
$provinceId = (int)$this->request->input('province_id');
|
|
$cityId = (int)$this->request->input('city_id');
|
|
|
|
$info = $this->systemCityConfigModel->getAddressByIdAndPid($cityId,$provinceId);
|
|
if (empty($info)) throw new ErrException('城市选择错误');
|
|
|
|
if ($this->systemCityModel->getInfoByCityId($cityId)) throw new ErrException('城市已存在');
|
|
|
|
$model = new SystemCity();
|
|
|
|
$model->province_id = $info->pid;
|
|
$model->city_id = $info->id;
|
|
$model->title = $info->title;
|
|
$model->status = $this->request->input('status', 1);
|
|
$model->is_del = 1;
|
|
|
|
if (!$model->save()) throw new ErrException('添加失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
public function changeStatus(): array
|
|
{
|
|
$id = (int)$this->request->input('id');
|
|
|
|
$model = $this->systemCityModel->getInfoById($id);
|
|
|
|
if (!$model) throw new ErrException('数据不存在');
|
|
|
|
$model->status = $model->status == CityCode::STATUS_ENABLE ? CityCode::STATUS_DISABLE : CityCode::STATUS_ENABLE;
|
|
|
|
if (!$model->save()) throw new ErrException('修改失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function delete(): array
|
|
{
|
|
$id = (int)$this->request->input('id');
|
|
|
|
$model = $this->systemCityModel->getInfoById($id);
|
|
|
|
if (!$model) throw new ErrException('数据不存在');
|
|
|
|
$model->is_del = CityCode::IS_DELETE;
|
|
|
|
if (!$model->save()) throw new ErrException('删除失败');
|
|
|
|
return $this->return->success();
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
* @throws \RedisException
|
|
*/
|
|
public function info(): array
|
|
{
|
|
$id = (int)$this->request->input('id');
|
|
|
|
$info = $this->systemCityModel->getInfoById($id);
|
|
|
|
if (!$info) throw new ErrException('数据不存在');
|
|
|
|
$allInfo = $this->cityCache->getCityList();
|
|
$allInfo = array_column($allInfo,'title','id');
|
|
|
|
$res = [
|
|
'id' => $info->id,
|
|
'title' => $info->title,
|
|
'status' => $info->status,
|
|
'province_id' => $info->province_id,
|
|
'province_name' => $allInfo[$info->province_id] ?? '',
|
|
'city_id' => $info->city_id,
|
|
'city_name' => $allInfo[$info->city_id]?? '',
|
|
];
|
|
|
|
return $this->return->success('success',$res);
|
|
}
|
|
} |