GroupBuyActiveGoodsAttr.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace addons\GroupBuy\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_group_buy_goods_attr".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城id
  13. * @property int $group_buy_active_id
  14. * @property int $goods_id 商品id;对应goods主表
  15. * @property int $attr_id 商品规格id;对应goods_attr.id;
  16. * @property float $group_buy_price 价格
  17. * @property int $stock 拼团库存
  18. * @property int $status 状态[-1:删除;0:禁用;1启用]
  19. * @property int $created_at
  20. * @property int $updated_at
  21. */
  22. class GroupBuyActiveGoodsAttr extends BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_group_buy_active_goods_attr}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'group_buy_active_id','goods_id', 'attr_id'], 'required'],
  38. [['mall_id', 'goods_id', 'attr_id', 'stock', 'status', 'created_at', 'updated_at'], 'integer'],
  39. [['group_buy_price'], 'number'],
  40. ];
  41. }
  42. // 关联模型
  43. public function getGoods()
  44. {
  45. return $this->hasOne(Goods::class, ['id' => 'goods_id']);
  46. }
  47. public function getGroupBuyActive()
  48. {
  49. return $this->hasOne(GroupBuyActive::class, ['id' => 'group_buy_active_id']);
  50. }
  51. public function getGoodsAttr()
  52. {
  53. return $this->hasOne(GoodsAttr::class, ['id' => 'attr_id']);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'mall_id' => '商城id',
  63. 'group_buy_active_id' => 'qimall_addons_group_buy_active.id',
  64. 'goods_id' => '商品id;对应goods主表',
  65. 'attr_id' => '商品规格id;对应goods_attr.id;',
  66. 'group_buy_price' => '价格',
  67. 'stock' => '拼团库存',
  68. 'status' => '状态[-1:删除;0:禁用;1启用]',
  69. 'created_at' => 'Created At',
  70. 'updated_at' => 'Update At',
  71. ];
  72. }
  73. public static function setData(array $params)
  74. {
  75. try {
  76. foreach ($params['form']['attr'] as $item){
  77. $param = $item;
  78. $param['mall_id'] = $params['mall_id'];
  79. $param['attr_id'] = $item['id'];
  80. $param['group_buy_active_id'] = $params['group_buy_active_id'];
  81. $model = new self();
  82. $model->loadDefaultValues();
  83. if (!$model->load($param, '')) throw new \Exception($model->getErrorMessage());
  84. if(empty($params['form']['is_attr'])) $model->stock = $params['group_buy_goods']['goods_stock'];
  85. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  86. }
  87. return true;
  88. } catch (\Exception $e) {
  89. self::$static_error = $e->getMessage();
  90. return false;
  91. }
  92. }
  93. /**
  94. * @desc:扣减规格库存
  95. * @Author: hua
  96. * @Date: 2021/11/9
  97. * @Time: 11:46
  98. * @Copyright: copyright (c) 2021 广东七件事集团
  99. * @param $goods_attr_id
  100. * @param $num +增加 -减少
  101. * @return bool
  102. * @throws Exception
  103. */
  104. public static function handleStock($mall_id,$group_buy_active_id,$goods_attr_id,$num){
  105. try{
  106. $model = self::findOne(['mall_id'=>$mall_id,'group_buy_active_id'=>$group_buy_active_id,'attr_id'=>$goods_attr_id]);
  107. if(empty($model)) throw new \Exception('规格不存在');
  108. if(!is_int($num)) throw new \Exception('库存数量必须为整数');
  109. if($num < 0 && $model->stock < abs($num)) throw new \Exception('库存不足');
  110. $model->stock += $num;
  111. $res = $model->save();
  112. if($res === false) throw new \Exception($model->getErrorMessage());
  113. return true;
  114. }catch(\Exception $e){
  115. self::setStaticError($e->getMessage());
  116. return false;
  117. }
  118. }
  119. }