| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace addons\CloudStock\common\services;
- use addons\CloudStock\common\models\CloudStockAgentCart;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\goods\GoodsAttr;
- class CloudStockAgentCartService extends BaseService
- {
- /**
- * 购物车数据
- *
- * @param array $goodsIds
- *
- * @return array
- */
- public function getCartList(array $goodsIds = []): array
- {
- $where = [
- ['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]
- ];
- if ($goodsIds) {
- $where[] = ['goods_id' => $goodsIds];
- }
- return CloudStockAgentCart::lists(
- [
- 'where' => $where,
- 'select' => 'goods_id, num'
- ]
- );
- }
- /**
- * 购物车数据
- *
- * @return int
- */
- public function getCartNum(): int
- {
- return CloudStockAgentCart::find()
- ->where([
- 'mall_id' => $this->mall_id,
- 'user_id' => $this->user_id,
- 'status' => StatusEnum::ENABLED
- ])
- ->sum('num') ?: 0;
- }
- /**
- * 购物车数据
- *
- * @param array $goodsGoodsNumList
- *
- * @return float
- */
- public function getCartWeight(array $goodsGoodsNumList): float
- {
- if (empty($goodsGoodsNumList)) {
- return 0;
- }
- $weight = GoodsAttr::find()
- ->where([
- 'goods_id' => array_column($goodsGoodsNumList, 'goods_id'),
- 'status' => StatusEnum::ENABLED,
- ])
- ->asArray()
- ->select('weight')
- ->indexBy('goods_id')
- ->column();
- $g = 0;
- foreach ($goodsGoodsNumList as $cartItem) {
- $g = bcadd($g, bcmul($weight[$cartItem['goods_id']] ?? 0, $cartItem['num'], 3), 3);
- }
- // 原来是g 转为kg对比
- return bcdiv($g, 1000, 3);
- }
- /**
- * 购物车总数
- *
- * @return array
- */
- public function getCurrentCartCount(): array
- {
- $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
- $isEnableOrderNumLimit = $configs['is_enable_order_num_limit'] ?? 0;
- // 没开启配置,则默认是件数
- if (!$isEnableOrderNumLimit) {
- $orderLimitType = 2;
- $orderLimitNum = 0;
- } else {
- $orderLimitType = $configs['order_limit_type'] ?? 2;
- $orderLimitNum = $configs['order_limit_num'] ?? 0;
- }
- $unitArr = [1 => 'kg', 2 => '件'];
- return [
- 'unit_text' => $unitArr[$orderLimitType],
- 'unit' => $orderLimitType,
- 'num' => $orderLimitType == 2 ? $this->getCartNum() : $this->getCartWeight($this->getCartList()),
- 'limit_num' => $orderLimitNum,
- 'is_enable_order_num_limit' => $isEnableOrderNumLimit
- ];
- }
- /**
- * 验证代理权重一次性可提货数量(验证单个商品)
- *
- * @param CloudStockLevelService $levelService
- * @param array $goodsGoodsNumList
- * @param bool $isBuy
- *
- * @return bool
- */
- public function checkLevelFillOnceMinAndMax(
- CloudStockLevelService $levelService,
- array $goodsGoodsNumList,
- bool $isBuy = true
- ): bool
- {
- foreach ($goodsGoodsNumList as $item) {
- if (!$levelService->checkFillOnceMinAndMax($item['num'], $isBuy)) {
- $this->setError(sprintf('商品ID:%d,%s', $item['goods_id'], $levelService->getError()));
- return false;
- }
- }
- return true;
- }
- /**
- * 将购物车所有商品置为删除
- *
- * @return void
- */
- public function delAll(int $orderId)
- {
- CloudStockAgentCart::updateAll(
- [
- 'status' => StatusEnum::DISABLED,
- 'order_id' => $orderId,
- 'updated_at' => time()
- ],
- [
- 'user_id' => $this->user_id,
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- ]
- );
- }
- }
|