Exemple #1
0
        public async Task <int> DeleteAsync(long id, CancellationToken token = default(CancellationToken))
        {
            var result = 0;

            using (var scope = transactionScopeBuilder.Create())
            {
                result = await deleteReceiptExcludeQueryProcessor.DeleteAsync(id, token);

                result = await deleteReceiptByIdQueryProcessor.DeleteAsync(id, token);

                scope.Complete();
            }
            return(result);
        }
        /// <summary>対象外 登録/戻し処理</summary>
        /// <param name="receiptExclude"><see cref="ReceiptExclude"/>の配列</param>
        /// <returns>
        /// 登録時、ReceiptExclude の削除後、新規登録
        /// 分割対象外対応
        /// ReceiptId で グループ化
        /// 最初に引き渡される ExcludeFlag によって、登録・削除処理が変わる
        /// 同時実行制御 で、エラーメッセージ表示のため、 ReceiptExcludeResult を返す
        /// </returns>
        public async Task <ReceiptExcludeResult> SaveAsync(IEnumerable <ReceiptExclude> excludes, CancellationToken token = default(CancellationToken))
        {
            var result = new ReceiptExcludeResult {
                ProcessResult  = new ProcessResult(),
                ReceiptExclude = new ReceiptExclude[] { },
            };
            var saveResult = new List <ReceiptExclude>();

            var loginUserId = excludes.Select(c => c.UpdateBy).FirstOrDefault();

            using (var scope = transactionScopeBuilder.Create())
            {
                foreach (var group in excludes.GroupBy(x => x.ReceiptId))
                {
                    var firstExclude = group.First();
                    var excludeFlag  = firstExclude.ExcludeFlag;
                    var categoryId   = firstExclude.ExcludeCategoryId;
                    var amount       = group.Sum(x => x.ExcludeAmount);
                    var updateAt     = firstExclude.ReceiptUpdateAt;

                    var updateItem = new Receipt {
                        Id                = group.Key,
                        ExcludeFlag       = excludeFlag,
                        ExcludeAmount     = amount,
                        ExcludeCategoryId = categoryId,
                        UpdateBy          = loginUserId,
                        UpdateAt          = updateAt,   /* Receipt.UpdateAt が変更されていると、更新件数が 0 になる */
                    };

                    var resultUpdateReceipt = 0;

                    if (excludeFlag == 1)
                    {
                        await deleteReceiptExcludeQueryProcessor.DeleteAsync(group.Key, token);

                        foreach (var item in group)
                        {
                            saveResult.Add(await addReceiptExcludeQueryProcessor.SaveAsync(item, token));
                        }
                        resultUpdateReceipt = await updateReceiptQueryProcessor.UpdateExcludeAmountAsync(updateItem, token);

                        await updateReceiptHeaderQueryProcessor.UpdateAsync(new ReceiptHeaderUpdateOption { ReceiptId = group.Key, UpdateBy = loginUserId }, token);
                    }
                    else /* (excludeFlag == 0) */
                    {
                        await deleteReceiptExcludeQueryProcessor.DeleteAsync(group.Key, token);

                        resultUpdateReceipt = await updateReceiptQueryProcessor.UpdateExcludeAmountAsync(updateItem, token);
                    }

                    if (resultUpdateReceipt == 0)
                    {
                        result.ProcessResult.ErrorCode = Rac.VOne.Common.ErrorCode.OtherUserAlreadyUpdated;
                        return(result);
                    }
                }

                scope.Complete();

                result.ReceiptExclude       = saveResult.ToArray();
                result.ProcessResult.Result = true;
            }
            return(result);
        }