MdGoods.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use yii\db\ActiveRecord;
  9. use yii\behaviors\TimestampBehavior;
  10. use Yii;
  11. /**
  12. * This is the model class for table "{{%md_goods}}".
  13. *
  14. * @property integer $id
  15. * @property integer $md_id
  16. * @property integer $goods_id
  17. * @property integer $status
  18. * @property integer $virtual_sales
  19. * @property string $attr
  20. * @property integer $price
  21. * @property string $delivery_type
  22. * @property integer $goods_num
  23. * @property integer $created_at
  24. * @property integer $updated_at
  25. */
  26. class MdGoods extends \yii\db\ActiveRecord
  27. {
  28. //活动价(临时字段)
  29. public $activityPrice = 0;
  30. /**
  31. * @inheritdoc
  32. */
  33. public static function tableName()
  34. {
  35. return '{{%md_goods}}';
  36. }
  37. public function behaviors()
  38. {
  39. return [
  40. [
  41. 'class' => TimestampBehavior::class,
  42. 'attributes' => [
  43. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  44. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  45. ]
  46. ]
  47. ];
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function rules()
  53. {
  54. return [
  55. [['md_id', 'goods_id', 'status', 'virtual_sales', 'goods_num'], 'integer'],
  56. [['price'], 'number'],
  57. [['attr', 'delivery_type'], 'string'],
  58. [['created_at', 'updated_at'], 'safe']
  59. ];
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => 'ID',
  68. 'md_id' => '门店id',
  69. 'goods_id' => '商品id',
  70. 'status' => '状态',
  71. 'virtual_sales' => '销量',
  72. 'attr' => '规格',
  73. 'price' => '售价',
  74. 'goods_num' => '总库存',
  75. 'delivery_type' => '配送方式',
  76. 'created_at' => '',
  77. 'updated_at' => ''
  78. ];
  79. }
  80. public function beforeSave($insert)
  81. {
  82. if (parent::beforeSave($insert)) {
  83. $goods_price = $this->price;
  84. $goods_num = $this->goods_num;
  85. $this->getPriceNum($goods_price, $goods_num);
  86. $this->price = $goods_price;
  87. $this->goods_num = $goods_num;
  88. return true;
  89. }
  90. }
  91. public function getPriceNum(&$goods_price, &$goods_num) {
  92. $attr = $this->attr;
  93. if (!empty($attr)) {
  94. $num = 0;
  95. $attr_rows = json_decode($attr, true);
  96. foreach ($attr_rows as $attr_row) {
  97. $num += intval($attr_row['num']);
  98. }
  99. $goods_num = $num;
  100. $price_arr = array_column($attr_rows, 'price');
  101. $goods_price = min($price_arr);
  102. }
  103. }
  104. /**
  105. * 获取商品可选的规格列表
  106. */
  107. public function getAttrGroupList($use_attr = 1)
  108. {
  109. $attr_rows = json_decode($this->attr, true);
  110. if (empty($attr_rows)) {
  111. return [];
  112. }
  113. $attr_group_list = [];
  114. foreach ($attr_rows as $attr_row) {
  115. foreach ($attr_row['attr_list'] as $i => $attr) {
  116. $attr_id = $attr['attr_id'];
  117. $attr = Attr::findOne(['id' => $attr_id, 'is_delete' => 0]);
  118. if (!$attr) {
  119. continue;
  120. }
  121. $in_list = false;
  122. foreach ($attr_group_list as $j => $attr_group) {
  123. if ($attr_group->attr_group_id == $attr->attr_group_id) {
  124. $attr_obj = (object)[
  125. 'attr_id' => $attr->id,
  126. 'attr_name' => $attr->attr_name,
  127. ];
  128. if (!in_array($attr_obj, $attr_group_list[$j]->attr_list)) {
  129. $attr_group_list[$j]->attr_list[] = $attr_obj;
  130. }
  131. $in_list = true;
  132. continue;
  133. }
  134. }
  135. if (!$in_list) {
  136. $attr_group = AttrGroup::findOne(['is_delete' => 0, 'id' => $attr->attr_group_id]);
  137. if ($attr_group) {
  138. $attr_group_list[] = (object)[
  139. 'attr_group_id' => $attr_group->id,
  140. 'attr_group_name' => $attr_group->attr_group_name,
  141. 'attr_list' => [
  142. (object)[
  143. 'attr_id' => $attr->id,
  144. 'attr_name' => $attr->attr_name,
  145. ],
  146. ],
  147. ];
  148. }
  149. }
  150. }
  151. }
  152. if ((int)$use_attr === 0) {
  153. $attr_group_list = [
  154. $attr_group_list[0]
  155. ];
  156. }
  157. return $attr_group_list;
  158. }
  159. }