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.

117 lines
3.5 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
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. use Hyperf\DbConnection\Db;
  12. class UserAddressService implements UserAddressServiceInterface
  13. {
  14. /**
  15. * @Inject
  16. * @var LocationServiceInterface
  17. */
  18. protected $locationService;
  19. /**
  20. * @Inject
  21. * @var DistributionPriceServiceInterface
  22. */
  23. protected $distributionPriceService;
  24. public function do($userAddressId,$user_id,$user_name,$address,$doorplate,$gender,$lat,$lng,$tel,$tags)
  25. {
  26. $userAddress = UserAddress::updateOrCreate(['id' => $userAddressId],
  27. [
  28. 'user_id' => $user_id,
  29. 'user_name' => $user_name,
  30. 'address' => $address,
  31. 'doorplate' => $doorplate,
  32. 'gender' => $gender,
  33. 'lat' => $lat,
  34. 'lng' => $lng,
  35. 'tel' => $tel,
  36. 'tags' => $tags
  37. ]);
  38. return $userAddress;
  39. }
  40. public function check()
  41. {
  42. }
  43. public function undo($userAddressId)
  44. {
  45. return UserAddress::destroy($userAddressId);
  46. }
  47. public function setDefault($userId,$userAddressId)
  48. {
  49. UserAddress::query()->where([
  50. ['user_id','=',$userId],
  51. ['is_default','=',1],
  52. ])->update(['is_default' => 0]);
  53. $userAddress = UserAddress::query()->find($userAddressId);
  54. $userAddress->is_default = 1;
  55. return $userAddress->save();
  56. }
  57. public function get($userAddressId)
  58. {
  59. $address['address'] = UserAddress::query()->find($userAddressId);
  60. $address['tags'] = $this->getTags();
  61. return $address;
  62. }
  63. public function getList($userId)
  64. {
  65. return UserAddress::query()->where('user_id',$userId)->get();
  66. }
  67. /**
  68. * @param $userAddressId
  69. * @param $marketId
  70. * @return false|float
  71. */
  72. public function getAddressAndDistributionPrice($userAddressId,$marketId)
  73. {
  74. $address = $this->get($userAddressId);
  75. $market = Market::query()->select('lng','lat')->find($marketId);
  76. if(empty($address['address']->lng) || empty($address['address']->lat) || empty($market->lng) || empty($market->lat)){
  77. throw new ErrorCodeException(ErrorCode::LOCATION_USER_ADDRESS);
  78. }
  79. $distance = $this->locationService->getDistanceByTencent($market->lng,$market->lat,$address['address']->lng,$address['address']->lat);
  80. $distributionPrice = $this->distributionPriceService->do($distance);
  81. if($distance >= 1000){
  82. $distance_text = '距您收货地址 ' . bcdiv($distance,1000,2) . 'km';
  83. }else{
  84. $distance_text = '距您收货地址 ' . $distance . 'm';
  85. }
  86. $res['address'] = $address;
  87. $res['delivery_distance'] = $distance;
  88. $res['distribution_price'] = $distributionPrice;
  89. // $res['distribution_text'] = '¥ '.$distributionPrice .'(' .$distance_text .')';
  90. $res['distribution_text'] = $distance_text;
  91. return $res;
  92. }
  93. public function getTags()
  94. {
  95. return [
  96. ['id' => 1, 'name' => '家'],
  97. ['id' => 2, 'name' => '父母家'],
  98. ['id' => 3, 'name' => '岳父母家'],
  99. ['id' => 4, 'name' => '公司'],
  100. ['id' => 5, 'name' => '朋友家'],
  101. ];
  102. }
  103. }