Esempio n. 1
0
        public void WriteExcelRegion(HttpPostedFileBase file)
        {
            Excel.Application app = new Excel.Application();
            Excel.Workbook    workBook;
            Excel.Worksheet   workSheet;
            // step 1: save file.
            string fileLocation = Server.MapPath("~/Content/" + file.FileName);

            if (System.IO.File.Exists(fileLocation))
            {
                System.IO.File.Delete(fileLocation);
            }
            file.SaveAs(fileLocation);

            // step 2: Read file.

            workBook  = app.Workbooks.Open(fileLocation);
            workSheet = workBook.ActiveSheet;
            Excel.Range range = workSheet.UsedRange;

            // Lấy toàn bộ danh sách vùng đưa vào list
            List <Models.RegionExcel> lstRegion = new List <Models.RegionExcel>();

            Models.RegionExcel region = new RegionExcel();
            for (int row = 2; row <= range.Rows.Count; row++)
            {
                region = new RegionExcel()
                {
                    Id         = int.Parse(range.Cells[row, 1].Text.ToString()),
                    Text       = range.Cells[row, 2].Text,
                    CityId     = int.Parse(range.Cells[row, 3].Text.ToString()),
                    DistrictId = int.Parse(range.Cells[row, 4].Text.ToString()),
                    Status     = int.Parse(range.Cells[row, 5].Text.ToString()),
                    NeighborId = int.Parse(range.Cells[row, 6].Text.ToString()),
                    ListWard   = range.Cells[row, 7].Text,
                    ListUser   = range.Cells[row, 8].Text
                };
                lstRegion.Add(region);
            }

            // Update To DB
            UpdateRegionToDB(lstRegion);

            workBook.Close(false, null, null);
            app.Quit();

            releaseObject(workSheet);
            releaseObject(workBook);
            releaseObject(app);
        }
Esempio n. 2
0
        public void WriteCSVRegion(HttpPostedFileBase file)
        {
            string fileLocation = Server.MapPath("~/Content/" + file.FileName);

            if (System.IO.File.Exists(fileLocation))
            {
                System.IO.File.Delete(fileLocation);
            }
            file.SaveAs(fileLocation);

            List <Models.RegionExcel> lstRegion = new List <Models.RegionExcel>();
            RegionExcel region = new RegionExcel();

            // Read sample data from CSV file
            using (CsvFileReader reader = new CsvFileReader(fileLocation))
            {
                CsvRow row = new CsvRow();
                reader.ReadRow(row);
                while (reader.ReadRow(row))
                {
                    region = new RegionExcel()
                    {
                        Id         = int.Parse(row[0]),
                        Text       = row[1],
                        CityId     = int.Parse(row[2]),
                        DistrictId = int.Parse(row[3]),
                        Status     = int.Parse(row[4]),
                        NeighborId = int.Parse(row[5]),
                        ListWard   = row[6],
                        ListUser   = row[7]
                    };

                    lstRegion.Add(region);
                }
            }

            // Update To DB
            UpdateRegionToDB(lstRegion);
        }
Esempio n. 3
0
        public void UpdateRegionToDB(List <RegionExcel> listRegion)
        {
            // Duyệt danh sách vùng, cập nhật vào database
            RegionService       regionService       = new RegionService();
            PlaceService        placeService        = new PlaceService();
            UserInRegionService userInRegionService = new UserInRegionService();
            List <RegionExcel>  listRegionLast      = listRegion;
            bool isUpdateLast = false;
            int  count        = listRegion.Count;

            for (int i = 0; i < count; i++)
            {
                RegionExcel item = listRegion[i];
                // Xử lý thêm, cập nhật danh sách vùng.
                Entities.Region entity = regionService.GetById(item.Id);
                entity.Text       = item.Text;
                entity.CityId     = item.CityId;
                entity.DistrictId = item.DistrictId;
                entity.Status     = item.Status == 1 ? true : false;
                entity.NeighborId = item.NeighborId;
                if (entity.Id > 0)
                {
                    // Update.
                    regionService.Update(entity);
                }
                else
                {
                    // Insert. lấy lại ID region.
                    entity.Id = regionService.Insert(entity);

                    // Cập nhật lại id vùng.
                    listRegionLast[i].Id = entity.Id;

                    // Cập nhật lại vùng lân cận.

                    listRegionLast.Where(r => r.NeighborId == item.Id).ToList().ForEach(r => r.NeighborId = entity.Id);

                    isUpdateLast = true;
                }

                // Xử lý cập nhật vào bảng Place.
                if (item.ListWard != "")
                {
                    string[] lstWard = item.ListWard.Split(',');
                    foreach (string ward in lstWard)
                    {
                        placeService.UpdateRegion(int.Parse(ward), entity.Id);
                    }
                }

                // Xử lý cập nhật vào bảng UserInRegion
                if (item.ListUser != "")
                {
                    // Lấy danh sách user của vùng cũ.
                    var lstUserOld   = regionService.ListUserItemByRegionId(item.Id);
                    var lstUserIDNew = item.ListUser.Split(',').ToList();
                    if (lstUserOld != null)
                    {
                        // Duyệt, xử lý dữ liệu cũ
                        foreach (var u in lstUserOld)
                        {
                            // Nếu danh sách mới mà không chứa ID user cũ thì xóa user đó đi.
                            // Cũng có thể chọn cách khác là cập nhật trạng thái, chuyển về Status = 0;
                            if (!lstUserIDNew.Contains(u.Id.ToString()))
                            {
                                userInRegionService.DeleteByUser(u.Id);
                            }
                        }
                    }

                    // Cập nhật những user mới.
                    Entities.UserInRegion userInRegion;
                    foreach (var uId in lstUserIDNew)
                    {
                        if (!lstUserOld.Any(x => x.Id.ToString() == uId))
                        {
                            userInRegion          = new Entities.UserInRegion();
                            userInRegion.UserId   = int.Parse(uId);
                            userInRegion.RegionId = entity.Id;
                            userInRegion.Status   = true;
                            userInRegionService.Save(userInRegion);
                            //userInRegionService.Insert(userInRegion);
                        }
                    }
                }
            }

            if (isUpdateLast)
            {
                UpdateRegionToDB(listRegionLast);
            }
        }