You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.9 KiB
114 lines
2.9 KiB
<?php
|
|
|
|
namespace api\logic;
|
|
|
|
use antgoods\goods\models\ars\Goods;
|
|
use antgoods\goods\models\ars\GoodsAttr;
|
|
use antgoods\goods\models\ars\GoodsSku;
|
|
use yii\di\Instance;
|
|
use yii\helpers\Url;
|
|
use yii\web\BadRequestHttpException;
|
|
use yii\web\NotFoundHttpException;
|
|
use Yii;
|
|
|
|
/**
|
|
* @author iron
|
|
* @email weiriron@gmail.com
|
|
* Class Helper
|
|
* @package api\logic
|
|
*/
|
|
class Helper
|
|
{
|
|
/**
|
|
* @param $array
|
|
* @return string
|
|
*/
|
|
public static function errorMessageStr($array): string
|
|
{
|
|
$data = [];
|
|
foreach ($array as $k => $v) {
|
|
$data[] = implode(' ', $v);
|
|
}
|
|
return implode(' ', $data);
|
|
}
|
|
|
|
/**
|
|
* @param $goodsId
|
|
* @param $count
|
|
* @param null $skuId
|
|
* @throws BadRequestHttpException
|
|
* @throws NotFoundHttpException
|
|
* 判断库存
|
|
*/
|
|
public static function checkStock($goodsId, $count, $skuId = null)
|
|
{
|
|
$sku = GoodsSku::findOne($skuId);
|
|
$goods = Goods::findOne($goodsId);
|
|
if (!$goods) {
|
|
throw new NotFoundHttpException('商品未找到');
|
|
}
|
|
if (!$sku && $goods->stock == Goods::UNLIMITED_STOCK) {
|
|
return;
|
|
}
|
|
$stock = $sku ? $sku->stock : $goods->stock;
|
|
if ($stock < $count) {
|
|
throw new BadRequestHttpException("商品{$goods->name}库存不足");
|
|
}
|
|
return;
|
|
}
|
|
|
|
public static function createdResponse($model, $view)
|
|
{
|
|
$response = Yii::$app->getResponse()->setStatusCode(201);
|
|
$id = implode(',', array_values($model->getPrimaryKey(true)));
|
|
$response->getHeaders()->set('Location', Url::toRoute([$view, 'id' => $id], true));
|
|
}
|
|
|
|
/**
|
|
* @param $goodsId
|
|
* @param $skuId
|
|
* @return int
|
|
* @throws NotFoundHttpException
|
|
* 计算商品价格
|
|
*/
|
|
public static function countPrice($goodsId, $skuId)
|
|
{
|
|
$goods = Goods::findOne($goodsId);
|
|
if (!$goods) {
|
|
throw new NotFoundHttpException('商品未找到');
|
|
}
|
|
$price = $goods->price;
|
|
$sku = GoodsSku::findOne($skuId);
|
|
if ($sku) {
|
|
$price = $sku->price;
|
|
}
|
|
return $price;
|
|
}
|
|
|
|
/**
|
|
* @param $skuId
|
|
* @return string
|
|
* @throws NotFoundHttpException
|
|
*/
|
|
public static function skuValue($skuId)
|
|
{
|
|
$sku = GoodsSku::findOne($skuId);
|
|
if (!$sku) {
|
|
return '';
|
|
}
|
|
$data = [];
|
|
$goodsAttr = array_filter(explode(',', $sku->goods_attr));
|
|
if (empty($goodsAttr)) {
|
|
throw new NotFoundHttpException('sku无对应商品属性');
|
|
}
|
|
foreach ($goodsAttr as $k => $v) {
|
|
$attr = GoodsAttr::findOne($v);
|
|
if (!$attr) {
|
|
throw new NotFoundHttpException('商品属性未找到');
|
|
}
|
|
$data[] = $attr->attr_value;
|
|
}
|
|
return implode(',', $data);
|
|
|
|
}
|
|
}
|