ScoreGoodsSkuPrice.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
<?php
namespace addons\shopro\model;
use think\Model;
use addons\shopro\exception\Exception;
use addons\shopro\model\GoodsSku;
use addons\shopro\model\GoodsSkuPrice;
use traits\model\SoftDelete;
/**
* 商品模型
*/
class ScoreGoodsSkuPrice extends Model
{
use SoftDelete;
// 表名,不含前缀
protected $name = 'shopro_score_goods_sku_price';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
protected $hidden = ['createtime', 'updatetime', 'deletetime'];
// 追加属性
protected $append = [
];
public static function getGoodsList($params)
{
$keyword = $params['keyword'] ?? '';
// 获取所有积分商品
$goods_ids = ScoreGoodsSkuPrice::field('goods_id')->where('status', 'up')->group('goods_id')->column('goods_id');
$goods = Goods::where('id', 'in', $goods_ids);
if (!empty($keyword)) {
$goods = $goods->where('title|subtitle','like','%'. $keyword .'%');
}
$goods = $goods->with('scoreGoodsSkuPrice')->paginate(10);
$data = $goods->items();
foreach ($data as $key => $g) {
if (count($g['score_goods_sku_price'])) {
$g['price'] = $g['score_goods_sku_price'][0]['score'];
if ($g['score_goods_sku_price'][0]['price'] > 0) {
$g['price'] .= '+' . $g['score_goods_sku_price'][0]['price'];
}
// 销量
$g['sales'] = array_sum(array_column($g['score_goods_sku_price'], 'sales'));
$g['stock'] = array_sum(array_column($g['score_goods_sku_price'], 'stock'));
}
$data[$key] = $g;
}
// $collection = collection($goods->items());
// $data = $collection->hidden(Goods::$list_hidden);
$goods->data = $data;
return $goods;
}
public static function getGoodsDetail($id)
{
$detail = Goods::where('id', $id)->find();
if (!$detail) {
throw new Exception('商品不存在或已下架');
}
// 增加商品附加数据, 活动默认全是 null
$detail = $detail->append(['service', 'sku', 'coupons']);
$detail->activity = null;
$detail->activity_type = null;
$score_sku_price = self::where([
'status' => 'up',
])->select();
$sku_price = GoodsSkuPrice::where([
'goods_id' => $detail['id'],
// 'status' => 'up', // 商品规格的上下架,不控制积分规格的上下架
])->select();
$new_sku_price = [];
foreach ($sku_price as $s => $k) {
foreach ($score_sku_price as $c) {
if ($k['id'] == $c['sku_price_id']) {
$new_sku_price[$s] = $k;
$new_sku_price[$s]['stock'] = $c['stock'];
$new_sku_price[$s]['sales'] = $c['sales'];
$new_sku_price[$s]['price'] = $c['price'];
$new_sku_price[$s]['score'] = $c['score'];
$new_sku_price[$s]['price_text'] = $c['score'] . '积分';
if ($c['price'] > 0) {
$new_sku_price[$s]['price_text'] .= '+¥' . $c['price'];
}
// 记录对应活动的规格的记录
$new_sku_price[$s]['item_goods_sku_price'] = $c;
break;
}
}
}
$sku_price = array_values($new_sku_price);
if (!count($sku_price)) {
throw new Exception('商品规格不存在或已下架');
}
$detail->price = $sku_price[0]['score'] . '积分';
if ($sku_price[0]['price'] > 0) {
$detail->price .= '+¥' . $sku_price[0]['price'];
}
$detail->sku_price = $sku_price;
$detail->sales = array_sum(array_column($sku_price, 'sales'));
$detail->stock = array_sum(array_column($sku_price, 'stock'));
return $detail;
}
}