//根据ID查询大类
 public DishType LoadParentId(int? id)
 {
     try
     {
         
         using (ChooseDishesEntities entities = new ChooseDishesEntities())
         {
             DishType type = new DishType(); ;
             if (id != null)
             {
                 type = (DishType)entities.DishType.Where(info => info.DishTypeId == id && info.Deleted == 0 && info.Status == 0).Single();
                 if (type == null)
                 {
                     type = new DishType();
                 }
             }
              
             return type;
         };
     }
     catch (Exception e)
     {
         throw e;
     }
 }
 /// <summary>
 /// 根据菜品单位和菜品类别对象加载菜品基本数据
 /// </summary>
 /// <param name="dus"></param>
 /// <param name="sub"></param>
 private void LoadDishBaseData(List<DishUnit> dus, DishType sub)
 {
     if (dus == null||sub==null)
     {
         return;
     }
     ICollection<Dish> dishes = sub.Dish;
     LoadDishData(dus, dishes);
 }
 /// <summary>
 /// 批量保存
 /// </summary>
 /// <param name="child"></param>
 private void SaveBatch(DishType child)
 {
     ICollection<Dish> dishes = child.Dish;
     if (dishes != null && dishes.Count > 0)
     {
         foreach (var dish in dishes)
         {
             //获取菜品下说有的做法
             List<DischesWayRef> dwrs = _DataService.QueryAllByDishesId(dish.DishId);
             foreach (var selectedItem in DishesWaySelectedItems)
             {
                 //根据小类id查找所有的菜品
                 //插入到做法关联表中
                 bool flag = true;
                 foreach (var dwr in dwrs)
                 {
                     //判断是否已然存在
                     if (selectedItem.DischesWayId == dwr.DischesWayId)
                     {
                         flag = false;
                         break;
                     }
                 }
                 if(flag)
                 _DataService.Add(dish.DishId, selectedItem.DischesWayId);
             }
         }
     }
 }
 public bool DeleteType(int typeId)
 {
     using (ChooseDishesEntities entities = new ChooseDishesEntities())
     {
         DishType type = new DishType();
         type.DishTypeId = typeId;
         type.Deleted = 1;
         DbEntityEntry<DishType> entry = entities.Entry<DishType>(type);
         entry.State = System.Data.Entity.EntityState.Unchanged;//Modified
         entry.Property("Deleted").IsModified = true;
         entry.Property("DishTypeId").IsModified = false;
         entities.Configuration.ValidateOnSaveEnabled = false;
         var result = entities.SaveChanges();
         entities.Configuration.ValidateOnSaveEnabled = true;
         if (result == 1)
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 }
        public bool UpdateType(DishType type)
        {
            try
            {
                using (ChooseDishesEntities entities = new ChooseDishesEntities())
                {
                    DbEntityEntry<DishType> entry = entities.Entry<DishType>(type);

                    entry.State = System.Data.Entity.EntityState.Unchanged;//Modified
                    entry.Property("Name").IsModified = true;
                    entry.Property("DishTypeId").IsModified = false;
                    //TODO 如果更新状态为“删除”则要检查是否关联了菜品
                    try
                    {
                        entities.Configuration.ValidateOnSaveEnabled = false;
                        int result = entities.SaveChanges();
                        entities.Configuration.ValidateOnSaveEnabled = true;

                        if (result == 1)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public Hashtable SaveType(DishType type)
        {
            try
            {
                using (ChooseDishesEntities entities = new ChooseDishesEntities())
                {
                    try
                    {
                        Hashtable hash = new Hashtable();//返回结果

                        List<DishType> types;
                        //检查类型编号或者类型名称是否重复
                        types = entities.DishType.Where(info => info.Deleted == 0 && (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)
                            {
                                hash.Add("message", "类型名称已经存在,请重新命名!");
                            }
                            else if (types[0].Code == type.Code)
                            {
                                hash.Add("message", "类型编号已经存在!");
                            }
                            return hash;
                        }
                        entities.DishType.Add(type);
                        int result = entities.SaveChanges();
                        if (result == 1)
                        {
                            hash.Add("code", 0);
                            hash.Add("message", "新增成功!");

                        }
                        else
                        {
                            hash.Add("code", 2);
                            hash.Add("message", "新增失败,请稍后再试!");

                        }
                        return hash;
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }

                };
            }
            catch (Exception e)
            {
                throw e;
            }
        }
 public DishType LoadTypeById(int id)
 {
     try
     {
         DishType type;
         using (ChooseDishesEntities entities = new ChooseDishesEntities())
         {
             type = (DishType)entities.DishType.Include("Dish").Where(info => info.DishTypeId == id);
             if (type == null)
             {
                 type = new DishType();
             }
             return type;
         };
     }
     catch (Exception e)
     {
         throw e;
     }
 }
        public DishType LoadFatherTypeById(int Id)
        {
            try
            {

                using (ChooseDishesEntities entities = new ChooseDishesEntities())
                {
                    DishType fatherType;
                    DishType type;
                    type = (DishType)entities.DishType.Include("Dish").Where(info => info.DishTypeId == Id);
                    if (type == null)
                    {
                        fatherType = new DishType();
                        return fatherType;
                    }
                    fatherType = (DishType)entities.DishType.Include("Dish").Where(info => info.DishTypeId == type.ParentId);
                    if (fatherType == null)
                    {
                        fatherType = new DishType();
                    }
                    return fatherType;
                };
            }
            catch (Exception e)
            {
                throw e;
            }
        }