'ID', 'mall_id' => 'Mall ID', 'user_id' => 'User ID', 'status' => 'Status', 'created_at' => 'Created At', 'updated_at' => 'Updated At', 'goods_id' => 'Goods ID', ]; } public function getGoods() { return $this->hasOne(Goods::class, ['id' => 'goods_id']); } /** * @param int $mall_id * @param int $user_id * @param array $goods * @return void * @throws \yii\db\Exception */ public static function batchSave(int $mall_id, int $user_id, int $agent_id, array $goods) { AreaGoods::updateAll(['status' => StatusEnum::DELETE], ['and', ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]], ['mall_id' => $mall_id], ['agent_id' => $agent_id], ['user_id' => $user_id]]); $data = []; // 添加 foreach ($goods as $good) { $data[] = [ 'user_id' => $user_id, 'goods_id' => $good['goods_id'], 'mall_id' => $mall_id, 'created_at' => time(), 'updated_at' => time(), 'agent_id' => $agent_id ]; } // 新增数据 AreaGoods::find()->createCommand()->batchInsert(AreaGoods::tableName(), array_keys($data[0]), $data)->execute(); } /** * @param int $mall_id * @param int $user_id * @return array */ public static function findAllGoods(int $mall_id, int $user_id) { $ids = AreaGoods::find() ->where(['mall_id' => $mall_id]) ->andWhere(['user_id' => $user_id]) ->andWhere(['status' => StatusEnum::ENABLED]) ->select('goods_id') ->column(); if (!empty($ids)) { Goods::lists([ 'where' => [ ['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED], ['id' => $ids] ], 'select'=> 'id, goods_name, cover_pic, price', 'order' => 'id desc' ]); } return []; } /** * @param int $mall_id * @param int $agent_id * @param int $goods_id * @return mixed|null */ public static function existByAgentIdAndGoodsId(int $mall_id, int $agent_id, int $goods_id) { return AreaGoods::getOne([ ['mall_id' => $mall_id], ['agent_id' => $agent_id], ['goods_id' => $goods_id], ['status' => StatusEnum::ENABLED] ], false, [], null, 'id'); } }