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.

113 lines
2.9 KiB

  1. <?php
  2. namespace api\logic;
  3. use antgoods\goods\models\ars\Goods;
  4. use antgoods\goods\models\ars\GoodsAttr;
  5. use antgoods\goods\models\ars\GoodsSku;
  6. use yii\di\Instance;
  7. use yii\helpers\Url;
  8. use yii\web\BadRequestHttpException;
  9. use yii\web\NotFoundHttpException;
  10. use Yii;
  11. /**
  12. * @author iron
  13. * @email weiriron@gmail.com
  14. * Class Helper
  15. * @package api\logic
  16. */
  17. class Helper
  18. {
  19. /**
  20. * @param $array
  21. * @return string
  22. */
  23. public static function errorMessageStr($array): string
  24. {
  25. $data = [];
  26. foreach ($array as $k => $v) {
  27. $data[] = implode(' ', $v);
  28. }
  29. return implode(' ', $data);
  30. }
  31. /**
  32. * @param $goodsId
  33. * @param $count
  34. * @param null $skuId
  35. * @throws BadRequestHttpException
  36. * @throws NotFoundHttpException
  37. * 判断库存
  38. */
  39. public static function checkStock($goodsId, $count, $skuId = null)
  40. {
  41. $sku = GoodsSku::findOne($skuId);
  42. $goods = Goods::findOne($goodsId);
  43. if (!$goods) {
  44. throw new NotFoundHttpException('商品未找到');
  45. }
  46. if (!$sku && $goods->stock == Goods::UNLIMITED_STOCK) {
  47. return;
  48. }
  49. $stock = $sku ? $sku->stock : $goods->stock;
  50. if ($stock < $count) {
  51. throw new BadRequestHttpException("商品{$goods->name}库存不足");
  52. }
  53. return;
  54. }
  55. public static function createdResponse($model, $view)
  56. {
  57. $response = Yii::$app->getResponse()->setStatusCode(201);
  58. $id = implode(',', array_values($model->getPrimaryKey(true)));
  59. $response->getHeaders()->set('Location', Url::toRoute([$view, 'id' => $id], true));
  60. }
  61. /**
  62. * @param $goodsId
  63. * @param $skuId
  64. * @return int
  65. * @throws NotFoundHttpException
  66. * 计算商品价格
  67. */
  68. public static function countPrice($goodsId, $skuId)
  69. {
  70. $goods = Goods::findOne($goodsId);
  71. if (!$goods) {
  72. throw new NotFoundHttpException('商品未找到');
  73. }
  74. $price = $goods->price;
  75. $sku = GoodsSku::findOne($skuId);
  76. if ($sku) {
  77. $price = $sku->price;
  78. }
  79. return $price;
  80. }
  81. /**
  82. * @param $skuId
  83. * @return string
  84. * @throws NotFoundHttpException
  85. */
  86. public static function skuValue($skuId)
  87. {
  88. $sku = GoodsSku::findOne($skuId);
  89. if (!$sku) {
  90. return '';
  91. }
  92. $data = [];
  93. $goodsAttr = array_filter(explode(',', $sku->goods_attr));
  94. if (empty($goodsAttr)) {
  95. throw new NotFoundHttpException('sku无对应商品属性');
  96. }
  97. foreach ($goodsAttr as $k => $v) {
  98. $attr = GoodsAttr::findOne($v);
  99. if (!$attr) {
  100. throw new NotFoundHttpException('商品属性未找到');
  101. }
  102. $data[] = $attr->attr_value;
  103. }
  104. return implode(',', $data);
  105. }
  106. }