using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;
using Helper;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.SqlClient;
using Helper.DBUtil;
namespace DAL
{
    /// <summary>
    /// 共享曲库(敬请期待)
    /// </summary>
    public class Shared : IMusic
    {
        public List<Music> Search(string word, int page, int num)
        {
            List<Music> list = new List<Music>();
            string sql = @"select Id,Name,Artist,Album,Source='Shared' 
                        from (select top (@num) Id,Name,Artist,Album,Source='Shared' 
                        from (select top ( @page * @num ) Id,Name,Artist,Album,Source='Shared'  
                        from Shared 
                        where Name like @word or Artist like @word 
                        order by Id asc )
                        as temp_sum_student 
                        order by Id desc ) temp_order
                        order by Id asc";
            DataTable dt = SqlHelper.Quary(sql,
            new SqlParameter("@num", num),
            new SqlParameter("@page", page),
            new SqlParameter("@word","%"+ word+"%")
            );
            return Music.TableToModel(dt);

        }

        public List<Music> SearchCollection(string id)
        {
            List<Music> list = new List<Music>();
            string sql = @"select Id,Name,Artist,Album,Source='Shared' from Shared where [Group]=@id";
            DataTable dt = SqlHelper.Quary(sql, new SqlParameter("@id", id));
            return Music.TableToModel(dt);
        }

        public bool GetData(Music m)
        {
            string sql = "select PicUrl,Url,Lrc from Shared Where Id=@id";
            DataRow dr = SqlHelper.Quary(sql,new SqlParameter("@id",m.Id)).Rows[0];
            m.PicUrl = dr["PicUrl"].ToString();
            m.Url = dr["Url"].ToString();
            m.Lrc = dr["Lrc"].ToString();
            return HttpHelper.CheckUri(m.Url);
        }

        public void GetLrc(Music m)
        {
            string sql = "select Lrc from Shared Where Id=@id";
            DataRow dr = SqlHelper.Quary(sql, new SqlParameter("@id", m.Id)).Rows[0];
            m.Lrc = dr["Lrc"].ToString();
        }
    }
}
