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); } }
public ActionResult UpdateUser(Models.RegionUserModel model) { if (ModelState.IsValid) { // Cập nhật vào bảng userInRegion UserInRegionService userInRegionService = new UserInRegionService(); var lstUserIDNew = model.UserOfRegionIds; // Bỏ những user trong vùng cũ (nếu không còn trong danh sách mới) // Lấy danh sách user của vùng cũ. var lstUserOld = regionService.ListUserItemByRegionId(model.Id); 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)) { 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 == uId)) { userInRegion = new Entities.UserInRegion(); userInRegion.UserId = uId; userInRegion.RegionId = model.Id; userInRegion.Status = true; //userInRegionService.Insert(userInRegion); userInRegionService.Save(userInRegion); } } TempData[AdminConfigs.TEMP_MESSAGE] = AdminConfigs.MESSAGE_UPDATE_SUCCESS; TempData[AdminConfigs.TEMP_REDIRECT] = @Url.Action("Index2", "Region"); } else { var errors = ModelState.Select(x => x.Value.Errors) .Where(y => y.Count > 0) .ToList(); TempData[AdminConfigs.TEMP_MESSAGE] = AdminConfigs.MESSAGE_UPDATE_ERROR; } // Get List Role RoleService roleService = new RoleService(); model.ListRole = roleService.ListAgency(); model.ListRole.Insert(0, new Entities.Item() { Id = 0, Text = "Chọn loại môi giới" }); // Lấy danh sách user của region //m.ListUserOfRegion = regionService.ListUserItemByRegionId(id); model.ListUserOfRegion = userService.ListAgencyByRegion(model.RoleId); // lấy danh sách user //m.ListUsers = userService.ListUserItemByRoleId(-1); model.ListUsers = userService.ListAgency(); ViewBag.ActionForm = "UpdateUser"; return(View(model)); }