Wxlitestoregoods.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
<?php
namespace addons\litestore\model;
use think\Model;
use app\admin\model\Litestoregoods;
class Wxlitestoregoods extends Litestoregoods
{
protected $append = ['goods_sales'];
public function getGoodsSalesAttr($value, $data)
{
return $data['sales_initial'] + $data['sales_actual'];
}
//这里是最新上架的8件商品
public function getNewList()
{
return $this->with(['spec', 'category'])
->where('is_delete', '=', 0)
->where('goods_status', '=', 10)
->order(['goods_id' => 'desc', 'goods_sort' => 'asc'])
->limit(8)
->select();
}
//这里是随机的8件商品
public function getRandom8()
{
return $this->with(['spec', 'category'])
->where('is_delete', '=', 0)
->where('goods_status', '=', 10)
->orderRaw('rand()')
->limit(8)
->select();
}
public static function detail($goods_id)
{
$dataout = self::get($goods_id, ['category', 'spec', 'specRel', 'freight']);
$dataout['image'] = cdnurl(explode(",",$dataout['images'])[0], true);
return $dataout;
}
/**
* 商品多规格信息
*/
public function getGoodsSku($goods_sku_id)
{
$goodsSkuData = array_column($this['spec'], null, 'spec_sku_id');
if (!isset($goodsSkuData[$goods_sku_id])) {
return false;
}
$goods_sku = $goodsSkuData[$goods_sku_id];
// 多规格文字内容
$goods_sku['goods_attr'] = '';
if ($this['spec_type'] === '20') {
$attrs = explode('_', $goods_sku['spec_sku_id']);
$spec_rel = array_column($this['specRel'], null, 'id');
foreach ($attrs as $specValueId) {
$goods_sku['goods_attr'] .= $spec_rel[$specValueId]['spec']['spec_name'] . ':'
. $spec_rel[$specValueId]['spec_value'] . '; ';
}
//这里格式化 展示图片
$goods_sku['img_show'] = $goods_sku['spec_image']=='' ? '': cdnurl($goods_sku['spec_image'], true);
}
return $goods_sku;
}
public function getListByIds($goodsIds) {
$dataout = $this->with(['category', 'spec', 'spec_rel.spec', 'freight.rule'])
->where('goods_id', 'in', $goodsIds)->select();
$blistdatarList = [];
foreach ($dataout as $index => $item) {
$item['image'] = cdnurl(explode(",",$item['images'])[0], true);
$blistdatarList[] = $item;
}
return $blistdatarList;
}
public function updateStockSales($goodsList)
{
// 整理批量更新商品销量
$goodsSave = [];
// 批量更新商品规格:sku销量、库存
$goodsSpecSave = [];
foreach ($goodsList as $goods) {
$goodsSave[] = [
'goods_id' => $goods['goods_id'],
'sales_actual' => ['inc', $goods['total_num']]
];
$specData = [
'goods_spec_id' => $goods['goods_spec_id'],
'goods_sales' => ['inc', $goods['total_num']]
];
// 付款减库存
if ($goods['deduct_stock_type'] === '20') {
$specData['stock_num'] = ['dec', $goods['total_num']];
}
$goodsSpecSave[] = $specData;
}
// 更新商品总销量
$this->allowField(true)->isUpdate()->saveAll($goodsSave);
// 更新商品规格库存
(new Litestoregoodsspec)->isUpdate()->saveAll($goodsSpecSave);
}
}