/**根据做法对象id作为参数修改做法
                     type.Name = odw.Name;   
                    type.Status = odw.Status;
                    type.UpdateBy = odw.UpdateBy;    //修改人编号
                    type.UpdateDatetime = odw.UpdateDatetime;   //修改时间
                    type.WayDetail = odw.WayDetail;
                    type.DischesWayId = odw.DischesWayId;
                    type.AddPrice = odw.AddPrice;
                    type.AddPriceByNum = odw.AddPriceByNum;
                    type.Deleted = odw.Deleted;
                    type.PingYing = odw.PingYing;
         */
        public bool UpdateDischesWay(DischesWay odw)
        {
            if (odw == null)
            {
                return false;
            }
            //修改  直接修改
            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {

                var type = entities.DischesWay.SingleOrDefault(bt => bt.DischesWayId == odw.DischesWayId);
                if (type != null)
                {
                    type.Name = odw.Name;
                    type.Status = odw.Status;
                    type.UpdateBy = odw.UpdateBy;
                    type.UpdateDatetime = odw.UpdateDatetime;
                    type.WayDetail = odw.WayDetail;
                    type.DischesWayId = odw.DischesWayId;
                    type.AddPrice = odw.AddPrice;
                    type.AddPriceByNum = odw.AddPriceByNum;
                    type.Deleted = odw.Deleted;
                    type.PingYing = odw.PingYing;
                }
                entities.SaveChanges();

                var newtype = entities.DischesWay.SingleOrDefault(bt => bt.DischesWayId == odw.DischesWayId);
                if (newtype != null)
                {
                    return true;
                }  
            }
            return false;
        }
        public int addLocation(string name, string no)
        {
            int flag = 0;
            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {
                Location local = new Location()
                {
                    Code = no,
                    Name = name,
                    CreateDatetime=DateTime.Now,
                    CreateBy=1,
                    Deleted=0,
                    Status=0

                };
                entities.Location.Add(local);
                try
                {
                    flag = entities.SaveChanges();
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }
            return flag;
        }
        //根据做法类型编码删除菜品做法类型,如果删除失败返回false,如果删除成功,则返回true
        public bool DeleteDischesWayName(int id)
        {
            //先判断是否存在有做法,如果有做法,则不能返回false
            DischesWayDataService odws = new DischesWayDataService();
            List<DischesWay> orgDischesWay = odws.FindAllDischesWay(id);
            if (orgDischesWay != null)
            {
                return false;
            }
            //删除
            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {
                DischesWayName booktype = new DischesWayName()
                {
                    DischesWayNameId = id,
                };
                DbEntityEntry<DischesWayName> entry = entities.Entry<DischesWayName>(booktype);
                entry.State = System.Data.EntityState.Deleted;

                entities.SaveChanges();

                var newtype = entities.DischesWayName.SingleOrDefault(bt => bt.DischesWayNameId == id);
                if (newtype != null)
                {
                    //Console.WriteLine(newtype.name);
                    return false;
                }
                else
                {
                    //Console.WriteLine("No Found");
                    return true;
                }
            }
           
        }
        public int editByLocation(string id, string name, string no)
        {
            int flag = 0;
            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {
                Location local = new Location()
                {
                    LocationId = int.Parse(id),
                    Code = no,
                    Name = name,
                    UpdateBy=1,
                    UpdateDatetime=DateTime.Now

                };
                DbEntityEntry<Location> entry = entities.Entry<Location>(local);
                entry.State = System.Data.EntityState.Unchanged;
                entry.Property("Code").IsModified = true;
                entry.Property("Name").IsModified = true;
                entry.Property("UpdateBy").IsModified = true;
                entry.Property("UpdateDatetime").IsModified = true;
                try
                {
                    flag=entities.SaveChanges();
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }
            return flag;
        }
        //修改菜品做法类型,传入的参数需要包含:DischesWayNameId/Code,Name,Status,Deleted,UpdateBy,UpdateDatetime字段
        public bool UpdateDischesWayName(DischesWayName dwn)
        {
            //修改  直接修改
            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {

                var type = entities.DischesWayName.SingleOrDefault(bt => bt.DischesWayNameId == dwn.DischesWayNameId);
                if (type != null)
                {
                    type.Name = dwn.Name;
                    type.Status = dwn.Status;
                    type.UpdateBy = dwn.UpdateBy;
                    type.UpdateDatetime = dwn.UpdateDatetime;
                    type.Deleted = dwn.Deleted;
                }
                entities.SaveChanges();

                var newtype = entities.DischesWayName.SingleOrDefault(bt => bt.DischesWayNameId == dwn.DischesWayNameId);
                if (newtype != null)
                {
                    return true;
                }
            }
            return false;
        }
 public List<Location> queryByLocation()
 {
     List<Location> local;
     using (ChooseDishesEntities entities = new ChooseDishesEntities())
     {
         local = entities.Location.Where(Location => Location.Deleted==0).ToList();
     }
     return local;
 }
 /**添加做法,传入的对象需要封装如下信息
         DischesWayId=1,    
        DischesWayNameId=2,    //做法类型id
        CreateDatetime=DateTime.Now,    //创建时间
        CreateBy=1,      //创建人编号
        Code=1,         //做法编码
        AddPrice=2,     //是否加价,默认为0
        Deleted=0,      //是否删除
        Name="三分熟加辣",    //做法名称
        Status=0,    //状态
        AddPriceByNum=0,     //是否按照分数加价
        PingYing="sfsjl",     //做法拼音
        WayDetail="要三分熟就可以了"    //做法说明(非必填)
  */
 public bool AddDischesWay(DischesWay odw)
 {
     //添加
     using (ChooseDishesEntities entities = new ChooseDishesEntities())
     {
         entities.DischesWay.Add(odw);
         entities.SaveChanges();
         var type = entities.DischesWay.SingleOrDefault(bt => bt.DischesWayId == odw.DischesWayId);
         if (type != null)
         {
             return true;
         }
     }
     return false;
 }
        //删除收银方式 返回true 为修改成功
        public bool deleteCashType(int Id)
        {
            try
            {
                using (ChooseDishesEntities entities = new ChooseDishesEntities())
                {
                    var type = entities.CashType.SingleOrDefault(bt => bt.Id == Id);
                    if (type != null)
                    {
                        type.Deleted = 1;
                        entities.SaveChanges();
                    }
                    else
                    {
                        return false;
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                e.ToString();
                return false;
            }

        }
        //修改收银方式  返回修改成功后的方式
        public CashType updateCashType(CashType cashType)
        {
            try
            {
                if (cashType == null || cashType.Id == null)
                {
                    return null;
                }
                using (ChooseDishesEntities entities = new ChooseDishesEntities())
                {
                    var type = entities.CashType.SingleOrDefault(bt => bt.Id == cashType.Id);
                    if (type != null)
                    {
                        type.IsBillIncome = cashType.IsBillIncome;
                        type.IsPaid = cashType.IsPaid;
                        type.IsPrivilege = cashType.IsPrivilege;
                        type.IsScore = cashType.IsScore;
                        type.KeepRecharge = cashType.KeepRecharge;
                        type.Keys = cashType.Keys;
                        type.LossesUsing = cashType.LossesUsing;
                        type.Name = cashType.Name;
                        type.Rate = cashType.Rate;
                        type.ReceptionUseing = cashType.ReceptionUseing;
                        type.RechargeUsing = cashType.RechargeUsing;
                        type.Status = cashType.Status;
                        type.SupplierUsing = cashType.SupplierUsing;
                        type.UpdateBy = type.UpdateBy;
                        type.UpdateDatetime = DateTime.Now;
                        type.UseingKeys = cashType.UseingKeys;
                        entities.SaveChanges();
                    }
                }
                return cashType;
            }
            catch (Exception e)
            {
                e.ToString();
                return null;
            }

        }
 //添加收银方式 返回添加成功后的收银方式
 public CashType addCashType(CashType cashType)
 {
     try
     {
         if (cashType == null)
         {
             return null;
         }
         CashType newCashType = null;
         using (ChooseDishesEntities entities = new ChooseDishesEntities())
         {
             var type = entities.CashType.SingleOrDefault(bt => bt.Code == cashType.Code);
             if (type != null)
             {
                 return null;
             }
             entities.CashType.Add(cashType);
             entities.SaveChanges();
             var typenew = entities.CashType.SingleOrDefault(bt => bt.Code == cashType.Code);
             if (typenew != null)
             {
                 newCashType = type;
             }
         }
         return newCashType;
     }
     catch (Exception e)
     {
         e.ToString();
         return null;
     }
 }
        /////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////  滕海东 ////////////////////////////////
        //加载所有的收银方式  传入收银类型编号
        public List<CashType> findCashType(int CashBaseTypeId)
        {
            List<CashType> list;
            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {
                if (CashBaseTypeId == 0)
                {
                    list = entities.CashType.ToList();
                }
                else
                {
                    list = entities.CashType.Where(CashType => CashType.CashBaseTypeId == CashBaseTypeId).ToList();
                }

            }
            return list;
        }
 public List<Bearing> queryByBearing()
 {
     List<Bearing> bear;
     using (ChooseDishesEntities entities = new ChooseDishesEntities())
     {
         bear = entities.Bearing.Where(Bearing => Bearing.Deleted == 0).ToList();
     }
     return bear;
 }
        /**根据指定编码删除对应做法*/
        public bool DeleteDischesWay(int id)
        {

            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {
                var type = entities.DischesWay.SingleOrDefault(bt => bt.DischesWayId == id);
                if (type == null)
                {
                    return false;
                }
                DischesWay booktype = new DischesWay()
                {
                    DischesWayId = id,
                };
                DbEntityEntry<DischesWay> entry = entities.Entry<DischesWay>(booktype);
                entry.State = System.Data.EntityState.Deleted;

                entities.SaveChanges();
                return true;
            }
        }
 /**根据做法类型编号查询做法*/
 public List<DischesWay> FindAllDischesWay(int wayId)
 {
     List<DischesWay> odws;
     using (ChooseDishesEntities entities = new ChooseDishesEntities())
     {
         odws = entities.DischesWay.Where(dw => dw.DischesWayId == wayId).ToList();
     }
     if (odws != null&&odws.Count>0)
     {
         return odws;
     }
     return null;
 }
 //查询菜品做法类型
 public List<DischesWayName> FindAllDischesWayName()
 {
     List<DischesWayName> dws;
     using (ChooseDishesEntities entities = new ChooseDishesEntities())
     {
         dws = entities.DischesWayName.Where(dw => dw.DischesWayNameId > 0).ToList();
     }
     if (dws != null)
     {
         return dws;
     }
     return null;
 }
        public int delByLocation(string id)
        {
            int flag = 0;
            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {
                Location local = new Location()
                {
                    LocationId = int.Parse(id),
                    Deleted=1,
                    UpdateBy = 1,
                    UpdateDatetime = DateTime.Now

                };
                DbEntityEntry<Location> entry = entities.Entry<Location>(local);
                entry.State = System.Data.EntityState.Unchanged;
                entry.Property("Deleted").IsModified = true;
                entry.Property("UpdateBy").IsModified = true;
                entry.Property("UpdateDatetime").IsModified = true;
                try
                {
                    entities.Configuration.ValidateOnSaveEnabled = false; 
                    flag = entities.SaveChanges();
                    entities.Configuration.ValidateOnSaveEnabled = true; 
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }
            return flag;
        }
        public int addBearing(string name, string no)
        {
            int flag = 0;
            using (ChooseDishesEntities entities = new ChooseDishesEntities())
            {
                Bearing bear = new Bearing()
                {
                    Code = int.Parse(no),
                    Name = name,
                    CreateDatetime = DateTime.Now,
                    CreateBy = 1,
                    Deleted = 1,
                    Status = 1

                };
                entities.Bearing.Add(bear);
                try
                {
                    flag = entities.SaveChanges();
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }
            return flag;
        }