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.

92 lines
2.4 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
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\DistributionPriceServiceInterface;
  8. use App\Service\v3\Interfaces\LocationServiceInterface;
  9. use App\Service\v3\Interfaces\UserAddressServiceInterface;
  10. use Hyperf\Di\Annotation\Inject;
  11. class UserAddressService implements UserAddressServiceInterface
  12. {
  13. /**
  14. * @Inject
  15. * @var LocationServiceInterface
  16. */
  17. protected $locationService;
  18. /**
  19. * @Inject
  20. * @var DistributionPriceServiceInterface
  21. */
  22. protected $distributionPriceService;
  23. public function do($userAddressId,$user_id,$user_name,$address,$doorplate,$gender,$lat,$lng,$tel,$tags)
  24. {
  25. $userAddress = UserAddress::updateOrCreate(['id' => $userAddressId],
  26. [
  27. 'user_id' => $user_id,
  28. 'user_name' => $user_name,
  29. 'address' => $address,
  30. 'doorplate' => $doorplate,
  31. 'gender' => $gender,
  32. 'lat' => $lat,
  33. 'lng' => $lng,
  34. 'tel' => $tel,
  35. 'tags' => $tags
  36. ]);
  37. return $userAddress;
  38. }
  39. public function check()
  40. {
  41. }
  42. public function undo($userAddressId)
  43. {
  44. return UserAddress::destroy($userAddressId);
  45. }
  46. public function setDefault($userId,$userAddressId)
  47. {
  48. UserAddress::query()->where([
  49. ['user_id','=',$userId],
  50. ['is_default','=',1],
  51. ])->decrement('is_default');
  52. $userAddress = UserAddress::query()->find($userAddressId);
  53. $userAddress->is_default = 1;
  54. return $userAddress->save();
  55. }
  56. public function get($userAddressId)
  57. {
  58. return UserAddress::query()->find($userAddressId);
  59. }
  60. public function getList($userId)
  61. {
  62. return UserAddress::query()->where('user_id',$userId)->get();
  63. }
  64. /**
  65. * @param $userAddressId
  66. * @param $marketId
  67. * @return false|float
  68. */
  69. public function getAddressAndDistributionPrice($userAddressId,$marketId)
  70. {
  71. $address = $this->get($userAddressId);
  72. $market = Market::query()->select('lng','lat')->find($marketId);
  73. $distance = $this->locationService->getDistanceByTencent($address->lng,$address->lat,$market->lng,$market->lat);
  74. $distributionPrice = $this->distributionPriceService->do($distance);
  75. $res['address'] = $address;
  76. $res['distribution_price'] = $distributionPrice;
  77. return $res;
  78. }
  79. }