| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace addons\DepositPresale\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_deposit_presale_goods_attr".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $deposit_presale_active_id
- * @property int $goods_id 商品id;对应goods主表
- * @property int $attr_id 商品规格id;对应goods_attr.id;
- * @property float $deposit_presale_price 预售价格
- * @property float $deposit_price 定金
- * @property int $stock 定金预售库存
- * @property int $purchase_limit_num 限购数量
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class DepositPresaleActiveGoodsAttr extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_deposit_presale_active_goods_attr}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'deposit_presale_active_id', 'goods_id', 'attr_id'], 'required'],
- [['mall_id', 'goods_id', 'attr_id', 'stock', 'status', 'created_at', 'updated_at'], 'integer'],
- [['deposit_presale_price', 'deposit_price', 'purchase_limit_num'], 'number'],
- [['purchase_limit_num'], 'number','min' => 0],
- [['purchase_limit_num'],'default', 'value' => 0]
- ];
- }
- // 关联模型
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['id' => 'goods_id']);
- }
- public function getDepositPresaleActive()
- {
- return $this->hasOne(DepositPresaleActive::class, ['id' => 'deposit_presale_active_id']);
- }
- public function getGoodsAttr()
- {
- return $this->hasOne(GoodsAttr::class, ['id' => 'attr_id']);
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'deposit_presale_active_id' => 'qimall_addons_deposit_presale_active.id',
- 'goods_id' => '商品id;对应goods主表',
- 'attr_id' => '商品规格id;对应goods_attr.id;',
- 'deposit_presale_price' => '预售价格',
- 'deposit_price' => '定金',
- 'stock' => '定金预售库存',
- 'purchase_limit_num' => '限购数量',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Update At',
- ];
- }
- public static function setData(array $params)
- {
- try {
- $presale_price = $params['deposit_presale_goods']['presale_price'];
- $deposit_price = $params['deposit_presale_goods']['deposit_price'];
- if ($params['deposit_presale_goods']['is_attr'] == 1) {
- $goods_id = 0;
- foreach ($params['form']['select_attr_groups'] as $item) {
- $model = self::findOne(['attr_id' => $item['id'], 'deposit_presale_active_id' => $params['deposit_presale_active_id']]);
- if (!$model) {
- $model = new self();
- }
- $model->loadDefaultValues();
- if(!$goods_id) $goods_id = $item['goods_id'];
- $insert_data = [
- 'mall_id' => $params['mall_id'],
- 'attr_id' => $item['id'],
- 'deposit_presale_price' => $item['deposit_presale_price'],
- 'deposit_price' => $item['deposit_price'],
- 'deposit_presale_active_id' => $params['deposit_presale_active_id'],
- 'goods_id' => $item['goods_id'] ?? $goods_id,
- 'stock' => $item['deposit_stock'],
- 'purchase_limit_num' => $item['purchase_limit_num']
- ];
- if (!$model->load($insert_data, '')) throw new \Exception($model->getErrorMessage());
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- }
- } else {
- foreach ($params['form']['attr'] as $item) {
- $param = $item;
- $param['mall_id'] = $params['mall_id'];
- $param['attr_id'] = $item['id'];
- $param['deposit_presale_price'] = $presale_price;
- $param['deposit_price'] = $deposit_price;
- $param['deposit_presale_active_id'] = $params['deposit_presale_active_id'];
- $param['purchase_limit_num'] = $params['deposit_presale_goods']['purchase_limit_num'];
- $model = self::findOne(['attr_id' => $item['id'], 'deposit_presale_active_id' => $params['deposit_presale_active_id']]);
- if (!$model) {
- $model = new self();
- }
- $model->loadDefaultValues();
- if (!$model->load($param, '')) throw new \Exception($model->getErrorMessage());
- if (empty($params['form']['is_attr'])) $model->stock = $params['deposit_presale_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, $deposit_presale_active_id, $goods_attr_id, $num)
- {
- try {
- $model = self::findOne(['mall_id' => $mall_id, 'deposit_presale_active_id' => $deposit_presale_active_id, 'attr_id' => $goods_attr_id, 'status' => StatusEnum::ENABLED]);
- 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;
- }
- }
- }
|