public Guid AddNewCustomerRequest(CreateNewUsersEntity item) { Guid UserID = Guid.Empty; using (FacilitiesEntities dB = new FacilitiesEntities()) { try { // Add data Customer Name In Customer Table Customer C = new Customer(); if (item.UserEntity.UserId == Guid.Empty) { if (!IsCustomerExists(item.CustomerEntity.CustomerName)) { // Add data User In User Table var U = VM_User.AddDataInUser <User>(item.UserEntity); //var UserId1 = dB.CustomerUsers.Where(x => x.User == U.UserId).FirstOrDefault(); if (U != null) { C.CustomerId = Guid.NewGuid(); C.CustomerName = item.CustomerEntity.CustomerName; C.ActiveFlag = "Y"; dB.Customers.Add(C); //Add data in clientcustomer Table ClientCustomer CC = new ClientCustomer(); CC.ClientCustomerId = Guid.NewGuid(); CC.Client = item.ClientCustomerEntity.Client; CC.Customer = C.CustomerId; CC.ActiveFlag = "Y"; dB.ClientCustomers.Add(CC); dB.SaveChanges(); item.CustomerUserEntity.User = U.UserId; item.CustomerUserEntity.Customer = C.CustomerId; VM_CustomerUsers.AddDataInCustomerUsers(item.CustomerUserEntity); UserID = U.UserId; } } } else { var CUS = dB.Customers.Where(a => a.CustomerId == item.CustomerEntity.CustomerId).FirstOrDefault(); if (CUS != null) { var user = VM_User.UpdateDataInUser <User>(item.UserEntity); item.CustomerUserEntity.ActiveFlag = item.UserEntity.ActiveFlag; VM_CustomerUsers.UpdateDataInCustomerUsers(item.CustomerUserEntity); CUS.CustomerName = item.CustomerEntity.CustomerName; dB.SaveChanges(); } } } catch (Exception ex) { throw (ex); } } return(UserID); }
public Guid SaveCustomerUsers(CustomerUsers item) { Guid UserId = Guid.Empty; using (FacilitiesEntities db = new FacilitiesEntities()) { try { CustomerUser CU = new CustomerUser(); if (item.UserEntity.UserId == Guid.Empty && item.CustomerUserEntity.CustomerUserId == Guid.Empty) { var U = VM_User.AddDataInUser <User>(item.UserEntity); item.CustomerUserEntity.User = U.UserId; UserId = U.UserId; item.CustomerUserEntity.ActiveFlag = U.ActiveFlag; CU.CustomerUserId = Guid.NewGuid(); CU.Customer = item.CustomerUserEntity.Customer; CU.Role = item.CustomerUserEntity.Role; CU.User = item.CustomerUserEntity.User; CU.ActiveFlag = "Y"; db.CustomerUsers.Add(CU); db.SaveChanges(); //VM_CustomerUsers.AddDataInCustomerUsers(item.CustomerUserEntity); var CustomerRole = db.Roles.Where(a => a.RoleId == item.CustomerUserEntity.Role && a.ActiveFlag == "Y").Select(m => m.RoleName).FirstOrDefault(); if (CustomerRole == "Customer location User") { if (item.CustomerLocationID != Guid.Empty) { CustomerLocationUser CL = new CustomerLocationUser(); CL.CustomerLocationUser1 = Guid.NewGuid(); CL.CustomerLocation = item.CustomerLocationID; CL.User = U.UserId; CL.ActiveFlag = "Y"; db.CustomerLocationUsers.Add(CL); db.SaveChanges(); } else { foreach (var value in item.CustomerLocation) { CustomerLocationUser CL = new CustomerLocationUser(); CL.CustomerLocationUser1 = Guid.NewGuid(); CL.CustomerLocation = value; CL.User = U.UserId; CL.ActiveFlag = "Y"; db.CustomerLocationUsers.Add(CL); db.SaveChanges(); } } } } else { //Nitin31082018 var U = VM_User.UpdateDataInUser <User>(item.UserEntity); var AfterActionUrl = db.CustomerUsers.Join(db.Roles, LH => LH.Role, CGL => CGL.RoleId, (LH, CGL) => new { RoleName = CGL.RoleName, User = LH.User }).Where(LH => LH.User == U.UserId).FirstOrDefault().RoleName; if (AfterActionUrl == "Customer location User") { var allRec = db.CustomerLocationUsers.Where(a => a.User == item.UserEntity.UserId); if (allRec != null) { db.CustomerLocationUsers.RemoveRange(allRec); db.SaveChanges(); } } //item.CustomerUserEntity = db.CustomerUsers.Where(p => p.User == item.UserEntity.UserId).FirstOrDefault(); var Rolename = db.Roles.Where(x => x.RoleId == item.CustomerUserEntity.Role && x.ActiveFlag == "Y").Select(m => m.RoleName).FirstOrDefault(); if (Rolename == "Customer location User") { if (item.CustomerLocationID != Guid.Empty) { var allRec = db.CustomerLocationUsers.Where(a => a.User == item.UserEntity.UserId); if (allRec != null) { db.CustomerLocationUsers.RemoveRange(allRec); db.SaveChanges(); } CustomerLocationUser CL = new CustomerLocationUser(); CL.CustomerLocationUser1 = Guid.NewGuid(); CL.CustomerLocation = item.CustomerLocationID; CL.User = U.UserId; CL.ActiveFlag = "Y"; db.CustomerLocationUsers.Add(CL); db.SaveChanges(); } else { var allRec = db.CustomerLocationUsers.Where(a => a.User == item.UserEntity.UserId); if (allRec != null) { db.CustomerLocationUsers.RemoveRange(allRec); db.SaveChanges(); } foreach (var value in item.CustomerLocation) { CustomerLocationUser CL = new CustomerLocationUser(); CL.CustomerLocationUser1 = Guid.NewGuid(); CL.CustomerLocation = value; CL.User = U.UserId; CL.ActiveFlag = "Y"; db.CustomerLocationUsers.Add(CL); db.SaveChanges(); } } } var CustomerUsersdetails = db.CustomerUsers.Where(p => p.User == item.UserEntity.UserId).FirstOrDefault(); CustomerUsersdetails.Role = item.CustomerUserEntity.Role; VM_CustomerUsers.UpdateDataInCustomerUsers(CustomerUsersdetails); } return(UserId); } catch (Exception ex) { return(UserId); } } }