吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 274|回复: 0
收起左侧

[求助] c# 分配数量

[复制链接]
7R903 发表于 2024-10-9 09:55

如下代码,遇到一个问题卡住了,比如allpoQty 是45000时,tempOSQty 分别为3000,27000,15000,
tempOSItem.Gap.GetValueOrDefault();这里每次第一次循环都是3000,也就是45000-3000,而不是经过汇总的,比如首次是45000-3000;第二次是45000-3000-27000,第三次是45000-3000-27000-15000;
这一句感觉也有问题,会找到一些实际已经分了,但是IsAllocation还是false的数据
tempOSList = osList.Where(x => x.Code + x.Customer == strKey).Where(x => !x.IsAllocation)
                .ToList();

我的需求是,将allpo中的qty和date分配到os中,分配的数量不能大于os的qty,如果allpo分配后,os有剩余数,需要拆成多行;反过来也一样,如下图:

初始数据


分配后结果


[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

class Program
{
    static void Main()
    {
        var osList = GetOS();
        var allpoList = GetALLPO();

        string strKey = string.Empty;
        var tempOSList = new List<OSList>();
       
        foreach (var allpoItem in allpoList)
        {
            int allpoQty = allpoItem.Qty.GetValueOrDefault();

            strKey = allpoItem.Code + allpoItem.Customer;

            //查找那些未分完的
            tempOSList = osList.Where(x => x.Code + x.Customer == strKey).Where(x => !x.IsAllocation)
                .ToList();

            if (tempOSList.Count == 0 || allpoQty <= 0) continue; // 简化条件

            osList.RemoveAll(x => x.Code + x.Customer == strKey && !x.IsAllocation);

            var add = new List<OSList>();
            foreach (var tempOSItem in tempOSList)
            {
                int tempOSQty = tempOSItem.Qty.GetValueOrDefault() - tempOSItem.Gap.GetValueOrDefault();

                int currentQty = Math.Min(allpoQty, tempOSQty);

                

                if (tempOSQty - currentQty == 0)
                {
                    tempOSItem.IsAllocation = true;
                }

                if (tempOSItem.Remarks == "allocated")
                {
                    var newadd = tempOSItem.Copy();
                    newadd.Gap = currentQty;
                    newadd.Date = allpoItem.Date;
                    newadd.Remarks = "allocated";
                    add.Add(newadd);
                }
                else
                {
                    tempOSItem.Gap = currentQty;
                    tempOSItem.Date = allpoItem.Date;
                    tempOSItem.Remarks = "allocated";
                    tempOSItem.AllocationSum += currentQty;
                }

                allpoQty -= currentQty;

                if (allpoQty <= 0)
                {
                   
                    break;
                } 

            }
            tempOSList.AddRange(add);
            osList.AddRange(tempOSList);
        }
        Console.ReadKey();
    }

    public static List<OSList> GetOS()
    {
        var os = new List<OSList>
        {
           new OSList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=30000},
           new OSList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=12000},
           new OSList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=45000},
           new OSList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=30000},
           new OSList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=33000},
           new OSList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=30000},
           new OSList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=27000}

        };
       
        return os;
    }


    public static List<ALLPOList> GetALLPO()
    {
        var allpo = new List<ALLPOList>
        {
           new ALLPOList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=45000,Date="2024/12/14"},
           new ALLPOList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=27000,Date="2024/12/10"},
           new ALLPOList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=30000,Date="2024/12/10"},
           new ALLPOList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=45000,Date="2024/12/14"},
           new ALLPOList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=30000,Date="2024/12/25"},
           new ALLPOList{ To="248776",End="300348",Code="Q13FC1350000200",Customer="1010217255039",Qty=30000,Date="2024/12/21"}
        };

        return allpo;
    }


    public class OSList
    {
        public string To { get; set; }
        public string End { get; set; }
        public string Code { get; set; }
        public string Customer { get; set; }
        public int? Qty { get; set; }

        public int? Gap { get; set; }

        public string Date { get; set; }

        public bool IsAllocation { get; set; }

        public int? AllocationSum { get; set; }

        public string Remarks { get; set; }
        public OSList Copy()
        {
            return new OSList
            {
                To = this.To,
                End = this.End,
                Code = this.Code,
                Customer = this.Customer,
                Qty = this.Qty ?? 0
            };
        }
    }


    public class ALLPOList
    {
        public string To { get; set; }
        public string End { get; set; }
        public string Code { get; set; }
        public string Customer { get; set; }
        public int? Qty { get; set; }

        public string Date { get; set; }
    }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 13:37

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表