using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
namespace BLL.Models
{
    public class MCartModels
    {
        /// <summary>
        /// 添加一个收藏
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="fav"></param>
        /// <returns></returns>
        public bool AddFav(string userid,string fav) {

            bool ret = false;
            string str_sqlo = "select * from [tb_mc_fav] where userid=" + userid;
            DataTable dt = new DataTable();
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sqlo).Tables[0];
            }
            catch (Exception ex) { }
            string str_sql = string.Empty;
            if(dt.Rows.Count==0){

                fav = fav + ",";
                str_sql = "inster into [tb_mc_fav] ( userid,fav)values( " + userid + ",'" + fav + "' )";


            }else{

                string str_fav = dt.Rows[0]["fav"].ToString();
                string[] strs_fav = str_fav.Split(new char[]  {','});
                StringBuilder str_favb = new StringBuilder();
                foreach(string s in strs_fav){

                    if (s != fav) {

                        str_favb.Append(s);
                        str_favb.Append(",");
                    }

                }
                str_favb.Append(fav);
                str_favb.Append(",");
               str_sql = "update  [tb_mc_article] set fav='"+str_favb.ToString()+"' where userid= "+userid;
                
            }
            
            
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
            
        }
        /// <summary>
        /// 删除一个收藏
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="fav"></param>
        /// <returns></returns>
        public bool DelFav(string userid,string fav) {

            bool ret = false;
            string str_sqlo = "select * from [tb_mc_fav] where userid=" + userid;
            DataTable dt = new DataTable();
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sqlo).Tables[0];
            }
            catch (Exception ex) { }
            string str_sql = string.Empty;


                string str_fav = dt.Rows[0]["fav"].ToString();
                string[] strs_fav = str_fav.Split(new char[] { ',' });
                StringBuilder str_favb = new StringBuilder();
                foreach (string s in strs_fav)
                {

                    if (s != fav)
                    {

                        str_favb.Append(s);
                        str_favb.Append(",");
                    }

                }

                str_sql = "update  [tb_mc_article] set fav='" + str_favb.ToString() + "' where userid= " + userid;




            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 取得收藏
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public string GetMyFav(string userid) {

            string ret = string.Empty;
            string str_sqlo = "select * from [tb_mc_fav] where userid=" + userid;
            DataTable dt = new DataTable();
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sqlo).Tables[0];
            }
            catch (Exception ex) { }
            string str_sql = string.Empty;
            if (dt.Rows.Count > 0)
            {

                ret = dt.Rows[0]["fav"].ToString();

            }
            return ret;
        }
        /// <summary>
        /// 跟新收藏
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="fav"></param>
        /// <returns></returns>
        public bool UpdateFav(string userid,string fav) {

            bool ret = false;
            string str_sqlo = "select * from [tb_mc_fav] where userid=" + userid;
            DataTable dt = new DataTable();
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sqlo).Tables[0];
            }
            catch (Exception ex) { }
            string str_sql = string.Empty;
            if (string.IsNullOrEmpty(fav)||fav.Length==0)
            {

                
                str_sql = "delete from [tb_mc_fav] where userid="+userid;


            }
            else
            {

                str_sql = "update  [tb_mc_article] set fav='" + fav + "' where userid= " + userid;

            }


            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        
        }
    }
}
