Storage.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace addons\StarChain\common\models;
  3. use common\enums\StatusEnum;
  4. use Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_star_chain_storage}}".
  8. *
  9. * @property int $id
  10. * @property int|null $mall_id
  11. * @property string|null $spu_id SPUID
  12. * @property int|null $mall_goods_id 商城商品ID
  13. * @property int|null $import -1 导入失败 0导入中 1导入中 2已导入
  14. * @property string|null $error 导入错误信息
  15. * @property int|null $is_delete
  16. * @property int|null $created_at
  17. * @property int|null $updated_at
  18. */
  19. class Storage extends BaseModel
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_star_chain_storage}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'mall_goods_id', 'import', 'is_delete', 'created_at', 'updated_at'], 'integer'],
  35. [['error'], 'string'],
  36. [['spu_id'], 'string', 'max' => 64],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => 'Mall ID',
  47. 'spu_id' => 'Spu ID',
  48. 'mall_goods_id' => 'Mall Goods ID',
  49. 'import' => 'Import',
  50. 'error' => 'Error',
  51. 'is_delete' => 'Is Delete',
  52. 'created_at' => 'Created At',
  53. 'updated_at' => 'Updated At',
  54. ];
  55. }
  56. /**
  57. * goods
  58. *
  59. * @return ActiveQueryInterface
  60. */
  61. public function getGoods()
  62. {
  63. return $this->hasOne(Goods::class, ['spu_id' => 'spu_id']);
  64. }
  65. /**
  66. * 批量更新商品
  67. *addons_star_chain_storage
  68. * @param integer $mall_id
  69. * @param array $list
  70. * @return void
  71. */
  72. public static function saveList($mall_id, $list)
  73. {
  74. $list = self::getStorageData($mall_id, $list);
  75. $spu_ids = array_column($list, 'spu_id');
  76. // 查询已存储
  77. $goods_list = self::find()->where(['spu_id' => $spu_ids, 'mall_id' => $mall_id, 'is_delete' => 0])->select('spu_id')->all();
  78. $has_spu_ids = array_column($goods_list, 'spu_id');
  79. // 新增数据
  80. $insert_ids = array_diff($spu_ids, $has_spu_ids);
  81. $insert_list = [];
  82. foreach ($list as $v) {
  83. in_array($v['spu_id'], $insert_ids) && $insert_list[] = $v;
  84. }
  85. // 剩余使用批量插入
  86. $insert_list && self::batchInsert($list);
  87. }
  88. /**
  89. * 获取添加选品库表数据
  90. *
  91. * @param integer $mall_id
  92. * @param array $spu_list
  93. * @return array
  94. */
  95. protected static function getStorageData($mall_id, $spu_list)
  96. {
  97. $data = [];
  98. foreach ($spu_list as $v) {
  99. $data[] = [
  100. 'mall_id' => $mall_id,
  101. 'spu_id' => strval($v['spuId']),
  102. ];
  103. }
  104. return $data;
  105. }
  106. /**
  107. * 移除选品商品
  108. *
  109. * @return boolean
  110. */
  111. public function remove()
  112. {
  113. $time = time();
  114. $this->is_delete = 1;
  115. $this->updated_at = $time;
  116. // 已导入商品
  117. if ($this->mall_goods_id) {
  118. if (!MallGoods::updateAll(['status' => StatusEnum::DELETE, 'updated_at' => $time], [
  119. 'id' => $this->mall_goods_id,
  120. ])) {
  121. throw new Exception("删除商品{$this->mall_goods_id}失败");
  122. }
  123. // 删除attr对应记录
  124. GoodsAttr::removeGoodsAttr($this->mall_goods_id);
  125. }
  126. // 更新记录
  127. if ($this->save()) {
  128. return true;
  129. }
  130. throw new Exception("删除商品{$this->mall_goods_id}失败");
  131. }
  132. /**
  133. * 通过Spu_Id获取商城商品Id
  134. *
  135. * @param integer $mall_id
  136. * @param string $spu_id
  137. * @return int|null
  138. */
  139. public static function getMallGoodsIdBySpuId(int $mall_id, string $spu_id)
  140. {
  141. return static::find()
  142. ->where(['mall_id' => $mall_id, 'spu_id' => $spu_id, 'is_delete' => 0])
  143. ->select('mall_goods_id')
  144. ->scalar();
  145. }
  146. }