Seven NYPD officers were arrested Wednesday after a three-year undercover investigation into their role in a low-level prostitution and gambling ring with tentacles in Brooklyn and Queens, according to multiple law enforcement officials.

The officers allegedly provided protection and other services, according to multiple law-enforcement sources. The Daily News reported that in some cases officers “received sex and massages” for their assistance.

A senior law-enforcement official told the Beast that those arrested include two detectives, two police officers, and three sergeants, along with roughly 40 civilians. The civilians include a retired detective who had worked on the NYPD’s vice squad and was described by one source as being a leader of the group. It was not clear what charges the officers will face.

All seven officers have been placed on desk duty and stripped of their guns and badges until the investigation is complete, sources said. Two additional detectives were not arrested but have been placed on desk duty as part of the probe.

The NYPD’s Internal Affairs Bureau began investigating the ring in April 2015 after an officer came forward with a tip about the sordid activity, the sources added.

The investigation involved a combination of undercover police officers, physical surveillance and court ordered electronic surveillance, one official said.

It came to an end Wednesday. A source told the Daily News that internal affairs officers swarmed the NYPD’s Brooklyn South Narcotics Squad and “immediately put the entire building on lockdown as they collected evidence. Everyone inside had to surrender any of their cellphones and electronic devices.”

The arrested cops — sergeants Carlos Crus, Louis Failla and Cliff Nieves; police officers Steven Nieves and Giancarlo Raspanti and detectives Gionanny Rojas-Acosta and Rene Samaniego — worked in various divisions across the department from the transit bureau to vice squad. The two detectives placed on modified duty, Manuel Rodriguez and Rafael Vega, are expected to face departmental charges.

The seven officers are scheduled to be arraigned in a Queens criminal courtroom on Thursday. The police commissioner along with Queens District Attorney Richard Brown will hold a press conference in the afternoon to announce the arrests.

The operation is the latest in the department’s stated effort to root out bad behavior in the ranks, officials said.

Commissioner James O’Neill Thursday afternoon said the takedown “has sent a clear message: there is no place in the NYPD for criminal or unethical behavior.”

“Today, those who swore an oath and then betrayed it have felt the consequences of that infidelity,” O’Neill went on. “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.”

Michael Daly contributed reporting.