public void geyBox(List <CalculateBoxModel> preOrderDetails) { //创建箱集合 //List<List<CalculateBoxModel>> calculateBoxModels = new List<List<CalculateBoxModel>>(); var ProductInfo = RedisCacheHelper.Get <BoxProductInfo>(CustomerId + "_" + preOrderDetails.FirstOrDefault().SKU); string boxType = ""; for (int i = 0; i < boxTypeModels.Count(); i++) { var boxVolume = preOrderDetails.Sum(c => c.Qty) * ProductInfo.SKUvolume; //当箱子可以装下,就先给一个箱类型,继续循环到小箱 if (boxTypeModels[i].BoxVolume >= boxVolume) { //比较箱子能否放下 if (ProductInfo.SKUlength <= boxTypeModels[i].BoxLength && ProductInfo.SKUwidth <= boxTypeModels[i].BoxWidth && ProductInfo.SKUhigh <= boxTypeModels[i].BoxHigh) { boxType = boxTypeModels[i].BoxType; } } else { if (i == 0) { //使用最大的箱子还装不下,就拆箱 //拆箱的逻辑是一件一件的往外拿 //再分配的过程中,会出现同样的数据但是ID不同,为保证数据的一致性和完整性。此处循环带着ID //calculateBoxModels.Add(); //boxModels = new List<CalculateBoxModel>(); CalculateBoxModels(preOrderDetails); break; } else { preOrderDetails.Each((c, d) => { d.BoxType = boxType; }); boxs.Add(preOrderDetails); //到这里说明这个箱子装不下了,该箱子也不是最大的箱子。那么就取上一次循环中的箱型 break; } } } ; }
public void CalculateBoxModels(List <CalculateBoxModel> calculates) { //先做一个箱子 List <CalculateBoxModel> boxModels = new List <CalculateBoxModel>(); var ProductInfo = RedisCacheHelper.Get <BoxProductInfo>(CustomerId + "_" + calculates.FirstOrDefault().SKU); do { var model = calculates.Where(a => a.Qty > 0).First(); //往箱子里面放东西 boxModels.Add(new CalculateBoxModel(model) { Qty = 1 }); model.Qty -= 1; var boxVolume = boxModels.Where(a => a.Qty > 0).Sum(c => c.Qty) * ProductInfo.SKUvolume; string boxType = ""; if (boxTypeModels[0].BoxVolume <= boxVolume) { if (calculates.Sum(a => a.Qty) == 1) { boxModels.Each((c, d) => { d.BoxType = "异常箱"; }); boxs.Add(boxModels); boxModels = new List <CalculateBoxModel>(); break; } //将最后放进去的东西拿出来 model.Qty += 1; boxModels.RemoveAt(boxModels.Count() - 1); //重新计算东西体积 boxVolume = boxModels.Where(a => a.Qty > 0).Sum(c => c.Qty) * ProductInfo.SKUvolume; for (int j = 0; j < boxTypeModels.Count; j++) { //当箱子可以装下,就先给一个箱类型,继续循环到小箱 if (boxTypeModels[j].BoxVolume >= boxVolume) { boxType = boxTypeModels[j].BoxType; } else if ((boxTypeModels[j].BoxVolume < boxVolume) && j == boxTypeModels.Count) { boxModels.Each((c, d) => { d.BoxType = boxTypeModels[j - 1].BoxType; }); boxs.Add(boxModels); boxModels = new List <CalculateBoxModel>(); break; } else { boxModels.Each((c, d) => { d.BoxType = boxType; }); boxs.Add(boxModels); boxModels = new List <CalculateBoxModel>(); break; } } } else if (boxTypeModels[0].BoxVolume > boxVolume && calculates.Sum(a => a.Qty) == 0) { //重新计算东西体积 boxVolume = boxModels.Where(a => a.Qty > 0).Sum(c => c.Qty) * ProductInfo.SKUvolume; for (int j = 0; j < boxTypeModels.Count; j++) { //当箱子可以装下,就先给一个箱类型,继续循环到小箱 if (boxTypeModels[j].BoxVolume >= boxVolume) { boxType = boxTypeModels[j].BoxType; } else if ((boxTypeModels[j].BoxVolume < boxVolume) && j == boxTypeModels.Count) { boxModels.Each((c, d) => { d.BoxType = boxTypeModels[j - 1].BoxType; }); boxs.Add(boxModels); boxModels = new List <CalculateBoxModel>(); break; } else { boxModels.Each((c, d) => { d.BoxType = boxType; }); boxs.Add(boxModels); boxModels = new List <CalculateBoxModel>(); break; } } } else if (ProductInfo.SKUlength > boxTypeModels[0].BoxLength || ProductInfo.SKUwidth > boxTypeModels[0].BoxWidth || ProductInfo.SKUhigh > boxTypeModels[0].BoxHigh) { boxModels.Each((c, d) => { d.BoxType = "异常箱"; }); boxs.Add(boxModels); boxModels = new List <CalculateBoxModel>(); break; } } while (calculates.Sum(a => a.Qty) > 0); }