using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.Data.Common;
namespace ImpDDal
{
    public class ImpItemType:DbCommandStore,IDDal.ItemTypeDal<ItemType>
    {
        #region ItemTypeDal<ItemType> 成员
        DbCommand cmd = GetCommand();

        #region 增加一个菜肴种类
        public int AddItemType(ItemType itemtype)
        {
            cmd.CommandText = "INSERT INTO UT_ItemType(TypeName) VALUES(@TN)";
            cmd.Parameters.Add(itemtype.TypeName);
            try
            {
                cmd.Connection.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("增加菜型种类失败" + ex.Message);
            }
            finally
            {
                cmd.Connection.Close();
            }
        }
        #endregion

        #region 删除一个菜肴种类
        public int DelteItemType(ItemType itemtype)
        {
            cmd.CommandText = "DELETE FROM UT_ItemType WHERE ItemTypeId=@ID";
            cmd.Parameters.Add(itemtype.ItemTypeId);
            try
            {
                cmd.Connection.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("删除菜型种类失败" + ex.Message);
            }
            finally
            {
                cmd.Connection.Close();
            }
        }
        #endregion

        #region 编辑一个菜肴种类
        public int EditItemType(ItemType itemtype)
        {
            cmd.CommandText = "UPDATE UT_ItemType SET TypeName=@TN WHERE ItemTypeId=@ID";
            cmd.Parameters.Add(itemtype.TypeName);
            cmd.Parameters.Add(itemtype.ItemTypeId);
            try
            {
                cmd.Connection.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("更新菜型种类失败" + ex.Message);
            }
            finally
            {
                cmd.Connection.Close();
            }
        }
        #endregion

        #region 返回菜肴类型
        public IList<ItemType> ItemTypes()
        {
            cmd.CommandText = "SELECT * FROM UT_ItemType";
            try
            {
                if(cmd.Connection.State==System.Data.ConnectionState.Closed)
                cmd.Connection.Open();
                DbDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                List<ItemType> list = new List<ItemType>();
                ItemType itemtype=null;
                while (reader.Read())
                {
                    itemtype=new ItemType(int.Parse(reader.GetValue(0).ToString()),reader.GetString(1));
                    list.Add(itemtype);
                }
                return list;
            }
            catch (Exception ex)
            {
                throw new Exception("查询菜型种类失败" + ex.Message);
            }
            finally
            {
                cmd.Connection.Close();
            }
        }
        #endregion

        #endregion
    }
}
