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.

114 lines
3.3 KiB

6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 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. if(empty($userAddressId)){
  38. UserAddress::query()->where(['user_id'=> $user_id,'is_default'=> 1])->update(['is_default' => 0]);
  39. UserAddress::query()->where('id',$userAddress->id)->update(['is_default' => 1]);
  40. }
  41. return $userAddress;
  42. }
  43. public function check()
  44. {
  45. }
  46. public function undo($userAddressId)
  47. {
  48. return UserAddress::destroy($userAddressId);
  49. }
  50. public function setDefault($userId,$userAddressId)
  51. {
  52. UserAddress::query()->where([
  53. ['user_id','=',$userId],
  54. ['is_default','=',1],
  55. ])->update(['is_default' => 0]);
  56. $userAddress = UserAddress::query()->find($userAddressId);
  57. $userAddress->is_default = 1;
  58. return $userAddress->save();
  59. }
  60. public function get($userAddressId)
  61. {
  62. $address['address'] = UserAddress::query()->find($userAddressId);
  63. $address['tags'] = $this->getTags();
  64. return $address;
  65. }
  66. public function getList($userId)
  67. {
  68. return UserAddress::query()->where('user_id',$userId)->get();
  69. }
  70. /**
  71. * @param $userAddressId
  72. * @param $marketId
  73. * @return false|float
  74. */
  75. public function getAddressAndDistributionPrice($userAddressId,$marketId)
  76. {
  77. $address = $this->get($userAddressId);
  78. $market = Market::query()->select('lng','lat')->find($marketId);
  79. if(empty($address['address']->lng) || empty($address['address']->lat) || empty($market->lng) || empty($market->lat)){
  80. throw new ErrorCodeException(ErrorCode::LOCATION_USER_ADDRESS);
  81. }
  82. $distance = $this->locationService->getDistanceByTencent($market->lng,$market->lat,$address['address']->lng,$address['address']->lat);
  83. $distributionPrice = $this->distributionPriceService->do($distance);
  84. $res['address'] = $address;
  85. $res['delivery_distance'] = $distance;
  86. $res['distribution_price'] = $distributionPrice;
  87. return $res;
  88. }
  89. public function getTags()
  90. {
  91. return [
  92. ['id' => 1, 'name' => '家'],
  93. ['id' => 2, 'name' => '父母家'],
  94. ['id' => 3, 'name' => '岳父母家'],
  95. ['id' => 4, 'name' => '公司'],
  96. ['id' => 5, 'name' => '朋友家'],
  97. ];
  98. }
  99. }