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(); } }