UserAddressService.php
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 逻辑层:用户收货地址 服务类,处理业务逻辑
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Services\User
* @package App\Services\User
* @author Richer <yangzi1028@163.com>
* @date 2022年5月19日16:41:59
* @copyright 2019-2020 Richer (http://www.umu888.com)
* @license http://www.umu888.com License
* @link http://www.umu888.com
*/
namespace App\Services\User;
use App\Models\User\UserAddress;
use App\Services\BaseService;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
/**
* Class UserAddressService.
*
* @category App\Services\User
* @package App\Services\User
* @author Richer <yangzi1028@163.com>
* @date 2022年5月19日16:41:59
* @copyright 2019-2020 Richer (http://www.umu888.com)
* @license http://www.umu888.com License
* @link http://www.umu888.com
*/
class UserAddressService extends BaseService
{
/**
* UserAddressService constructor.
*
* @param UserAddress $model 注入模型
*/
public function __construct(UserAddress $model)
{
// 执行父类构造方法
parent::__construct($model);
}
/**
* Display a listing of the resource.
*
* @return array|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection
*/
public function list()
{
// 当前登录用户的角色
return $this->getLoginUser()->addresses()->orderByDesc('default')->orderBy('sort')->get();
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request 请求
*
* @return bool|\Illuminate\Http\Response
*
* @throws ModelNotFoundException
*/
public function store($request)
{
$user = $this->getLoginUser();
// 新增的数据设置为私有
$request['province'] = $this->getRegionNameByCode($request->province_code);
$request['city'] = $this->getRegionNameByCode($request->city_code);
$request['district'] = $this->getRegionNameByCode($request->district_code);
$request['user_id'] = optional($user)->id ?: 0;
$result = $this->create($request);
if ($result == false) {
return false;
}
// 如果设置了 default 则需要将其他的数据设置为 0
if ($request->default == 1) {
$this->getLoginUser()->addresses()->where('id', '<>', $result->id)->update(['default' => 0]);
}
return $result;
}
/**
* Update the specified resource in storage.
*
* @param Request $request 请求
* @param integer $id 主键id
*
* @return bool
*
* @throws ModelNotFoundException
*/
public function update($request, $id)
{
// 新增的数据设置为私有
$request['province'] = $this->getRegionNameByCode($request->province_code);
$request['city'] = $this->getRegionNameByCode($request->city_code);
$request['district'] = $this->getRegionNameByCode($request->district_code);
$result = parent::update($request, $id);
if ($result == false) {
return false;
}
// 如果设置了 default 则需要将其他的数据设置为 0
if ($request->default == 1) {
$this->getLoginUser()->addresses()->where('id', '<>', $id)->where('default', 1)->update(['default' => 0]);
}
return $result;
}
/**
* Update the specified resource in storage.
*
* @param integer $id 主键id
*
* @return bool
*/
public function setDefault($id)
{
$model = $this->model->findOrFail($id);
$model->default = 1;
$result = $model->save();
if ($result == false) {
return false;
}
$this->getLoginUser()->addresses()->where('id', '<>', $id)->where('default', 1)->update(['default' => 0]);
return $result;
}
}