public BranchDepartment GetSingle(Int32 Id) { BranchDepartment obj = new BranchDepartment(); try { string query = "select * from branch_departments where ID = " + Id + ""; SqlCommand cmd = new SqlCommand(query, mySQLDBConnect.connection); mySQLDBConnect.OpenConnection(); DataTable dt = new DataTable(); SqlDataAdapter dA = new SqlDataAdapter(cmd); dA.Fill(dt); obj = (from x in dt.AsEnumerable() select new BranchDepartment { Id = x.Field <Int32>("id"), BranchId = x.Field <Int32>("branch_id"), DepartmentId = x.Field <Int32>("department_id"), Status = x.Field <Int32>("status"), CreatedBy = x.Field <Int32>("created_by"), CreatedDate = x.Field <DateTime>("created_date"), UpdatedBy = x.Field <Int32?>("updated_by") == null ? 0 : x.Field <Int32>("updated_by"), UpdatedDate = x.Field <DateTime?>("updated_date"), }).ToList().FirstOrDefault(); mySQLDBConnect.CloseConnection(); } catch (Exception ex) { Console.Write(ex.ToString()); } return(obj); }
public ActionResult Create(BranchDepartmentViewModel model) { var urlRefer = Request["UrlReferrer"]; if (ModelState.IsValid) { var BranchDepartment = new BranchDepartment(); AutoMapper.Mapper.Map(model, BranchDepartment); BranchDepartment.IsDeleted = false; BranchDepartment.CreatedUserId = WebSecurity.CurrentUserId; BranchDepartment.ModifiedUserId = WebSecurity.CurrentUserId; BranchDepartment.CreatedDate = DateTime.Now; BranchDepartment.ModifiedDate = DateTime.Now; BranchDepartmentRepository.InsertBranchDepartment(BranchDepartment); TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess; if (Request["IsPopup"] == "true" || Request["IsPopup"] == "True") { TempData[Globals.SuccessMessageKey] = App_GlobalResources.Wording.InsertSuccess; ViewBag.closePopup = "true"; model.Id = BranchDepartment.Id; ViewBag.urlRefer = urlRefer; return(View(model)); } return(Redirect(urlRefer)); } return(View(model)); }
public BranchDepartment UpdateData(BranchDepartment obj) { try { obj = IBranchDepartmentRepo.UpdateData(obj); } catch (Exception ex) { Console.Write(ex.ToString()); } return(obj); }
public BranchDepartment DeleteData(Int32 Id) { BranchDepartment obj = new BranchDepartment(); try { obj = IBranchDepartmentRepo.DeleteData(Id); } catch (Exception ex) { Console.Write(ex.ToString()); } return(obj); }
public BranchDepartment UpdateData(BranchDepartment obj) { try { string query = obj.ObjectToQuery <BranchDepartment>("update"); SqlCommand cmd = new SqlCommand(query, mySQLDBConnect.connection); mySQLDBConnect.OpenConnection(); cmd.ExecuteNonQuery(); mySQLDBConnect.CloseConnection(); } catch (Exception ex) { Console.Write(ex.ToString()); } return(obj); }
public BranchDepartment PostData(BranchDepartment obj) { try { string query = obj.ObjectToQuery <BranchDepartment>("insert") + "SELECT SCOPE_IDENTITY()"; SqlCommand cmd = new SqlCommand(query, mySQLDBConnect.connection); mySQLDBConnect.OpenConnection(); //cmd.ExecuteNonQuery(); obj.Id = Convert.ToInt32(cmd.ExecuteScalar()); mySQLDBConnect.CloseConnection(); } catch (Exception ex) { Console.Write(ex.ToString()); } return(obj); }
public Int32 DeleteBulkData(List <Int32> Ids) { Int32 result = 0; try { string query = ""; Ids.ForEach(Id => { BranchDepartment obj = GetSingle(Id); query += obj.ObjectToQuery <BranchDepartment>("update"); }); SqlCommand cmd = new SqlCommand(query, mySQLDBConnect.connection); mySQLDBConnect.OpenConnection(); result = cmd.ExecuteNonQuery(); mySQLDBConnect.CloseConnection(); } catch (Exception ex) { Console.Write(ex.ToString()); } return(result); }
public void PostFile(HttpRequestMessage request) { try { string idString = User.Identity.Name; User user = IUserService.GetDataByPropertyName(nameof(Utility.User.ApiAuthToken), idString, true, 0, 0, false).FirstOrDefault(); idString = user.Id.ToString(); Stream stream = request.Content.ReadAsStreamAsync().Result; byte[] fileBytes = new byte[stream.Length]; stream.Read(fileBytes, 0, fileBytes.Length); stream.Close(); string FileName = request.Headers.ToList().Where(x => x.Key == "FileName").FirstOrDefault().Value.FirstOrDefault().ToString(); string mimeType = request.Content.Headers.ContentType.MediaType; string fileExtension = Extension.GetDefaultExtension(mimeType); if (fileExtension == null || fileExtension == "") { fileExtension = "." + mimeType.Split('/')[1]; } string FileNameWithoutExtension = FileName.Split('.')[0]; string[] keys = FileNameWithoutExtension.Split('_'); //string batchKey = keys[0]; string batchKey = keys[0]; //string batchNo = keys[1]; string setKey = keys[1]; string aaNo = keys[2]; string deptCode = keys[3]; string branchCode = keys[4]; string accNo = keys[5]; string docType = ""; string pageCount = ""; string fileNameOld = ""; string savePath = ConfigurationManager.AppSettings["SavePath"].ToString(); string[] filesToDelete = new string[] { }; string[] filesToDeletePDF = new string[] { }; LogWriter logWriter = new LogWriter("BatchStatusController: Check"); if (fileExtension == ".pdf") { docType = keys[6]; pageCount = keys[7]; fileNameOld = batchKey + "_" + setKey + "_" + aaNo + "_" + deptCode + "_" + branchCode + "_" + accNo + "_" + docType + "_" + pageCount; filesToDeletePDF = Directory.GetFiles(savePath, fileNameOld + "*.pdf"); } else if (fileExtension == ".xml") { fileNameOld = batchKey + "_" + setKey + "_" + aaNo + "_" + deptCode + "_" + branchCode + "_" + accNo; logWriter = new LogWriter("BatchStatusController: fileNameOld - " + fileNameOld); filesToDelete = Directory.GetFiles(savePath, fileNameOld + "*.xml"); logWriter = new LogWriter("BatchStatusController: filesToDelete Count - " + filesToDelete.Count()); } //if (filesToDelete.Count() > 0) //{ // foreach (string fNamePath in filesToDelete) // { // string fName = Path.GetFileName(fNamePath); // string[] keysOld = fName.Split('_'); // string timestampOld = keysOld[6]; // logWriter = new LogWriter("BatchStatusController: timestampOld - " + timestampOld); // string[] AllFilesToDelete = Directory.GetFiles(savePath, fileNameOld + "*" + timestampOld + "*"); // logWriter = new LogWriter("BatchStatusController: fName - " + fName); // logWriter = new LogWriter("BatchStatusController: AllFilesToDelete - " + AllFilesToDelete.Count()); // foreach (string file in AllFilesToDelete) // { // File.Delete(file); // } // } //} //if (filesToDeletePDF.Count() > 0) //{ // foreach (string fNamePath in filesToDeletePDF) // { // string fName = Path.GetFileName(fNamePath); // string[] keysOld = fName.Split('_'); // string timestampOld = keysOld[8]; // timestampOld = timestampOld.Remove(timestampOld.Length - 1); // logWriter = new LogWriter("BatchStatusController: timestampOld - " + timestampOld); // string[] AllFilesToDelete = Directory.GetFiles(savePath, fileNameOld + "*" + timestampOld + "*"); // logWriter = new LogWriter("BatchStatusController: fName - " + fName); // logWriter = new LogWriter("BatchStatusController: AllFilesToDelete - " + AllFilesToDelete.Count()); // foreach (string file in AllFilesToDelete) // { // File.Delete(file); // } // } //} Branch branch = new Branch(); Department department = new Department(); BranchDepartment branchDepartment = new BranchDepartment(); List <Branch> branchMatches = IBranchService.GetDataByPropertyName(nameof(Branch.Code), branchCode, true, 0, 0, true); List <Department> departmentMatches = IDepartmentService.GetDataByPropertyName(nameof(Department.Code), deptCode, true, 0, 0, true); if (branchMatches.Count > 0 && departmentMatches.Count > 0) { branch = branchMatches.FirstOrDefault(); department = departmentMatches.FirstOrDefault(); ConfigurationController configurationController = new ConfigurationController(); ConfigurationDTO config = (ConfigurationDTO)configurationController.Get(user.ApiAuthToken); if (config.responseMsg == "Branch") { var dept = config.departments.Where(x => x.Id == department.Id).FirstOrDefault(); if (dept == null) { string customMessage = "Department not active!"; HttpResponseMessage response = request.CreateErrorResponse(HttpStatusCode.Unauthorized, customMessage); response.ReasonPhrase = customMessage; throw new HttpResponseException(response); } } string deptId = department.Id.ToString(); string branchId = branch.Id.ToString(); string localPath = "/Content/Files/"; string dirFullPath = HttpContext.Current.Server.MapPath(localPath); //string savePath = ConfigurationManager.AppSettings["SavePath"].ToString(); string filePath = savePath + FileName; string fileFullPath = savePath + FileName; if (File.Exists(fileFullPath)) { File.Delete(fileFullPath); } using (Stream file = File.OpenWrite(fileFullPath)) { file.Write(fileBytes, 0, fileBytes.Length); } } } catch (Exception ex) { LogWriter logWriter = new LogWriter("Exception - BatchStatusController: " + ex.Message); var httpResponse = ex as HttpResponseException; if (httpResponse.Response.StatusCode == HttpStatusCode.Unauthorized && httpResponse.Response.ReasonPhrase == "Department not active!") { string customMessage = "Department not active!"; HttpResponseMessage response = request.CreateErrorResponse(HttpStatusCode.Unauthorized, customMessage); response.ReasonPhrase = customMessage; throw new HttpResponseException(response); } } }
public void PostFile(HttpRequestMessage request) { try { string idString = User.Identity.Name; User user = IUserService.GetDataByPropertyName(nameof(Utility.User.ApiAuthToken), idString, true, 0, 0, false).FirstOrDefault(); idString = user.Id.ToString(); var xmlDoc = new XmlDocument(); xmlDoc.Load(request.Content.ReadAsStreamAsync().Result); MemoryStream xmlStream = new MemoryStream(); xmlDoc.Save(xmlStream); xmlStream.Flush();//Adjust this if you want read your data xmlStream.Position = 0; XmlSerializer deserializer = new XmlSerializer(typeof(BatchStatusModel)); TextReader textReader = new StreamReader(xmlStream); BatchStatusModel batchStatusModel; batchStatusModel = (BatchStatusModel)deserializer.Deserialize(textReader); if (batchStatusModel != null) { LogWriter logWriter = new LogWriter("API call start " + batchStatusModel.BatchKey + " ,StageId " + batchStatusModel.StageId + ",Status " + batchStatusModel.Status + ",DepartmentId " + batchStatusModel.DepartmentId + ", Count " + batchStatusModel.BatchCount + ", Date" + DateTime.Now); //LogWriter logWriter = new LogWriter("API call start- " + batchStatusModel.BatchKey + " ,StageId " + batchStatusModel.StageId +",Status " + batchStatusModel.Status + ",DepartmentId " + batchStatusModel.DepartmentId + ", Date" + DateTime.Now); } Branch branch = new Branch(); Department department = new Department(); BranchDepartment branchDepartment = new BranchDepartment(); List <Branch> branchMatches = IBranchService.GetDataByPropertyName(nameof(Branch.Code), batchStatusModel.BranchId, true, 0, 0, true); List <Department> departmentMatches = IDepartmentService.GetDataByPropertyName(nameof(Department.Code), batchStatusModel.DepartmentId, true, 0, 0, true); if (branchMatches.Count > 0 && departmentMatches.Count > 0) { branch = branchMatches.FirstOrDefault(); department = departmentMatches.FirstOrDefault(); ConfigurationController configurationController = new ConfigurationController(); ConfigurationDTO config = (ConfigurationDTO)configurationController.Get(user.ApiAuthToken); if (config.responseMsg == "Branch") { var dept = config.departments.Where(x => x.Id == department.Id).FirstOrDefault(); if (dept == null) { string customMessage = "Department not active!"; HttpResponseMessage response = request.CreateErrorResponse(HttpStatusCode.Unauthorized, customMessage); response.ReasonPhrase = customMessage; throw new HttpResponseException(response); } } } StringBuilder filter = new StringBuilder(); filter.Append(" 1=1"); string batchKeyColumnName = Converter.GetColumnNameByPropertyName <Batch>(nameof(Batch.BatchKey)); string batchNoColumnName = Converter.GetColumnNameByPropertyName <Batch>(nameof(Batch.BatchNo)); string branchIdColumnName = Converter.GetColumnNameByPropertyName <Batch>(nameof(Batch.BranchId)); string departmentIdColumnName = Converter.GetColumnNameByPropertyName <Batch>(nameof(Batch.DepartmentId)); //string stageIdColumnName = Converter.GetColumnNameByPropertyName<Batch>(nameof(Batch.StageId)); string statusColumnName = Converter.GetColumnNameByPropertyName <Batch>(nameof(Batch.Status)); filter.Append(" and " + batchKeyColumnName + " = '" + batchStatusModel.BatchKey + "'"); filter.Append(" and " + batchNoColumnName + " = '" + batchStatusModel.BatchNo + "'"); filter.Append(" and " + branchIdColumnName + " = '" + branch.Id + "'"); filter.Append(" and " + departmentIdColumnName + " = '" + department.Id + "'"); //filter.Append(" and " + stageIdColumnName + " = '" + batchStatusModel.StageId + "'"); filter.Append(" and " + statusColumnName + " = '1'"); List <Batch> batchMatches = IBatchService.GetDataByFilter(filter.ToString(), 0, 0, true); if (batchMatches.Count > 0) { Batch batch = batchMatches.FirstOrDefault(); batch.BatchUser = batchStatusModel.UserId; batch.BatchCount = batchStatusModel.BatchCount; if (batchStatusModel.Status == 9) { batch.Status = 0; } else { batch.BatchStatus = batchStatusModel.Status; } if (batch.StageId != batchStatusModel.StageId) { batch.StageId = batchStatusModel.StageId; } // batch.UpdatedDate = (batchStatusModel.UpdatedDate == "" ? DateTime.Now : Convert.ToDateTime(batchStatusModel.UpdatedDate)); batch.UpdatedDate = DateTime.Now; batch.UpdatedBy = Convert.ToInt32(idString); if (batchStatusModel.StageId == 4 && batchStatusModel.Status == 1) { if (batch.StageId == 4 && batch.Status == 0) { batch.BatchStatus = 0; } } if (!(batchStatusModel.StageId == 4 && batchStatusModel.Status == 0)) { LogWriter logWriter2 = new LogWriter("API call update " + batchStatusModel.BatchKey + " ,StageId " + batchStatusModel.StageId + ",Status " + batchStatusModel.Status + ",DepartmentId " + batchStatusModel.DepartmentId + ", Count " + batchMatches.Count + ", Date" + DateTime.Now); IBatchService.UpdateData(batch); } if ((batchStatusModel.StageId == 3 && batchStatusModel.Status == 1)) { string savePath = ConfigurationManager.AppSettings["SavePath"].ToString(); string[] filesToDelete = Directory.GetFiles(savePath, batch.BatchKey + "*"); if (filesToDelete.Count() > 0) { foreach (string file in filesToDelete) { File.Delete(file); } } } if ((batch.StageId == 4 && batch.Status == 1 && batchStatusModel.StageId == 4 && batchStatusModel.Status == 1)) { DirectoryInfo info = new DirectoryInfo(ConfigurationManager.AppSettings["SavePath"].ToString()); FileInfo[] filesByTime = info.GetFiles(batch.BatchKey + "*").OrderBy(x => x.CreationTime).ToArray(); //string savePath = ConfigurationManager.AppSettings["SavePath"].ToString(); //string[] filesToDelete = Directory.GetFiles(savePath, batch.BatchKey + "*"); if (filesByTime.Count() > 0) { foreach (FileInfo fileInfo in filesByTime) { DateTime lastWriteTime = fileInfo.LastWriteTime; DateTime currentDateTime = DateTime.Now; currentDateTime = currentDateTime.AddSeconds(-7); // currentDateTime = currentDateTime.AddSeconds(-1); if (lastWriteTime < currentDateTime) { // File.Delete(fileInfo.FullName); } } } } } else { DateTime dateTime; string buddhaDateTime = batchStatusModel.CreatedDate.ToString(); IFormatProvider buddhaCulture = CultureInfo.CreateSpecificCulture("th-TH"); bool IsBuddhaDate = DateTime.TryParse(buddhaDateTime, buddhaCulture, DateTimeStyles.None, out dateTime); if (IsBuddhaDate) { IFormatProvider culture = CultureInfo.CreateSpecificCulture("en-US"); DateTime englishDate; if (DateTime.TryParse(dateTime.ToString(), culture, DateTimeStyles.None, out englishDate)) { batchStatusModel.CreatedDate = englishDate.ToString("yyyy-MM-ddTHH:mm:ss"); } } Batch batch = new Batch() { BranchId = branch.Id, DepartmentId = department.Id, StageId = batchStatusModel.StageId, BatchKey = batchStatusModel.BatchKey, BatchNo = batchStatusModel.BatchNo, BatchUser = batchStatusModel.UserId, BatchCount = batchStatusModel.BatchCount, BatchStatus = batchStatusModel.Status, CreatedDate = DateTime.Now, UpdatedDate = DateTime.Now, //CreatedDate = (batchStatusModel.CreatedDate == "" ? DateTime.Now : Convert.ToDateTime(batchStatusModel.CreatedDate)), //UpdatedDate = (batchStatusModel.UpdatedDate == "" ? DateTime.Now : Convert.ToDateTime(batchStatusModel.UpdatedDate)), CreatedBy = Convert.ToInt32(idString), UpdatedBy = Convert.ToInt32(idString), Status = 1, }; LogWriter logWriter2 = new LogWriter("API call insert " + batchStatusModel.BatchKey + " ,StageId " + batchStatusModel.StageId + ",Status " + batchStatusModel.Status + ",DepartmentId " + batchStatusModel.DepartmentId + ", Count " + batchStatusModel.BatchCount + ", Date" + DateTime.Now); IBatchService.PostData(batch); } LogWriter logWriter1 = new LogWriter("API call end " + batchStatusModel.BatchKey + " ,StageId " + batchStatusModel.StageId + ",Status " + batchStatusModel.Status + ",DepartmentId " + batchStatusModel.DepartmentId + ", Count " + batchStatusModel.BatchCount + ", Date" + DateTime.Now); textReader.Close(); } catch (Exception ex) { LogWriter logWriter = new LogWriter("Exception - BatchStatusController: " + ex.Message); var httpResponse = ex as HttpResponseException; if (httpResponse.Response.StatusCode == HttpStatusCode.Unauthorized && httpResponse.Response.ReasonPhrase == "Department not active!") { string customMessage = "Department not active!"; HttpResponseMessage response = request.CreateErrorResponse(HttpStatusCode.Unauthorized, customMessage); response.ReasonPhrase = customMessage; throw new HttpResponseException(response); } } }
protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["hdnNumberPerPage"] != "" && Request.QueryString["hdnNumberPerPage"] != null) { hdnNumberPerPage.Value = Request.QueryString["hdnNumberPerPage"].ToString(); } if (Request.QueryString["hdnCurrentPageNo"] != "" && Request.QueryString["hdnCurrentPageNo"] != null) { hdnCurrentPageNo.Value = Request.QueryString["hdnCurrentPageNo"].ToString(); } if (Request.QueryString["hdnTotalRecordsCount"] != "" && Request.QueryString["hdnTotalRecordsCount"] != null) { hdnTotalRecordsCount.Value = Request.QueryString["hdnTotalRecordsCount"].ToString(); } StringBuilder filter = new StringBuilder(); filter.Append(" 1=1 "); if (Request.QueryString["IsNewSearch"] != "" && Request.QueryString["IsNewSearch"] != null) { IsNewSearch.Value = Request.QueryString["IsNewSearch"].ToString(); } if (IsNewSearch.Value == "1") { hdnCurrentPageNo.Value = ""; } if (Request.QueryString["Search"] != "" && Request.QueryString["Search"] != null) { Search.Value = Request.QueryString["Search"].ToString(); string columnNameName = Converter.GetColumnNameByPropertyName <Branch>(nameof(Silverlake.Utility.Branch.Name)); filter.Append(" and " + columnNameName + " like '%" + Search.Value + "%'"); string columnNameCode = Converter.GetColumnNameByPropertyName <Branch>(nameof(Silverlake.Utility.Branch.Code)); filter.Append(" or " + columnNameCode + " like '%" + Search.Value + "%'"); } int skip = 0, take = 10; if (hdnCurrentPageNo.Value == "") { skip = 0; take = 10; hdnNumberPerPage.Value = "10"; hdnCurrentPageNo.Value = "1"; hdnTotalRecordsCount.Value = IBranchService.GetCountByFilter(filter.ToString()).ToString(); } else { skip = (Convert.ToInt32(hdnCurrentPageNo.Value) - 1) * 10; take = 10; } List <Branch> objs = IBranchService.GetDataByFilter(filter.ToString(), skip, take, false); List <Department> departments = IDepartmentService.GetData(0, 0, false); StringBuilder asb = new StringBuilder(); int index = 1; foreach (Branch obj in objs) { StringBuilder departmentsHTML = new StringBuilder(); List <BranchDepartment> branchDepartments = IBranchDepartmentService.GetDataByPropertyName(nameof(BranchDepartment.BranchId), obj.Id.ToString(), true, 0, 0, false); bool isSelectAllChecked = obj.IsAll == 1 ? true : false; departmentsHTML.Append(@" <label class='icheck'> <div class='flat-blue single-row'> <div class='checkbox'> <input type='checkbox' name='checkRow' class='checkRow selectAll' value='' " + (isSelectAllChecked ? "checked" : "") + @"/> <label>Select All</label><br/> </div> </div> </label> "); foreach (Department d in departments) { bool isChecked = false; if (isSelectAllChecked) { isChecked = true; } else if (branchDepartments.Count > 0) { BranchDepartment bd = branchDepartments.Where(x => x.DepartmentId == d.Id && x.Status == 1).FirstOrDefault(); if (bd == null) { isChecked = true; } } departmentsHTML.Append(@" <label class='icheck'> <div class='flat-green single-row'> <div class='checkbox'> <input type='checkbox' name='checkRow' class='checkRow' value='" + d.Id + @"' " + (isChecked ? "checked" : "") + @"/> <label>" + d.Code + @"</label><br/> </div> </div> </label> "); } Company company = ICompanyService.GetSingle(obj.CompanyId); asb.Append(@"<tr> <td class='icheck'> <div class='square single-row'> <div class='checkbox'> <input type='checkbox' name='checkRow' class='checkRow' value='" + obj.Id + @"' /> <label>" + index + @"</label><br/> </div> </div> <span class='row-status'>" + (obj.Status == 1 ? "<span class='label label-success'>Active</span>" : "<span class='label label-danger'>Inactive</span>") + @"</span> </td> <td><strong>" + obj.Code + "</strong><br/>" + obj.Name + @"</td> <td style='width: 300px;'> " + departmentsHTML.ToString() + @" </td> <td>" + obj.CreatedDate.ToString("dd/MM/yyyy") + @"</td> <td>" + (obj.UpdatedDate == null ? "-" : obj.UpdatedDate.Value.ToString("dd/MM/yyyy")) + @"</td> </tr>"); index++; } branchesTBody.InnerHtml = asb.ToString(); }
public static object UpdateDepartments(List <IdDepartments> allObjs) { Int32 LoginUserId = 0; if (HttpContext.Current.Session["UserId"] != null) { LoginUserId = Convert.ToInt32(HttpContext.Current.Session["UserId"].ToString()); } foreach (IdDepartments bds in allObjs) { int branchId = bds.Id; Branch branch = IBranchService.GetSingle(branchId); if (bds.isSelectAll) { branch.IsAll = 1; branch.UpdatedDate = DateTime.Now; branch.UpdatedBy = LoginUserId; IBranchService.UpdateData(branch); List <BranchDepartment> branchDepartments = IBranchDepartmentService.GetDataByPropertyName(nameof(BranchDepartment.BranchId), branchId.ToString(), true, 0, 0, false); branchDepartments.ForEach(x => { x.UpdatedBy = LoginUserId; x.UpdatedDate = DateTime.Now; x.Status = 0; }); IBranchDepartmentService.UpdateBulkData(branchDepartments); } else { //List<Department> departments = IDepartmentService.GetData(0, 0, false); List <BranchDepartment> branchDepartments = IBranchDepartmentService.GetDataByPropertyName(nameof(BranchDepartment.BranchId), branchId.ToString(), true, 0, 0, false); foreach (BranchDepartment bd in branchDepartments) { int?deptId = bds.DepartmentIds.Where(x => x == bd.DepartmentId).FirstOrDefault(); if (deptId == 0) { BranchDepartment bdNew = bd; bd.Status = 0; bd.UpdatedBy = LoginUserId; bd.UpdatedDate = DateTime.Now; IBranchDepartmentService.UpdateData(bd); } else { BranchDepartment bdNew = bd; bd.Status = 1; bd.UpdatedBy = LoginUserId; bd.UpdatedDate = DateTime.Now; IBranchDepartmentService.UpdateData(bd); } } foreach (int departmentId in bds.DepartmentIds) { BranchDepartment bd = branchDepartments.Where(x => x.DepartmentId == departmentId).FirstOrDefault(); if (bd == null) { bd = new BranchDepartment { BranchId = branchId, DepartmentId = departmentId, CreatedBy = LoginUserId, CreatedDate = DateTime.Now, UpdatedBy = LoginUserId, UpdatedDate = DateTime.Now, Status = 1 }; IBranchDepartmentService.PostData(bd); } else { if (bd.Status == 0) { bd.Status = 1; bd.UpdatedBy = LoginUserId; bd.UpdatedDate = DateTime.Now; IBranchDepartmentService.UpdateData(bd); } } } branch.IsAll = 0; branch.UpdatedDate = DateTime.Now; branch.UpdatedBy = LoginUserId; IBranchService.UpdateData(branch); } } return(true); }