A retired NYPD vice detective put his know-how to bad use — allegedly masterminding a prostitution and gambling ring that got him and seven active cops arrested, The Post has learned.

Three sergeants, two detectives and two police officers spent Wednesday night in custody ahead of arraignments Thursday on charges including enterprise corruption, promoting prostitution and official misconduct, sources said.

The retired vice detective, whose name was not immediately released, was arrested Tuesday night, sources said.

The busts are part of a three-year, Internal Affairs probe into cop-protected brothels run on Roosevelt Avenue in Queens and in Sunset Park, Brooklyn.

But they were just the beginning.

Investigators are targeting an additional 30 cops for questioning and possible arrest, sources said Wednesday night.

More than 40 civilians have also been arrested, sources said.

The investigation was launched in 2015, “after a member of the department suspected illegal activity by other members of the department and reported to Internal Affairs,” a source said.

Over the next three years, investigators spent thousands of hours on surveillance and undercover work, the source said.

Multiple wire-taps were also used.

“Today, those who swore an oath and then betrayed it have felt the consequences of that infidelity,” NYPD Police Commissioner James O’Neill said of the busts.

“The people of this Department are rightly held to the highest standard, and should they fail to meet it, the penalty will be swift and severe.”

Part of the investigation was centered on the Brooklyn South Narcotics building, which spent much of Wednesday on lock-down, with the electronic devices of all personnel seized.

Also as part of Wednesday’s busts, Internal Affairs officers stormed the 72nd Precinct in Sunset Park.

They ordered the desk supervisor to take them downstairs to the locker room, where they clipped a detective’s lock, confiscating his locker’s contents, sources said.

That cop — who had only been assigned to the 72nd Precinct about five months ago, working previously in IAB — was placed on modified duty, but not arrested, the sources said.

Mike Palladino, head of the Detectives Endowment Association, said, “The allegations are disturbing and if true reflect negatively on the entire NYPD.

“However, like everyone else, the detectives have a presumption of innocence until proven otherwise.”

Police sources identified those arrested, and their charges, as:

Sgt. Carlos Cruz, 69th Precinct Det. Squad, enterprise corruption; Sgt. Louis Failla, Queens Evidence Collection Team, official misconduct; Sgt. Cliff Nieves, Transit Bureau Investigation, promoting prostitution; PO Steven Nieves, 84th Precinct, promoting prostitution; PO Giancarlo Raspanti, 109th Precinct, official misconduct; Det. Gionanny Rojas-Acosta, Criminal Investigation Division Training, enterprise corruption; Det. Rene Samaniego, Brooklyn South Vice, enterprise corruption.

Additional reporting by Laura Italiano