作者 SHW\戥岁。。

计算折扣

... ... @@ -8,22 +8,22 @@ class Litestoregoods extends Model
{
// 表名
protected $name = 'litestore_goods';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = [
'spec_type_text',
'deduct_stock_type_text',
'goods_status_text',
'is_delete_text'
'is_delete_text',
];
protected static function init()
{
... ... @@ -33,66 +33,66 @@ class Litestoregoods extends Model
});
}
public function getSpecTypeList()
{
return ['10' => __('Spec_type 10'),'20' => __('Spec_type 20')];
}
return ['10' => __('Spec_type 10'), '20' => __('Spec_type 20')];
}
public function getDeductStockTypeList()
{
return ['10' => __('Deduct_stock_type 10'),'20' => __('Deduct_stock_type 20')];
return ['10' => __('Deduct_stock_type 10'), '20' => __('Deduct_stock_type 20')];
}
public function getMakeforList()
{
return ['1' => __('Makefor 1'),'2' => __('Makefor 2')];
return ['1' => __('Makefor 1'), '2' => __('Makefor 2')];
}
public function getIsIndexList()
{
return ['0' => __('Is_index 0'),'1' => __('Is_index 1')];
return ['0' => __('Is_index 0'), '1' => __('Is_index 1')];
}
public function getGoodsStatusList()
{
return ['10' => __('Goods_status 10'),'20' => __('Goods_status 20')];
}
return ['10' => __('Goods_status 10'), '20' => __('Goods_status 20')];
}
public function getIsDeleteList()
{
return ['0' => __('Is_delete 0'),'1' => __('Is_delete 1')];
}
return ['0' => __('Is_delete 0'), '1' => __('Is_delete 1')];
}
public function getSpecTypeTextAttr($value, $data)
{
{
$value = $value ? $value : (isset($data['spec_type']) ? $data['spec_type'] : '');
$list = $this->getSpecTypeList();
$list = $this->getSpecTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getDeductStockTypeTextAttr($value, $data)
{
{
$value = $value ? $value : (isset($data['deduct_stock_type']) ? $data['deduct_stock_type'] : '');
$list = $this->getDeductStockTypeList();
$list = $this->getDeductStockTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getGoodsStatusTextAttr($value, $data)
{
{
$value = $value ? $value : (isset($data['goods_status']) ? $data['goods_status'] : '');
$list = $this->getGoodsStatusList();
$list = $this->getGoodsStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getIsDeleteTextAttr($value, $data)
{
{
$value = $value ? $value : (isset($data['is_delete']) ? $data['is_delete'] : '');
$list = $this->getIsDeleteList();
$list = $this->getIsDeleteList();
return isset($list[$value]) ? $list[$value] : '';
}
... ... @@ -118,7 +118,7 @@ class Litestoregoods extends Model
*/
public function spec()
{
return $this->hasMany('Litestoregoodsspec','goods_id','goods_id');
return $this->hasMany('Litestoregoodsspec', 'goods_id', 'goods_id');
}
/**
... ... @@ -126,7 +126,7 @@ class Litestoregoods extends Model
*/
public function specRel()
{
return $this->belongsToMany('Litestorespecvalue', 'litestore_goods_spec_rel','spec_value_id','goods_id');
return $this->belongsToMany('Litestorespecvalue', 'litestore_goods_spec_rel', 'spec_value_id', 'goods_id');
}
/**
... ... @@ -146,7 +146,7 @@ class Litestoregoods extends Model
* @param $isUpdate
* @throws \Exception
*/
public function addGoodsSpec(&$data,$params,$specparams,$isUpdate = false)
public function addGoodsSpec(&$data, $params, $specparams, $isUpdate = false)
{
// 更新模式: 先删除所有规格
$model = new Litestoregoodsspec;
... ... @@ -154,19 +154,22 @@ class Litestoregoods extends Model
// 添加规格数据
if ($data['spec_type'] === '10') {
// 单规格
$specparams['discount'] = (round($specparams['line_price'] / $specparams['goods_price'], 2) * 100) . '折';
$this->spec()->save($specparams);
} else if ($data['spec_type'] === '20') {
// 添加商品与规格关系记录
$model->addGoodsSpecRel($this['goods_id'],$params['spec_attr']);
$model->addGoodsSpecRel($this['goods_id'], $params['spec_attr']);
// 添加商品sku
$model->addSkuList($this['goods_id'],$params['spec_list']);
$model->addSkuList($this['goods_id'], $params['spec_list']);
}
}
public function removesku(){
public function removesku()
{
// 删除商品sku
(new Litestoregoodsspec)->removeAll($this['goods_id']);
}
/**
* 获取规格信息
*/
... ... @@ -177,13 +180,13 @@ class Litestoregoods extends Model
foreach ($spec_rel as $item) {
if (!isset($specAttrData[$item['spec_id']])) {
$specAttrData[$item['spec_id']] = [
'group_id' => $item['spec']['id'],
'group_id' => $item['spec']['id'],
'group_name' => $item['spec']['spec_name'],
'spec_items' => [],
];
}
$specAttrData[$item['spec_id']]['spec_items'][] = [
'item_id' => $item['pivot']['spec_value_id'],
'item_id' => $item['pivot']['spec_value_id'],
'spec_value' => $item['spec_value'],
];
}
... ... @@ -193,15 +196,15 @@ class Litestoregoods extends Model
foreach ($skuData as $item) {
$specListData[] = [
'goods_spec_id' => $item['goods_spec_id'],
'spec_sku_id' => $item['spec_sku_id'],
'rows' => [],
'form' => [
'goods_no' => $item['goods_no'],
'goods_price' => $item['goods_price'],
'spec_sku_id' => $item['spec_sku_id'],
'rows' => [],
'form' => [
'goods_no' => $item['goods_no'],
'goods_price' => $item['goods_price'],
'goods_weight' => $item['goods_weight'],
'line_price' => $item['line_price'],
'stock_num' => $item['stock_num'],
'spec_image' => $item['spec_image'],
'line_price' => $item['line_price'],
'stock_num' => $item['stock_num'],
'spec_image' => $item['spec_image'],
],
];
}
... ...
<?php
namespace app\admin\model\litestore;
use think\Model;
class Litestoregoodsspec extends Model
... ... @@ -8,7 +9,7 @@ class Litestoregoodsspec extends Model
// 表名
protected $name = 'litestore_goods_spec';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
... ... @@ -29,7 +30,9 @@ class Litestoregoodsspec extends Model
foreach ($spec_list as $item) {
$data[] = array_merge($item['form'], [
'spec_sku_id' => $item['spec_sku_id'],
'goods_id' => $goods_id,
'goods_id' => $goods_id,
'discount' => (round($item['form']['line_price'] / $item['form']['goods_price'], 2) * 100) . '折',
]);
}
return $this->saveAll($data);
... ... @@ -48,8 +51,8 @@ class Litestoregoodsspec extends Model
array_map(function ($val) use (&$data, $goods_id) {
array_map(function ($item) use (&$val, &$data, $goods_id) {
$data[] = [
'goods_id' => $goods_id,
'spec_id' => $val['group_id'],
'goods_id' => $goods_id,
'spec_id' => $val['group_id'],
'spec_value_id' => $item['item_id'],
];
}, $val['spec_items']);
... ... @@ -66,7 +69,7 @@ class Litestoregoodsspec extends Model
public function removeAll($goods_id)
{
$model = new Litestoregoodsspecrel;
$model->where('goods_id','=', $goods_id)->delete();
return $this->where('goods_id','=', $goods_id)->delete();
$model->where('goods_id', '=', $goods_id)->delete();
return $this->where('goods_id', '=', $goods_id)->delete();
}
}
... ...
... ... @@ -134,6 +134,7 @@ class Classification extends Api
->find();
$item['price'] = $goods_spec['goods_price'];
$item['line_price'] = $goods_spec['line_price'];
$item['discount'] = $goods_spec['discount'];
// 总库存
$item->append(['stock_num']);
});
... ...