/// <summary>
        /// 收货
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        /// <remarks>
        /// 1. 判断库存是否有货:不需要判断,在第二步会顺带判断
        /// 2. 按照入库时间正序排列,减库存
        /// 3. 增加交易记录与收益记录
        /// </remarks>
        public async Task <Result <string> > receive(List <SaleModel> list)
        {
            Result <string> result = new Result <string>();

            try
            {
                using (LayerDbContext context = new LayerDbContext())
                {
                    //2. 按照入库时间正序排列,减库存
                    List <Inventory> inventoryList = null;
                    foreach (var item in list)
                    {
                        inventoryList = context.inventory.Where(x => x.materialId == item.materialId).OrderBy(x => x.createdDate).ToList();
                        //卖出数量
                        float saleCount = item.count;
                        //成本价综合
                        float sumPriceIn = 0;
                        for (int i = 0; i < inventoryList.Count; i++)
                        {
                            if (inventoryList[i].count > saleCount)
                            {
                                inventoryList[i].count -= saleCount;
                                sumPriceIn             += inventoryList[i].priceIn * saleCount;
                                saleCount = 0;
                                break;
                            }
                            else
                            {
                                saleCount  -= inventoryList[i].count;
                                sumPriceIn += inventoryList[i].priceIn * inventoryList[i].count;
                                context.inventory.Remove(inventoryList[i]);
                            }
                        }
                        //saleCount !=0 说明 库存货不够了,需要报异常,让客户重新选择下单
                        if (saleCount != 0)
                        {
                            throw new Exception(item.materialName + " 库存不足,仅剩余" + (item.count - saleCount).ToString());
                        }
                        //3.1增加交易记录
                        TradingRecord trade = new TradingRecord
                        {
                            cashOrder   = item.cashOrder,
                            count       = item.count,
                            createdBy   = item.createdBy,
                            createdDate = DateTime.Now,
                            materialId  = item.materialId
                        };
                        context.tradingRecord.Add(trade);
                        //3.2 增加收益记录
                        ProfitRecord profit = new ProfitRecord
                        {
                            createdDate = trade.createdDate,
                            priceIn     = sumPriceIn,
                            priceOut    = item.priceOut,
                            profit      = item.priceOut * item.count - sumPriceIn,
                            cashOrder   = item.cashOrder,
                            materialId  = item.materialId
                        };
                        context.profitRecord.Add(profit);
                    }
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception err)
            {
                result.addError(err.Message);
            }
            return(result);
        }
예제 #2
0
파일: API.cs 프로젝트: cezary12/kokos
        /// <summary>
        /// Handler for streaming message.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="args">Args</param>
        private void StreamingMessageHandler(object sender, IncomingMessageArgs args)
        {
            var        message      = args.Message.Parameters.GetString("update");
            JSONObject responseBody = (JSONObject)JSONObject.Parse(message);
            string     commandName  = responseBody["command"].ToString();

            if (RecordReceived != null)
            {
                RecordReceived.Invoke(message);
            }

            switch (commandName)
            {
            case "tickPrices":
                TickRecord tickRecord = new TickRecord((JSONObject)responseBody["data"]);
                if (TickPricesRecordReceived != null)
                {
                    TickPricesRecordReceived.Invoke(tickRecord);
                }
                break;

            case "trade":
                StreamingTradeRecord tradeRecord = new StreamingTradeRecord((JSONObject)responseBody["data"]);
                if (TradeRecordReceived != null)
                {
                    TradeRecordReceived.Invoke(tradeRecord);
                }
                break;

            case "indicators":
                IndicatorsRecord balanceRecord = new IndicatorsRecord((JSONObject)responseBody["data"]);
                if (AccountIndicatorsRecordReceived != null)
                {
                    AccountIndicatorsRecordReceived.Invoke(balanceRecord);
                }
                break;

            case "orderStatus":
                OrderStatusRecord tradeStatusRecord = new OrderStatusRecord((JSONObject)responseBody["data"]);
                if (OrderStatusRecordReceived != null)
                {
                    OrderStatusRecordReceived.Invoke(tradeStatusRecord);
                }
                break;

            case "profit":
                ProfitRecord profitRecord = new ProfitRecord((JSONObject)responseBody["data"]);
                if (ProfitRecordReceived != null)
                {
                    ProfitRecordReceived.Invoke(profitRecord);
                }
                break;

            case "news":
                NewsRecord newsRecord = new NewsRecord((JSONObject)responseBody["data"]);
                if (NewsRecordReceived != null)
                {
                    NewsRecordReceived.Invoke(newsRecord);
                }
                break;

            case "keepAlive":
                KeepAliveRecord keepAliveRecord = new KeepAliveRecord((JSONObject)responseBody["data"]);
                if (KeepAliveRecordReceived != null)
                {
                    KeepAliveRecordReceived.Invoke(keepAliveRecord);
                }
                break;

            case "candle":
                StreamingCandleRecord candleRecord = new StreamingCandleRecord((JSONObject)responseBody["data"]);
                if (CandleRecordReceived != null)
                {
                    CandleRecordReceived.Invoke(candleRecord);
                }
                break;

            case "error":
                StreamingErrorRecord streamingErrorRecord = new StreamingErrorRecord((JSONObject)responseBody["data"]);
                if (ErrorRecordReceived != null)
                {
                    ErrorRecordReceived.Invoke(streamingErrorRecord);
                }
                break;

            default:
                throw new APICommunicationException("Unknown streaming record received");
            }
        }