HappinessStoreGoods.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace addons\Happiness\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 common\models\goods\GoodsCate;
  8. use common\models\goods\GoodsCateRelate;
  9. use common\models\user\User;
  10. use Yii;
  11. /**
  12. * This is the model class for table "qimall_addons_store_clerk".
  13. */
  14. class HappinessStoreGoods extends BaseActiveRecord
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public static function tableName()
  20. {
  21. return '{{%addons_happiness_store_goods}}';
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function rules()
  27. {
  28. return [
  29. [['mall_id', 'goods_id', 'store_id', 'sales_num', 'is_on_sale', 'created_at', 'updated_at', 'status', 'stock'], 'integer'],
  30. [['freight_type'], 'string']
  31. ];
  32. }
  33. public function getGood()
  34. {
  35. return $this->hasOne(Goods::class, ['id' => 'goods_id']);
  36. }
  37. public function getGoods()
  38. {
  39. return $this->hasMany(Goods::class, ['id' => 'goods_id']);
  40. }
  41. // 分类关系
  42. public function getGoodsCateRelate()
  43. {
  44. return $this->hasMany(GoodsCateRelate::class, ['goods_id' => 'goods_id']);
  45. }
  46. // 分类
  47. public function getCats()
  48. {
  49. return $this->hasMany(GoodsCate::class, ['id' => 'cate_id'])
  50. // ->where(['mch_id' => 0])
  51. ->via('goodsCateRelate');
  52. }
  53. public function getAttr()
  54. {
  55. return $this->hasMany(GoodsAttr::class, ['goods_id' => 'goods_id'])->select('id,goods_id,name,price')->where(['is_show'=> 1, 'status' => 1])->indexBy('id');
  56. }
  57. public function getAttrs()
  58. {
  59. return $this->hasMany(GoodsAttr::class, ['goods_id' => 'goods_id'])->where(['is_show'=> 1, 'status' => 1]);
  60. }
  61. public function getYunAttr()
  62. {
  63. return $this->hasMany(HappinessStoreGoodsAttr::class, ['yun_goods_id' => 'id'])->where(['status' => 1])
  64. ->indexBy('attr_id');
  65. }
  66. public function getYunGoods()
  67. {
  68. return $this->hasOne(HappinessGoods::class, ['goods_id' => 'goods_id'])->where(['is_on_sale' => 1, 'status' => 1]);
  69. }
  70. /**
  71. * @desc:保存数据
  72. * @Author: hua
  73. * @Date: 2021/10/23
  74. * @Time: 18:07
  75. * @Copyright: copyright (c) 2021 广东七件事集团
  76. * @param array $params
  77. * @return StoreClerk|false|null
  78. */
  79. public static function setData(array $params)
  80. {
  81. try {
  82. if (!empty($params['mall_id']) && (!empty($params['goods_id']) || !empty($params['id'])) && !empty($params['store_id'])) {
  83. if (!empty($params['id'])) {
  84. $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'],'id' => $params['id'], 'status' => StatusEnum::ENABLED]);
  85. } else {
  86. $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'],'goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED]);
  87. }
  88. if (empty($model)) {
  89. $model = new self();
  90. $model->loadDefaultValues();
  91. }
  92. else
  93. {
  94. $params['stock']=$params['stock']+$model->stock;
  95. }
  96. } else {
  97. $model = new self();
  98. $model->loadDefaultValues();
  99. }
  100. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  101. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  102. if (isset($params['is_on_sale']) && !empty($model->goods_id) && !empty($model->store_id)) {
  103. HappinessStoreGoodsAttr::updateAll(['is_on_sale' => $params['is_on_sale']], ['mall_id' => $params['mall_id'], 'goods_id' => $model->goods_id, 'store_id' => $params['store_id']]);
  104. }
  105. return $model;
  106. } catch (\Exception $e) {
  107. self::$static_error = $e->getMessage();
  108. return false;
  109. }
  110. }
  111. public static function computeMemberPrice($price, $sign_id, $user_level, $is_attr, $is_member_price, $member_price_setting, $discount)
  112. {
  113. $after_price = $price;
  114. switch ($is_member_price) {
  115. case 0:
  116. $after_price = bcdiv($price * $discount, 10, 2);
  117. break;
  118. case 1:
  119. //单独设置会员价
  120. if (!empty($is_attr)) {
  121. //多规格
  122. if (isset($member_price_setting['level' . $user_level][$sign_id])) $member_price_discount = $member_price_setting['level' . $user_level][$sign_id];
  123. } else {
  124. //默认规格
  125. if (isset($member_price_setting[$user_level])) $member_price_discount = $member_price_setting[$user_level];
  126. }
  127. if (!empty($member_price_discount)) {
  128. $after_price = 0;
  129. if ($price >= $member_price_discount) $after_price = bcmul($member_price_discount, 1, 2);
  130. }
  131. break;
  132. }
  133. return $after_price;
  134. }
  135. }