DepositPresaleActiveGoodsAttr.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace addons\DepositPresale\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\goods\Goods;
  6. use common\models\goods\GoodsAttr;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_addons_deposit_presale_goods_attr".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城id
  13. * @property int $deposit_presale_active_id
  14. * @property int $goods_id 商品id;对应goods主表
  15. * @property int $attr_id 商品规格id;对应goods_attr.id;
  16. * @property float $deposit_presale_price 预售价格
  17. * @property float $deposit_price 定金
  18. * @property int $stock 定金预售库存
  19. * @property int $purchase_limit_num 限购数量
  20. * @property int $status 状态[-1:删除;0:禁用;1启用]
  21. * @property int $created_at
  22. * @property int $updated_at
  23. */
  24. class DepositPresaleActiveGoodsAttr extends BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_deposit_presale_active_goods_attr}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id', 'deposit_presale_active_id', 'goods_id', 'attr_id'], 'required'],
  40. [['mall_id', 'goods_id', 'attr_id', 'stock', 'status', 'created_at', 'updated_at'], 'integer'],
  41. [['deposit_presale_price', 'deposit_price', 'purchase_limit_num'], 'number'],
  42. [['purchase_limit_num'], 'number','min' => 0],
  43. [['purchase_limit_num'],'default', 'value' => 0]
  44. ];
  45. }
  46. // 关联模型
  47. public function getGoods()
  48. {
  49. return $this->hasOne(Goods::class, ['id' => 'goods_id']);
  50. }
  51. public function getDepositPresaleActive()
  52. {
  53. return $this->hasOne(DepositPresaleActive::class, ['id' => 'deposit_presale_active_id']);
  54. }
  55. public function getGoodsAttr()
  56. {
  57. return $this->hasOne(GoodsAttr::class, ['id' => 'attr_id']);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'id' => 'ID',
  66. 'mall_id' => '商城id',
  67. 'deposit_presale_active_id' => 'qimall_addons_deposit_presale_active.id',
  68. 'goods_id' => '商品id;对应goods主表',
  69. 'attr_id' => '商品规格id;对应goods_attr.id;',
  70. 'deposit_presale_price' => '预售价格',
  71. 'deposit_price' => '定金',
  72. 'stock' => '定金预售库存',
  73. 'purchase_limit_num' => '限购数量',
  74. 'status' => '状态[-1:删除;0:禁用;1启用]',
  75. 'created_at' => 'Created At',
  76. 'updated_at' => 'Update At',
  77. ];
  78. }
  79. public static function setData(array $params)
  80. {
  81. try {
  82. $presale_price = $params['deposit_presale_goods']['presale_price'];
  83. $deposit_price = $params['deposit_presale_goods']['deposit_price'];
  84. if ($params['deposit_presale_goods']['is_attr'] == 1) {
  85. $goods_id = 0;
  86. foreach ($params['form']['select_attr_groups'] as $item) {
  87. $model = self::findOne(['attr_id' => $item['id'], 'deposit_presale_active_id' => $params['deposit_presale_active_id']]);
  88. if (!$model) {
  89. $model = new self();
  90. }
  91. $model->loadDefaultValues();
  92. if(!$goods_id) $goods_id = $item['goods_id'];
  93. $insert_data = [
  94. 'mall_id' => $params['mall_id'],
  95. 'attr_id' => $item['id'],
  96. 'deposit_presale_price' => $item['deposit_presale_price'],
  97. 'deposit_price' => $item['deposit_price'],
  98. 'deposit_presale_active_id' => $params['deposit_presale_active_id'],
  99. 'goods_id' => $item['goods_id'] ?? $goods_id,
  100. 'stock' => $item['deposit_stock'],
  101. 'purchase_limit_num' => $item['purchase_limit_num']
  102. ];
  103. if (!$model->load($insert_data, '')) throw new \Exception($model->getErrorMessage());
  104. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  105. }
  106. } else {
  107. foreach ($params['form']['attr'] as $item) {
  108. $param = $item;
  109. $param['mall_id'] = $params['mall_id'];
  110. $param['attr_id'] = $item['id'];
  111. $param['deposit_presale_price'] = $presale_price;
  112. $param['deposit_price'] = $deposit_price;
  113. $param['deposit_presale_active_id'] = $params['deposit_presale_active_id'];
  114. $param['purchase_limit_num'] = $params['deposit_presale_goods']['purchase_limit_num'];
  115. $model = self::findOne(['attr_id' => $item['id'], 'deposit_presale_active_id' => $params['deposit_presale_active_id']]);
  116. if (!$model) {
  117. $model = new self();
  118. }
  119. $model->loadDefaultValues();
  120. if (!$model->load($param, '')) throw new \Exception($model->getErrorMessage());
  121. if (empty($params['form']['is_attr'])) $model->stock = $params['deposit_presale_goods']['goods_stock'];
  122. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  123. }
  124. }
  125. return true;
  126. } catch (\Exception $e) {
  127. self::$static_error = $e->getMessage();
  128. return false;
  129. }
  130. }
  131. /**
  132. * @desc:扣减规格库存
  133. * @Author: hua
  134. * @Date: 2021/11/9
  135. * @Time: 11:46
  136. * @Copyright: copyright (c) 2021 广东七件事集团
  137. * @param $goods_attr_id
  138. * @param $num +增加 -减少
  139. * @return bool
  140. * @throws Exception
  141. */
  142. public static function handleStock($mall_id, $deposit_presale_active_id, $goods_attr_id, $num)
  143. {
  144. try {
  145. $model = self::findOne(['mall_id' => $mall_id, 'deposit_presale_active_id' => $deposit_presale_active_id, 'attr_id' => $goods_attr_id, 'status' => StatusEnum::ENABLED]);
  146. if (empty($model)) throw new \Exception('规格不存在');
  147. if (!is_int($num)) throw new \Exception('库存数量必须为整数');
  148. if ($num < 0 && $model->stock < abs($num)) throw new \Exception('库存不足');
  149. $model->stock += $num;
  150. $res = $model->save();
  151. if ($res === false) throw new \Exception($model->getErrorMessage());
  152. return true;
  153. } catch (\Exception $e) {
  154. self::setStaticError($e->getMessage());
  155. return false;
  156. }
  157. }
  158. }