public IResult InsertOrUpdateOpening(EntityAndApprovalViewModel entityAndApprovalViewModel)
        {
            var result = new Result
            {
                Operation = Operation.Create,
                Status    = Status.Success
            };

            try
            {
                var openingViewModel = entityAndApprovalViewModel.openingViewModel;
                if (openingViewModel.OpeningId != Guid.Empty)
                {
                    result.Body = UpdateOpening(entityAndApprovalViewModel);
                }
                else
                {
                    var openingModel = new Openings();
                    openingModel.MapFromViewModel(openingViewModel, (ClaimsIdentity)_principal.Identity);
                    var openingSkillList = new List <OpeningSkills>();
                    var openingSkills    = openingViewModel.PrimarySkillTypes.Union(openingViewModel.SecondarySkillTypes).ToList();

                    foreach (var item in openingSkills)
                    {
                        var openingSkill = new OpeningSkills
                        {
                            SkillId   = item.SkillId,
                            SkillType = item.OpeningSkillType
                        };
                        openingSkill.MapAuditColumns((ClaimsIdentity)_principal.Identity);
                        openingSkillList.Add(openingSkill);
                    }
                    _openingRepository.CreateOpening(openingModel, openingSkillList);

                    entityAndApprovalViewModel.openingViewModel.OpeningId = openingModel.OpeningId;
                    var approvalTransactionViewModel = _approvalManagerService.AddApprovalTransaction(entityAndApprovalViewModel);

                    // var users = _approvalRepository.GetUserForOpeningApproval(approvalTransactionViewModel);
                    //users.ForEach(user =>
                    //{
                    //    MailDetailModel mailDetail = new MailDetailModel();
                    //    mailDetail.EmailId = user.Email;
                    //    mailDetail.Subject = "Opening Approval";
                    //    mailDetail.Template = TemplateType.Appoval;
                    //    mailDetail.MessageBody = approvalTransactionViewModel;
                    //    GenericHelper.Send(mailDetail, _configuration);
                    //});
                    result.Body = approvalTransactionViewModel;
                }
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                result.Status  = Status.Fail;
            }
            return(result);
        }
        public ApprovalTransactionViewModel UpdateOpening(EntityAndApprovalViewModel entityAndApprovalViewModel)
        {
            var openingViewModel = entityAndApprovalViewModel.openingViewModel;
            var openingModel     = new Openings();

            openingModel.MapFromViewModel(openingViewModel, (ClaimsIdentity)_principal.Identity);
            var openingDetail      = _openingRepository.GetByID(openingViewModel.OpeningId);
            var skillViewModelList = openingViewModel.PrimarySkillTypes.Union(openingViewModel.SecondarySkillTypes).ToList();
            var skillModelList     = openingDetail.OpeningSkills.Where(x => (x.IsActive && !x.IsDeleted)).Select(x => x.Skill).ToList();

            var existingSkills = skillViewModelList.Select(x => x.SkillId).Intersect(skillModelList.Select(x => x.SkillId)).ToList();
            var addingSkills   = skillViewModelList.Select(x => x.SkillId).Except(existingSkills).ToList();
            var removingSkills = skillModelList.Select(x => x.SkillId).Except(existingSkills).ToList();

            if (existingSkills.Any())
            {
                var openingSkills = openingDetail.OpeningSkills.Where(x => existingSkills.Contains(x.SkillId)).ToList();
                openingSkills.ForEach(x => x.MapAuditColumns((ClaimsIdentity)_principal.Identity));
            }

            if (removingSkills.Any())
            {
                var openingSkills = openingDetail.OpeningSkills.Where(x => removingSkills.Contains(x.SkillId)).ToList();
                openingSkills.ForEach(x => x.MapDeleteColumns((ClaimsIdentity)_principal.Identity));
            }

            var openingSkillList = new List <OpeningSkills>();

            if (addingSkills.Any())
            {
                var addingSkillList = skillViewModelList.Where(x => addingSkills.Contains(x.SkillId)).ToList();
                foreach (var item in addingSkillList)
                {
                    var openingSkill = new OpeningSkills()
                    {
                        OpeningId = openingDetail.OpeningId,
                        SkillId   = item.SkillId,
                        SkillType = item.OpeningSkillType
                    };
                    openingSkill.MapAuditColumns((ClaimsIdentity)_principal.Identity);
                    openingSkillList.Add(openingSkill);
                }
            }

            if (openingSkillList.Any())
            {
                _openingRepository.UpdateOpeningSkills(openingSkillList);
            }
            _openingRepository.Update(openingModel);
            entityAndApprovalViewModel.approvalTransactionViewModel.NextEventOrderNumber += 1;
            _approvalManagerService.ManageApprovalTransaction(entityAndApprovalViewModel);
            _openingRepository.SaveChanges();
            return(entityAndApprovalViewModel.approvalTransactionViewModel);
        }