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.

112 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\ErrorCode;
  4. use App\Exception\ErrorCodeException;
  5. use App\Model\v3\Market;
  6. use App\Model\v3\UserAddress;
  7. use App\Service\v3\Interfaces\LocationServiceInterface;
  8. use App\Service\v3\Interfaces\UserAddressServiceInterface;
  9. use Hyperf\Di\Annotation\Inject;
  10. class UserAddressService implements UserAddressServiceInterface
  11. {
  12. /**
  13. * @Inject
  14. * @var LocationServiceInterface
  15. */
  16. protected $locationService;
  17. public function do($userAddressId,$user_id,$user_name,$address,$doorplate,$gender,$lat,$lng,$tel,$tags)
  18. {
  19. $userAddress = UserAddress::updateOrCreate(['id' => $userAddressId],
  20. [
  21. 'user_id' => $user_id,
  22. 'user_name' => $user_name,
  23. 'address' => $address,
  24. 'doorplate' => $doorplate,
  25. 'gender' => $gender,
  26. 'lat' => $lat,
  27. 'lng' => $lng,
  28. 'tel' => $tel,
  29. 'tags' => $tags
  30. ]);
  31. return $userAddress;
  32. }
  33. public function check()
  34. {
  35. }
  36. public function undo($userAddressId)
  37. {
  38. return UserAddress::destroy($userAddressId);
  39. }
  40. public function setDefault($userId,$userAddressId)
  41. {
  42. UserAddress::query()->where([
  43. ['user_id','=',$userId],
  44. ['is_default','=',1],
  45. ])->decrement('is_default');
  46. $userAddress = UserAddress::query()->find($userAddressId);
  47. $userAddress->is_default = 1;
  48. return $userAddress->save();
  49. }
  50. public function get($userAddressId)
  51. {
  52. return UserAddress::query()->find($userAddressId);
  53. }
  54. public function getList($userId)
  55. {
  56. return UserAddress::query()->where('user_id',$userId)->get();
  57. }
  58. /**
  59. * @param $userAddressId
  60. * @param $marketId
  61. * @return false|float
  62. */
  63. public function getAddressAndDistributionRrice($userAddressId,$marketId)
  64. {
  65. $address = $this->get($userAddressId);
  66. $market = Market::query()->select('lng','lat')->find($marketId);
  67. $distance = $this->locationService->getDistanceByTencent($address->lng,$address->lat,$market->lng,$market->lat);
  68. $distributionRrice = $this->calculateDistributionRrice($distance);
  69. $res['address'] = $address;
  70. $res['distribution_price'] = $distributionRrice;
  71. return $res;
  72. }
  73. /**
  74. * @param $distance
  75. * @return false|float
  76. */
  77. function calculateDistributionRrice($distance)
  78. {
  79. $km = ceil($distance/1000);
  80. switch ($km){
  81. case ($km > 3 && $km < 5) :
  82. $distributionRrice = bcmul(0.70,($km-3),2);
  83. break;
  84. case ($km >= 5 && $km < 7) :
  85. $distributionRrice = bcmul(1.00,($km-3),2);
  86. break;
  87. case ($km >= 7 && $km < 10) :
  88. $distributionRrice = bcmul(1.50,($km-3),2);
  89. break;
  90. case ($km >= 10) :
  91. $distributionRrice = bcmul(1.50,($km-3),2);
  92. // throw new ErrorCodeException(ErrorCode::LOCATION_LONG_DISTANCE);
  93. break;
  94. default:
  95. $distributionRrice = 0;
  96. break;
  97. }
  98. $distributionRrice = bcadd($distributionRrice,3.50,2);
  99. return $distributionRrice;
  100. }
  101. }