| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace addons\GroupBuy\common\models;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use Yii;
- /**
- * This is the model class for table "qimall_addons_group_buy_goods_attr".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $group_buy_active_id
- * @property int $goods_id 商品id;对应goods主表
- * @property int $attr_id 商品规格id;对应goods_attr.id;
- * @property float $group_buy_price 价格
- * @property int $stock 拼团库存
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class GroupBuyActiveGoodsAttr extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_group_buy_active_goods_attr}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'group_buy_active_id','goods_id', 'attr_id'], 'required'],
- [['mall_id', 'goods_id', 'attr_id', 'stock', 'status', 'created_at', 'updated_at'], 'integer'],
- [['group_buy_price'], 'number'],
- ];
- }
- // 关联模型
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id']);
- }
- public function getGroupBuyActive()
- {
- return $this->hasOne(GroupBuyActive::class, ['id' => 'group_buy_active_id']);
- }
- public function getGoodsAttr()
- {
- return $this->hasOne(GoodsAttr::class, ['id' => 'attr_id']);
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'group_buy_active_id' => 'qimall_addons_group_buy_active.id',
- 'goods_id' => '商品id;对应goods主表',
- 'attr_id' => '商品规格id;对应goods_attr.id;',
- 'group_buy_price' => '价格',
- 'stock' => '拼团库存',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Update At',
- ];
- }
- public static function setData(array $params)
- {
- try {
- foreach ($params['form']['attr'] as $item){
- $param = $item;
- $param['mall_id'] = $params['mall_id'];
- $param['attr_id'] = $item['id'];
- $param['group_buy_active_id'] = $params['group_buy_active_id'];
- $model = new self();
- $model->loadDefaultValues();
- if (!$model->load($param, '')) throw new \Exception($model->getErrorMessage());
- if(empty($params['form']['is_attr'])) $model->stock = $params['group_buy_goods']['goods_stock'];
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- }
- return true;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @desc:扣减规格库存
- * @Author: hua
- * @Date: 2021/11/9
- * @Time: 11:46
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $goods_attr_id
- * @param $num +增加 -减少
- * @return bool
- * @throws Exception
- */
- public static function handleStock($mall_id,$group_buy_active_id,$goods_attr_id,$num){
- try{
- $model = self::findOne(['mall_id'=>$mall_id,'group_buy_active_id'=>$group_buy_active_id,'attr_id'=>$goods_attr_id]);
- if(empty($model)) throw new \Exception('规格不存在');
- if(!is_int($num)) throw new \Exception('库存数量必须为整数');
- if($num < 0 && $model->stock < abs($num)) throw new \Exception('库存不足');
- $model->stock += $num;
- $res = $model->save();
- if($res === false) throw new \Exception($model->getErrorMessage());
- return true;
- }catch(\Exception $e){
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|