コード例 #1
0
        public void UpdateProduct(HttpPostedFileBase excelfile, int?AccountId)
        {
            #region    // Duyệt danh sách insert
            EntityDataContext _context      = new EntityDataContext();
            string            ErrorEmpty    = "";
            string            ErrorIsNumber = "";
            string            Error         = "";
            bool check           = true;
            int  QuantityProduct = 0;
            try
            {
                using (var package = new ExcelPackage(excelfile.InputStream))
                {
                    using (TransactionScope ts = new TransactionScope())
                    {
                        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                        int            col       = 1;
                        for (int row = 7; worksheet.Cells[row, col].Value != null; row++)
                        {
                            #region    //Gán giá trị tạm
                            int     ProductId    = 0;
                            decimal ImportPrice  = 0;
                            decimal ShippingFee  = 0;
                            decimal ExchangeRate = 0;
                            decimal pprice1      = 0;
                            decimal pprice2      = 0;
                            decimal pprice3      = 0;
                            decimal pprice4      = 0;

                            #endregion

                            #region     // Kiểm tra giá trị cột
                            //Mã sản phẩm
                            if (worksheet.Cells[row, 2].Text == "")
                            {
                                check       = false;
                                ErrorEmpty += " Dòng " + row + " Cột 'Mã sản phẩm' ,";
                            }
                            else if (int.TryParse(worksheet.Cells[row, 2].Value.ToString(), out ProductId) == false)
                            {
                                check          = false;
                                ErrorIsNumber += " Dòng " + row + " Cột 'Mã sản phẩm' ,";
                            }

                            //Mã Sản Phẩm Cửa hàn
                            if (worksheet.Cells[row, 3].Text == "")
                            {
                                check       = false;
                                ErrorEmpty += " Dòng " + row + " Cột 'Mã Sản Phẩm Cửa hàng' ,";
                            }

                            //Tên Sản Phẩm
                            if (worksheet.Cells[row, 5].Text == "")
                            {
                                check       = false;
                                ErrorEmpty += " Dòng " + row + " Cột 'Tên Sản Phẩm' ,";
                            }

                            //Giá nhập
                            if (worksheet.Cells[row, 6].Text == "")
                            {
                                check       = false;
                                ErrorEmpty += " Dòng " + row + " Cột 'Giá nhập' ,";
                            }
                            else if (decimal.TryParse(worksheet.Cells[row, 6].Value.ToString(), out ImportPrice) == false)
                            {
                                check          = false;
                                ErrorIsNumber += " Dòng " + row + " Cột 'Giá nhập' ,";
                            }

                            //Tỷ giá
                            if (worksheet.Cells[row, 7].Text == "")
                            {
                                check       = false;
                                ErrorEmpty += " Dòng " + row + " Cột 'Tỷ giá' ,";
                            }
                            else if (decimal.TryParse(worksheet.Cells[row, 7].Value.ToString(), out ExchangeRate) == false)
                            {
                                check          = false;
                                ErrorIsNumber += " Dòng " + row + " Cột 'Tỷ giá' ,";
                            }

                            //Phí vận chuyển
                            if (worksheet.Cells[row, 8].Text == "")
                            {
                                check       = false;
                                ErrorEmpty += " Dòng " + row + " Cột 'Phí vận chuyển' ,";
                            }
                            else if (decimal.TryParse(worksheet.Cells[row, 8].Value.ToString(), out ShippingFee) == false)
                            {
                                check          = false;
                                ErrorIsNumber += " Dòng " + row + " Cột 'Phí vận chuyển' ,";
                            }

                            //Vip
                            if (worksheet.Cells[row, 9].Text == "")
                            {
                                check       = false;
                                ErrorEmpty += " Dòng " + row + " Cột 'Vip' ,";
                            }
                            else if (decimal.TryParse(worksheet.Cells[row, 9].Value.ToString(), out pprice1) == false)
                            {
                                check          = false;
                                ErrorIsNumber += " Dòng " + row + " Cột 'Vip' ,";
                            }

                            //Vip-Bạc
                            if (worksheet.Cells[row, 10].Text == "")
                            {
                                check       = false;
                                ErrorEmpty += " Dòng " + row + " Cột 'Vip-Bạc' ,";
                            }
                            else if (decimal.TryParse(worksheet.Cells[row, 10].Value.ToString(), out pprice2) == false)
                            {
                                check          = false;
                                ErrorIsNumber += " Dòng " + row + " Cột 'Vip-Bạc' ,";
                            }

                            //Vip-Vàng
                            if (worksheet.Cells[row, 11].Text == "")
                            {
                                check       = false;
                                ErrorEmpty += " Dòng " + row + " Cột 'Vip-Vàng' ,";
                            }
                            else if (decimal.TryParse(worksheet.Cells[row, 11].Value.ToString(), out pprice3) == false)
                            {
                                check          = false;
                                ErrorIsNumber += " Dòng " + row + " Cột 'Vip-Vàng' ,";
                            }

                            //Vip-Bạch kim
                            if (worksheet.Cells[row, 12].Text == "")
                            {
                                check       = false;
                                ErrorEmpty += " Dòng " + row + " Cột 'Vip-Bạch kim' ,";
                            }
                            else if (decimal.TryParse(worksheet.Cells[row, 12].Value.ToString(), out pprice4) == false)
                            {
                                check          = false;
                                ErrorIsNumber += " Dòng " + row + " Cột 'Vip-Bạch kim' ,";
                            }

                            #endregion

                            if (check)
                            {
                                #region     //Cập nhật Product

                                ProductModel p = _context.ProductModel.Where(m => m.ProductId == ProductId).FirstOrDefault();
                                p.ProductStoreCode = worksheet.Cells[row, 3].Value.ToString();
                                if (worksheet.Cells[row, 4].Text != "")
                                {
                                    p.ProductCode = worksheet.Cells[row, 4].Value.ToString();
                                }
                                p.ProductName = worksheet.Cells[row, 5].Value.ToString();
                                p.ImportPrice = ImportPrice;
                                p.ShippingFee = ShippingFee;
                                p.COGS        = p.ImportPrice * ExchangeRate + p.ShippingFee;

                                #endregion

                                #region     // Cập nhật giá VIP
                                //Product Price 1
                                ProductPriceModel price1 = _context.ProductPriceModel.Where(pp => pp.ProductId == ProductId && pp.CustomerLevelId == 1).FirstOrDefault();
                                price1.Price = pprice1;
                                //Product Price 2
                                ProductPriceModel price2 = _context.ProductPriceModel.Where(pp => pp.ProductId == ProductId && pp.CustomerLevelId == 2).FirstOrDefault();
                                price2.Price = pprice2;

                                //Product Price 3
                                ProductPriceModel price3 = _context.ProductPriceModel.Where(pp => pp.ProductId == ProductId && pp.CustomerLevelId == 3).FirstOrDefault();
                                price3.Price = pprice3;

                                //Product Price 3
                                ProductPriceModel price4 = _context.ProductPriceModel.Where(pp => pp.ProductId == ProductId && pp.CustomerLevelId == 4).FirstOrDefault();
                                price4.Price = pprice4;

                                #endregion

                                #region     // Update Database
                                _context.Entry(price1).State = System.Data.Entity.EntityState.Modified;
                                _context.Entry(price2).State = System.Data.Entity.EntityState.Modified;
                                _context.Entry(price3).State = System.Data.Entity.EntityState.Modified;
                                _context.Entry(price4).State = System.Data.Entity.EntityState.Modified;
                                _context.Entry(p).State      = System.Data.Entity.EntityState.Modified;
                                _context.SaveChanges();
                                #endregion
                            }
                            QuantityProduct++;
                        }
                        ts.Complete();
                    }
                }
            }
            catch (EntityException ex)
            {
                Error = "Xảy ra lỗi trong quá trình cập nhật !" + ex;
            }

            #region Note
            NotificationModel n = new NotificationModel();
            if (check == true && Error == "")
            {
                n.Note = "Đã cập nhật thành công giá của " + QuantityProduct + " sản phẩm !";
            }
            else
            {
                n.Note = "Cập nhật không thành công !";
                if (Error != "")
                {
                    n.Note += Error;
                }
                if (ErrorEmpty != "")
                {
                    n.Note += "  Bị rỗng :" + ErrorEmpty;
                }
                if (ErrorIsNumber != "")
                {
                    n.Note += "  Không là số :" + ErrorEmpty;
                }
            }
            n.AccountId             = AccountId;
            n.CreateDate            = DateTime.Now;
            n.Actived               = true;
            _context.Entry(n).State = System.Data.Entity.EntityState.Added;
            _context.SaveChanges();
            #endregion

            _context.Dispose();
            #endregion
        }
コード例 #2
0
 protected override void Dispose(bool disposing)
 {
     db.Dispose();
     base.Dispose(disposing);
 }
コード例 #3
0
ファイル: BaseController.cs プロジェクト: PikaLinh/Working
 protected override void Dispose(bool disposing)
 {
     _context.Dispose();
     base.Dispose(disposing);
 }