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.
 
 

165 lines
4.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\ShopCartServiceInterface;
use App\Service\v3\Interfaces\UserAddressServiceInterface;
use App\Service\v3\SfExpress;
/** @var Inject 注解使用 */
use Hyperf\Di\Annotation\Inject;
class UserAddressService implements UserAddressServiceInterface
{
/**
* @Inject
* @var LocationServiceInterface
*/
protected $locationService;
/**
* @Inject
* @var ShopCartServiceInterface
*/
protected $shopCartService;
/**
* @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
* @param string $shopCartIds
* @param string $deliveryTimeNote
* @return array
*/
public function getAddressAndDistributionPrice($userAddressId, $marketId, string $shopCartIds, string $deliveryTimeNote)
{
$address = $this->get($userAddressId);
$market = Market::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);
$storeList = $this->shopCartService->getGoodsByShopcartId($shopCartIds);
$weight = 0;
foreach ($storeList as $item) {
foreach ($item['shopping_cart'] as $v) {
$weight += $v['goods']['weight'] * $v['num'];
}
}
$sfParams = [
'user_lng' => $address['address']['lng'],
'user_lat' => $address['address']['lat'],
'user_address' => $address['address']['address'],
'weight' => $weight,
'shop' => [
'shop_name' => $market->name,
'shop_phone' => $market->tel,
'shop_address' => $market->address,
'shop_lng' => $market->lng,
'shop_lat' => $market->lat,
],
];
# 预约单处理
$sfParams = array_merge($sfParams, SfExpress::getInstance()->deliveryTimeNote2expectTime($deliveryTimeNote));
$distributionPrice = SfExpress::getInstance()->getDeliveryCost($sfParams);
$originalPrice = SfExpress::getInstance()->getOriginDeliveryCost($distributionPrice);
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 > $distributionPrice ? '¥ '.$originalPrice : '';
return $res;
}
public function getTags()
{
return [
['id' => 1, 'name' => '家'],
['id' => 2, 'name' => '父母家'],
['id' => 3, 'name' => '岳父母家'],
['id' => 4, 'name' => '公司'],
['id' => 5, 'name' => '朋友家'],
];
}
}