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.
 
 

128 lines
3.9 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;
use Hyperf\DbConnection\Db;
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($market->lng,$market->lat,$address['address']->lng,$address['address']->lat);
$distributionPrice = $this->distributionPriceService->do($distance);
$originalPrice = $this->distributionPriceService->original($distance);
if($distance >= 1000){
$distance_text = '距您收货地址 ' . bcdiv($distance,1000,2) . 'km';
}else{
$distance_text = '距您收货地址 ' . $distance . 'm';
}
/**
* distributionPrice 配送费
* originalPrice 配送费原价
* style 前端输出样式
*/
$res['address'] = $address;
$res['delivery_distance'] = $distance;
$res['distribution_price'] = $distributionPrice;
$res['original_price'] = $originalPrice;
$res['style'] = 'strike';
// $res['distribution_text'] = '¥ '.$distributionPrice .'(' .$distance_text .')';
$res['distribution_text'] = $distance_text;
$res['distribution_price_text'] = '¥ '.$distributionPrice;
$res['original_price_text'] = '¥ '.$originalPrice;
return $res;
}
public function getTags()
{
return [
['id' => 1, 'name' => '家'],
['id' => 2, 'name' => '父母家'],
['id' => 3, 'name' => '岳父母家'],
['id' => 4, 'name' => '公司'],
['id' => 5, 'name' => '朋友家'],
];
}
}