CloudStockAgentCartService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace addons\CloudStock\common\services;
  3. use addons\CloudStock\common\models\CloudStockAgentCart;
  4. use common\enums\AddonsEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\goods\GoodsAttr;
  7. class CloudStockAgentCartService extends BaseService
  8. {
  9. /**
  10. * 购物车数据
  11. *
  12. * @param array $goodsIds
  13. *
  14. * @return array
  15. */
  16. public function getCartList(array $goodsIds = []): array
  17. {
  18. $where = [
  19. ['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]
  20. ];
  21. if ($goodsIds) {
  22. $where[] = ['goods_id' => $goodsIds];
  23. }
  24. return CloudStockAgentCart::lists(
  25. [
  26. 'where' => $where,
  27. 'select' => 'goods_id, num'
  28. ]
  29. );
  30. }
  31. /**
  32. * 购物车数据
  33. *
  34. * @return int
  35. */
  36. public function getCartNum(): int
  37. {
  38. return CloudStockAgentCart::find()
  39. ->where([
  40. 'mall_id' => $this->mall_id,
  41. 'user_id' => $this->user_id,
  42. 'status' => StatusEnum::ENABLED
  43. ])
  44. ->sum('num') ?: 0;
  45. }
  46. /**
  47. * 购物车数据
  48. *
  49. * @param array $goodsGoodsNumList
  50. *
  51. * @return float
  52. */
  53. public function getCartWeight(array $goodsGoodsNumList): float
  54. {
  55. if (empty($goodsGoodsNumList)) {
  56. return 0;
  57. }
  58. $weight = GoodsAttr::find()
  59. ->where([
  60. 'goods_id' => array_column($goodsGoodsNumList, 'goods_id'),
  61. 'status' => StatusEnum::ENABLED,
  62. ])
  63. ->asArray()
  64. ->select('weight')
  65. ->indexBy('goods_id')
  66. ->column();
  67. $g = 0;
  68. foreach ($goodsGoodsNumList as $cartItem) {
  69. $g = bcadd($g, bcmul($weight[$cartItem['goods_id']] ?? 0, $cartItem['num'], 3), 3);
  70. }
  71. // 原来是g 转为kg对比
  72. return bcdiv($g, 1000, 3);
  73. }
  74. /**
  75. * 购物车总数
  76. *
  77. * @return array
  78. */
  79. public function getCurrentCartCount(): array
  80. {
  81. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
  82. $isEnableOrderNumLimit = $configs['is_enable_order_num_limit'] ?? 0;
  83. // 没开启配置,则默认是件数
  84. if (!$isEnableOrderNumLimit) {
  85. $orderLimitType = 2;
  86. $orderLimitNum = 0;
  87. } else {
  88. $orderLimitType = $configs['order_limit_type'] ?? 2;
  89. $orderLimitNum = $configs['order_limit_num'] ?? 0;
  90. }
  91. $unitArr = [1 => 'kg', 2 => '件'];
  92. return [
  93. 'unit_text' => $unitArr[$orderLimitType],
  94. 'unit' => $orderLimitType,
  95. 'num' => $orderLimitType == 2 ? $this->getCartNum() : $this->getCartWeight($this->getCartList()),
  96. 'limit_num' => $orderLimitNum,
  97. 'is_enable_order_num_limit' => $isEnableOrderNumLimit
  98. ];
  99. }
  100. /**
  101. * 验证代理权重一次性可提货数量(验证单个商品)
  102. *
  103. * @param CloudStockLevelService $levelService
  104. * @param array $goodsGoodsNumList
  105. * @param bool $isBuy
  106. *
  107. * @return bool
  108. */
  109. public function checkLevelFillOnceMinAndMax(
  110. CloudStockLevelService $levelService,
  111. array $goodsGoodsNumList,
  112. bool $isBuy = true
  113. ): bool
  114. {
  115. foreach ($goodsGoodsNumList as $item) {
  116. if (!$levelService->checkFillOnceMinAndMax($item['num'], $isBuy)) {
  117. $this->setError(sprintf('商品ID:%d,%s', $item['goods_id'], $levelService->getError()));
  118. return false;
  119. }
  120. }
  121. return true;
  122. }
  123. /**
  124. * 将购物车所有商品置为删除
  125. *
  126. * @return void
  127. */
  128. public function delAll(int $orderId)
  129. {
  130. CloudStockAgentCart::updateAll(
  131. [
  132. 'status' => StatusEnum::DISABLED,
  133. 'order_id' => $orderId,
  134. 'updated_at' => time()
  135. ],
  136. [
  137. 'user_id' => $this->user_id,
  138. 'mall_id' => $this->mall_id,
  139. 'status' => StatusEnum::ENABLED,
  140. ]
  141. );
  142. }
  143. }