You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.1 KiB
111 lines
3.1 KiB
<?php
|
|
|
|
|
|
namespace App\Service\v3\Implementations;
|
|
use App\Constants\v3\ErrorCode;
|
|
use App\Exception\ErrorCodeException;
|
|
use App\Model\v3\Market;
|
|
use App\Model\v3\UserAddress;
|
|
use App\Service\v3\Interfaces\DistributionPriceServiceInterface;
|
|
use App\Service\v3\Interfaces\LocationServiceInterface;
|
|
use App\Service\v3\Interfaces\UserAddressServiceInterface;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
class UserAddressService implements UserAddressServiceInterface
|
|
{
|
|
/**
|
|
* @Inject
|
|
* @var LocationServiceInterface
|
|
*/
|
|
protected $locationService;
|
|
|
|
/**
|
|
* @Inject
|
|
* @var DistributionPriceServiceInterface
|
|
*/
|
|
protected $distributionPriceService;
|
|
|
|
public function do($userAddressId,$user_id,$user_name,$address,$doorplate,$gender,$lat,$lng,$tel,$tags)
|
|
{
|
|
$userAddress = UserAddress::updateOrCreate(['id' => $userAddressId],
|
|
[
|
|
'user_id' => $user_id,
|
|
'user_name' => $user_name,
|
|
'address' => $address,
|
|
'doorplate' => $doorplate,
|
|
'gender' => $gender,
|
|
'lat' => $lat,
|
|
'lng' => $lng,
|
|
'tel' => $tel,
|
|
'tags' => $tags
|
|
]);
|
|
return $userAddress;
|
|
}
|
|
|
|
public function check()
|
|
{
|
|
|
|
}
|
|
|
|
public function undo($userAddressId)
|
|
{
|
|
return UserAddress::destroy($userAddressId);
|
|
}
|
|
|
|
public function setDefault($userId,$userAddressId)
|
|
{
|
|
UserAddress::query()->where([
|
|
['user_id','=',$userId],
|
|
['is_default','=',1],
|
|
])->update(['is_default' => 0]);
|
|
$userAddress = UserAddress::query()->find($userAddressId);
|
|
$userAddress->is_default = 1;
|
|
return $userAddress->save();
|
|
}
|
|
|
|
public function get($userAddressId)
|
|
{
|
|
$address['address'] = UserAddress::query()->find($userAddressId);
|
|
$address['tags'] = $this->getTags();
|
|
|
|
return $address;
|
|
}
|
|
|
|
public function getList($userId)
|
|
{
|
|
return UserAddress::query()->where('user_id',$userId)->get();
|
|
}
|
|
|
|
/**
|
|
* @param $userAddressId
|
|
* @param $marketId
|
|
* @return false|float
|
|
*/
|
|
public function getAddressAndDistributionPrice($userAddressId,$marketId)
|
|
{
|
|
$address = $this->get($userAddressId);
|
|
$market = Market::query()->select('lng','lat')->find($marketId);
|
|
if(empty($address['address']->lng) || empty($address['address']->lat) || empty($market->lng) || empty($market->lat)){
|
|
throw new ErrorCodeException(ErrorCode::LOCATION_USER_ADDRESS);
|
|
}
|
|
|
|
$distance = $this->locationService->getDistanceByTencent($address['address']->lng,$address['address']->lat,$market->lng,$market->lat);
|
|
$distributionPrice = $this->distributionPriceService->do($distance);
|
|
$res['address'] = $address;
|
|
$res['delivery_distance'] = $distance;
|
|
$res['distribution_price'] = $distributionPrice;
|
|
return $res;
|
|
}
|
|
|
|
public function getTags()
|
|
{
|
|
return [
|
|
['id' => 1, 'name' => '家'],
|
|
['id' => 2, 'name' => '父母家'],
|
|
['id' => 3, 'name' => '岳父母家'],
|
|
['id' => 4, 'name' => '公司'],
|
|
['id' => 5, 'name' => '朋友家'],
|
|
];
|
|
}
|
|
|
|
}
|