|
|
@ -2,11 +2,21 @@ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Service\v3\Implementations; |
|
|
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\Model\v3\UserAddress; |
|
|
|
|
|
use App\Service\v3\Interfaces\LocationServiceInterface; |
|
|
use App\Service\v3\Interfaces\UserAddressServiceInterface; |
|
|
use App\Service\v3\Interfaces\UserAddressServiceInterface; |
|
|
|
|
|
use Hyperf\Di\Annotation\Inject; |
|
|
|
|
|
|
|
|
class UserAddressService implements UserAddressServiceInterface |
|
|
class UserAddressService implements UserAddressServiceInterface |
|
|
{ |
|
|
{ |
|
|
|
|
|
/** |
|
|
|
|
|
* @Inject |
|
|
|
|
|
* @var LocationServiceInterface |
|
|
|
|
|
*/ |
|
|
|
|
|
protected $locationService; |
|
|
|
|
|
|
|
|
public function do($userAddressId,$user_id,$user_name,$address,$doorplate,$gender,$lat,$lng,$tel,$tags) |
|
|
public function do($userAddressId,$user_id,$user_name,$address,$doorplate,$gender,$lat,$lng,$tel,$tags) |
|
|
{ |
|
|
{ |
|
|
@ -55,4 +65,54 @@ class UserAddressService implements UserAddressServiceInterface |
|
|
{ |
|
|
{ |
|
|
return UserAddress::query()->where('user_id',$userId)->get(); |
|
|
return UserAddress::query()->where('user_id',$userId)->get(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param $userAddressId |
|
|
|
|
|
* @param $marketId |
|
|
|
|
|
* @return false|float |
|
|
|
|
|
*/ |
|
|
|
|
|
public function getAddressAndDistributionRrice($userAddressId,$marketId) |
|
|
|
|
|
{ |
|
|
|
|
|
$address = $this->get($userAddressId); |
|
|
|
|
|
$market = Market::query()->select('lng','lat')->find($marketId); |
|
|
|
|
|
$result = $this->locationService->getDistanceByTencent($address->lng,$address->lat,$market->lng,$market->lat); |
|
|
|
|
|
$resultArr = json_decode($result,true); |
|
|
|
|
|
if($resultArr['status'] > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ErrorCodeException(ErrorCode::LOCATION_ERROR,$resultArr['message']); |
|
|
|
|
|
} |
|
|
|
|
|
$distance = $resultArr['result']['routes'][0]['distance']; |
|
|
|
|
|
$distributionRrice = $this->calculateDistributionRrice($distance); |
|
|
|
|
|
$res['address'] = $address; |
|
|
|
|
|
$res['distribution_price'] = $distributionRrice; |
|
|
|
|
|
return $res; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param $distance |
|
|
|
|
|
* @return false|float |
|
|
|
|
|
*/ |
|
|
|
|
|
function calculateDistributionRrice($distance) |
|
|
|
|
|
{ |
|
|
|
|
|
$km = ceil($distance/1000); |
|
|
|
|
|
switch ($km){ |
|
|
|
|
|
case ($km > 3 && $km < 5) : |
|
|
|
|
|
$distributionRrice = bcmul(0.70,($km-3),2); |
|
|
|
|
|
break; |
|
|
|
|
|
case ($km >= 5 && $km < 7) : |
|
|
|
|
|
$distributionRrice = bcmul(1.00,($km-3),2); |
|
|
|
|
|
break; |
|
|
|
|
|
case ($km >= 7 && $km < 10) : |
|
|
|
|
|
$distributionRrice = bcmul(1.50,($km-3),2); |
|
|
|
|
|
break; |
|
|
|
|
|
case ($km >= 10) : |
|
|
|
|
|
throw new ErrorCodeException(ErrorCode::LOCATION_LONG_DISTANCE); |
|
|
|
|
|
break; |
|
|
|
|
|
default: |
|
|
|
|
|
$distributionRrice = 0; |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
$distributionRrice = bcadd($distributionRrice,3.50,2); |
|
|
|
|
|
return $distributionRrice; |
|
|
|
|
|
} |
|
|
} |
|
|
} |