Store.php
3.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
148
149
150
151
152
153
154
155
156
<?php
namespace addons\groupon\model;
use think\Model;
use addons\groupon\exception\Exception;
use think\Db;
use traits\model\SoftDelete;
/**
* 门店
*/
class Store extends Model
{
use SoftDelete;
// 表名,不含前缀
protected $name = 'groupon_store';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
protected $hidden = ['deletetime'];
// 追加属性
protected $append = [
'openweeks_arr',
'distance_text',
'image_first',
'buy_num'
];
/**
* 返回当前门店信息
*/
public static function info()
{
$store = session('current_oper_store');
return $store ? : null;
}
public function scopeShow($query)
{
return $query->where('status', 1);
}
public static function getStoreDetail($params) {
$id = $params['id'] ?? 0;
$store = Store::show()->where('selfetch', 1)->where('id', $id)->find();
if (!$store) {
throw new Exception('门店不存在');
}
return $store;
}
/**
* 门店列表
*
* @param [type] $params
* @return void
*/
public static function getStoreList($params)
{
$user = User::info();
$name = $params['name'] ?? 0;
$latitude = $params['latitude'] ?? 0;
$longitude = $params['longitude'] ?? 0;
$area_id = $params['area_id'] ?? 0;
$per_page = $params['per_page'] ?? 0;
// 查询自提点
$store = Store::show()->where('selfetch', 1);
// 门店名称
if ($name) {
$store = $store->where('name', 'like', '%'.$name.'%');
}
// 坐标
if ($latitude && $longitude) {
$store = $store->field('*, ' . getDistanceBuilder($latitude, $longitude))->order('distance', 'asc');
}
// 区域
if ($area_id) {
$store = $store->where('area_id', $area_id);
}
$store = $store->paginate($per_page ?? 10);
return $store;
}
public function getOpenweeksArrAttr($value, $data) {
return $data['openweeks'] ? array_map('intval', explode(',', $data['openweeks'])) : [];
}
public function getImageFirstAttr($value, $data)
{
return $this->images[0] ? : '';
}
public function getImagesAttr($value, $data)
{
$imagesArray = [];
if (!empty($value)) {
$imagesArray = explode(',', $value);
foreach ($imagesArray as &$v) {
$v = cdnurl($v, true);
}
return $imagesArray;
}
return $imagesArray;
}
public function getDistanceTextAttr($value, $data) {
$distance_text = '';
$distance = $data['distance'] ?? 0;
switch (true) {
case $distance >= 1000;
$distance_text = round(($distance / 1000), 2) . 'km';
break;
default :
$distance_text = $distance . 'm';
break;
}
return $distance_text;
}
public function getBuyNumAttr($value, $data)
{
$order_num = Order::payed()->where('store_id', $data['id'])->count();
return $order_num;
}
public function userStore() {
return $this->hasMany(UserStore::class, 'store_id', 'id');
}
}