先进先出减去数量股票?

时间:2017-08-16 06:19:27

标签: c#

我有一个数据库详细信息名称item_detail

No(autoincrement)      Id_Item    qty
--------------------------------------
       1                  1        10
       2                  1        20
       3                  2        20
       4                  2        20
       5                  3        30
       6                  3        40

因此,对于第1项中的第1项而言,第1项是第1项,而项目1的所有数量均为30

如果我有来自我的交易的datagridview1的数据

   Id_Item    qtyOUT
   ---------------
      1         20        
      2         30

如何从datagridview1中减去qty到我的数据库,所以我想要这样的结果

 No(autoincrement)     Id_Item    qty
--------------------------------------
       1                  1        0
       2                  1        10
       3                  2        0
       4                  2        10
       5                  3        30
       6                  3        40

所以检查是否没有1 id项目1只有数量10减去0然后移动仍然有价值的id_item 1,根据首先出现的交易号。

在减去之后我想将该交易过程放入表ITEM_TRANSACTION

 NoTrans(autoincrement) no(from item_detail)      qtyOut
---------------------------------------------------------
       1                       1                    10
       2                       2                    10
       3                       3                    20
       4                       4                    10

trycode

private void calculate()
{
// Transaction: Id_Item, qtyOUT
IEnumerable<ItemOut> contextItemOut = null;
// ItemDetail: No, Id_Item, qty
IEnumerable<ItemDetail> contextItemDetails = null;

Dictionary<int, decimal> itemOutQuantities = contextItemOut.ToDictionary(x => x.Id_Item, x => x.qtyOUT);

var itemDetails = contextItemDetails.OrderBy(x => x.No).ToList();

foreach (var item in itemDetails)
{
    decimal outQty;
    if (itemOutQuantities.TryGetValue(item.Id_Item, out outQty))
    {
        var qtyChange = Math.Min(outQty, item.qty);
        item.qty -= qtyChange;
        outQty -= qtyChange;
        if (outQty == 0)
        {
            ITEM_TRANSACTION trans = new ITEM_TRANSACTION();
            trans.id_Item = item;
            trans.qtyOut = qtyChange; //maybe its wrong
            itemOutQuantities.Remove(item.Id_Item);
        }
        else
        {
            itemOutQuantities[item.Id_Item] = outQty;
        }
    }
}

// itemDetails qty values are corrected

}

所以请帮忙

1 个答案:

答案 0 :(得分:1)

为了简单起见,我假设您的数据以IEnumerable<TEntity>提供(不要像我的代码一样保留null),您可以适应数据表或任何值存储

将您的qtyOUT条目转换为字典以便高效访问,然后以No顺序循环您的item_detail条目,并为每个细节检查是否有值要减去。

private void calculate()
{
    // Transaction: Id_Item, qtyOUT
    IEnumerable<ItemOut> contextItemOut = null;
    // ItemDetail: No, Id_Item, qty
    IEnumerable<ItemDetail> contextItemDetails = null;

    Dictionary<int, decimal> itemOutQuantities = contextItemOut.ToDictionary(x => x.Id_Item, x => x.qtyOUT);

    var itemDetails = contextItemDetails.OrderBy(x => x.No).ToList();

    // Data for ITEM_TRANSACTION
    ICollection<ItemTransaction> transactions = new List<ItemTransaction>();

    foreach (var item in itemDetails)
    {
        decimal outQty;
        if (itemOutQuantities.TryGetValue(item.Id_Item, out outQty))
        {
            var qtyChange = Math.Min(outQty, item.qty);
            item.qty -= qtyChange;
            outQty -= qtyChange;
            if (outQty == 0)
            {
                itemOutQuantities.Remove(item.Id_Item);
            }
            else
            {
                itemOutQuantities[item.Id_Item] = outQty;
            }

            if (qtyChange != 0)
            {
                transactions.Add(new ItemTransaction { /*No = autoincrement, */ NoTrans = item.No, qtyOUT = qtyChange });
            }
        }
    }

    // itemDetails qty values are corrected
}