UserAddressService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Services\Dwbs;
  3. use App\Repositories\Criteria\Dwbs\UserAddressCriteria;
  4. use App\Repositories\Eloquent\Dwbs\UserAddressRepositoryEloquent;
  5. use App\Repositories\Enums\ModelStatusEnum;
  6. use App\Repositories\Models\Dwbs\UserAddress;
  7. use App\Repositories\Presenters\Dwbs\UserAddressPresenter;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Crypt;
  10. class UserAddressService
  11. {
  12. /**
  13. * @var UserAddressRepositoryEloquent
  14. */
  15. private $repository;
  16. /**
  17. * UserAddressService constructor.
  18. *
  19. * @param UserAddressRepositoryEloquent $userAddressRepositoryEloquent
  20. */
  21. public function __construct(UserAddressRepositoryEloquent $repository)
  22. {
  23. $this->repository = $repository;
  24. }
  25. /**
  26. * @param Request $request
  27. *
  28. * @return mixed
  29. * @throws \Prettus\Repository\Exceptions\RepositoryException
  30. */
  31. public function handleList(Request $request)
  32. {
  33. $this->repository->pushCriteria(new UserAddressCriteria($request));
  34. $this->repository->setPresenter(UserAddressPresenter::class);
  35. return $this->repository->searchListsPage();
  36. }
  37. /**
  38. * @param $id
  39. *
  40. * @return \Illuminate\Database\Eloquent\Model
  41. */
  42. public function handleProfile($id)
  43. {
  44. $this->repository->setPresenter(UserAddressPresenter::class);
  45. return $this->repository->searchBy($id);
  46. }
  47. /**
  48. * @param array $data
  49. *
  50. * @return mixed
  51. * @throws \Prettus\Validator\Exceptions\ValidatorException
  52. */
  53. public function handleStore($data)
  54. {
  55. $data['mobile_encryption'] = Crypt::encryptString($data['mobile']);
  56. $data['mobile'] = mobile_hidden($data['mobile']);
  57. $userAddress = $this->repository->create($data);
  58. $this->handleCheckUserDefault($data, $userAddress->id);
  59. return $userAddress;
  60. }
  61. /**
  62. * 检查是否默认
  63. * @param $data
  64. * @param $cid
  65. * @return true
  66. */
  67. public function handleCheckUserDefault($data, $cid = 0)
  68. {
  69. if ($data['is_default']) {
  70. UserAddress::query()->where('user_id', $data['user_id'])->where('is_default', ModelStatusEnum::OK)->where('id', '<>', $cid)->update(['is_default' => ModelStatusEnum::PAUSE]);
  71. return true;
  72. }
  73. // $is_default = UserAddress::query()->where('user_id', $data['user_id'])->where('is_default', ModelStatusEnum::OK)->where('status', ModelStatusEnum::OK)->exists();
  74. // if ($is_default) return true;
  75. // UserAddress::query()->where('user_id', $data['user_id'])->where('id', '=', $cid)->update(['is_default' => ModelStatusEnum::OK]);
  76. return true;
  77. }
  78. /**
  79. * @param array $data
  80. *
  81. * @return mixed
  82. * @throws \Prettus\Validator\Exceptions\ValidatorException
  83. */
  84. public function handleUpdate($data)
  85. {
  86. $userAddress = $this->repository->update($data, $data['id']);
  87. $this->handleCheckUserDefault($userAddress, $userAddress->id);
  88. return $userAddress;
  89. }
  90. /**
  91. * @param Request $request
  92. *
  93. * @return mixed
  94. * @throws \Prettus\Validator\Exceptions\ValidatorException
  95. */
  96. public function handleDelete($id)
  97. {
  98. return $this->repository->where('user_id', login_user_id())->where('id', $id)->delete();
  99. }
  100. /**
  101. * 选项
  102. * @param Request $request
  103. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection|mixed
  104. * @throws \Prettus\Repository\Exceptions\RepositoryException
  105. */
  106. public function handleSelectOptions(Request $request)
  107. {
  108. $this->repository->pushCriteria(new UserAddressCriteria($request));
  109. return $this->repository->all(['id', 'name']);
  110. }
  111. /**
  112. * 批量删除
  113. * @param $ids
  114. * @return mixed
  115. */
  116. public function handleBatchDelete($ids)
  117. {
  118. return $this->repository->whereIn('id', $ids)->delete();
  119. }
  120. /**
  121. * @param Request $request
  122. *
  123. * @return mixed
  124. * @throws \Prettus\Repository\Exceptions\RepositoryException
  125. */
  126. public function handleAll(Request $request)
  127. {
  128. $this->repository->pushCriteria(new UserAddressCriteria($request));
  129. $this->repository->setPresenter(UserAddressPresenter::class);
  130. return $this->repository->get();
  131. }
  132. /**
  133. * @param Request $request
  134. *
  135. * @return mixed
  136. * @throws \Prettus\Repository\Exceptions\RepositoryException
  137. */
  138. public function handleIds(Request $request)
  139. {
  140. $this->repository->pushCriteria(new UserAddressCriteria($request));
  141. return $this->repository->pluck('id');
  142. }
  143. public function handleMeDefaule($user_id)
  144. {
  145. $address_id = UserAddress::query()->where('user_id', $user_id)->where('is_default', ModelStatusEnum::OK)->where('status', ModelStatusEnum::OK)->value('id');
  146. if (!$address_id) {
  147. $address_id = UserAddress::query()->where('user_id', $user_id)->where('status', ModelStatusEnum::OK)->orderByDesc('updated_at')->value('id') ?? 0;
  148. }
  149. if (!$address_id) return false;
  150. $this->repository->setPresenter(UserAddressPresenter::class);
  151. $model = $this->repository->find($address_id);
  152. return $model;
  153. }
  154. }