public Block CreateNewBlock(Database.Client miner, Database.Order order) { Block block = new Block() { Nonce = new Random().Next(1, 9999), PreviousHash = GetLatestBlock().CurrentHash, Mapping = new Dictionary <string, decimal>(), Miner = miner.PublicKey, Data = ObjectToString(order), Transactions = new List <Transaction>(), Difficulty = Difficulty, TimeStamp = DateTime.UtcNow, }; block.CurrentHash = BlockHash(block); if (Mine(block)) { block.Mapping.Add("StorageChain", Storage); block.Mapping.Add($"{miner.PublicKey}", miner.Wallet); block.Signature = SignBlock(block, miner); AddBlock(block); return(block); } else { return(null); } }
/// <summary> /// 按卖家分别创建订单 /// </summary> protected virtual void CreateOrdersBySellers() { var orderManager = Application.Ioc.Resolve <OrderManager>(); var userRepository = RepositoryResolver.Resolve <User>(Context); var productRepository = RepositoryResolver.Resolve <Product>(Context); var orderRepository = RepositoryResolver.Resolve <Database.Order>(Context); var transactionRepository = RepositoryResolver .ResolveRepository <PaymentTransactionRepository>(Context); var products = new Dictionary <long, Product>(); var groupedProductParameters = Parameters.OrderProductParametersList.Select(p => new { productParameters = p, product = products.GetOrCreate(p.ProductId, () => productRepository.GetById(p.ProductId)) }).GroupBy(p => (p.product.Seller == null) ? null : (long?)p.product.Seller.Id).ToList(); var now = DateTime.UtcNow; foreach (var group in groupedProductParameters) { // 计算订单的价格 // 支付手续费需要单独提取出来 var orderPrice = orderManager.CalculateOrderPrice( Parameters.CloneWith(group.Select(o => o.productParameters).ToList())); var paymentFee = orderPrice.Parts.FirstOrDefault(p => p.Type == "PaymentFee"); orderPrice.Parts.Remove(paymentFee); // 生成订单 var order = new Database.Order() { Buyer = userRepository.GetById(Parameters.UserId), BuyerSessionId = Parameters.SessionId, Seller = (group.Key == null) ? null : userRepository.GetById(group.Key), State = OrderState.WaitingBuyerPay, OrderParameters = Parameters.OrderParameters, TotalCost = orderPrice.Parts.Sum(), Currency = orderPrice.Currency, TotalCostCalcResult = orderPrice, OriginalTotalCostCalcResult = orderPrice, CreateTime = now, LastUpdated = now, StateTimes = new OrderStateTimes() { { OrderState.WaitingBuyerPay, now } } }; // 添加关联的订单商品 // 这里会重新计算单价,但应该和之前的计算结果一样 foreach (var obj in group) { var unitPrice = orderManager.CalculateOrderProductUnitPrice( Parameters.UserId, obj.productParameters); var orderCount = obj.productParameters.MatchParameters.GetOrderCount(); var properties = obj.productParameters.MatchParameters.GetProperties(); var orderProduct = new OrderProduct() { Order = order, Product = obj.product, MatchParameters = obj.productParameters.MatchParameters, Count = orderCount, UnitPrice = unitPrice.Parts.Sum(), Currency = unitPrice.Currency, UnitPriceCalcResult = unitPrice, OriginalUnitPriceCalcResult = unitPrice, CreateTime = now, LastUpdated = now }; // 添加关联的属性 foreach (var productToPropertyValue in obj.product .FindPropertyValuesFromPropertyParameters(properties)) { orderProduct.PropertyValues.Add(new Database.OrderProductToPropertyValue() { OrderProduct = orderProduct, Category = obj.product.Category, Property = productToPropertyValue.Property, PropertyValue = productToPropertyValue.PropertyValue, PropertyValueName = productToPropertyValue.PropertyValueName }); } order.OrderProducts.Add(orderProduct); } // 添加关联的订单留言 var comment = Parameters.OrderParameters.GetOrderComment(); if (!string.IsNullOrEmpty(comment)) { order.OrderComments.Add(new Database.OrderComment() { Order = order, Creator = order.Buyer, Side = OrderCommentSide.BuyerComment, Content = comment, CreateTime = now }); } // 生成订单编号 order.Serial = SerialGenerator.GenerateFor(order); // 保存订单 orderRepository.Save(ref order); Result.CreatedOrders.Add(order); // 创建订单交易 // 因为目前只能使用后台的支付接口,所以收款人应该是null var paymentApiId = Parameters.OrderParameters.GetPaymentApiId(); var transaction = transactionRepository.CreateTransaction( OrderTransactionHandler.ConstType, paymentApiId, order.TotalCost, (paymentFee == null) ? 0 : paymentFee.Delta, order.Currency, (order.Buyer == null) ? null : (long?)order.Buyer.Id, null, order.Id, order.Serial); Result.CreatedTransactions.Add(transaction); } }