//新增桌类
        public int SaveTableType(TableType type)
        {
            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {
                try
                {
                    Hashtable hash = new Hashtable();//返回结果

                    List<TableType> types;
                    //检查类型编号或者类型名称是否重复
                    types = entities.TableType.Where(info => info.Name == type.Name || info.Code == type.Code).ToList();
                    if (types != null && types.Count > 0)
                    {
                        hash.Add("code", 1);
                        if (types[0].Name == type.Name)
                        {
                            throw new ServiceException("类型名称已经存在,请重新命名!");
                        }
                        else if (types[0].Code == type.Code)
                        {
                            throw new ServiceException("类型编号已经存在!");
                        }
                    }
                    entities.TableType.Add(type);
                    entities.SaveChanges();
                    return type.TableTypeId;
                }
                catch (Exception e)
                {
                    throw new ServiceException(e.Message);
                }
            };
        }
 public TableType LoadTableTypeById(int typeId)
 {
     TableType type;
     using (ChooseDishesEntities entities = new ChooseDishesEntities())
     {
         type = entities.TableType.Where(
             info => info.Deleted == 0
                 && (info.TableTypeId == typeId)
         ).ToList()[0];
         if (type == null)
         {
             type = new TableType();
         }
         return type;
     };
 }
        public void UpdateTypeMethod()
        {
            try
            {
                TableType type = new TableType();
                type.Code = Code;
                type.Name = Name;
                type.PeopleMax = Convert.ToInt32(PeopleMax);
                type.PeopleMin = Convert.ToInt32(PeopleMin);
                //type.Prefix = Prefix;
                type.PriceType = (int)PriceType;
                type.ServerfreeModel = (int)ServerfreeModel;
                type.LowConsCalcType = (int)LowConsCalcType;
                type.ServerFeeCalcType = (int)ServerFeeCalcType;
                type.Status = 0;
                type.Deleted = 0;
                type.ColorType = ColorType;
                type.CreateDatetime = DateTime.Now;
                type.CreateBy = SubjectUtils.GetAuthenticationId();
                type.UpdateBy = SubjectUtils.GetAuthenticationId();
                type.UpdateDatetime = DateTime.Now;
                type.CanDiscount = CanDiscount;
                type.InLowConsume = InLowConsume;

                TableViewModel tableViewModel = ServiceLocator.Current.GetInstance<TableViewModel>();
                if (currentTableTypeId == null)
                {//修改窗口修改餐桌类型 
                    type.TableTypeId = tableViewModel.SelectedType.TableTypeId;
                }
                else//新增窗口修改餐桌类型
                {
                    type.TableTypeId = (int)currentTableTypeId;
                }

                ServerFeeDetail.UpdateBy = SubjectUtils.GetAuthenticationId();
                ServerFeeDetail.ServerfreeAccountType = (int)ServerfreeAccountType;

                LowConsumerDetail.ConsumerMode = (int)ConsumerMode;
                LowConsumerDetail.UpdateBy = SubjectUtils.GetAuthenticationId();

                TableService.UpdateTableTypeAll(type, ServerFeeDetail, LowConsumerDetail);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public int SaveTypeMethod()
        {
            try
            {
                TableType type = new TableType();
                type.Code = Code;
                type.Name = Name;
                type.PeopleMax = Convert.ToInt32(PeopleMax);
                type.PeopleMin = Convert.ToInt32(PeopleMin);
                //type.Prefix = Prefix;
                type.PriceType = (int)PriceType;
                type.ServerfreeModel = (int)ServerfreeModel;
                type.LowConsCalcType = (int)LowConsCalcType;
                type.ServerFeeCalcType = (int)ServerFeeCalcType;
                type.Status = 0;
                type.Deleted = 0;
                type.ColorType = ColorType;
                type.CreateDatetime = DateTime.Now;
                type.CreateBy = SubjectUtils.GetAuthenticationId();
                type.UpdateBy = SubjectUtils.GetAuthenticationId();
                type.UpdateDatetime = DateTime.Now;

                ServerFeeDetail.CreateBy = SubjectUtils.GetAuthenticationId();
                ServerFeeDetail.DataType = 1;
                ServerFeeDetail.ServerfreeAccountType = (int)ServerfreeAccountType;

                LowConsumerDetail.CreateBy = SubjectUtils.GetAuthenticationId();
                LowConsumerDetail.DataType = 2;
                LowConsumerDetail.ConsumerMode = (int)ConsumerMode;

                int tableTypeId = TableService.SaveTableTypeAll(type, ServerFeeDetail, LowConsumerDetail);

                MessageBox.Show("新增成功!");


                //刷新界面上的目录树
                TableViewModel tableModel = ServiceLocator.Current.GetInstance<TableViewModel>();
                _TableTypeTree = tableModel.TableTypeTree;
                TableTypeTreeNode tableTypeTreeNode = new TableTypeTreeNode(type.TableTypeId.ToString(), type.Name, tableModel.TableTypeTree[0], false, null);
                tableTypeTreeNode.Action = tableModel.LoadTableTypeAction;
                tableModel.TableTypeTree[0].Children.Add(tableTypeTreeNode);
                return tableTypeId;
            }
            catch (Exception e)
            {
                throw new ServiceException(e.Message);
            }

        }
 /// <summary>
 /// 新增桌类,包括服务费设置、低消设置
 /// </summary>
 /// <param name="type"></param>
 /// <param name="serverFeeDetail"></param>
 /// <param name="lowConsumerDetail"></param>
 /// <returns></returns>
 public int SaveTableTypeAll(TableType type, TableTypeDetail serverFeeDetail, TableTypeDetail lowConsumerDetail)
 {
     try
     {
         int typeId = SaveTableType(type);
         serverFeeDetail.TableTypeId = typeId;
         lowConsumerDetail.TableTypeId = typeId;
         SaveTableTypeDetail(serverFeeDetail);
         SaveTableTypeDetail(lowConsumerDetail);
         return typeId;
     }
     catch (Exception e)
     {
         throw new ServiceException(e.Message);
     }
 }
        /**删除桌类,逻辑删除**/
        public void DeleteTableType(int typeId)
        {
            try
            {
                using (ChooseDishesEntities entities = new ChooseDishesEntities())
                {
                    //检查是否被餐桌关联 

                    List<IShow.ChooseDishes.Data.Table> tableList = LoadTableByTypeId(typeId);
                    if (tableList != null && tableList.Count > 0)
                    {
                        throw new ServiceException("该餐桌类别关联了餐桌,不能删除!");
                    }
                    TableType type = new TableType();
                    type.TableTypeId = typeId;
                    type.Deleted = 1;
                    DbEntityEntry<TableType> entry = entities.Entry<TableType>(type);
                    entry.State = System.Data.Entity.EntityState.Unchanged;//Modified
                    entry.Property("Deleted").IsModified = true;

                    entities.Configuration.ValidateOnSaveEnabled = false;
                    int result = entities.SaveChanges();
                    entities.Configuration.ValidateOnSaveEnabled = true;
                    if (result == 1)
                    {
                        throw new ServiceException("修改失败!");
                    }
                }
            }
            catch (Exception e)
            {
                throw new ServiceException(e.Message);
            }
        }
        public void UpdateTableType(TableType type)
        {

            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {
                DbEntityEntry<TableType> entry = entities.Entry<TableType>(type);

                entry.State = System.Data.Entity.EntityState.Unchanged;//Modified
                entry.Property("Name").IsModified = true;
                entry.Property("PeopleMin").IsModified = true;
                entry.Property("PeopleMax").IsModified = true;
                entry.Property("PriceType").IsModified = true;
                entry.Property("ServerfreeModel").IsModified = true;
                entry.Property("ColorType").IsModified = true;
                entry.Property("UpdateBy").IsModified = true;
                entry.Property("UpdateDatetime").IsModified = true;
                entry.Property("LowConsCalcType").IsModified = true;
                entry.Property("ServerFeeCalcType").IsModified = true;
                entry.Property("CanDiscount").IsModified = true;
                entry.Property("InLowConsume").IsModified = true;
                try
                {
                    entities.Configuration.ValidateOnSaveEnabled = false;
                    int result = entities.SaveChanges();
                    entities.Configuration.ValidateOnSaveEnabled = true;
                }
                catch (Exception e)
                {
                    throw new ServiceException(e.Message);
                }
            }
        }
 /// <summary>
 /// 修改餐桌类型,包括服务费设置、低消设置
 /// </summary>
 /// <param name="type"></param>
 /// <param name="serverFeeDetail"></param>
 /// <param name="lowConsumerDetail"></param>
 public void UpdateTableTypeAll(TableType type, TableTypeDetail serverFeeDetail, TableTypeDetail lowConsumerDetail)
 {
     try
     {
         UpdateTableType(type);
         UpdateTableTypeDetail(serverFeeDetail);
         UpdateTableTypeDetail(lowConsumerDetail);
     }
     catch (Exception e)
     {
         throw new ServiceException("修改失败!");
     }
 }