コード例 #1
0
        protected override async Task <Result> DoTheJob(PublishTypiconJob job)
        {
            //находим черновик
            var found = _dbContext.GetTypiconVersion(job.TypiconId, TypiconVersionStatus.Draft);

            if (found.Failure)
            {
                //TODO: SendMessage to Owner and sender

                return(Fail(job, found.Error));
            }

            var version = found.Value;

            var errMsg = version.Validate();

            if (errMsg.Count() > 0)
            {
                return(Fail(job, string.Join("", "Ошибка: ", errMsg)));
            }

            var prevStatus = version.Typicon.Status;

            //TypiconEntity
            version.Typicon.Status = TypiconStatus.Publishing;
            await _dbContext.UpdateTypiconEntityAsync(version.Typicon);

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    version.IsModified = false;
                    version.ModifiedYears.Clear();

                    //new draft
                    var clone = version.Clone();
                    //задаем доп значения
                    clone.TypiconId     = job.TypiconId;
                    clone.VersionNumber = version.VersionNumber + 1;
                    clone.BDate         = null;
                    //Сохраняем, чтобы не было конфликта цикличных зависимостей
                    await _dbContext.UpdateTypiconVersionAsync(clone);

                    //копируем вложенные коллекции
                    version.CopyChildrenTo(clone);
                    //сохраняем
                    await _dbContext.UpdateTypiconVersionAsync(clone);

                    //old publish
                    var oldPublish = _dbContext.GetTypiconVersion(job.TypiconId, TypiconVersionStatus.Published);
                    if (oldPublish.Success)
                    {
                        oldPublish.Value.EDate = DateTime.Now;
                    }

                    //new publish
                    version.BDate = DateTime.Now;

                    //outputforms
                    await _dbContext.ClearOutputFormsAsync(job.TypiconId, job.DeleteModifiedOutputDays);

                    //TODO: SendMessage to Owner and sender


                    //typiconEntity
                    version.Typicon.Status = TypiconStatus.Published;

                    _dbContext.SaveChanges();

                    transaction.Commit();

                    return(Finish(job));
                }
                catch (Exception ex)
                {
                    transaction.Rollback();

                    //version.Typicon.Status = prevStatus;
                    //version.IsModified = true;
                    await _dbContext.UpdateTypiconEntityStatusAsync(version.TypiconId, prevStatus);

                    return(Fail(job, ex.Message));
                }
            }
        }