private async Task SavePayment(Payment payment, SqlConnection conn, SqlTransaction tx) { DataAccess.StructuredParamValue amounts = null; if (payment.PersonalAmounts.Count > 0) { amounts = new DataAccess.StructuredParamValue(_amountsMetadata, payment.PersonalAmounts.Count); foreach (PersonalAmount pa in payment.PersonalAmounts) { amounts.NewRecord(); amounts.AddInt32(pa.EmployeeId); amounts.AddDecimal(pa.Amount); } } using (SqlCommand cmd = _sqlServer.GetSpCommand("payment.SavePayment", conn, tx)) { cmd.AddIntParam("@PlaceId", payment.PlaceId); cmd.AddIntParam("@EmployeeId", payment.EmployeeId); cmd.AddIntParam("@ShareSchemeHistoryId", payment.ShareSchemeHistoryId); cmd.AddTinyIntParam("@Status", (byte)payment.Status); cmd.AddTinyIntParam("@ReasonToReturn", (byte?)payment.ReasonToReturn); cmd.AddCharParam("@DataSource", 6, payment.DataSource); cmd.AddCharParam("@Provider", 6, payment.Provider); cmd.AddDecimalParam("@OriginalAmount", 18, 2, payment.OriginalAmount); cmd.AddDecimalParam("@ReceivedAmount", 18, 2, payment.ReceivedAmount); cmd.AddDecimalParam("@BankCommissionAmount", 18, 2, payment.BankCommissionAmount); cmd.AddDecimalParam("@AgentCommissionAmount", 18, 2, payment.AgentCommissionAmount); cmd.AddDecimalParam("@IncomeAmount", 18, 2, payment.IncomeAmount); cmd.AddDecimalParam("@PayoutAmount", 18, 2, payment.PayoutAmount); cmd.AddDateTime2Param("@PaymentDateTime", payment.PaymentDateTime); cmd.AddBitParam("@IsTimeSpecified", payment.IsTimeSpecified); cmd.AddDateTime2Param("@ArrivalDateTime", payment.ArrivalDateTime); cmd.AddVarCharParam("@DocumentName", 100, (payment.DocumentId.HasValue ? null : payment.DocumentName)); SqlParameter documentIdParam = cmd.AddIntParam("@DocumentId", payment.DocumentId).InputOutput(); cmd.AddVarCharParam("@DocumentNumber", 40, (payment.DocumentId.HasValue ? null : payment.DocumentNumber)); cmd.AddDateParam("@DocumentDate", (payment.DocumentId.HasValue ? null : payment.DocumentDate)); cmd.AddVarCharParam("@ExternalId", 50, payment.ExternalId); cmd.AddNVarCharParam("@Fio", 100, payment.Fio); cmd.AddNVarCharParam("@Address", 150, payment.Address); cmd.AddNVarCharParam("@Purpose", 150, payment.Purpose); cmd.AddNVarCharMaxParam("@RawData", payment.RawData); cmd.AddStructuredParam("@Amounts", "payment.PaymentShare", amounts); SqlParameter PaymentIdParam = cmd.AddBigIntParam("@PaymentId").Output(); SqlParameter StatusParam = cmd.AddTinyIntParam("@FinalStatus").Output(); await cmd.ExecuteNonQueryAsync(); payment.Id = PaymentIdParam.GetInt64(); payment.Status = (PaymentStatus)StatusParam.GetByte(); if (!payment.DocumentId.HasValue) { payment.DocumentId = documentIdParam.GetInt32OrNull(); } } }
private async Task <bool> SavePaymentRecords(List <OperationHistoryRequest.Operation> list) { DataAccess.StructuredParamValue idList = new DataAccess.StructuredParamValue(_idToCheckMetadata, list.Count); for (int i = list.Count - 1, j = 1; i >= 0; i--, j++) { OperationHistoryRequest.Operation o = list[i]; idList.NewRecord(); idList.AddInt32(j); idList.AddGuid(o.id); } using (SqlConnection conn = _sqlServer.GetConnection()) { await conn.OpenAsync(); HashSet <Guid> paymentsToSave = new HashSet <Guid>(); bool? firstRecordAlreadyInDb = null; using (SqlCommand cmd = _sqlServer.GetSpCommand("payment.CheckModApiPaymentList", conn)) { cmd.AddStructuredParam("@IdList", "payment.OrderedGuidList", idList); using (SqlDataReader dr = await cmd.ExecuteReaderAsync()) { while (dr.Read()) { if (!firstRecordAlreadyInDb.HasValue) { firstRecordAlreadyInDb = (dr.GetInt32("SeqNum") != 1); } paymentsToSave.Add(dr.GetGuid("Id")); } } } if (paymentsToSave.Count > 0) { DataAccess.StructuredParamValue paymentList = new DataAccess.StructuredParamValue(_paymentToStoreMetadata, paymentsToSave.Count); for (int i = list.Count - 1, j = 1; i >= 0; i--, j++) { OperationHistoryRequest.Operation o = list[i]; if (paymentsToSave.Contains(o.id)) { paymentList.NewRecord(); paymentList.AddInt32(j); paymentList.AddGuid(o.id); paymentList.AddDateTime(DateTime.ParseExact(o.executed, "s", CultureInfo.InvariantCulture)); paymentList.AddString(JsonConvert.SerializeObject(o)); } } using (SqlCommand cmd = _sqlServer.GetSpCommand("payment.SaveModApiPaymentList", conn)) { cmd.AddStructuredParam("@List", "payment.ModApiPaymentList", paymentList); await cmd.ExecuteNonQueryAsync(); } } return(!firstRecordAlreadyInDb.HasValue || firstRecordAlreadyInDb.Value); } }