GoodsModel.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace addons\QiJuhe\common\models;
  3. /**
  4. * This is the model class for table "{{%addons_qijuhe_goods_new}}".
  5. *
  6. * @property int $id
  7. * @property int|null $mall_id
  8. * @property int|null $supply_id
  9. * @property int|null $mall_goods_id
  10. * @property int|null $goods_id
  11. * @property string|null $title 商品名称
  12. * @property string|null $image_url 商品主图
  13. * @property int|null $source 来源 0中台 1云仓(聚合供应链) 2京东 6阿里 7天猫
  14. * @property float|null $agreement_price 协议价
  15. * @property float|null $guide_price 指导价
  16. * @property float|null $activity_price 营销价
  17. * @property float|null $market_price 市场价
  18. * @property float|null $sale_price 销售价
  19. * @property float|null $cost_price 成本价
  20. * @property int $stock 库存
  21. * @property int $sales 销量
  22. * @property int $min_buy_qty 销量
  23. * @property string|null $sn 商品编号
  24. * @property int|null $is_display 上下架 0下1上
  25. * @property string|null $desc 商品描述
  26. * @property string|null $unit 商品单位
  27. * @property string|null $barcode 商品条码
  28. * @property string|null $gallery 商品组图json
  29. * @property string|null $detail_images 商品详情
  30. * @property string|null $attrs 商品属性json
  31. * @property string|null $skus 商品规格json
  32. * @property string|null $brand 商品品牌json
  33. * @property string|null $category_1
  34. * @property string|null $category_2
  35. * @property string|null $category_3
  36. * @property string|null $source_name
  37. */
  38. class GoodsModel extends \yii\db\ActiveRecord
  39. {
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function tableName()
  44. {
  45. return '{{%addons_qijuhe_goods}}';
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['mall_id', 'supply_id', 'goods_id', 'mall_goods_id', 'source', 'stock', 'sales', 'is_display', 'min_buy_qty'], 'integer'],
  54. [['agreement_price', 'guide_price', 'activity_price', 'market_price', 'sale_price', 'cost_price'], 'number'],
  55. [['desc', 'detail_images', 'attrs', 'skus', 'brand'], 'string'],
  56. [['title', 'image_url', 'sn', 'barcode', 'category_1', 'category_2', 'category_3', 'source_name'], 'string', 'max' => 255],
  57. [['unit'], 'string', 'max' => 32],
  58. ];
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function attributeLabels()
  64. {
  65. return [
  66. 'id' => 'ID',
  67. 'mall_id' => 'Mall ID',
  68. 'supply_id' => 'Supply ID',
  69. 'mall_goods_id' => '商城对应的商品id',
  70. 'goods_id' => '中台商品id',
  71. 'title' => '商品名称',
  72. 'image_url' => '商品主图',
  73. 'source' => '来源 0中台 1云仓(聚合供应链) 2京东 6阿里 7天猫',
  74. 'agreement_price' => '协议价',
  75. 'guide_price' => '指导价',
  76. 'activity_price' => '营销价',
  77. 'market_price' => '市场价',
  78. 'sale_price' => '销售价',
  79. 'cost_price' => '成本价',
  80. 'stock' => '库存',
  81. 'sales' => '销量',
  82. 'sn' => '商品编号',
  83. 'is_display' => '上下架 0下1上',
  84. 'desc' => '商品描述',
  85. 'unit' => '商品单位',
  86. 'barcode' => '商品条码',
  87. 'gallery' => '商品组图json',
  88. 'detail_images' => '商品详情',
  89. 'attrs' => '商品属性json',
  90. 'skus' => '商品规格json',
  91. 'brand' => '商品品牌json',
  92. 'category_1' => 'Category 1',
  93. 'category_2' => 'Category 2',
  94. 'category_3' => 'Category 3',
  95. ];
  96. }
  97. // 过滤字段
  98. public static function filterField(array $data): array
  99. {
  100. $safe_attr = self::getTableSchema()->getColumnNames();
  101. $not_safe_attr = array_diff(array_keys($data), $safe_attr);
  102. foreach ($not_safe_attr as $key) {
  103. unset($data[$key]);
  104. }
  105. return $data;
  106. }
  107. /**
  108. * 通过ids下架商品
  109. *
  110. * @param array $ids
  111. * @return int
  112. */
  113. public static function GoodsPullByMiddleIds(array $ids)
  114. {
  115. return static::updateAll([
  116. 'is_display' => 0
  117. ], [
  118. 'goods_id' => $ids
  119. ]);
  120. }
  121. /**
  122. * @param int $goods_id
  123. * @param int $sku_id
  124. * @return int
  125. */
  126. public static function getMinBuyQty(int $goods_id, int $sku_id)
  127. {
  128. $sku = GoodsSkuModel::getSku($goods_id, $sku_id);
  129. if (empty($sku)) return 0;
  130. return (int) self::find()->where(['goods_id' => $sku->supply_goods_id, 'mall_id' => $sku->mall_id])->select('min_buy_qty')->scalar();
  131. }
  132. }