0], ['type', 'in', 'range' => [PurchaseFavorite::FAVORITE_GOODS, PurchaseFavorite::FAVORITE_SHOP]], [['goods_ids', 'page', 'limit', 'saas_id', 'store_id'], 'safe'], ]; } public function search() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } if ($this->type == PurchaseFavorite::FAVORITE_GOODS) { $query = PurchaseFavorite::find()->where(['type' => PurchaseFavorite::FAVORITE_GOODS, 'is_delete' => 0, 'saas_id' => $this->saas_id]); $count = $query->count(); $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]); $list = $query->select('*')->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()->all(); foreach ($list as $i => $favorite) { $form = new PlatformForm(); $form->id = $favorite['goods_id']; $glist = $form->goodsInfo(); $goods = ''; if($glist['code'] == 0 && $glist['data']['count'] == 1){ $goods = $glist['data']['list'][0]; } if (!$goods) { continue; } if ($goods->is_negotiable) { $goods->price = Goods::GOODS_NEGOTIABLE; } $list[$i]['goods'] = $goods; } $newList = []; foreach ($list as $i => $v){ $supid = $v['goods']['supplier_id']; $newList[$supid]['supplier'] = $v['goods']['supplier']; $newList[$supid]['goods_list'][] = $v; } return [ 'code' => 0, 'data' => (object)[ 'row_count' => $count, 'page_count' => $pagination->pageCount, 'list' => array_values($newList), ], ]; } } public function add() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } $errors = 0; foreach ($this->goods_ids as $gid) { $existFavorite = PurchaseFavorite::findOne([ 'saas_id' => $this->saas_id, 'goods_id' => $gid, 'type' => $this->type, ]); if ($existFavorite) { $existFavorite->is_delete = 0; if (!$existFavorite->save()) { $errors++; } }else{ $favorite = new PurchaseFavorite(); $favorite->store_id = $this->store_id; $favorite->user_id = $this->user_id; $favorite->saas_id = $this->saas_id; $favorite->goods_id = $gid; $favorite->type = $this->type; $favorite->created_at = time(); if (!$favorite->save()) { $errors++; } } } if($errors == count($this->goods_ids)){ return [ 'code' => 1, 'msg' => 'fail', ]; } if($errors){ return [ 'code' => 0, 'msg' => '收藏失败数量:' . $errors, ]; }else{ return [ 'code' => 0, 'msg' => 'success', ]; } } public function remove() { $res = PurchaseFavorite::updateAll(['is_delete' => 1], ['saas_id' => $this->saas_id, 'goods_id' => $this->goods_ids, 'is_delete' => 0]); if ($res) { return [ 'code' => 0, 'msg' => '删除成功', ]; } else { return [ 'code' => 1, 'msg' => 'fail', 'err' => $res ]; } } }