作者 SHW\戥岁。。

计算折扣

@@ -8,22 +8,22 @@ class Litestoregoods extends Model @@ -8,22 +8,22 @@ class Litestoregoods extends Model
8 { 8 {
9 // 表名 9 // 表名
10 protected $name = 'litestore_goods'; 10 protected $name = 'litestore_goods';
11 - 11 +
12 // 自动写入时间戳字段 12 // 自动写入时间戳字段
13 protected $autoWriteTimestamp = 'int'; 13 protected $autoWriteTimestamp = 'int';
14 14
15 // 定义时间戳字段名 15 // 定义时间戳字段名
16 protected $createTime = 'createtime'; 16 protected $createTime = 'createtime';
17 protected $updateTime = 'updatetime'; 17 protected $updateTime = 'updatetime';
18 - 18 +
19 // 追加属性 19 // 追加属性
20 protected $append = [ 20 protected $append = [
21 'spec_type_text', 21 'spec_type_text',
22 'deduct_stock_type_text', 22 'deduct_stock_type_text',
23 'goods_status_text', 23 'goods_status_text',
24 - 'is_delete_text' 24 + 'is_delete_text',
25 ]; 25 ];
26 - 26 +
27 27
28 protected static function init() 28 protected static function init()
29 { 29 {
@@ -33,66 +33,66 @@ class Litestoregoods extends Model @@ -33,66 +33,66 @@ class Litestoregoods extends Model
33 }); 33 });
34 } 34 }
35 35
36 - 36 +
37 public function getSpecTypeList() 37 public function getSpecTypeList()
38 { 38 {
39 - return ['10' => __('Spec_type 10'),'20' => __('Spec_type 20')];  
40 - } 39 + return ['10' => __('Spec_type 10'), '20' => __('Spec_type 20')];
  40 + }
41 41
42 public function getDeductStockTypeList() 42 public function getDeductStockTypeList()
43 { 43 {
44 - return ['10' => __('Deduct_stock_type 10'),'20' => __('Deduct_stock_type 20')]; 44 + return ['10' => __('Deduct_stock_type 10'), '20' => __('Deduct_stock_type 20')];
45 } 45 }
46 46
47 public function getMakeforList() 47 public function getMakeforList()
48 { 48 {
49 - return ['1' => __('Makefor 1'),'2' => __('Makefor 2')]; 49 + return ['1' => __('Makefor 1'), '2' => __('Makefor 2')];
50 } 50 }
51 51
52 public function getIsIndexList() 52 public function getIsIndexList()
53 { 53 {
54 - return ['0' => __('Is_index 0'),'1' => __('Is_index 1')]; 54 + return ['0' => __('Is_index 0'), '1' => __('Is_index 1')];
55 } 55 }
56 56
57 public function getGoodsStatusList() 57 public function getGoodsStatusList()
58 { 58 {
59 - return ['10' => __('Goods_status 10'),'20' => __('Goods_status 20')];  
60 - } 59 + return ['10' => __('Goods_status 10'), '20' => __('Goods_status 20')];
  60 + }
61 61
62 public function getIsDeleteList() 62 public function getIsDeleteList()
63 { 63 {
64 - return ['0' => __('Is_delete 0'),'1' => __('Is_delete 1')];  
65 - } 64 + return ['0' => __('Is_delete 0'), '1' => __('Is_delete 1')];
  65 + }
66 66
67 67
68 public function getSpecTypeTextAttr($value, $data) 68 public function getSpecTypeTextAttr($value, $data)
69 - { 69 + {
70 $value = $value ? $value : (isset($data['spec_type']) ? $data['spec_type'] : ''); 70 $value = $value ? $value : (isset($data['spec_type']) ? $data['spec_type'] : '');
71 - $list = $this->getSpecTypeList(); 71 + $list = $this->getSpecTypeList();
72 return isset($list[$value]) ? $list[$value] : ''; 72 return isset($list[$value]) ? $list[$value] : '';
73 } 73 }
74 74
75 75
76 public function getDeductStockTypeTextAttr($value, $data) 76 public function getDeductStockTypeTextAttr($value, $data)
77 - { 77 + {
78 $value = $value ? $value : (isset($data['deduct_stock_type']) ? $data['deduct_stock_type'] : ''); 78 $value = $value ? $value : (isset($data['deduct_stock_type']) ? $data['deduct_stock_type'] : '');
79 - $list = $this->getDeductStockTypeList(); 79 + $list = $this->getDeductStockTypeList();
80 return isset($list[$value]) ? $list[$value] : ''; 80 return isset($list[$value]) ? $list[$value] : '';
81 } 81 }
82 82
83 83
84 public function getGoodsStatusTextAttr($value, $data) 84 public function getGoodsStatusTextAttr($value, $data)
85 - { 85 + {
86 $value = $value ? $value : (isset($data['goods_status']) ? $data['goods_status'] : ''); 86 $value = $value ? $value : (isset($data['goods_status']) ? $data['goods_status'] : '');
87 - $list = $this->getGoodsStatusList(); 87 + $list = $this->getGoodsStatusList();
88 return isset($list[$value]) ? $list[$value] : ''; 88 return isset($list[$value]) ? $list[$value] : '';
89 } 89 }
90 90
91 91
92 public function getIsDeleteTextAttr($value, $data) 92 public function getIsDeleteTextAttr($value, $data)
93 - { 93 + {
94 $value = $value ? $value : (isset($data['is_delete']) ? $data['is_delete'] : ''); 94 $value = $value ? $value : (isset($data['is_delete']) ? $data['is_delete'] : '');
95 - $list = $this->getIsDeleteList(); 95 + $list = $this->getIsDeleteList();
96 return isset($list[$value]) ? $list[$value] : ''; 96 return isset($list[$value]) ? $list[$value] : '';
97 } 97 }
98 98
@@ -118,7 +118,7 @@ class Litestoregoods extends Model @@ -118,7 +118,7 @@ class Litestoregoods extends Model
118 */ 118 */
119 public function spec() 119 public function spec()
120 { 120 {
121 - return $this->hasMany('Litestoregoodsspec','goods_id','goods_id'); 121 + return $this->hasMany('Litestoregoodsspec', 'goods_id', 'goods_id');
122 } 122 }
123 123
124 /** 124 /**
@@ -126,7 +126,7 @@ class Litestoregoods extends Model @@ -126,7 +126,7 @@ class Litestoregoods extends Model
126 */ 126 */
127 public function specRel() 127 public function specRel()
128 { 128 {
129 - return $this->belongsToMany('Litestorespecvalue', 'litestore_goods_spec_rel','spec_value_id','goods_id'); 129 + return $this->belongsToMany('Litestorespecvalue', 'litestore_goods_spec_rel', 'spec_value_id', 'goods_id');
130 } 130 }
131 131
132 /** 132 /**
@@ -146,7 +146,7 @@ class Litestoregoods extends Model @@ -146,7 +146,7 @@ class Litestoregoods extends Model
146 * @param $isUpdate 146 * @param $isUpdate
147 * @throws \Exception 147 * @throws \Exception
148 */ 148 */
149 - public function addGoodsSpec(&$data,$params,$specparams,$isUpdate = false) 149 + public function addGoodsSpec(&$data, $params, $specparams, $isUpdate = false)
150 { 150 {
151 // 更新模式: 先删除所有规格 151 // 更新模式: 先删除所有规格
152 $model = new Litestoregoodsspec; 152 $model = new Litestoregoodsspec;
@@ -154,19 +154,22 @@ class Litestoregoods extends Model @@ -154,19 +154,22 @@ class Litestoregoods extends Model
154 // 添加规格数据 154 // 添加规格数据
155 if ($data['spec_type'] === '10') { 155 if ($data['spec_type'] === '10') {
156 // 单规格 156 // 单规格
  157 + $specparams['discount'] = (round($specparams['line_price'] / $specparams['goods_price'], 2) * 100) . '折';
157 $this->spec()->save($specparams); 158 $this->spec()->save($specparams);
158 } else if ($data['spec_type'] === '20') { 159 } else if ($data['spec_type'] === '20') {
159 // 添加商品与规格关系记录 160 // 添加商品与规格关系记录
160 - $model->addGoodsSpecRel($this['goods_id'],$params['spec_attr']); 161 + $model->addGoodsSpecRel($this['goods_id'], $params['spec_attr']);
161 // 添加商品sku 162 // 添加商品sku
162 - $model->addSkuList($this['goods_id'],$params['spec_list']); 163 + $model->addSkuList($this['goods_id'], $params['spec_list']);
163 } 164 }
164 } 165 }
165 166
166 - public function removesku(){ 167 + public function removesku()
  168 + {
167 // 删除商品sku 169 // 删除商品sku
168 (new Litestoregoodsspec)->removeAll($this['goods_id']); 170 (new Litestoregoodsspec)->removeAll($this['goods_id']);
169 } 171 }
  172 +
170 /** 173 /**
171 * 获取规格信息 174 * 获取规格信息
172 */ 175 */
@@ -177,13 +180,13 @@ class Litestoregoods extends Model @@ -177,13 +180,13 @@ class Litestoregoods extends Model
177 foreach ($spec_rel as $item) { 180 foreach ($spec_rel as $item) {
178 if (!isset($specAttrData[$item['spec_id']])) { 181 if (!isset($specAttrData[$item['spec_id']])) {
179 $specAttrData[$item['spec_id']] = [ 182 $specAttrData[$item['spec_id']] = [
180 - 'group_id' => $item['spec']['id'], 183 + 'group_id' => $item['spec']['id'],
181 'group_name' => $item['spec']['spec_name'], 184 'group_name' => $item['spec']['spec_name'],
182 'spec_items' => [], 185 'spec_items' => [],
183 ]; 186 ];
184 } 187 }
185 $specAttrData[$item['spec_id']]['spec_items'][] = [ 188 $specAttrData[$item['spec_id']]['spec_items'][] = [
186 - 'item_id' => $item['pivot']['spec_value_id'], 189 + 'item_id' => $item['pivot']['spec_value_id'],
187 'spec_value' => $item['spec_value'], 190 'spec_value' => $item['spec_value'],
188 ]; 191 ];
189 } 192 }
@@ -193,15 +196,15 @@ class Litestoregoods extends Model @@ -193,15 +196,15 @@ class Litestoregoods extends Model
193 foreach ($skuData as $item) { 196 foreach ($skuData as $item) {
194 $specListData[] = [ 197 $specListData[] = [
195 'goods_spec_id' => $item['goods_spec_id'], 198 'goods_spec_id' => $item['goods_spec_id'],
196 - 'spec_sku_id' => $item['spec_sku_id'],  
197 - 'rows' => [],  
198 - 'form' => [  
199 - 'goods_no' => $item['goods_no'],  
200 - 'goods_price' => $item['goods_price'], 199 + 'spec_sku_id' => $item['spec_sku_id'],
  200 + 'rows' => [],
  201 + 'form' => [
  202 + 'goods_no' => $item['goods_no'],
  203 + 'goods_price' => $item['goods_price'],
201 'goods_weight' => $item['goods_weight'], 204 'goods_weight' => $item['goods_weight'],
202 - 'line_price' => $item['line_price'],  
203 - 'stock_num' => $item['stock_num'],  
204 - 'spec_image' => $item['spec_image'], 205 + 'line_price' => $item['line_price'],
  206 + 'stock_num' => $item['stock_num'],
  207 + 'spec_image' => $item['spec_image'],
205 ], 208 ],
206 ]; 209 ];
207 } 210 }
1 <?php 1 <?php
2 2
3 namespace app\admin\model\litestore; 3 namespace app\admin\model\litestore;
  4 +
4 use think\Model; 5 use think\Model;
5 6
6 class Litestoregoodsspec extends Model 7 class Litestoregoodsspec extends Model
@@ -8,7 +9,7 @@ class Litestoregoodsspec extends Model @@ -8,7 +9,7 @@ class Litestoregoodsspec extends Model
8 9
9 // 表名 10 // 表名
10 protected $name = 'litestore_goods_spec'; 11 protected $name = 'litestore_goods_spec';
11 - 12 +
12 // 自动写入时间戳字段 13 // 自动写入时间戳字段
13 protected $autoWriteTimestamp = 'int'; 14 protected $autoWriteTimestamp = 'int';
14 15
@@ -29,7 +30,9 @@ class Litestoregoodsspec extends Model @@ -29,7 +30,9 @@ class Litestoregoodsspec extends Model
29 foreach ($spec_list as $item) { 30 foreach ($spec_list as $item) {
30 $data[] = array_merge($item['form'], [ 31 $data[] = array_merge($item['form'], [
31 'spec_sku_id' => $item['spec_sku_id'], 32 'spec_sku_id' => $item['spec_sku_id'],
32 - 'goods_id' => $goods_id, 33 + 'goods_id' => $goods_id,
  34 + 'discount' => (round($item['form']['line_price'] / $item['form']['goods_price'], 2) * 100) . '折',
  35 +
33 ]); 36 ]);
34 } 37 }
35 return $this->saveAll($data); 38 return $this->saveAll($data);
@@ -48,8 +51,8 @@ class Litestoregoodsspec extends Model @@ -48,8 +51,8 @@ class Litestoregoodsspec extends Model
48 array_map(function ($val) use (&$data, $goods_id) { 51 array_map(function ($val) use (&$data, $goods_id) {
49 array_map(function ($item) use (&$val, &$data, $goods_id) { 52 array_map(function ($item) use (&$val, &$data, $goods_id) {
50 $data[] = [ 53 $data[] = [
51 - 'goods_id' => $goods_id,  
52 - 'spec_id' => $val['group_id'], 54 + 'goods_id' => $goods_id,
  55 + 'spec_id' => $val['group_id'],
53 'spec_value_id' => $item['item_id'], 56 'spec_value_id' => $item['item_id'],
54 ]; 57 ];
55 }, $val['spec_items']); 58 }, $val['spec_items']);
@@ -66,7 +69,7 @@ class Litestoregoodsspec extends Model @@ -66,7 +69,7 @@ class Litestoregoodsspec extends Model
66 public function removeAll($goods_id) 69 public function removeAll($goods_id)
67 { 70 {
68 $model = new Litestoregoodsspecrel; 71 $model = new Litestoregoodsspecrel;
69 - $model->where('goods_id','=', $goods_id)->delete();  
70 - return $this->where('goods_id','=', $goods_id)->delete(); 72 + $model->where('goods_id', '=', $goods_id)->delete();
  73 + return $this->where('goods_id', '=', $goods_id)->delete();
71 } 74 }
72 } 75 }
@@ -134,6 +134,7 @@ class Classification extends Api @@ -134,6 +134,7 @@ class Classification extends Api
134 ->find(); 134 ->find();
135 $item['price'] = $goods_spec['goods_price']; 135 $item['price'] = $goods_spec['goods_price'];
136 $item['line_price'] = $goods_spec['line_price']; 136 $item['line_price'] = $goods_spec['line_price'];
  137 + $item['discount'] = $goods_spec['discount'];
137 // 总库存 138 // 总库存
138 $item->append(['stock_num']); 139 $item->append(['stock_num']);
139 }); 140 });