| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace addons\StarChain\common\models;
- use common\enums\StatusEnum;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "{{%addons_star_chain_storage}}".
- *
- * @property int $id
- * @property int|null $mall_id
- * @property string|null $spu_id SPUID
- * @property int|null $mall_goods_id 商城商品ID
- * @property int|null $import -1 导入失败 0导入中 1导入中 2已导入
- * @property string|null $error 导入错误信息
- * @property int|null $is_delete
- * @property int|null $created_at
- * @property int|null $updated_at
- */
- class Storage extends BaseModel
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_star_chain_storage}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'mall_goods_id', 'import', 'is_delete', 'created_at', 'updated_at'], 'integer'],
- [['error'], 'string'],
- [['spu_id'], 'string', 'max' => 64],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'spu_id' => 'Spu ID',
- 'mall_goods_id' => 'Mall Goods ID',
- 'import' => 'Import',
- 'error' => 'Error',
- 'is_delete' => 'Is Delete',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * goods
- *
- * @return ActiveQueryInterface
- */
- public function getGoods()
- {
- return $this->hasOne(Goods::class, ['spu_id' => 'spu_id']);
- }
-
- /**
- * 批量更新商品
- *addons_star_chain_storage
- * @param integer $mall_id
- * @param array $list
- * @return void
- */
- public static function saveList($mall_id, $list)
- {
- $list = self::getStorageData($mall_id, $list);
- $spu_ids = array_column($list, 'spu_id');
- // 查询已存储
- $goods_list = self::find()->where(['spu_id' => $spu_ids, 'mall_id' => $mall_id, 'is_delete' => 0])->select('spu_id')->all();
- $has_spu_ids = array_column($goods_list, 'spu_id');
- // 新增数据
- $insert_ids = array_diff($spu_ids, $has_spu_ids);
- $insert_list = [];
- foreach ($list as $v) {
- in_array($v['spu_id'], $insert_ids) && $insert_list[] = $v;
- }
- // 剩余使用批量插入
- $insert_list && self::batchInsert($list);
- }
- /**
- * 获取添加选品库表数据
- *
- * @param integer $mall_id
- * @param array $spu_list
- * @return array
- */
- protected static function getStorageData($mall_id, $spu_list)
- {
- $data = [];
- foreach ($spu_list as $v) {
- $data[] = [
- 'mall_id' => $mall_id,
- 'spu_id' => strval($v['spuId']),
- ];
- }
- return $data;
- }
- /**
- * 移除选品商品
- *
- * @return boolean
- */
- public function remove()
- {
- $time = time();
- $this->is_delete = 1;
- $this->updated_at = $time;
- // 已导入商品
- if ($this->mall_goods_id) {
- if (!MallGoods::updateAll(['status' => StatusEnum::DELETE, 'updated_at' => $time], [
- 'id' => $this->mall_goods_id,
- ])) {
- throw new Exception("删除商品{$this->mall_goods_id}失败");
- }
- // 删除attr对应记录
- GoodsAttr::removeGoodsAttr($this->mall_goods_id);
- }
- // 更新记录
- if ($this->save()) {
- return true;
- }
- throw new Exception("删除商品{$this->mall_goods_id}失败");
- }
- /**
- * 通过Spu_Id获取商城商品Id
- *
- * @param integer $mall_id
- * @param string $spu_id
- * @return int|null
- */
- public static function getMallGoodsIdBySpuId(int $mall_id, string $spu_id)
- {
- return static::find()
- ->where(['mall_id' => $mall_id, 'spu_id' => $spu_id, 'is_delete' => 0])
- ->select('mall_goods_id')
- ->scalar();
- }
- }
|