public async Task <Tuple <AccountObject, AtNotify> > CreateAccountObject(AccountObject input, List <string> ListIdDepartment, List <string> ListIdRole, string userId) { try { var checkAccount = await _context.AccountObject.AnyAsync(c => c.UserName == input.UserName); if (!checkAccount) { input.PassWord = EncryptProvider.Sha1(input.PassWord); input.AtCreatedDate = GetDateTimeFromServer(); var model = await _context.AccountObject.AddAsync(input); foreach (var itemD in ListIdDepartment) { var model_DA = new Department_Account { AtCreatedBy = userId, AtCreatedDate = GetDateTimeFromServer(), AtLastModifiedBy = userId, AtLastModifiedDate = GetDateTimeFromServer(), AtRowStatus = (int)AtRowStatus.Normal, FK_AccountObject = input.Id, FK_Department = itemD }; await _context.Department_Account.AddAsync(model_DA); } foreach (var itemR in ListIdRole) { var model_RA = new Role_AccountObject { AtCreatedBy = userId, AtCreatedDate = GetDateTimeFromServer(), AtLastModifiedBy = userId, AtLastModifiedDate = GetDateTimeFromServer(), AtRowStatus = (int)AtRowStatus.Normal, FK_AccountObject = input.Id, FK_Role = itemR, }; await _context.Role_AccountObject.AddAsync(model_RA); } await _context.SaveChangesAsync(); await WrtiteAudittingLog(new MSC_AudittingLog { Description = "Thêm mới AccountObject với ID: " + model.Entity.Id, UserID = userId }, AtSerialNoConts.CODE_LOG_CREATE); return(new Tuple <AccountObject, AtNotify>(model.Entity, AtNotify.InsertCompelete)); } else { return(new Tuple <AccountObject, AtNotify>(null, AtNotify.DuplicateCode)); } } catch (Exception ex) { throw ex; } }
public async Task <AtNotify> EditAccountObject(AccountObjectDmInput_Edit input, string userId) { try { var model = await _context.AccountObject.FirstOrDefaultAsync(c => c.Id == input.Id); if (model == null) { return(AtNotify.NotFound); } if (!model.AtRowVersion.SequenceEqual(input.AtRowVersion)) { return(AtNotify.PhienGiaoDichHetHan); } model.AtLastModifiedBy = userId; model.AtLastModifiedDate = GetDateTimeFromServer(); //Lưu data Old var data_old = new AccountObjectDmInput_Edit { Id = model.Id, AccountObjectName = model.AccountObjectName, AtRowVersion = model.AtRowVersion, Email = model.Email, Phone = model.Phone, }; var mapper = _mapper.Map(input, model); var data_new = new AccountObjectDmInput_Edit { Id = mapper.Id, AccountObjectName = mapper.AccountObjectName, AtRowVersion = mapper.AtRowVersion, Email = mapper.Email, Phone = mapper.Phone, ListIdDepartment = new List <string>(), ListIdRole = new List <string>() }; //Xóa cái củ var getListRoleAccount = await _context.Role_AccountObject.Where(c => c.FK_AccountObject == mapper.Id).ToListAsync(); //lưu list role cũ data_old.ListIdRole = new List <string>(getListRoleAccount.Select(c => c.FK_Role).ToList()); _context.Role_AccountObject.RemoveRange(getListRoleAccount); var getListDepartment = await _context.Department_Account.Where(c => c.FK_AccountObject == mapper.Id).ToListAsync(); //Luu list Department cũ data_old.ListIdDepartment = new List <string>(getListDepartment.Select(c => c.FK_Department).ToList()); _context.Department_Account.RemoveRange(getListDepartment); //Thêm mới foreach (var itemD in input.ListIdDepartment) { var model_DA = new Department_Account { AtCreatedBy = userId, AtCreatedDate = GetDateTimeFromServer(), AtLastModifiedBy = userId, AtLastModifiedDate = GetDateTimeFromServer(), AtRowStatus = (int)AtRowStatus.Normal, FK_AccountObject = mapper.Id, FK_Department = itemD }; await _context.Department_Account.AddAsync(model_DA); data_new.ListIdDepartment.Add(model_DA.FK_Department); } foreach (var itemR in input.ListIdRole) { var model_RA = new Role_AccountObject { AtCreatedBy = userId, AtCreatedDate = GetDateTimeFromServer(), AtLastModifiedBy = userId, AtLastModifiedDate = GetDateTimeFromServer(), AtRowStatus = (int)AtRowStatus.Normal, FK_AccountObject = mapper.Id, FK_Role = itemR, }; await _context.Role_AccountObject.AddAsync(model_RA); data_new.ListIdRole.Add(model_RA.FK_Role); } string json_DataNew = JsonConvert.SerializeObject(data_new); string json_DataOld = JsonConvert.SerializeObject(data_old); await WrtiteAudittingLog(new MSC_AudittingLog { Description = "Chỉnh sửa AccountObject với ID: " + mapper.Id, UserID = userId, Data_New = json_DataNew, Data_Old = json_DataOld }, AtSerialNoConts.CODE_LOGC_UPDATE); await _context.SaveChangesAsync(); return(AtNotify.UpdateCompelete); } catch (Exception ex) { throw ex; } }