using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace CitySunlight.Product
{
    public static class ProductManager
    {
        private static SqlHelper sql = new SqlHelper("Data Source=.;Initial Catalog=myApplication;Persist Security Info=True;User ID=sa;Password=325325");
        const String tbname = "tb_product";
        public enum Info
        {
            ID,
            UserName,
            ItemName,
            Class,
            PicUrl,
            Price,
            Amount
        }
        /// <summary>
        /// 根据信息查询产品
        /// </summary>
        /// <param name="it">查询类型</param>
        /// <param name="value">查询值</param>
        /// <returns>查询到的数据表</returns>
        public static DataTable SearchProduct(Info it, String value)
        {
            String cmd = String.Format("SELECT * FROM {0} WHERE {1} LIKE '%{2}%'", tbname, it.ToString(), value);
            return sql.ExecuteDataTable(cmd, CommandType.Text);
        }
        /// <summary>
        /// 估计产品信息查询产品
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="itemname">产品名</param>
        /// <param name="ClassId">产品类</param>
        /// <param name="price">价格</param>
        /// <param name="amount">库存</param>
        /// <returns></returns>
        public static DataTable SearchProduct(String username, String itemname, String ClassId, String price, String amount)
        {
            String cmd = "select * from {0} where ";
            cmd += String.Format("{0} like '%{1}%'", Info.UserName.ToString(), username);
            cmd += String.Format("and {0} like '%{1}%'", Info.ItemName.ToString(), itemname);
            cmd += String.Format("and {0} like '%{1}%'", Info.Class.ToString(), ClassId);
            cmd += String.Format("and {0} like '%{1}%'", Info.Price.ToString(), price);
            cmd += String.Format("and {0} like '%{1}%'", Info.Amount.ToString(), amount);
            String sqlcmd = String.Format(cmd, tbname, itemname, ClassId);
            return sql.ExecuteDataTable(sqlcmd, CommandType.Text);
        }
        /// <summary>
        /// 根据产品信息查询产品并分页
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="itemname">产品名</param>
        /// <param name="ClassId">产品类</param>
        /// <param name="price">价格</param>
        /// <param name="amount">库存</param>
        /// <param name="Index">第几页</param>
        /// <param name="Size">每页大小</param>
        /// <returns>数据表</returns>
        public static DataTable SearchProduct(String username, String itemname, String ClassId, String price, String amount, int Index, int Size)
        {//select * from (select *,row_number() over (order by Id asc) as number from {0} where {1} like '%{2}%') temp where temp.number between {3} and {4}
            int start = ((Index == 0 ? 1 : Index) - 1) * Size + 1;
            int end = start + Size - 1;
            String cmd = "select * from (select *,row_number() over (order by Id asc) as number from {0} where {1}) temp where temp.number between {2} and {3}";
            String condition = String.Format("{0} like '%{1}%'", Info.UserName.ToString(), username);
            condition += String.Format("and {0} like '%{1}%'", Info.ItemName.ToString(), itemname);
            condition += String.Format("and {0} like '%{1}%'", Info.Class.ToString(), ClassId);
            condition += String.Format("and {0} like '%{1}%'", Info.Price.ToString(), price);
            condition += String.Format("and {0} like '%{1}%'", Info.Amount.ToString(), amount);
            String sqlcmd = String.Format(cmd, tbname, condition, start, end);
            return sql.ExecuteDataTable(sqlcmd, CommandType.Text);
        }
        /// <summary>
        /// 获得所有产品的数据表
        /// </summary>
        /// <returns>DataTable 数据表</returns>
        public static DataTable GetAllProduct()
        {
            return sql.ExecuteDataTable("select * from " + tbname, CommandType.Text);
        }
        /// <summary>
        /// 获取指定ID产品的数据行
        /// </summary>
        /// <param name="Id"></param>
        /// <returns>DataRow 数据行</returns>
        public static DataRow GetProductById(int Id)
        {
            return sql.ExecuteDataTable("select * from " + tbname + " where Id='" + Id + "'", CommandType.Text).Rows[0];
        }
        /// <summary>
        /// 获取第几页,几个产品
        /// </summary>
        /// <param name="Index">第几页</param>
        /// <param name="Size">每页大小</param>
        /// <returns></returns>
        public static DataTable GetSomeProduct(int Index, int Size)
        {
            int start = (Index - 1) * Size + 1;
            int end = start + Size - 1;
            String cmd = "select * from (select *,row_number() over (order by Id asc) as number from {0}) temp where temp.number between {1} and {2}";
            String sqlcmd = String.Format(cmd, tbname, start, end);
            return sql.ExecuteDataTable(sqlcmd, CommandType.Text);
        }
        /// <summary>
        /// 通过用户名获取用户所有产品信息(数据库部分)
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static DataTable GetUserProduct(String username)
        {
            return sql.ExecuteDataTable("select * from " + tbname + " where UserName='" + username + "'", CommandType.Text);
        }
        /// <summary>
        /// 通过ID获取产品指定信息(数据库部分)
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="type"></param>
        /// <returns>String 指定数据的字符串格式</returns>
        public static String GetProductInfoById(int Id, String type)
        {
            return sql.ExecuteDataTable("select " + type + " from " + tbname + " where Id='" + Id + "'", CommandType.Text).Rows[0][0].ToString();
        }
        /// <summary>
        /// 通过ID编辑产品信息(数据库部分)
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns>bool 是否成功</returns>
        public static bool EditProductInfoById(int Id, String type, String value)
        {
            if (sql.ExecuteNonQuery("update " + tbname + " set " + type + "='" + value + "' where Id='" + Id + "'", CommandType.Text) > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 添加产品信息(数据库部分)
        /// </summary>
        /// <param name="username"></param>
        /// <param name="itemname"></param>
        /// <param name="ClassId"></param>
        /// <param name="picurl"></param>
        /// <param name="price"></param>
        /// <param name="amount"></param>
        /// <returns>bool 是否成功</returns>
        public static bool AddProduct(String username, String itemname, int ClassId, String picurl, decimal price, int amount)
        {
            String sqlcmd = "insert into  tb_product(UserName,ItemName,Class,PicUrl,Price,Amount) values('{0}','{1}','{2}','{3}','{4}','{5}')";
            sqlcmd = String.Format(sqlcmd, username, itemname, ClassId, picurl, price, amount);
            if (sql.ExecuteNonQuery(sqlcmd, CommandType.Text) > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 更新产品信息(数据库部分)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="username"></param>
        /// <param name="itemname"></param>
        /// <param name="ClassId"></param>
        /// <param name="picurl"></param>
        /// <param name="price"></param>
        /// <param name="amount"></param>
        /// <returns>bool 是否成功</returns>
        public static bool EditProduct(int id, String username, String itemname, int ClassId, String picurl, decimal price, int amount)
        {
            String sqlcmd = "update tb_product set UserName='{0}',ItemName='{1}',Class='{2}',PicUrl='{3}',Price='{4}',Amount='{5}' where id='" + id + "'";
            sqlcmd = String.Format(sqlcmd, username, itemname, ClassId, picurl, price, amount);
            if (sql.ExecuteNonQuery(sqlcmd, CommandType.Text) > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 删除产品信息(数据库部分)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>bool 是否成功</returns>
        public static bool DeleteProduct(int id)
        {
            String sqlcmd = "delete from " + tbname + " where id='" + id + "'";
            if (sql.ExecuteNonQuery(sqlcmd, CommandType.Text) > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 判断商品ID是否存在
        /// </summary>
        /// <param name="Id"></param>
        /// <returns>bool 是否存在</returns>
        public static bool isExist(int Id)
        {
            return sql.ExecuteDataTable("select * from " + tbname + " where Id='" + Id + "'", CommandType.Text).Rows.Count > 0;
        }
        /// <summary>
        /// DataRow转换成商品信息
        /// </summary>
        /// <param name="Product"></param>
        /// <param name="it"></param>
        /// <returns></returns>
        public static String GetProductInfo(DataRow Product, Info it)
        {
            return Product[it.ToString()].ToString();
        }
        ////通过商品ID获取产品信息
        public static String GetProductInfo(int id, Info it)
        {
            return GetProductInfoById(id, it.ToString());
        }
        ////通过商品ID编辑产品信息
        public static bool EditProductInfo(int id, Info it, String value)
        {
            return EditProductInfoById(id, it.ToString(), value);
        }
        /// <summary>
        /// 通过商品ID创建产品展示连接
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns>String 商品链接</returns>
        public static String NameToUrl(int id, String name)
        {
            return String.Format("<a href=\"/Product/ProductShow.aspx?id={0}\">{1}</a>", id, name);
        }

    }
}