Files
nl-mall-api/app/Service/CodeGeneration/UserReceivingAddressService.php

112 lines
3.0 KiB
PHP

<?php
namespace App\Service\CodeGeneration;
use App\BaseApp\BaseService;
use App\Models\RegionModel;
use App\Models\UserReceivingAddressModel;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class UserReceivingAddressService extends BaseService
{
public function __construct()
{
parent::__construct();
$this->selectField = ["id", "user_id", "province", "total_address", "city", "district", "name", "phone", "is_default", "detailed_address", "complete_address", "status", "created_at", "updated_at", "deleted_at"];
$this->queryField = ['user_id' => '=','province' => '=','city' => '=','district' => '=','detailed_address' => '=','complete_address' => '=,status='];
$this->model = UserReceivingAddressModel::class;
$this->with = [
'user:id,nick_name',
'provinceInfo:id,name',
'cityInfo:id,name',
'districtInfo:id,name',
];
}
/**
* 获取用户收货地址列表
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function list(): array
{
$result = $this->getPageList();
foreach ($result['items'] as &$v) {
$v['total_address'] = json_decode($v['total_address']);
}
return $result;
}
/**
* 获取用户收货地址详情
* @param $id
* @return mixed
* @throws Exception
*/
public function detail($id)
{
return $this->getDetail($id);
}
/**
* 用户收货地址下拉列表
* @return mixed
*/
public function option()
{
$this->optionField = ['id', 'name'];
return $this->getOption();
}
/**
* 创建用户收货地址
* @param $params
* @return mixed
* @throws Exception
*/
public function create($params): mixed
{
$params = $this->checkAddress($params);
return $this->insert($params);
}
/**
* 编辑用户收货地址
* @param $id
* @param $params
* @return mixed
* @throws Exception
*/
public function update($id, $params): mixed
{
$params = $this->checkAddress($params);
return $this->save($id, $params);
}
/**
* 删除用户收货地址
* @param $ids
* @return mixed|true
* @throws Exception
*/
public function delete($ids): mixed
{
return $this->del($ids);
}
public function checkAddress($params)
{
$data = RegionModel::whereIn('id', $params['total_address'])->get(['id', 'name']);
$regionName = array_column($data->toArray(), 'name');
$params['complete_address'] = implode('', $regionName). ' '. $params['detailed_address'];
$params['province'] = $params['total_address'][0];
$params['city'] = $params['total_address'][1];
$params['district'] = $params['total_address'][2];
$params['total_address'] = json_encode($params['total_address']);
return $params;
}
}