There were more than 7,000 detentions for compulsory mental health care and treatment in Scotland last year, figures show.