AddressService.php
4.5 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
140
141
142
143
144
145
146
147
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 逻辑层:用户收货地址 服务类,处理业务逻辑
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Services\User
* @package App\Services\User
* @author Richer <yangzi1028@163.com>
* @date 2020年06月18日 10:38:01
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Services\User;
use App\Models\User\Address;
use App\Models\User\SupplierDyXcxUser;
use App\Models\User\SupplierXcxUser;
use App\Services\BaseService;
/**
* Class UserAddressService.
*
* @category App\Services\User
* @package App\Services\User
* @author Richer <yangzi1028@163.com>
* @date 2020年06月18日 10:38:01
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class AddressService extends BaseService
{
/**
* UserAddressService constructor.
*
* @param Address $model 注入模型
*/
public function __construct(Address $model)
{
// 执行父类构造方法
parent::__construct($model);
}
/**
* Display a listing of the resource.
*
* @return array|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection
*/
public function list()
{
// 当前登录用户的角色
$list = $this->getLoginUser()->addresses()->orderByDesc('default')->orderBy('sort')->get();
return $list;
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request 请求
*
* @return bool|\Illuminate\Http\Response
*
* @throws \Illuminate\Database\Eloquent\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;
if ($user->appid && get_class($user) == SupplierXcxUser::class) {
$request['appid'] = $user->appid;
}
if ($user->appid && get_class($user) == SupplierDyXcxUser::class) {
$request['dy_appid'] = $user->appid;
}
$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 \App\Services\Response|bool
*
* @throws \Illuminate\Database\Eloquent\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 \App\Services\Response|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;
}
}