在最近正在开发的MES系统中经常要处理一个过账动作,需要根据用户输入的良品数,不良品数或者待检数抑或者是检验样板数量自动去结存表中加减工单结存数量。下面以一个实例说明:
首先定义一个实体属性QtyTest类:
1 public class QtyTest 2 { 3 public int goodQty { get; set; }//良品数 4 public int reworkQty { get; set; }//返工数 5 public int scrapQty { get; set; }//报废数 6 }
主要代码实现如下:
1 class Program 2 { 3 private static List<int> qtyList = new List<int>(); 4 private static int GoodQty; 5 private static int ReworkQty; 6 private static int ScrapQty; 7 private static void Main(string[] args) 8 { 9 Console.Write("请输入良品数:"); 10 GoodQty = Convert.ToInt32(Console.ReadLine()); 11 Console.Write("请输入返工数:"); 12 ReworkQty = Convert.ToInt32(Console.ReadLine()); 13 Console.Write("请输入报废数:"); 14 ScrapQty = Convert.ToInt32(Console.ReadLine()); 15 16 List<QtyTest> list = new List<QtyTest>(); 17 //模拟工单数据集,实际以工单下达实际早晚顺序加减库存 18 DataTable DT = new DataTable(); 19 DT.Columns.Add("WJudgeQty", typeof(Int32)); 20 DataRow dr = DT.NewRow(); 21 dr["WJudgeQty"] = 100; 22 DT.Rows.Add(dr); 23 DataRow dr1 = DT.NewRow(); 24 dr1["WJudgeQty"] = 80; 25 DT.Rows.Add(dr1); 26 DataRow dr2 = DT.NewRow(); 27 dr2["WJudgeQty"] = 60; 28 DT.Rows.Add(dr2); 29 30 Fun(0, DT); 31 32 //如果只需要输入两个数,则/2即可,以此类推 33 for (int i = 0; i < qtyList.Count / 3; i++) 34 { 35 int temp = qtyList.Count / 3; 36 QtyTest qtyTest = new QtyTest(); 37 qtyTest.goodQty = qtyList[i]; 38 qtyTest.reworkQty = qtyList[i + temp]; 39 qtyTest.scrapQty = qtyList[i + 2 * temp]; 40 list.Add(qtyTest); 41 } 42 foreach (QtyTest qty in list) 43 { 44 Console.WriteLine("良品数:" + qty.goodQty + " 返工数:" + qty.reworkQty + " 报废数:" + qty.scrapQty); 45 } 46 } 47 48 private static void OutputQty(List<int> listQty) 49 { 50 foreach (int qty in listQty) 51 { 52 qtyList.Add(qty); 53 } 54 } 55 56 private static void Fun(int temp, DataTable DT) 57 { 58 DataTable DTTemp = new DataTable(); 59 DTTemp.Columns.Add("WJudgeQty", typeof(Int32)); 60 int qty = 0; 61 if (temp == 3) 62 return; 63 else 64 { 65 switch (temp) 66 { 67 case 0: 68 qty = GoodQty; 69 break; 70 case 1: 71 qty = ReworkQty; 72 break; 73 case 2: 74 qty = ScrapQty; 75 break; 76 } 77 List<int> goodQty = new List<int>(); 78 int sumJudgeQty = 0; 79 int tempGoodQty = qty; 80 //依照先扣良品数再扣返工数最后扣报废数的扣数逻辑进行判断 81 for (int i = 0; i < DT.Rows.Count; i++) 82 { 83 int WJudgeQty = Convert.ToInt32(DT.Rows[i]["WJudgeQty"]); 84 if (qty >= WJudgeQty) 85 { 86 goodQty.Add(WJudgeQty); 87 DataRow dr = DTTemp.NewRow(); 88 dr["WJudgeQty"] = 0; 89 DTTemp.Rows.Add(dr); 90 sumJudgeQty += WJudgeQty; 91 qty = qty - WJudgeQty; 92 } 93 else if (qty < WJudgeQty && qty > 0) 94 { 95 goodQty.Add(qty); 96 DataRow dr = DTTemp.NewRow(); 97 dr["WJudgeQty"] = WJudgeQty - qty; 98 DTTemp.Rows.Add(dr); 99 sumJudgeQty += qty; 100 qty = qty - WJudgeQty; 101 } 102 else if (qty == 0 || sumJudgeQty == tempGoodQty) 103 { 104 goodQty.Add(0); 105 DataRow dr = DTTemp.NewRow(); 106 dr["WJudgeQty"] = WJudgeQty; 107 DTTemp.Rows.Add(dr); 108 } 109 } 110 temp++; 111 OutputQty(goodQty); 112 Fun(temp, DTTemp); 113 } 114 } 115 116 }
测试效果如下:
可灵活根据用户输入的数量有序的自动加减工单结存。如有更好的解决方案,不吝赐教。