1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace App\Modules\Mini\Services;
- use App\Base\BaseService;
- use App\Models\User\User;
- use App\Models\User\UserAddress;
- class AddressService extends BaseService
- {
- public function store(User $user, $params)
- {
- $id = $params['id'];
- if ($id) {
- $address = UserAddress::where("id", $id)->where("user_id", $user->id)->firstOrFail();
- } else {
- $address = new UserAddress();
- $address->user_id = $user->id;
- }
- $address->address = $params['address'];
- $address->name = $params['name'];
- $address->phone = $params['phone'];
- $address->is_default = (int)$params['is_default'];
- if (UserAddress::where("user_id", $user->id)->count() == 0) {
- $address->is_default = 1;
- }
- $address->area = $params['area'];
- $address->save();
- if ($address->is_default == 1) {
- UserAddress::where("user_id", $user->id)->where("id", "<>", $address->id)->update([
- "is_default" => 0,
- ]);
- }
- }
- public function paginate(User $user, $params)
- {
- $p = UserAddress::where("user_id", $user->id)->orderByDesc("id")->paginate($params['page_size']);
- return [
- "total" => $p->total(),
- "page_total" => $p->lastPage(),
- "list" => array_map(function (UserAddress $model) {
- return [
- "id" => $model->id,
- "is_default" => $model->is_default,
- "name" => $model->name,
- "phone" => $model->phone,
- "area" => $model->area,
- "address" => $model->address,
- ];
- }, $p->items()),
- ];
- }
- }
|