5120], [['monthly_purchase','additional_purchase_restrictions'], 'string'], [['over_level_list_2', 'restrict_permissions', 'price_difference'], 'string'], [['sort'], 'number'], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => '商城id', 'goods_id' => '商品id', 'agent_price' => '代理商进货价设置', 'goods_type' => '商品类型;0 商城商品', 'equal_level_list' => '平级奖设置', 'fill_level_list' => '补货奖设置', 'over_level_list' => '越级奖设置', 'over_level_list_2' => '越2级奖设置', 'is_allow_fill' => '是否允许向平台补货', 'is_take_by_range' => '是否开启区间拿货价', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'monthly_purchase' => '月限购', 'additional_purchase_restrictions' => '增加额外限购', 'price_difference' => '购买价差礼包价格', 'is_buhuo' => '补货赠送优惠券', 'is_peihuo' => '配货赠送优惠券', ]; } public static function setData(array $params) { $t = Yii::$app->db->beginTransaction(); try { $new_goods_id = $params['goods_id']; $query = self::find()->where(['mall_id' => $params['mall_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); $additional_purchase_restrictions= null; if (!empty($params['id'])) { $query->andWhere(['<>', 'id', $params['id']]); $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($model)) throw new \Exception('服务不存在或已删除'); $additional_purchase_restrictions = $model['additional_purchase_restrictions']; $old_goods_id = $model->goods_id; if(!empty($new_goods_id) && $new_goods_id!=$old_goods_id){ self::delCloudGoodsAfter($params['mall_id'],$old_goods_id); } } else { $model = new self(); $model->loadDefaultValues(); } if(empty($params['restrict_permissions'])){ $params['restrict_permissions'] = []; } $params['restrict_permissions'] = json_encode($params['restrict_permissions']); $res = $query->one(); if ($res) throw new \Exception('该商品已经被添加过'); if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage()); if (!$model->save()) throw new \Exception($model->getErrorMessage()); $t->commit(); if(empty($params['id'])){ CloudStockAgentGuota::asynComputeFillNums($params['mall_id']); }elseif (!empty($model)){ if(!empty($new_goods_id)&&!empty($old_goods_id) && $new_goods_id!=$old_goods_id){ CloudStockAgentGuota::asynComputeFillNums($params['mall_id']); }elseif(empty($params['additional_purchase_restrictions'])||$additional_purchase_restrictions!=$params['additional_purchase_restrictions']){ CloudStockAgentGuota::asynComputeFillNums($params['mall_id']); } } return $model; } catch (\Exception $e) { $t->rollBack(); self::$static_error = $e->getMessage(); return false; } } public static function delCloudGoodsAfter($mall_id,$goods_id){ CloudStockAgentGoods::updateAll(['status'=>StatusEnum::DELETE],['goods_id'=>$goods_id,'mall_id'=>$mall_id]); $data = ['goods_id'=>$goods_id,'mall_id'=>$mall_id]; \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, $data, CloudStockAgentGuota::class, 'computeByGoodsDel'); } public static function patch($params) { $model = self::getOne([['goods_id' => $params['goods_id']], ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]]]); if (empty($model)) throw new \Exception('服务不存在或已删除'); if (isset($params['is_allow_fill']) && $params['is_allow_fill'] != '') { $model->is_allow_fill = $params['is_allow_fill']; } if (!$model->save()) { return false; } return true; } /** * @name: * @msg: 获取商品补货价格 * @param {int} $mall_id * @param {int} $user_stock_agent_level 会员代理商等级 * @param {array} $good_ids 商品id,必须是数组。如果获取一个商品的补货价格,把商品id放数组内 * @param {int} $index 是否特定获取某一个商品的补货价格,0:否;如果需要获取特定商品补货价格,$index 传入商品 id * @return {*} */ public static function getReplenshPrice(int $mall_id, int $user_stock_agent_level, array $good_ids, int $index = 0) : int { $good_ids = array_values($good_ids); if (empty(self::$cloud_stock_goods)) { $cloud_stock_goods = self::find() ->select('goods_id,agent_price,is_take_by_range') ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $good_ids]) ->asArray() ->indexBy('goods_id') ->all(); self::$cloud_stock_goods = $cloud_stock_goods; } else { $cloud_stock_goods = self::$cloud_stock_goods; } if (empty(self::$goods)) { $mall_goods = Goods::find() ->select('id,goods_name,price,cover_pic') ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_ids, 'is_on_sale' => 1, 'is_attr' => 0]) ->asArray() ->indexBy('id') ->all(); self::$goods = $mall_goods; } else { $mall_goods = self::$goods; } if (1 == count($good_ids) && empty($index)) $index = $good_ids[0]; if (!empty($index)) { $replenish_price = self::parseReplenishPrice($cloud_stock_goods[$index] ?? [], $mall_goods[$index] ?? [], $user_stock_agent_level); } else { foreach ($cloud_stock_goods as $index => $cloud_stock_good) { $replenish_price[$index] = self::parseReplenishPrice($cloud_stock_good, $mall_goods[$index] ?? [], $user_stock_agent_level); } } return $replenish_price; } /** * @name: * @msg: 提取补货价格 * @param {CloudStockGoods} $cloud_stock_good * @param {Goods} $goods * @param {int} $user_stock_agent_level * @param {int} $index * @return {*} */ private static function parseReplenishPrice(array $cloud_stock_good, array $good, int $user_stock_agent_level): int { $price = $good['price'] ?? 0; if (!empty($cloud_stock_good)) { $all_level_prices = json_decode($cloud_stock_good['agent_price'], true); foreach ($all_level_prices as $level_price) { if ($level_price['level'] == $user_stock_agent_level) { $agent_level_price = $level_price; } } if (!empty($agent_level_price) && 1 == $agent_level_price['is_use']) { $replenish_price = $agent_level_price['price'] ?: $price; } else { $replenish_price = $price; } } else { $replenish_price = $price; } if (!is_numeric($replenish_price)) { $replenish_price = $price; } return $replenish_price; } /** * @name: * @msg: 获取当前会员最大的补货数量 * @param {int} $goods_id 商品 id * @param {int} $user_id 会员 id * @param {int} $mall_id 商城 id * @return mixed 有错误返回 false,否则返回得到的库存数量 */ public static function allPerantAgentGoodStocks(int $goods_id, int $user_id, int $mall_id) { $stock_good = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $goods_id]); if (empty($stock_good)) { self::$static_error = '云库存商品不存在'; return false; } if (0 == $stock_good->is_allow_fill) { // 不允许向平台补货 $current_agent_info = CloudStockAgent::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $user_id]); $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id); $parent_id_arr = array_reverse($parent_id_arr); $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic'); $agent_user_ids = CloudStockAgent::find() ->select('user_id') ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $parent_id_arr]) ->andWhere(['>', 'level', $current_agent_info->level]) ->andWhere(!empty($configs['is_show_setting']) && !empty($configs['is_show_stock_permission']) ? ['=', 'is_stock_deducted_by_subordinate', StatusEnum::DISABLED] : []) // 如果开启了"前端用户库存权限",则只查允许被下级补货的代理 ->asArray() ->column(); $all_stocks = CloudStockAgentGoods::find() ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $goods_id, 'user_id' => $agent_user_ids]) ->sum('num'); $all_stocks = $all_stocks ?? 0; } else { $good = Goods::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $goods_id, 'is_on_sale' => 1, 'is_attr' => 0]); $all_stocks = $good->stock; } return $all_stocks; } /** * @name: * @msg: 当前会员是否可以补货当前商品 * @param {int} $mall_id 商城 id * @param {int} $goods_id 商品 id * @param {int} $user_curr_level 当前会员的代理商等级 * @return bool */ public static function canFillGood(int $mall_id, int $goods_id, int $user_curr_level, int $user_id) { $stock_good = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'goods_id' => $goods_id]); if (empty($stock_good)) { self::$static_error = '云库存商品不存在'; return false; } $highest_level = CloudStockLevel::find() ->select('id,level,name') ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]) ->orderBy('level desc') ->limit(1) ->one(); if (0 == $stock_good->is_allow_fill && $highest_level->level == $user_curr_level) { self::$static_error = null; return false; } else { $res = self::allPerantAgentGoodStocks($goods_id, $user_id, $mall_id); if (empty($res)) { self::$static_error = '可补货数量不足'; return false; } return true; } } }