public static bool ImportThietBiRieng(String fileName, String sheet)
        {
            try
            {
                int line = 0;
                System.Data.DataTable dt = new System.Data.DataTable();
                const int STT = 0;
                const int MATB = 1;
                const int TENTB = 2;
                const int MOTA = 3;
                const int LOAITB = 4;
                const int PHONG = 5;
                const int TINHTRANG = 6;
                const int NGAYTAO = 7;
                const int NGAYMUA = 8;
                const int NGAYLAP = 9;
                const int HINHANH = 10;
                const int PASS = 11;
                dt = OpenFile(fileName, sheet);
                if (dt != null)
                {
                    int lines = dt.Rows.Count;
                    foreach (System.Data.DataRow row in dt.Rows)
                    {
                        line++;
                        DevExpress.XtraSplashScreen.SplashScreenManager.Default.SetWaitFormCaption("Import TB theo CT... " +
                            String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0.0}", (line * 1.0 / lines) * 100) + "%");
                        if (!row[PASS].Equals("Pass"))
                        {
                            bool ok = false;
                            if (row[TENTB] != DBNull.Value && row[LOAITB] != DBNull.Value && row[PHONG] != DBNull.Value && row[TINHTRANG] != DBNull.Value)
                            {
                                LoaiThietBi objLoai = LoaiThietBi.getAll().FirstOrDefault(c => c.ten.ToUpper() == row[LOAITB].ToString().Trim().ToUpper());
                                if (objLoai != null)
                                {
                                    try
                                    {
                                        TinhTrang objTinhTrang = new TinhTrang();
                                        Phong objPhong = Phong.getAll().FirstOrDefault(c => c.ten.ToUpper() == row[PHONG].ToString().Trim().ToUpper());
                                        if (objPhong != null)
                                        {
                                            objTinhTrang = TinhTrang.getAll().FirstOrDefault(c => c.value.ToUpper() == row[TINHTRANG].ToString().Trim().ToUpper());
                                            if (objTinhTrang != null)
                                            {
                                                ok = true;
                                            }
                                            else
                                            {
                                                WriteFile(fileName, sheet, row[STT].ToString().Trim(), "Error (Không có tình trạng)");
                                            }
                                        }
                                        else
                                        {
                                            WriteFile(fileName, sheet, row[STT].ToString().Trim(), "Error (Không có phòng)");
                                        }
                                        if (ok)
                                        {
                                            ThietBi objThietBi = new ThietBi();
                                            objThietBi.subId = row[MATB] != DBNull.Value ? row[MATB].ToString().Trim() : null;
                                            objThietBi.ten = row[TENTB].ToString().Trim();
                                            objThietBi.loaithietbi = objLoai;
                                            objThietBi.mota = row[MOTA].ToString().Trim();
                                            objThietBi.date_create = row[NGAYTAO] != DBNull.Value ? (DateTime?)Convert.ToDateTime(row[NGAYTAO]) : DateTime.Now;
                                            objThietBi.ngaymua = row[NGAYMUA] != DBNull.Value ? (DateTime?)Convert.ToDateTime(row[NGAYMUA]) : DateTime.Now;
                                            if (row[HINHANH] != DBNull.Value)
                                            {
                                                String[] file_names = row[HINHANH].ToString().Trim().Split(',');
                                                objThietBi.hinhanhs = AddImage(fileName, file_names);
                                            }
                                            if (objThietBi.add() > 0 && DBInstance.commit() > 0)
                                            {
                                                CTThietBi obj = new CTThietBi();
                                                obj.thietbi = objThietBi;
                                                obj.phong = objPhong;
                                                obj.tinhtrang = objTinhTrang;
                                                obj.soluong = 1;
                                                obj.mota = "Import";
                                                obj.ngay = row[NGAYLAP] != DBNull.Value ? (DateTime?)Convert.ToDateTime(row[NGAYLAP]) : DateTime.Now;
                                                if (obj.add() > 0 && DBInstance.commit() > 0)
                                                {
                                                    WriteFile(fileName, sheet, row[STT].ToString().Trim(), "Pass");
                                                }
                                                else
                                                {
                                                    WriteFile(fileName, sheet, row[STT].ToString().Trim(), "Error");
                                                }
                                            }
                                            else
                                            {
                                                WriteFile(fileName, sheet, row[STT].ToString().Trim(), "Error (Lỗi khi thêm thiết bị)");
                                            }
                                        }

                                    }
                                    catch (Exception ex)
                                    {
                                        Debug.WriteLine("ExcelDataBaseHelper->ImportThietBiChung: " + ex.Message);
                                        WriteFile(fileName, sheet, row[STT].ToString().Trim(), "Error");
                                    }
                                }
                                else
                                {
                                    WriteFile(fileName, sheet, row[STT].ToString().Trim(), "Error (Không có loại thiết bị)");
                                }
                            }
                            else
                            {
                                WriteFile(fileName, sheet, row[STT].ToString().Trim(), "Error (Không đủ thông tin)");
                            }
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ExcelDataBaseHelper->ImportThietBiChung: " + ex.Message);
                return false;
            }
        }
Ejemplo n.º 2
0
        private void CRUD()
        {
            try
            {
                switch (function)
                {
                case "add":
                    objThietBi = new ThietBi();
                    setDataObj();
                    if (objThietBi.add() > 0 && DBInstance.commit() > 0)
                    {
                        XtraMessageBox.Show("Thêm thiết bị thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        reLoadAndFocused(objThietBi.id);
                    }
                    break;

                case "edit":
                    setDataObj();
                    if (objThietBi.update() > 0 && DBInstance.commit() > 0)
                    {
                        XtraMessageBox.Show("Sửa thiết bị thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        reLoadAndFocused(objThietBi.id);
                    }
                    break;

                case "delete":
                    //Xóa sạch, ko luyến tiếc
                    int[]   indexCacRow = gridViewThietBi.GetSelectedRows();
                    String  message     = "";
                    Boolean thanhcong   = true;
                    if (indexCacRow.Count() == 1)
                    {
                        message = "Bạn có chắc là xóa thiết bị này?";
                    }
                    if (indexCacRow.Count() > 1)
                    {
                        message = "Bạn có chắc là xóa những thiết bị đã chọn?";
                    }
                    if (indexCacRow.Count() > 0)
                    {
                        if (XtraMessageBox.Show(message, "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            //splashScreenManager.ShowWaitForm();
                            //splashScreenManager.SetWaitFormCaption("Đang xóa thiết bị");
                            for (int i = 0; i < indexCacRow.Count(); i++)
                            {
                                try
                                {
                                    objThietBi = gridViewThietBi.GetRow(indexCacRow[i]) as ThietBi;
                                    if (objThietBi.ctthietbis != null)
                                    {
                                        for (int j = 0; j < objThietBi.ctthietbis.Count; j++)
                                        {
                                            //KHÔNG NÊN XÓA BĂNG TAY KIÊU NÀY
                                            //objThietBi.ctthietbis.ElementAt(j).delete();
                                        }
                                    }
                                    if (objThietBi.logthietbis != null)
                                    {
                                        for (int j = 0; j < objThietBi.logthietbis.Count; j++)
                                        {
                                            //KHÔNG NÊN XÓA BĂNG TAY KIÊU NÀY
                                            //objThietBi.logthietbis.ElementAt(j).delete();
                                        }
                                    }
                                    thanhcong = objThietBi.delete() > 0 && DBInstance.commit() > 0;
                                }
                                catch
                                {
                                    thanhcong = false;
                                    break;
                                }
                            }
                            if (thanhcong)
                            {
                                reLoad();
                                //splashScreenManager.CloseWaitForm();
                                XtraMessageBox.Show("Xóa thiết bị thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                reLoad();
                                //splashScreenManager.CloseWaitForm();
                                XtraMessageBox.Show("Đã xảy ra lỗi", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(this.Name + " : CRUD : " + ex.Message);
            }
        }
Ejemplo n.º 3
0
 private void CRUD()
 {
     try
     {
         switch (function)
         {
             case "add":
                 objThietBi = new ThietBi();
                 setDataObj();
                 if (objThietBi.add() > 0 && DBInstance.commit() > 0)
                 {
                     XtraMessageBox.Show("Thêm thiết bị thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     reLoadAndFocused(objThietBi.id);
                 }
                 break;
             case "edit":
                 setDataObj();
                 if (objThietBi.update() > 0 && DBInstance.commit() > 0)
                 {
                     XtraMessageBox.Show("Sửa thiết bị thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     reLoadAndFocused(objThietBi.id);
                 }
                 break;
             case "delete":
                 //Xóa sạch, ko luyến tiếc
                 int[] indexCacRow = gridViewThietBi.GetSelectedRows();
                 String message = "";
                 Boolean thanhcong = true;
                 if (indexCacRow.Count() == 1)
                     message = "Bạn có chắc là xóa thiết bị này?";
                 if (indexCacRow.Count() > 1)
                     message = "Bạn có chắc là xóa những thiết bị đã chọn?";
                 if (indexCacRow.Count() > 0)
                 {
                     if (XtraMessageBox.Show(message, "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                     {
                         //splashScreenManager.ShowWaitForm();
                         //splashScreenManager.SetWaitFormCaption("Đang xóa thiết bị");
                         for (int i = 0; i < indexCacRow.Count(); i++)
                         {
                             try
                             {
                                 objThietBi = gridViewThietBi.GetRow(indexCacRow[i]) as ThietBi;
                                 if (objThietBi.ctthietbis != null)
                                 {
                                     for (int j = 0; j < objThietBi.ctthietbis.Count; j++)
                                     {
                                         //KHÔNG NÊN XÓA BĂNG TAY KIÊU NÀY
                                         //objThietBi.ctthietbis.ElementAt(j).delete();
                                     }
                                 }
                                 if (objThietBi.logthietbis != null)
                                 {
                                     for (int j = 0; j < objThietBi.logthietbis.Count; j++)
                                     {
                                         //KHÔNG NÊN XÓA BĂNG TAY KIÊU NÀY
                                         //objThietBi.logthietbis.ElementAt(j).delete();
                                     }
                                 }
                                 thanhcong = objThietBi.delete() > 0 && DBInstance.commit() > 0;
                             }
                             catch
                             {
                                 thanhcong = false;
                                 break;
                             }
                         }
                         if (thanhcong)
                         {
                             reLoad();
                             //splashScreenManager.CloseWaitForm();
                             XtraMessageBox.Show("Xóa thiết bị thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                         }
                         else
                         {
                             reLoad();
                             //splashScreenManager.CloseWaitForm();
                             XtraMessageBox.Show("Đã xảy ra lỗi", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                         }
                     }
                 }
                 break;
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine(this.Name + " : CRUD : " + ex.Message);
     }
 }