CloudInventoryGoods.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 $user_id
  16. * @property integer $store_id
  17. * @property integer $goods_id
  18. * @property string $name
  19. * @property string $pic
  20. * @property string $attr
  21. * @property integer $price
  22. * @property integer $platform_price
  23. * @property integer $virtual_sales
  24. * @property integer $num
  25. * @property integer $cloud_inventory_cat_id
  26. * @property integer $created_at
  27. * @property integer $updated_at
  28. */
  29. class CloudInventoryGoods extends \yii\db\ActiveRecord
  30. {
  31. //活动价(临时字段)
  32. public $activityPrice = 0;
  33. /**
  34. * @inheritdoc
  35. */
  36. public static function tableName()
  37. {
  38. return '{{%cloud_inventory_goods}}';
  39. }
  40. public function behaviors()
  41. {
  42. return [
  43. [
  44. 'class' => TimestampBehavior::class,
  45. 'attributes' => [
  46. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  47. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  48. ]
  49. ]
  50. ];
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function rules()
  56. {
  57. return [
  58. [['store_id','user_id', 'goods_id', 'virtual_sales', 'cloud_inventory_cat_id', 'num'], 'integer'],
  59. [['price','platform_price'], 'number'],
  60. [['attr','pic','name'], 'string'],
  61. [['created_at', 'updated_at'], 'safe']
  62. ];
  63. }
  64. /**
  65. * @inheritdoc
  66. */
  67. public function attributeLabels()
  68. {
  69. return [
  70. 'id' => 'ID',
  71. 'user_id' => '用户id',
  72. 'store_id' => '商城id',
  73. 'goods_id' => '商品id',
  74. 'name' => '商品名称',
  75. 'virtual_sales' => '销量',
  76. 'cloud_inventory_cat_id' => '云库存商品类别',
  77. 'attr' => '规格',
  78. 'price' => '售价',
  79. 'platform_price' => '平台价',
  80. 'num' => '库存',
  81. 'pic' => '商品缩略图',
  82. 'created_at' => '',
  83. 'updated_at' => ''
  84. ];
  85. }
  86. public function beforeSave($insert)
  87. {
  88. if (parent::beforeSave($insert)) {
  89. $goods_price = $this->price;
  90. $goods_num = $this->goods_num;
  91. $this->getPriceNum($goods_price, $goods_num);
  92. $this->price = $goods_price;
  93. $this->goods_num = $goods_num;
  94. return true;
  95. }
  96. }
  97. public function getPriceNum(&$goods_price, &$goods_num) {
  98. $attr = $this->attr;
  99. if (!empty($attr)) {
  100. $num = 0;
  101. $attr_rows = json_decode($attr, true);
  102. foreach ($attr_rows as $attr_row) {
  103. $num += intval($attr_row['num']);
  104. }
  105. $goods_num = $num;
  106. $price_arr = array_column($attr_rows, 'price');
  107. $goods_price = min($price_arr);
  108. }
  109. }
  110. /**
  111. * 获取商品可选的规格列表
  112. */
  113. public function getAttrGroupList($use_attr = 1)
  114. {
  115. $attr_rows = json_decode($this->attr, true);
  116. if (empty($attr_rows)) {
  117. return [];
  118. }
  119. $attr_group_list = [];
  120. foreach ($attr_rows as $attr_row) {
  121. foreach ($attr_row['attr_list'] as $i => $attr) {
  122. $attr_id = $attr['attr_id'];
  123. $attr = Attr::findOne(['id' => $attr_id, 'is_delete' => 0]);
  124. if (!$attr) {
  125. continue;
  126. }
  127. $in_list = false;
  128. foreach ($attr_group_list as $j => $attr_group) {
  129. if ($attr_group->attr_group_id == $attr->attr_group_id) {
  130. $attr_obj = (object)[
  131. 'attr_id' => $attr->id,
  132. 'attr_name' => $attr->attr_name,
  133. ];
  134. if (!in_array($attr_obj, $attr_group_list[$j]->attr_list)) {
  135. $attr_group_list[$j]->attr_list[] = $attr_obj;
  136. }
  137. $in_list = true;
  138. continue;
  139. }
  140. }
  141. if (!$in_list) {
  142. $attr_group = AttrGroup::findOne(['is_delete' => 0, 'id' => $attr->attr_group_id]);
  143. if ($attr_group) {
  144. $attr_group_list[] = (object)[
  145. 'attr_group_id' => $attr_group->id,
  146. 'attr_group_name' => $attr_group->attr_group_name,
  147. 'attr_list' => [
  148. (object)[
  149. 'attr_id' => $attr->id,
  150. 'attr_name' => $attr->attr_name,
  151. ],
  152. ],
  153. ];
  154. }
  155. }
  156. }
  157. }
  158. if ((int)$use_attr === 0) {
  159. $attr_group_list = [
  160. $attr_group_list[0]
  161. ];
  162. }
  163. return $attr_group_list;
  164. }
  165. }