Goods.php
10.4 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
namespace app\api\model;
use think\Db;
use think\Model;
class Goods extends Model
{
// 表名
protected $name = 'goods';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
];
protected static function init()
{
self::afterInsert(function ($row) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
});
}
public function getIsGroupList()
{
return ['1' => __('Is_group 1'), '2' => __('Is_group 2')];
}
public function getIsVipPriceList()
{
return ['1' => __('Is_vip_price 1'), '2' => __('Is_vip_price 2')];
}
public function getIsRecommendList()
{
return ['1' => __('Is_recommend 1'), '2' => __('Is_recommend 2')];
}
public function getStatusList()
{
return ['1' => __('Status 1'), '2' => __('Status 2')];
}
public function getIsGroupTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['is_group']) ? $data['is_group'] : '');
$list = $this->getIsGroupList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getIsVipPriceTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['is_vip_price']) ? $data['is_vip_price'] : '');
$list = $this->getIsVipPriceList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getIsRecommendTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['is_recommend']) ? $data['is_recommend'] : '');
$list = $this->getIsRecommendList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getImageAttr($value)
{
if ($value) $value = cdnurl($value);
return $value;
}
public function getImagesAttr($value)
{
$arr = explode(',', $value);
$data = [];
foreach ($arr as $k => $v) {
$data[$k] = cdnurl($v);
}
return $data;
}
public function selectHotData($where, $limit,$lang)
{
$where['g.status'] = 1;
if ($lang == 'ch') $field = 'g.id goods_id,g.ch_name name,g.image,g.sales_actual+g.sales_initial sales';
else $field = 'g.id goods_id,g.en_name name,g.image,g.sales_actual+g.sales_initial sales';
$data = $this->alias('g')
->join('fa_country c', 'g.country_id=c.id')
->where($where)
->field($field)
->limit($limit)
->order('sales desc')
->select();
return $data;
}
public function selectData($where, $limit,$lang)
{
$where['g.status'] = 1;
if ($lang == 'ch') $field = 'g.id goods_id,g.ch_name name,g.image,g.goods_price,is_group,c.ch_name country_name,is_vip_price,group_price,stock_num,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';
else $field = 'g.id goods_id,g.en_name name,g.image,g.goods_price,is_group,c.en_name country_name,is_vip_price,group_price,stock_num,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';
$data = $this->alias('g')
->join('fa_country c', 'g.country_id=c.id')
->where($where)
->field($field)
->limit($limit)
->order('g.weigh desc')
->select();
return $data;
}
public function selectPageData($where,$page,$limit,$lang)
{
$where['g.status'] = 1;
if ($lang == 'ch') $field = 'g.id goods_id,g.ch_name name,g.image,g.goods_price,c.ch_name country_name,is_vip_price,is_group,group_price,vip_price,stock_num,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';
else $field = 'g.id goods_id,g.en_name name,g.image,g.goods_price,c.en_name country_name,is_vip_price,is_group,group_price,vip_price,stock_num,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';
$total = $this->alias('g')
->join('fa_country c', 'g.country_id=c.id')
->join('fa_category cate1', 'g.category_one_id=cate1.id')
->join('fa_category cate2', 'g.category_two_id=cate2.id')
->join('fa_category cate3', 'g.category_three_id=cate3.id')
->where($where)
->count();
$list = $this->alias('g')
->join('fa_country c', 'g.country_id=c.id')
->join('fa_category cate1', 'g.category_one_id=cate1.id')
->join('fa_category cate2', 'g.category_two_id=cate2.id')
->join('fa_category cate3', 'g.category_three_id=cate3.id')
->where($where)
->field($field)
->order('g.weigh desc')
->page($page,$limit)
->select();
return ['total' => $total, 'list' => $list];
}
public function getInfo($where,$lang)
{
$where['g.status'] = 1;
if ($lang == 'ch') $field = 'g.id goods_id,g.ch_name name,g.image,g.status,g.goods_price,g.images,g.goods_no,g.ch_specification specification,g.ch_save_where save_where,g.ch_period period,g.ch_content content,g.stock_num,g.sales_actual,g.group_price,g.vip_price,g.is_vip_price,g.is_group,c.ch_name country_name,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';
else $field = 'g.id goods_id,g.en_name name,g.image,g.status,g.goods_price,g.images,g.goods_no,g.en_specification specification,g.en_save_where save_where,g.en_period period,g.en_content content,g.stock_num,g.sales_actual,g.group_price,g.vip_price,g.is_vip_price,g.is_group,c.en_name country_name,g.vip1_price,g.vip1_price,g.one_agency_price,g.two_agency_price,g.three_agency_price';
$data = $this->alias('g')
->join('fa_country c', 'g.country_id=c.id')
->where($where)
->field($field)
->find();
return $data;
}
public function selectGoodsStatus($where)
{
$list = $this->alias('g')
// ->join('fa_depot d', 'g.id=d.goods_id')
->where($where)
->field('g.id,g.ch_name,g.status,g.stock_num')
->select();
return $list;
}
/**
* 获取仓库库存
* @param $where
* @return mixed
*/
public function getAreaStockNum($where){
$where['type'] = '1';
$stockNum = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where($where)
->field('d.stock_num,g.ch_name')
->find();
return $stockNum;
}
/**
* 获取仓库id
* @param $goodsId
* @param $areaId
* @return mixed
*/
public function getDepot($goodsId,$areaId,$number,$type){
$arr = [];//仓库对应需扣除库存信息
//获取全部仓库库存
$sum_stock_num = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId,'d.type' => $type])
->field('d.id,d.stock_num')
->sum('d.stock_num');
if($number > $sum_stock_num){
return false; //库存不足
}
//获取当前省仓库库存
$province_depot = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.area_id' => $areaId, 'd.type' => $type])
->field('d.id,d.stock_num,d.area_id,d.type,g.id goods_id')->order('d.weigh desc')
->find();
$province_stock_num = !empty($province_depot['stock_num']) ? $province_depot['stock_num'] : 0;
$residue_number = $number - $province_stock_num;//还需多少库存
if($residue_number > 0){
//需要其它仓库提供库存
if ($province_stock_num){
$arr1['depots_id'] = $province_depot['id'];
$arr1['area_id'] = $province_depot['area_id'];
$arr1['goods_id'] = $province_depot['goods_id'];
$arr1['type'] = $province_depot['type'];
$arr1['number'] = $province_stock_num;
if ($province_depot) $arr[] = $arr1;
}
$depots = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.id' => ['not in',$province_depot['id']], 'd.type' => $type])
->field('d.id,d.stock_num,d.area_id,d.type,g.id goods_id')->order('d.weigh desc')->select();
foreach($depots as $key => $depot){
$arr1['depots_id'] = $depot['id'];
$arr1['area_id'] = $depot['area_id'];
$arr1['goods_id'] = $depot['goods_id'];
$arr1['type'] = $depot['type'];
if($depot['stock_num'] < $residue_number){
//还是不够
$arr1['number'] = $depot['stock_num'];
$arr[] = $arr1;
$residue_number = $residue_number - $depot['stock_num'];
}else{
//足够
$arr1['number'] = $residue_number;
$arr[] = $arr1;
break;
}
}
}else{
$arr1['depots_id'] = $province_depot['id'];
$arr1['area_id'] = $province_depot['area_id'];
$arr1['goods_id'] = $province_depot['goods_id'];
$arr1['type'] = $province_depot['type'];
$arr1['number'] = $number;
$arr[] = $arr1;
}
return $arr;
}
/**
* 检查商品是否有库存
* @param $goodsId
* @param $number
* @return boolean
*/
public function checkStockNum($goodsId,$number){
$sum_stock_num = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId])
->where('type',1)
->field('d.id,d.stock_num')
->sum('d.stock_num');
if($number > $sum_stock_num){
return false; //库存不足
}else{
return true;
}
}
}