商品价格读取

pull/1/head
limu 10 months ago
parent 3cf0677057
commit 250a660114
  1. 43
      app/api/model/Goods.php
  2. 26
      app/store/model/goods/GoodsPrice.php

@ -26,6 +26,7 @@ use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use app\store\model\goods\GoodsPrice as GoodsPriceModel;
/**
* 商品模型
@ -346,9 +347,51 @@ class Goods extends GoodsModel
return $this->setGoodsData($goodsInfo, function ($goods) {
// 计算并设置商品会员价
$this->getEnableGradeMoney() && $this->setGoodsGradeMoney($goods);
//计算plus 分销价格
$this->setGoodsMoney($goods);
});
}
/**
* 设置商品plus 分销价格
* @param Goods $goods
* @throws BaseException
*/
private function setGoodsMoney(self $goods)
{
// 判断是否登录
if (!UserService::isLogin()) {
return;
}
$catService = new \app\store\model\GoodsCategoryRel();
$catIds = $catService->where(['goods_id' => $goods->goods_id])->column('category_id');
//读取分类集
$price_list_plus = $price_list_dealer = [];
foreach ($catIds as $k => $v) {
$price_list_plus[] = GoodsPriceModel::getDiscountPrice($v, 1, $goods['goods_price_min']);
$price_list_dealer[] = GoodsPriceModel::getDiscountPrice($v, 2, $goods['goods_price_min']);
}
//价格判断
if (UserService::isstore()) {
$goods['goods_price_min_plus'] = min($price_list_plus);
$goods['goods_price_min_dealer'] = min($price_list_dealer);
} elseif (UserService::isPlusMember()) {
$goods['goods_price_min_plus'] = min($price_list_plus);
} elseif (UserService::isDealerMember()) {
$goods['goods_price_min_dealer'] = min($price_list_dealer);
}
// 会员折扣价: 商品sku列表
if ($goods->getRelation('skuList')) {
foreach ($goods['skuList'] as &$skuItem) {
$skuItem['goods_price'] = $skuItem['goods_price'];
}
}
// 会员折扣价: 已选择的商品sku(用于购物车)
if ($goods->getAttr('skuInfo')) {
$goods['skuInfo']['goods_price'] = $goods['skuInfo']['goods_price'];
}
}
/**
* 设置商品的会员价
* @param Goods $goods

@ -12,6 +12,7 @@ declare (strict_types=1);
namespace app\store\model\goods;
use app\common\library\helper;
use think\Paginator;
use think\Collection;
use app\common\model\goods\GoodsPrice as GoodsPriceModel;
@ -143,4 +144,29 @@ class GoodsPrice extends GoodsPriceModel
}
return $detail->delete() !== false;
}
/**
* 获取和计算折扣后的价格
* @param $catId
* @param $type
* @param $originalPrice
* @return string
*/
public static function getDiscountPrice($catId, $type = 1, $originalPrice): string
{
$info = self::where(['cat_id' => $catId, 'type' => $type])
->where('min_price', '<=', $originalPrice)
->order('min_price desc')
->find();
//没有配置返回原价
if (!$info) {
return $originalPrice;
}
// 使用高精度方法计算等级折扣;
$discountPrice = helper::bcmul($originalPrice, $info->markup_rate / 100, 2);
//取优惠后的价格
$lastPrice = bcsub($originalPrice, $discountPrice, 2);
return helper::number2($lastPrice, true);
}
}

Loading…
Cancel
Save