| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- namespace addons\CloudStockTwo\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\user\UserPartitionRelationship;
- use Yii;
- /**
- * This is the model class for table "{{%addons_cloud_stock_goods".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $goods_id 商品id
- * @property string|null $agent_price 代理商进货价设置
- * @property int $goods_type 商品类型;0 商城商品
- * @property string|null $equal_level_list 平级奖设置
- * @property string|null $fill_level_list 补货奖设置
- * @property string|null $over_level_list 越级奖设置
- * @property string|null $over_level_list_2 越2级奖设置
- * @property int $is_allow_fill 是否允许向平台补货
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class CloudStockTwoGoods extends BaseActiveRecord
- {
- private static $cloud_stock_goods = null;
- private static $goods = null;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_cloud_stock_two_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'goods_id', 'goods_type', 'is_allow_fill', 'status', 'created_at', 'updated_at'], 'integer'],
- [['agent_price', 'equal_level_list', 'fill_level_list', 'over_level_list'], 'string', 'max' => 5120],
- [['over_level_list_2'], 'string'],
- ];
- }
- /**
- * {@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' => '是否允许向平台补货',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public static function setData(array $params)
- {
- try {
- $query = self::find()->where(['mall_id' => $params['mall_id'], 'goods_id' => $params['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- 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('服务不存在或已删除');
- } else {
- $model = new self();
- $model->loadDefaultValues();
- }
- $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());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function patch($params)
- {
- $model = self::getOne([['goods_id' => $params['goods_id']]]);
- 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) : float
- {
- $good_ids = array_values($good_ids);
- if (empty(self::$cloud_stock_goods)) {
- $cloud_stock_goods = self::find()
- ->select('goods_id,agent_price')
- ->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 {CloudStockTwoGoods} $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): float
- {
- $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 = CloudStockTwoAgent::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);
- $agent_user_ids = CloudStockTwoAgent::find()
- ->select('user_id')
- ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'user_id' => $parent_id_arr])
- ->andWhere(['>', 'level', $current_agent_info->level])
- ->asArray()
- ->column();
- $all_stocks = CloudStockTwoAgentGoods::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 = CloudStockTwoLevel::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)) {
- return false;
- }
- return true;
- }
- }
- }
|