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;
            }
        }
Exemple #2
0
        public async Task <ActionResult <AtResult <AccountObjectDmInput_Edit> > > AccountObject_Edit([FromBody]   AccountObjectDmInput_Edit input)
        {
            if (await CheckPermission(_context))
            {
                try
                {
                    if (!ModelState.IsValid)
                    {
                        return(new AtResult <AccountObjectDmInput_Edit>(AtNotify.UpdateFail));
                    }
                    var ouput = await _logicAccountObj.EditAccountObject(input, UserId);

                    if (ouput == AtNotify.PhienGiaoDichHetHan || ouput == AtNotify.NotFound)
                    {
                        return(new AtResult <AccountObjectDmInput_Edit>(ouput));
                    }

                    return(new AtResult <AccountObjectDmInput_Edit>(input));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                return(new AtResult <AccountObjectDmInput_Edit>(AtNotify.KhongCoQuyenTruyCap));
            }
        }