| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- <?php
- namespace addons\CloudStock\common\models;
- use common\enums\AddonsEnum;
- use common\enums\RabbitMqEnum;
- 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 string|null $price_difference 购买价差礼包价格
- * @property int $is_allow_fill 是否允许向平台补货
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property string|null $monthly_purchase 月限购
- * @property string|null $additional_purchase_restrictions 增加额外限购
- * @property float|null $sort 排序值,小排前
- * @property int|null $min_replenish 最低进货数量,0不限制
- * @property int|null $warning_quantity 预警数量
- * @property string|null $restrict_permissions 限制权限
- * @property int|null $is_buhuo 补货赠送优惠券
- * @property int|null $is_peihuo 配货赠送优惠券
- */
- class CloudStockGoods extends BaseActiveRecord
- {
- private static $cloud_stock_goods = null;
- private static $goods = null;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_cloud_stock_goods}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'goods_id', 'goods_type', 'is_allow_fill', 'status', 'created_at', 'updated_at', 'is_take_by_range', 'is_find_equal', 'min_replenish',
- 'warning_quantity','is_buhuo','is_peihuo'], 'integer'],
- [['agent_price', 'equal_level_list', 'fill_level_list', 'over_level_list'], 'string', 'max' => 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;
- }
- }
- }
|