$this->mall_id, 'status' => StatusEnum::ENABLED, ]; $where = [$baseWhere]; if (!empty($params['goods_id'])) { $where[] = ['=', 'goods_id', $params['goods_id']]; } if(!empty($params['cats'])){ $goods_cate_relate_list = GoodsCateRelate::lists(['where'=>[['cate_id'=>$params['cats']]]]); $goods_ids = array_column($goods_cate_relate_list,'goods_id'); $where[] = ['in', 'goods_id', $goods_ids]; } $list = ScoreExpansionRecommendRewardGoods::listPage([ 'where' => $where, 'with' => [ 'goods' => function ($query) { $query->select('id, goods_name, price, cover_pic')->with(['cats']); } ], ]); foreach ($list['list'] as &$item) { $item['created_at_text'] = date('Y-m-d H:i:s', $item['created_at']); $item['setting'] = json_decode($item['setting'], true) ?: []; } // 返回所有已参与的商品 $list['selected_goods_ids'] = $this->getSelectedGoodsIds(); return $list; } /** * @param array $post * * @return bool */ public function edit(array $post): bool { $post['mall_id'] = $this->mall_id; if (ScoreExpansionRecommendRewardGoods::setData($post) === false) { $this->setError(ScoreExpansionRecommendRewardGoods::getStaticError()); return false; } return true; } /** * @param int $id * * @return bool */ public function del(int $id): bool { return (bool)ScoreExpansionRecommendRewardGoods::updateAll( ['status' => StatusEnum::DELETE, 'updated_at' => time()], ['id' => $id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id] ); } /** * @param array $goodsIds * * @return bool * @throws Exception */ public function save(array $goodsIds): bool { $selectedGoodsIds = $this->getSelectedGoodsIds(); // 要插入的商品id $insertGoodsIds = array_diff($goodsIds, $selectedGoodsIds); if (empty($insertGoodsIds)) { $this->setError('不存在要添加的商品'); return false; } $batchInsert = []; $now = time(); foreach ($insertGoodsIds as $goodsId) { $batchInsert[] = [ 'mall_id' => $this->mall_id, 'goods_id' => $goodsId, 'is_alone' => 0, 'created_at' => $now, 'updated_at' => $now ]; } return (bool)Yii::$app ->db ->createCommand() ->batchInsert( ScoreExpansionRecommendRewardGoods::tableName(), array_keys($batchInsert[0]), $batchInsert ) ->execute(); } /** * @return array */ public function getSelectedGoodsIds() { return ScoreExpansionRecommendRewardGoods::find() ->where([ 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED ]) ->select('goods_id') ->column(); } }