IntegralGoods.php
5.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
157
158
159
160
161
162
163
164
<?php
namespace app\api\model;
use think\Model;
class IntegralGoods extends Model
{
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 selectPageData($where, $page, $limit,$lang)
{
$where['g.status'] = 1;
if ($lang == 'ch') $field = 'g.id integral_goods_id,g.ch_name name,g.image,g.original_price,g.goods_price,g.integral,c.ch_name country_name,stock_num';
else $field = 'g.id integral_goods_id,g.en_name name,g.image,g.original_price,g.goods_price,g.integral,c.en_name country_name,stock_num';
$total = $this->alias('g')
->join('fa_country c', 'g.country_id=c.id')
->where($where)
->count();
$list = $this->alias('g')
->join('fa_country c', 'g.country_id=c.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 integral_goods_id,g.ch_name name,g.image,g.status,g.original_price,g.goods_price,g.integral,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,c.ch_name country_name';
else $field = 'g.id integral_goods_id,g.en_name name,g.image,g.status,g.original_price,g.goods_price,g.integral,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,c.en_name country_name';
$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'] = '2';
$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){
$arr = [];//仓库对应需扣除库存信息
//获取全部仓库库存
$sum_stock_num = $this->alias('g')
->join('fa_integral_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId])
->field('d.id,d.stock_num')
->sum('d.stock_num');
if($number > $sum_stock_num){
return false; //库存不足
}
//获取当前省仓库库存
$province_depot = $this->alias('g')
->join('fa_integral_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.area_id' => $areaId])
->field('d.id,d.stock_num')->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){
//需要其它仓库提供库存
$arr1['depots_id'] = $province_depot['id'];
$arr1['number'] = $province_stock_num;
$arr[] = $arr1;
$depots = $this->alias('g')
->join('fa_integral_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.id' => ['not in',$province_depot['id']]])
->field('d.id,d.stock_num')->order('d.weigh desc')->select();
foreach($depots as $key => $depot){
$arr1['depots_id'] = $depot['id'];
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['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',2)
->field('d.id,d.stock_num')
->sum('d.stock_num');
if($number > $sum_stock_num){
return false; //库存不足
}else{
return true;
}
}
}