public bool Create(ProductDataViewModel productDataViewModel)
        {
            bool ret = false;
            try
            {

                ProductData p = new ProductData();

                p.Brand = productDataViewModel.Brand;
                p.Cost = productDataViewModel.Cost;
                p.EndMoney = productDataViewModel.EndMoney;
                p.EndNum = productDataViewModel.EndNum;
                p.Name = productDataViewModel.Name;
                p.Note = productDataViewModel.Note;
                p.Price = productDataViewModel.Price;
                p.ProductClass = productDataViewModel.ProductClass;
                p.ProductID = productDataViewModel.ProductID;
                p.SecurityNum = productDataViewModel.SecurityNum;
                p.Unit = productDataViewModel.Unit;
                p.ProductClassID = productDataViewModel.ProductClassID;
                p.Note2 = productDataViewModel.Note2;
                p.SW = productDataViewModel.SW;

                db.ProductData.Add(p);
                db.SaveChanges();

                ret = true;
            }
            catch
            {
            }
            return ret;
        }
        public bool CheckedUsed(ProductDataViewModel p)
        {
            try
            {
                if (db.SugProduct.Where(x => x.ProductNum == p.Num).Count() != 0)
                    return false;
                if (db.CustomerContract.Where(x=>x.ProductID ==p.ProductID).Count() != 0)
                    return false;
            }
            catch
            {
                //return false;
            }
            return true;

        }
 public bool CheckedUsedChgPID(ProductDataViewModel p)
 {
     try
     {
         ProductData o = db.ProductData.Find(p.Num);
         if (db.CustomerContract.Where(x => x.ProductID == o.ProductID).Count() > 0){
             if(o.ProductID != p.ProductID || o.ProductClassID != p.ProductClassID)
                 return false;
         }
     }
     catch
     {
         //return false;
     }
     return true;
 }
 public bool Delete(ProductDataViewModel productDataViewModel)
 {
     bool ret = false;
     try
     {
         ProductData p = db.ProductData.Find(productDataViewModel.Num);
         db.ProductData.Remove(p);
         db.SaveChanges();
         ret = true;
     }
     catch
     {
     }
     return ret;
 }
        public bool Update(ProductDataViewModel productDataViewModel)
        {
            bool ret = false;
            try
            {
                ProductData p = db.ProductData.Find(productDataViewModel.Num);

                p.Brand = productDataViewModel.Brand;
                p.Cost = productDataViewModel.Cost;
                p.EndMoney = productDataViewModel.EndMoney;
                p.EndNum = productDataViewModel.EndNum;
                p.Name = productDataViewModel.Name;
                p.Note = productDataViewModel.Note;
                p.Price = productDataViewModel.Price;
                p.ProductClass = productDataViewModel.ProductClass;
                p.ProductID = productDataViewModel.ProductID;
                p.SecurityNum = productDataViewModel.SecurityNum;
                p.Unit = productDataViewModel.Unit;
                p.ProductClassID = productDataViewModel.ProductClassID;
                p.Note2 = productDataViewModel.Note2;
                p.SW = productDataViewModel.SW;

                db.SaveChanges();
                ret = true;
            }
            catch (DbUpdateConcurrencyException ex)
            {
                // Update the values of the entity that failed to save from the store
                ex.Entries.Single().Reload();
            }
            catch (DbEntityValidationException ex)
            {
                var errorMessages = ex.EntityValidationErrors
                         .SelectMany(x => x.ValidationErrors)
                         .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
            return ret;
        }
        public IQueryable<ProductDataViewModel> Read()
        {
            List<ProductDataViewModel> ret = new List<ProductDataViewModel>();
            var qry = db.ProductData;
            foreach (ProductData o in qry)
            {
                ProductDataViewModel p = new ProductDataViewModel();

                p.Num = o.ProductNum;
                p.Name = o.Name;
                p.Brand = o.Brand;
                p.ProductClass = o.ProductClass;
                p.Note = o.Note;
                p.ProductID = o.ProductID;
                p.Unit = o.Unit;
                p.Note2 = o.Note2;
                p.SW = o.SW;
                p.SWNote = "否";
                if (p.SW)
                    p.SWNote = "是";
                if (o.ProductClassID != null)
                {
                    p.ProductNote = o.ProductClass1.Note;
                    p.ProductClassID = o.ProductClassID.Value;
                }
                p.SecurityNum = o.SecurityNum;
                p.Price = o.Price;
                p.Cost = o.Cost;
                p.EndNum = o.EndNum;
                p.EndMoney = o.EndMoney;
                
                ret.Add(p);
            }
            return ret.AsQueryable();
        }